Skip to content

Commit 746b39a

Browse files
committed
rs: more IS [NOT] *
1 parent 5e6fb71 commit 746b39a

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

src/ast/mod.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ pub enum Expr {
161161
QualifiedWildcard(Vec<Ident>),
162162
/// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col`
163163
CompoundIdentifier(Vec<Ident>),
164-
/// `IS NULL` expression
165-
IsNull(Box<Expr>),
166-
/// `IS NOT NULL` expression
167-
IsNotNull(Box<Expr>),
168-
/// `[ NOT ] IN (val1, val2, ...)`
164+
/// `IS [NOT] { NULL | FALSE | TRUE | UNKNOWN }` expression
165+
Is {
166+
expr: Box<Expr>,
167+
check: &'static str,
168+
negated: bool,
169+
},
169170
InList {
170171
expr: Box<Expr>,
171172
list: Vec<Expr>,
@@ -285,8 +286,17 @@ impl fmt::Display for Expr {
285286
Expr::Wildcard => f.write_str("*"),
286287
Expr::QualifiedWildcard(q) => write!(f, "{}.*", display_separated(q, ".")),
287288
Expr::CompoundIdentifier(s) => write!(f, "{}", display_separated(s, ".")),
288-
Expr::IsNull(ast) => write!(f, "{} IS NULL", ast),
289-
Expr::IsNotNull(ast) => write!(f, "{} IS NOT NULL", ast),
289+
Expr::Is {
290+
expr,
291+
check,
292+
negated,
293+
} => write!(
294+
f,
295+
"{} IS {}{}",
296+
expr,
297+
if *negated { "NOT " } else { "" },
298+
check
299+
),
290300
Expr::InList {
291301
expr,
292302
list,

src/parser.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -844,13 +844,24 @@ impl<'a> Parser<'a> {
844844
))
845845
}
846846
Keyword::IS => {
847-
if self.parse_keyword(Keyword::NULL) {
848-
Ok((Expr::IsNull(Box::new(expr)), true))
849-
} else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
850-
Ok((Expr::IsNotNull(Box::new(expr)), true))
851-
} else {
852-
self.expected("NULL or NOT NULL after IS", self.peek_token())
853-
}
847+
let negated = self.parse_keyword(Keyword::NOT);
848+
let check = match self.next_token() {
849+
Token::Word(w) if w.keyword == Keyword::NULL => "NULL",
850+
Token::Word(w) if w.keyword == Keyword::FALSE => "FALSE",
851+
Token::Word(w) if w.keyword == Keyword::TRUE => "TRUE",
852+
Token::Word(w) if w.keyword == Keyword::UNKNOWN => "UNKNOWN",
853+
unexpected => {
854+
return self.expected("NULL, FALSE, TRUE, or UNKNOWN", unexpected)
855+
}
856+
};
857+
Ok((
858+
Expr::Is {
859+
expr: Box::new(expr),
860+
check,
861+
negated,
862+
},
863+
true,
864+
))
854865
}
855866
Keyword::NOT
856867
| Keyword::IN

0 commit comments

Comments
 (0)