Skip to content

DuckDB: support LIMIT 10% and TABLESAMPLE without a method - #2802

Merged
manticore-projects merged 2 commits into
JSQLParser:masterfrom
guizmaii:duckdb-limit-percent-tablesample
Oct 13, 2026
Merged

manticore-projects merged 2 commits into
JSQLParser:masterfrom
guizmaii:duckdb-limit-percent-tablesample

Conversation

@guizmaii

@guizmaii guizmaii commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

For DuckDB's percentage limits (docs) and TABLESAMPLE forms (docs).

Input Before After
LIMIT 10%, LIMIT 40 PERCENT, LIMIT 50% OFFSET 2 error Limit with getPercentUnit() % / PERCENT
TABLESAMPLE 10%, TABLESAMPLE 10 ROWS, TABLESAMPLE reservoir(10 ROWS) REPEATABLE (1) error SampleClause, method optional as for USING SAMPLE
FROM range(10) r USING SAMPLE 10% sample silently dropped kept and printed
  • LIMIT scans its clause once for a top-level % directly followed by the end of the clause (end of input, ;, ), a set operator, OFFSET followed by a value, FETCH FIRST/NEXT); the Pratt loop stops at that token only. That % used to be a modulo missing its right operand, i.e. an error, so LIMIT 10 % 3, LIMIT a % offset + 1 (a column named offset) and modulos in nested sub-selects parse as before. After DuckDB's TABLESAMPLE n, OFFSET belongs to the query.
  • TableFunction implemented setSampleClause as a no-op; it now stores and prints the clause.
  • checkGrammarAmbiguity: no conflicts. ./gradlew check: 10,118 tests, 0 failures. On ~9,760 statements extracted from this repository's tests, results are identical to master.
  • DuckDB's own test suite (test/sql, 70,729 statements): 50 more parse, none less.

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

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.

- `LIMIT 10%` and `LIMIT 10 PERCENT` set the new `Limit.percentUnit`.
  Inside a LIMIT, a `%` followed by the end of the clause is the unit,
  not a modulo; `LIMIT 10 % 3` and `LIMIT a % offset` are unchanged.
- TABLESAMPLE takes DuckDB's optional method and reservoir, as USING
  SAMPLE already does: `TABLESAMPLE 10%`, `TABLESAMPLE reservoir(10 ROWS)`.
- A table function keeps and prints its sample clause: `range(10) r
  USING SAMPLE 10%` used to lose it.
- LIMIT scans its clause once for the top-level % followed by its end,
  and the Pratt loop stops at that token only, so `LIMIT a % offset + 1`
  and modulos nested in sub-selects are unchanged.
- OFFSET ends the clause only before a value, FETCH only before FIRST or
  NEXT.
- After DuckDB's TABLESAMPLE n, OFFSET belongs to the query.
- The unit is printed after NULL and ALL too.
@guizmaii
guizmaii force-pushed the duckdb-limit-percent-tablesample branch from f26ed7f to 4566175 Compare October 13, 2026 10:50
@manticore-projects
manticore-projects merged commit 49b5ea9 into JSQLParser:master Oct 13, 2026
10 checks passed
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