partiql / partiql-lang-kotlin

PartiQL libraries and tools in Kotlin.
https://partiql.org/
Apache License 2.0
538 stars 60 forks source link

[main/v1] partiql-ast: modeling of `s_f_w.set_op` does not allow for left associative set operations #1507

Open alancai98 opened 2 months ago

alancai98 commented 2 months ago

Description

Current modeling on main/v1: https://github.com/partiql/partiql-lang-kotlin/blob/edef7fb8048a07fc3fef913ccb812d49434bf4c1/partiql-ast/src/main/resources/partiql_ast.ion#L494-L510

Assuming SFW_1, SFW_2, SFW_3 represent 3 different SFW queries. Then the following cannot be modeled in the current AST using set_op.s_f_w:

(SFW_1 EXCEPT SFW_2) EXCEPT SFW_3

Set operations are by default left-associative, so SFW_1 <set_op> SFW_2 <set_op> SFW_3 is also not representable.

To Reproduce

Steps to reproduce the behavior:

  1. Attempt to create the above query using the AST

Expected Behavior

Additional Context

alancai98 commented 2 months ago

From prior discussion, the modeling of SQL set ops needs to also support LIMIT, OFFSET, and ORDER BY, which is not currently supported in the parser.

For example the following does not currently parse:

PartiQL> (SELECT a FROM <<{'a': 1}>> LIMIT 1 UNION SELECT a FROM <<{'a': 2}>> LIMIT 1) LIMIT 1
   | !!
Parser Error: at line 1, column 79: unexpected token found, LIMIT : LIMIT
mismatched input 'LIMIT' expecting {<EOF>, ';'}

In the AST, we can consider a modeling more similar to what partiql-lang-rust uses: https://github.com/partiql/partiql-lang-rust/blob/c91c5a4727f65523dea55fbdd9b9530d1df19d63/partiql-ast/src/ast.rs#L270-L278.

Alternatively, we can look at what another SQL parsing crate uses: https://docs.rs/sqlparser/0.48.0/sqlparser/ast/enum.SetExpr.html.