Skip to content

DuckDB: support list comprehensions - #2798

Merged
manticore-projects merged 2 commits into
JSQLParser:masterfrom
guizmaii:duckdb-list-comprehension
Oct 13, 2026
Merged

manticore-projects merged 2 commits into
JSQLParser:masterfrom
guizmaii:duckdb-list-comprehension

Conversation

@guizmaii

@guizmaii guizmaii commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

For DuckDB's list comprehensions (docs):

SELECT [x * 2 FOR x IN l IF x > 1], [x + i FOR x, i IN l] FROM t
  • New ListComprehension expression: element expression, one or two variables (element, 1-based index), list expression, optional IF condition.
  • A [ opens a comprehension only when FOR follows at its top level (isListComprehensionAhead(), one pass over the token chain; a 20,000-element array parses as fast as on master); every other [ is still an ArrayConstructor. [... FOR ...] was a syntax error in every dialect, so nothing that parsed before changes. SQL Server's [name] quoting is lexed before this and unaffected.
  • ExpressionVisitor.visit(ListComprehension) is a default method visiting the element, list and condition, so existing visitors keep compiling. The deparser, adapter, validator and TablesNamesFinder implement it (tables in a sub-select list are found).
  • checkGrammarAmbiguity: no conflicts. ./gradlew check: 10,120 tests, 0 failures.
  • DuckDB's own test suite (test/sql, 70,729 statements): 91 more parse, none less.

@guizmaii
guizmaii marked this pull request as ready for review October 13, 2026 08:57
Copilot AI balanced review requested due to automatic review settings October 13, 2026 08:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@guizmaii
guizmaii force-pushed the duckdb-list-comprehension branch 3 times, most recently from 327524b to bb13af8 Compare October 13, 2026 21:48
`[x * 2 FOR x IN l IF x > 1]` and `[x + i FOR x, i IN l]` parse as the
new `ListComprehension`. A `[` opens one when a `FOR` follows at its top
level; any other `[` is still an array. `visit(ListComprehension)` is a
default method that visits the element, list and condition, so existing
`ExpressionVisitor` implementations keep compiling.
`isListComprehensionAhead()` called `getToken(i)` for each token, which
walks from the current token every time: parsing a 10,000-element array
took seconds. It now follows the token chain.
@guizmaii
guizmaii force-pushed the duckdb-list-comprehension branch from bb13af8 to 75371f7 Compare October 13, 2026 21:57
@manticore-projects
manticore-projects merged commit e7a5a43 into JSQLParser:master Oct 13, 2026
10 checks passed
@manticore-projects

Copy link
Copy Markdown
Contributor

Thank you much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants