Skip to content

Commit

Permalink
fix(binder): resolve array_concat types without intermediate RwError …
Browse files Browse the repository at this point in the history
…backtrace capture
  • Loading branch information
xiangjinwu committed Nov 2, 2022
1 parent c153787 commit 477f071
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/frontend/src/expr/type_inference/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@ use crate::expr::{Expr as _, ExprImpl};
///
/// If you also need to cast them to this type, and there are more than 2 exprs, check out
/// [`align_types`].
pub fn least_restrictive(lhs: DataType, rhs: DataType) -> Result<DataType> {
pub fn least_restrictive(lhs: DataType, rhs: DataType) -> std::result::Result<DataType, ErrorCode> {
if lhs == rhs {
Ok(lhs)
} else if cast_ok(&lhs, &rhs, CastContext::Implicit) {
Ok(rhs)
} else if cast_ok(&rhs, &lhs, CastContext::Implicit) {
Ok(lhs)
} else {
Err(ErrorCode::BindError(format!("types {:?} and {:?} cannot be matched", lhs, rhs)).into())
Err(ErrorCode::BindError(format!(
"types {:?} and {:?} cannot be matched",
lhs, rhs
)))
}
}

/// Find the `least_restrictive` type over a list of `exprs`, and add implicit cast when necessary.
/// Used by `VALUES`, `CASE`, `UNION`, etc. See [PG](https://www.postgresql.org/docs/current/typeconv-union-case.html).
pub fn align_types<'a>(exprs: impl Iterator<Item = &'a mut ExprImpl>) -> Result<DataType> {
pub fn align_types<'a>(
exprs: impl Iterator<Item = &'a mut ExprImpl>,
) -> std::result::Result<DataType, ErrorCode> {
use std::mem::swap;

let exprs = exprs.collect_vec();
Expand All @@ -60,7 +65,8 @@ pub fn align_types<'a>(exprs: impl Iterator<Item = &'a mut ExprImpl>) -> Result<
for e in exprs {
let mut dummy = ExprImpl::literal_bool(false);
swap(&mut dummy, e);
*e = dummy.cast_implicit(ret_type.clone())?;
// unwrap: cast to least_restrictive type always succeeds
*e = dummy.cast_implicit(ret_type.clone()).unwrap();
}
Ok(ret_type)
}
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/expr/type_inference/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,15 @@ fn infer_type_for_special(
}
}))
.map(Some)
.map_err(Into::into)
}
ExprType::In => {
align_types(inputs.iter_mut())?;
Ok(Some(DataType::Boolean))
}
ExprType::Coalesce => {
ensure_arity!("coalesce", 1 <= | inputs |);
align_types(inputs.iter_mut()).map(Some)
align_types(inputs.iter_mut()).map(Some).map_err(Into::into)
}
ExprType::ConcatWs => {
ensure_arity!("concat_ws", 2 <= | inputs |);
Expand Down

0 comments on commit 477f071

Please sign in to comment.