You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parser: Cannot parse COUNT(CASE WHEN x NOT NULL THEN 1 END) #1920
I'm not sure if this is generally accepted SQL syntax but duckdb supports it:
> duckdb
DuckDB v1.3.0 (Ossivalis) 71c5c07cdd
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
> WITH t AS (SELECT NULL as x) SELECT COUNT(CASE WHEN x NOT NULL THEN 1 END) AS x FROM t;
┌───────┐
│ x │
│ int64 │
├───────┤
│ 0 │
└───────┘
>
However the parser rejects it:
#[test]fntest_duckdb_case(){let real_sql = r#"WITH t AS (SELECT NULL as x) SELECT COUNT(CASE WHEN x NOT NULL THEN 1 END) AS x FROM t"#;assert_eq!(duckdb().verified_stmt(real_sql).to_string(), real_sql);}
This fails w/ WITH t AS (SELECT NULL as x) SELECT COUNT(CASE WHEN x NOT NULL THEN 1 END) AS x FROM t: ParserError("Expected: ), found: WHEN")
I've tried stepping through myself and got as far as looking at parse_case_expr and it's failing here:
llet mut conditions = vec![];loop{let condition = self.parse_expr()?;self.expect_keyword_is(Keyword::THEN)?; <-- fails here
let result = self.parse_expr()?;
conditions.push(CaseWhen{ condition, result });if !self.parse_keyword(Keyword::WHEN){break;}}
At that point, condition is just the Expr x and conditions is still empty, it still needs to parse NOT NULL before expecting the THEN keyword, but I'm not sure what the fix is.
I did a bit more stepping through and I think I see the issue, it's in get_next_precedence_default, specifically in the Keyword::NOT arm:
Token::Word(w) if w.keyword == Keyword::NOT => match parser.peek_nth_token(1).token {
// The precedence of NOT varies depending on keyword that
// follows it. If it is followed by IN, BETWEEN, or LIKE,
// it takes on the precedence of those tokens. Otherwise, it
// is not an infix operator, and therefore has zero
// precedence.
Token::Word(w) if w.keyword == Keyword::IN => Ok(p!(Between)),
Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(p!(Between)),
Token::Word(w) if w.keyword == Keyword::LIKE => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::MATCH => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(p!(Like)),
_ => Ok(self.prec_unknown()),
},
I think this is missing a case for Keyword::NULL but I need to do some more research to confirm that.
To confirm, I see that x IS NULL parses ok:
WITH t AS (SELECTNULLAS x) SELECTCOUNT(CASE WHEN x IS NULL THEN 0 ELSE 1 END) AS x FROM t
So I need to see if x NOT NULL is a duckdb-ism or a more generally accepted expression.
I did some research and x NOT NULL isn't universally supported, but is supported by both DuckDB and SQLite. Additionally DuckDB, SQLite, and Postgres support x NOTNULL.
I think the way forward is to add supports_notnull_expr and supports_not_null_expr methods to the Dialect trait, both defaulting to false but overridden to true for the above dialects, and then inspect that in the parse_expr. I'm not a huge fan of the two methods being so similarly named, happy to consider alternative names.
Activity
ryanschneider commentedon Jul 2, 2025
Ah, FWIW this passes:
So the issue is specifically w/ the
NOT NULLnot being included in the expression. I'll dig deeper when I have a chance.ryanschneider commentedon Jul 2, 2025
I did a bit more stepping through and I think I see the issue, it's in
get_next_precedence_default, specifically in theKeyword::NOTarm:I think this is missing a case for
Keyword::NULLbut I need to do some more research to confirm that.To confirm, I see that
x IS NULLparses ok:So I need to see if
x NOT NULLis a duckdb-ism or a more generally accepted expression.ryanschneider commentedon Jul 2, 2025
I did some research and
x NOT NULLisn't universally supported, but is supported by both DuckDB and SQLite. Additionally DuckDB, SQLite, and Postgres supportx NOTNULL.ryanschneider commentedon Jul 2, 2025
I think the way forward is to add
supports_notnull_exprandsupports_not_null_exprmethods to theDialecttrait, both defaulting to false but overridden to true for the above dialects, and then inspect that in theparse_expr. I'm not a huge fan of the two methods being so similarly named, happy to consider alternative names.