Commit c58efe9
committed
fix: allow scalar subquery as LIMIT row count (#2359)
PostgreSQL allows any expression, including a scalar subquery, as the LIMIT
row count, e.g. LIMIT (SELECT COUNT(*) FROM t WHERE ...) or
LIMIT GREATEST(0, (SELECT ...)). JSQLParser rejected these.
PlainSelect disambiguated the ClickHouse "LIMIT ... BY ..." branch from a
plain limit with a numeric LOOKAHEAD(7) on LimitBy(). A numeric lookahead
cannot see past a long parenthesized subquery, so for LIMIT (subquery) it
wrongly committed to the LIMIT BY branch and then failed at the missing BY
keyword. (Short subqueries such as LIMIT (SELECT 1) happened to stay under
the lookahead window and worked, which is why the bug only surfaced for
longer ones.)
The disambiguation is moved to where it belongs: parse the LIMIT row count
once via LimitWithOffset() (which already accepts a parenthesized
subquery), then check the immediately following token for BY. A token scan
for BY would not work, because a subquery may contain ORDER BY (a K_BY).
The now-unused LimitBy() production is removed; LimitWithOffset already
carries byExpressions, so no AST or public API change.
All LIMIT shapes keep working: LIMIT n, LIMIT n, m, LIMIT n OFFSET m,
OFFSET m LIMIT n, LIMIT ALL, and ClickHouse LIMIT n BY ... / LIMIT n, m BY ...
Fixes #2359
Signed-off-by: 付典 <fudianchn@gmail.com>1 parent 7ced34d commit c58efe9
2 files changed
Lines changed: 46 additions & 20 deletions
File tree
- src
- main/jjtree/net/sf/jsqlparser/parser
- test/java/net/sf/jsqlparser/expression
Lines changed: 13 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5383 | 5383 | | |
5384 | 5384 | | |
5385 | 5385 | | |
5386 | | - | |
5387 | | - | |
| 5386 | + | |
| 5387 | + | |
| 5388 | + | |
| 5389 | + | |
| 5390 | + | |
| 5391 | + | |
| 5392 | + | |
| 5393 | + | |
| 5394 | + | |
| 5395 | + | |
| 5396 | + | |
| 5397 | + | |
| 5398 | + | |
5388 | 5399 | | |
5389 | 5400 | | |
5390 | 5401 | | |
| |||
6773 | 6784 | | |
6774 | 6785 | | |
6775 | 6786 | | |
6776 | | - | |
6777 | | - | |
6778 | | - | |
6779 | | - | |
6780 | | - | |
6781 | | - | |
6782 | | - | |
6783 | | - | |
6784 | | - | |
6785 | | - | |
6786 | | - | |
6787 | | - | |
6788 | | - | |
6789 | | - | |
6790 | | - | |
6791 | | - | |
6792 | | - | |
6793 | | - | |
6794 | 6787 | | |
6795 | 6788 | | |
6796 | 6789 | | |
| |||
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
34 | 67 | | |
35 | 68 | | |
36 | 69 | | |
| |||
0 commit comments