Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(binder): resolve array_concat types without intermediate RwError #6171

Merged
merged 2 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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