fix: treat a positional parameter '?' as a valid end of the ternary then-branch - #2477
Merged
manticore-projects merged 1 commit intoAug 16, 2026
Conversation
…hen-branch A bare '?' (anonymous token, no kind constant) was missing from canEndExpression(), so a ternary whose then-branch ends with a positional parameter was silently misread as a jsonb operator: SELECT a ? ? : c FROM t parsed as JsonOperator(a, JsonExpression(?:c)) instead of TernaryExpression(a, JdbcParameter, c). Recognize it by image, like the closing brackets, and pin the AST shape with a regression test. Follow-up to JSQLParser#2476. Signed-off-by: 付典 <fudianchn@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A ternary whose then-branch ends with a plain positional parameter parses as a
TernaryExpressionagain:Why / Root cause
Follow-up to #2476, found while regression-testing the snapshot build (reported in #2476 (comment)):
canEndExpression()whitelistedS_PARAMETER($Nstyle) but not the bare?. The bare?is an anonymous token (no token-kind constant, outside the non-reserved range), so the scan skipped the:closing the then-branch and the parser silently committed to the jsonb reading. On current master the statements above parse asJsonOperator(a, JsonExpression(?:c))while7b64825still parsed them asTernaryExpression(a, JdbcParameter, c)(regression). In the wrong tree the?is no longer aJdbcParameternode (relevant for parameter extraction), and the wrong AST deparse round-trips stably (a ? ?:c), which is why the existing round-trip case did not catch it.How
Add
|| "?".equals(t.image)to the default branch ofcanEndExpression(), the same image comparison already used for")"and"]": a positional parameter is a complete primary expression, so a:directly after it closes the then-branch. The?|/?&operators have different images and are unaffected. The statements fixed by #2476 are unaffected as well: there the:followsnullor an operator token, so the new check is not reached (j ? :key,j ? 'k' AND x = :p,j ? ?all keep their current reading).Scope
One condition in the ternary-vs-jsonb disambiguation predicate plus an AST-shape regression test; grammar productions and AST classes are untouched.
Testing
TernaryExpressionTest.testTernaryWithPositionalParameterThenBranchpins the AST shape (rootTernaryExpression, then-branchJdbcParameter/Addition, else-branchColumn): it fails on current master withClassCastException: JsonOperator cannot be cast to TernaryExpressionand passes with this change. The round-trip list gainsSELECT a ? b + ? : c FROM tandSELECT a ? ? : ? FROM t. All 45TernaryExpressionTesttests pass with this change.Performance
gradle jmh,JSQLParserBenchmark.parseSQLStatementsonperformance.sql,version=latest, 10 forks x 10 iterations (100 samples per run, two interleaved runs per build) on a 32-core host:master(5080d19)The two builds interleave across the repeated runs (pair 1:
+0.78%, pair 2:-0.32%); every delta lies within the 99.9% confidence intervals: no regression.