Skip to content

Commit

Permalink
fix(planner): consider NULL in type checker
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 committed Jun 17, 2022
1 parent 400fd52 commit da5c10d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 8 additions & 1 deletion query/src/sql/planner/plans/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use common_ast::ast::BinaryOperator;
use common_datavalues::BooleanType;
use common_datavalues::DataTypeImpl;
use common_datavalues::DataValue;
use common_datavalues::NullType;
use common_exception::ErrorCode;
use common_exception::Result;
use common_functions::scalars::FunctionFactory;
Expand Down Expand Up @@ -410,7 +411,13 @@ pub struct ComparisonExpr {

impl ScalarExpr for ComparisonExpr {
fn data_type(&self) -> DataTypeImpl {
BooleanType::new_impl()
let mut data_type = BooleanType::new_impl();
if self.left.data_type() == DataTypeImpl::Null(NullType {})
|| self.right.data_type() == DataTypeImpl::Null(NullType {})
{
data_type = NullType::new_impl();
}
data_type
}

fn used_columns(&self) -> ColumnSet {
Expand Down
10 changes: 8 additions & 2 deletions query/src/sql/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use crate::sql::plans::Scalar;
use crate::sql::plans::SubqueryExpr;
use crate::sql::plans::SubqueryType;
use crate::sql::BindContext;
use crate::sql::ScalarExpr;

/// A helper for type checking.
///
Expand Down Expand Up @@ -650,15 +651,20 @@ impl<'a> TypeChecker<'a> {
let op = ComparisonOp::try_from(op)?;
let (left, _) = self.resolve(left, None).await?;
let (right, _) = self.resolve(right, None).await?;

let mut data_type = BooleanType::new_impl();
if left.data_type() == DataTypeImpl::Null(NullType {})
|| right.data_type() == DataTypeImpl::Null(NullType {})
{
data_type = NullType::new_impl();
}
Ok((
ComparisonExpr {
op,
left: Box::new(left),
right: Box::new(right),
}
.into(),
BooleanType::new_impl(),
data_type,
))
}
BinaryOperator::And => {
Expand Down

0 comments on commit da5c10d

Please sign in to comment.