risingwavelabs / risingwave

Best-in-class stream processing, analytics, and management. Perform continuous analytics, or build event-driven applications, real-time ETL pipelines, and feature stores in minutes. Unified streaming and batch. PostgreSQL compatible.
https://go.risingwave.com/slack
Apache License 2.0
6.94k stars 573 forks source link

refactor(sqlparser/binder): extra parentheses around subquery are handled inconsistently #14502

Open xiangjinwu opened 9 months ago

xiangjinwu commented 9 months ago

BoundQuery could be created for each parenthesis, even though they are not considered as a Subquery

I see.

In the non-subquery or scalar-subquery case, extra levels of parentheses are Expr::Nested, including 1 + (((select 2))). There is a single Expr::Subquery and a single Query. This covers 1 + (((select 2) + 3) + 4).

But for subqueries with its own syntax, extra levels of parentheses are SetExpr::Query, including exists(((select 2))). There is a single Expr::Subquery with multiple Query levels, to accommodate exists(((select 2) union (select 3)) union (select 4))

Let me open an issue to track whether these two should be unified, especially the unsupported 1 < any(((select 2))) is parsed as case A today be may belong to case B.

Originally posted by @xiangjinwu in https://github.com/risingwavelabs/risingwave/pull/14430#pullrequestreview-1814733434

To view the parsed ast: cargo run --bin sqlparser <<< '(((select 2)));'

xiangjinwu commented 9 months ago

The Expr::Subquery case is expected to allow SetExpr::Query as well, as in PostgreSQL:

test=# select 10 + (((select 2) union (select 3)) except (select 2));
 ?column? 
----------
       13
(1 row)

However, our sqlparser is reporting an error:

sql parser error: Expected ), found: union at line:1, column:31

To summarize: