Skip to content

Commit

Permalink
don't align type, but cast
Browse files Browse the repository at this point in the history
  • Loading branch information
xxchan committed Aug 9, 2024
1 parent e075f1e commit 1272e64
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
14 changes: 8 additions & 6 deletions src/expr/impl/src/scalar/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,21 @@ fn map(key: ListRef<'_>, value: ListRef<'_>) -> Result<MapValue, ExprError> {
/// ----
/// 300
///
/// #TODO: support this (Cannot implicitly cast ''3':Varchar' to polymorphic type Any)
/// query T
/// select map_access(map_from_entries(array[1,2,3], array[100,200,300]), '3');
/// ----
/// 300
///
/// #XXX: return error or NULL here?
/// query T
/// query error
/// select map_access(map_from_entries(array[1,2,3], array[100,200,300]), 1.0);
/// ---
/// NULL
/// ----
/// db error: ERROR: Failed to run the query
///
/// Caused by these errors (recent errors listed first):
/// 1: Failed to bind expression: map_access(map_from_entries(ARRAY[1, 2, 3], ARRAY[100, 200, 300]), 1.0)
/// 2: Bind error: Cannot access numeric in map(integer,integer): cannot cast type "numeric" to "integer" in Implicit context
///
///
/// #TODO: support this (Bind error: Cannot implicitly cast ''a':Varchar' to polymorphic type Any)
/// query T
/// select map_access(map_from_entries(array['a','b','c'], array[1,2,3]), 'a');
/// ----
Expand Down
27 changes: 8 additions & 19 deletions src/frontend/src/expr/type_inference/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use itertools::Itertools as _;
use num_integer::Integer as _;
use risingwave_common::bail_no_function;
use risingwave_common::hash::VirtualNode;
use risingwave_common::types::{DataType, MapType, StructType};
use risingwave_common::types::{DataType, StructType};
use risingwave_common::util::iter_util::ZipEqFast;
pub use risingwave_expr::sig::*;
use risingwave_pb::expr::agg_call::PbType as PbAggKind;
Expand All @@ -26,9 +26,7 @@ use thiserror_ext::AsReport;
use super::{align_types, cast_ok_base, CastContext};
use crate::error::{ErrorCode, Result};
use crate::expr::type_inference::cast::align_array_and_element;
use crate::expr::{
cast_ok, is_row_function, Expr as _, ExprImpl, ExprType, FunctionCall, InputRef,
};
use crate::expr::{cast_ok, is_row_function, Expr as _, ExprImpl, ExprType, FunctionCall};

/// Infers the return type of a function. Returns `Err` if the function with specified data types
/// is not supported on backend.
Expand Down Expand Up @@ -617,23 +615,14 @@ fn infer_type_for_special(
ExprType::MapAccess => {
ensure_arity!("map_access", | inputs | == 2);
let map_type = inputs[0].return_type().into_map();
let key_type = map_type.key().clone();
let mut key_dummy_expr = InputRef::new(0, key_type).into();
let common_key_type = align_types([&mut key_dummy_expr, &mut inputs[1]].into_iter());
match common_key_type {
Ok(common_key_type) => {
let new_map = DataType::Map(MapType::from_kv(
common_key_type.clone(),
map_type.value().clone(),
));
inputs[0].cast_implicit_mut(new_map)?;
Ok(Some(map_type.value().clone()))
}
Err(e) => Err(ErrorCode::BindError(format!(
"Cannot access {} in {}: {}",
// We do not align the map's key type with the input type here, but cast the latter to the former instead.
// e.g., for {1:'a'}[1.0], if we align them, we will get "numeric" as the key type, which violates the map type's restriction.
match inputs[1].cast_implicit_mut(map_type.key().clone()) {
Ok(()) => Ok(Some(map_type.value().clone())),
Err(_) => Err(ErrorCode::BindError(format!(
"Cannot access {} in {}",
inputs[1].return_type(),
inputs[0].return_type(),
e.to_report_string()
))
.into()),
}
Expand Down

0 comments on commit 1272e64

Please sign in to comment.