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

feat(expr): Implement IS [NOT] DISTINCT FROM expression #2582

Merged
merged 9 commits into from
May 17, 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
29 changes: 29 additions & 0 deletions e2e_test/batch/basic_test.slt
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,34 @@ select extract(year from date '1993-09-01');
----
1993

statement ok
drop table t;

# Test for distinct from
statement ok
create table t (v1 int, v2 int);

statement ok
insert into t values (null, null), (1, null), (null, 2), (3, 3), (4, 5);

query I
select v1 IS DISTINCT FROM v2 from t;
----
f
t
t
f
t

query II
select v1 IS NOT DISTINCT FROM v2 from t;
----
t
f
f
t
f


statement ok
drop table t;
47 changes: 47 additions & 0 deletions src/frontend/src/binder/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ impl Binder {
Expr::IsNotTrue(expr) => self.bind_is_operator(ExprType::IsNotTrue, *expr),
Expr::IsFalse(expr) => self.bind_is_operator(ExprType::IsFalse, *expr),
Expr::IsNotFalse(expr) => self.bind_is_operator(ExprType::IsNotFalse, *expr),
Expr::IsDistinctFrom(left, right) => self.bind_distinct_from(false, *left, *right),
Expr::IsNotDistinctFrom(left, right) => self.bind_distinct_from(true, *left, *right),
Expr::Case {
operand,
conditions,
Expand Down Expand Up @@ -302,6 +304,51 @@ impl Binder {
Ok(FunctionCall::new(func_type, vec![expr])?.into())
}

pub(super) fn bind_distinct_from(
&mut self,
negated: bool,
left: Expr,
right: Expr,
) -> Result<ExprImpl> {
let left = self.bind_expr(left)?;
let right = self.bind_expr(right)?;
let both_not_null = FunctionCall::new(
ExprType::And,
vec![
FunctionCall::new(ExprType::IsNotNull, vec![left.clone()])?.into(),
FunctionCall::new(ExprType::IsNotNull, vec![right.clone()])?.into(),
],
);

let func_call = FunctionCall::new(
ExprType::Or,
vec![
FunctionCall::new(
ExprType::And,
vec![
FunctionCall::new(ExprType::IsNull, vec![left.clone()])?.into(),
FunctionCall::new(ExprType::IsNull, vec![right.clone()])?.into(),
],
)?
.into(),
FunctionCall::new(
ExprType::And,
vec![
both_not_null?.into(),
FunctionCall::new(ExprType::Equal, vec![left, right])?.into(),
],
)?
.into(),
],
);

if negated {
Ok(func_call?.into())
} else {
Ok(FunctionCall::new(ExprType::Not, vec![func_call?.into()])?.into())
}
}

pub(super) fn bind_cast(&mut self, expr: Expr, data_type: AstDataType) -> Result<ExprImpl> {
self.bind_expr(expr)?
.cast_explicit(bind_data_type(&data_type)?)
Expand Down