-
Notifications
You must be signed in to change notification settings - Fork 631
Fix jdbc-v2: discard values list positions that do not address the original SQL #3018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
2f48658
c1eb908
7d2edf6
8ac80e0
ccbc639
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,9 +123,69 @@ | |
|
|
||
| stmt.setUseFunction(parsedStmt.isFuncUsed()); | ||
| parseParameters(sql, stmt); | ||
| discardValuesListPositionsNotMatchingOriginalSql(sql, stmt); | ||
| return stmt; | ||
| } | ||
|
|
||
| /** | ||
| * The token manager records keyword positions as offsets into the SQL it rebuilds from the token stream, which | ||
| * is not always identical to the SQL it was given: semicolons are dropped and JDBC escape sequences are | ||
| * rewritten. Consumers of the values list positions slice the original SQL, so when the two have drifted apart | ||
| * the positions address the wrong characters or point past the end of the string. Discard them in that case to | ||
| * let the generic parameter substitution path handle the statement. | ||
| */ | ||
| private void discardValuesListPositionsNotMatchingOriginalSql(String sql, ParsedPreparedStatement stmt) { | ||
| int startPosition = stmt.getAssignValuesListStartPosition(); | ||
| int stopPosition = stmt.getAssignValuesListStopPosition(); | ||
| if (startPosition < 0 || stopPosition < 0) { | ||
| return; | ||
| } | ||
|
|
||
| boolean matches = stopPosition > startPosition && stopPosition < sql.length() | ||
| && sql.charAt(startPosition) == '(' && closesParenthesizedGroup(sql, startPosition, stopPosition); | ||
| if (matches) { | ||
| int[] paramPositions = stmt.getParamPositions(); | ||
| for (int i = 0; i < stmt.getArgCount(); i++) { | ||
| if (paramPositions[i] < startPosition || paramPositions[i] > stopPosition) { | ||
| matches = false; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!matches) { | ||
| LOG.debug("Values list positions [{}, {}] do not match the original SQL", startPosition, stopPosition); | ||
| stmt.setAssignValuesListStartPosition(-1); | ||
| stmt.setAssignValuesListStopPosition(-1); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fallback batch clear still breaksHigh Severity Discarding mismatched values list positions forces the generic Reviewed by Cursor Bugbot for commit 8ac80e0. Configure here.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The List<StringBuilder> l = Collections.emptyList();
l.clear(); // returns, size = 0The point about the test stopping at The rows of Leaving the thread open for a reviewer to close. |
||
| } | ||
|
|
||
| /** | ||
| * Tells whether the parenthesis opened at {@code startPosition} is closed exactly at {@code stopPosition}, | ||
| * ignoring parentheses inside quoted text. | ||
| */ | ||
| private boolean closesParenthesizedGroup(String sql, int startPosition, int stopPosition) { | ||
| int depth = 0; | ||
| try { | ||
| for (int i = startPosition; i <= stopPosition; i++) { | ||
| char ch = sql.charAt(i); | ||
| if (ClickHouseUtils.isQuote(ch)) { | ||
| i = ClickHouseUtils.skipQuotedString(sql, i, sql.length(), ch) - 1; | ||
|
Check warning on line 173 in jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/SqlParserFacade.java
|
||
| if (i > stopPosition) { | ||
| return false; | ||
| } | ||
| } else if (ch == '(') { | ||
| depth++; | ||
| } else if (ch == ')' && --depth == 0) { | ||
| return i == stopPosition; | ||
| } | ||
| } | ||
| } catch (IllegalArgumentException e) { // unterminated quoted text | ||
| return false; | ||
| } | ||
| return false; | ||
| } | ||
|
polyglotAI-bot marked this conversation as resolved.
|
||
|
|
||
| private List<String> processRoles(Map<String, String> settings) { | ||
| String rolesCount = settings.get("_ROLES_COUNT"); | ||
| if (rolesCount != null) { | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discard leaves values groups set
Medium Severity
When values list positions are discarded,
assignValuesGroupsstays at1.ConnectionImplstill treats that as a simple single-group insert for the beta RowBinary writer wheneveruseFunctionis false, so statements likeVALUES ({d:Int32}, ?)can be routed toWriterStatementImplinstead of the generic substitution path. The ANTLR discard helper already clears the group count for this reason.Reviewed by Cursor Bugbot for commit 8ac80e0. Configure here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked this empirically rather than by inspection, and it does not reproduce.
The discard only fires when the recorded positions do not delimit the values list of the original SQL, which requires the rebuilt SQL to differ in length at or before the values list — in practice a JDBC escape sequence inside the values list. I probed every such statement the JavaCC backend parses without errors (the rejected rows of
testValuesListPositionsDP), printing whatConnectionImplroutes on:useFunctionistruein every case — the escape has to sit inside a function call for the statement to reach this code at all — soConnectionImplnever takes theWriterStatementImplbranch, whatever the group count says.The example in the comment,
VALUES ({d:Int32}, ?), never reaches the discard: a bare escape at the start of a value group makes the JavaCC token manager recordKEYWORD_VALUES_STARTwithoutKEYWORD_VALUES_END, andparsePreparedStatementthrowsNullPointerExceptionon the missing end key before the check runs. That is the separate defect of #3015, fixed in #3014.The parallel with the ANTLR4 helper does not hold either. There the group count comes from an error-recovered parse tree and is itself untrustworthy, so it is cleared. Here the parse succeeded and the count is correct — exactly one value group — and only the offsets drifted. Setting it to
0would record something false, and would change the beta RowBinary routing of statements whose positions were never used by that path (WriterStatementImpldoes not read the values list positions), with no case that can show the difference. Keeping the change limited to the positions that are actually wrong.