Skip to content

Commit

Permalink
feat(sqlparser): support IS [NOT] UNKNOWN (#9965)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangjinwu authored May 24, 2023
1 parent b707303 commit e90d704
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 4 deletions.
15 changes: 15 additions & 0 deletions e2e_test/batch/basic/boolean.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,18 @@ select booleq(true,true),
boolne('true'::bool,true);
----
t f f t f t t f t t f

query TTTT
select
true is unknown,
null is unknown,
't' is not unknown,
'on' is not unknown;
----
f t t t

statement error
select 1 is unknown;

statement error
select 'a' is not unknown;
9 changes: 9 additions & 0 deletions src/frontend/src/binder/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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::IsUnknown(expr) => self.bind_is_unknown(ExprType::IsNull, *expr),
Expr::IsNotUnknown(expr) => self.bind_is_unknown(ExprType::IsNotNull, *expr),
Expr::IsDistinctFrom(left, right) => self.bind_distinct_from(*left, *right),
Expr::IsNotDistinctFrom(left, right) => self.bind_not_distinct_from(*left, *right),
Expr::Case {
Expand Down Expand Up @@ -441,6 +443,13 @@ impl Binder {
Ok(FunctionCall::new(func_type, vec![expr])?.into())
}

pub(super) fn bind_is_unknown(&mut self, func_type: ExprType, expr: Expr) -> Result<ExprImpl> {
let expr = self
.bind_expr_inner(expr)?
.cast_implicit(DataType::Boolean)?;
Ok(FunctionCall::new(func_type, vec![expr])?.into())
}

pub(super) fn bind_distinct_from(&mut self, left: Expr, right: Expr) -> Result<ExprImpl> {
let left = self.bind_expr_inner(left)?;
let right = self.bind_expr_inner(right)?;
Expand Down
2 changes: 2 additions & 0 deletions src/meta/src/manager/catalog/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ impl QueryRewriter<'_> {
| Expr::IsNotTrue(expr)
| Expr::IsFalse(expr)
| Expr::IsNotFalse(expr)
| Expr::IsUnknown(expr)
| Expr::IsNotUnknown(expr)
| Expr::InList { expr, .. }
| Expr::SomeOp(expr)
| Expr::AllOp(expr)
Expand Down
6 changes: 6 additions & 0 deletions src/sqlparser/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ pub enum Expr {
IsFalse(Box<Expr>),
/// `IS NOT FALSE` operator
IsNotFalse(Box<Expr>),
/// `IS UNKNOWN` operator
IsUnknown(Box<Expr>),
/// `IS NOT UNKNOWN` operator
IsNotUnknown(Box<Expr>),
/// `IS DISTINCT FROM` operator
IsDistinctFrom(Box<Expr>, Box<Expr>),
/// `IS NOT DISTINCT FROM` operator
Expand Down Expand Up @@ -427,6 +431,8 @@ impl fmt::Display for Expr {
Expr::IsNotTrue(ast) => write!(f, "{} IS NOT TRUE", ast),
Expr::IsFalse(ast) => write!(f, "{} IS FALSE", ast),
Expr::IsNotFalse(ast) => write!(f, "{} IS NOT FALSE", ast),
Expr::IsUnknown(ast) => write!(f, "{} IS UNKNOWN", ast),
Expr::IsNotUnknown(ast) => write!(f, "{} IS NOT UNKNOWN", ast),
Expr::InList {
expr,
list,
Expand Down
4 changes: 4 additions & 0 deletions src/sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,10 @@ impl Parser {
Ok(Expr::IsFalse(Box::new(expr)))
} else if self.parse_keywords(&[Keyword::NOT, Keyword::FALSE]) {
Ok(Expr::IsNotFalse(Box::new(expr)))
} else if self.parse_keyword(Keyword::UNKNOWN) {
Ok(Expr::IsUnknown(Box::new(expr)))
} else if self.parse_keywords(&[Keyword::NOT, Keyword::UNKNOWN]) {
Ok(Expr::IsNotUnknown(Box::new(expr)))
} else if self.parse_keyword(Keyword::NULL) {
Ok(Expr::IsNull(Box::new(expr)))
} else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/regress/data/expected/boolean.out
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ SELECT
b IS NOT TRUE AS isnottrue,
b IS FALSE AS isfalse,
b IS NOT FALSE AS isnotfalse,
b IS NULL AS isunknown,
b IS NOT NULL AS isnotunknown
b IS UNKNOWN AS isunknown,
b IS NOT UNKNOWN AS isnotunknown
FROM booltbl3 ORDER BY o;
d | istrue | isnottrue | isfalse | isnotfalse | isunknown | isnotunknown
-------+--------+-----------+---------+------------+-----------+--------------
Expand Down
4 changes: 2 additions & 2 deletions src/tests/regress/data/sql/boolean.sql
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ SELECT
b IS NOT TRUE AS isnottrue,
b IS FALSE AS isfalse,
b IS NOT FALSE AS isnotfalse,
b IS NULL AS isunknown,
b IS NOT NULL AS isnotunknown
b IS UNKNOWN AS isunknown,
b IS NOT UNKNOWN AS isnotunknown
FROM booltbl3 ORDER BY o;


Expand Down

0 comments on commit e90d704

Please sign in to comment.