Repository navigation
Description
Description
SqlParserFacade.parseParameters (jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/SqlParserFacade.java:467-491) is a linear scan that finds ? JDBC placeholders. It skips:
'/"/`quoted tokens (viaClickHouseUtils.skipQuotedString, which does handle\escapes and doubled quotes),--and#line comments,/* ... */block comments (nesting-aware, viaClickHouseUtils.skipMultiLineComment).
It does not know two token kinds the server lexer accepts:
//line comments (SELECT 1 //x→1on the server),$tag$ ... $tag$heredocs / dollar-quoted strings (SELECT $$?$$→?on the server).
Consequently a ? that appears inside a // comment or inside a heredoc is counted as a bind parameter. PreparedStatement then expects a value that the application has no way to supply, and executeQuery() fails with SQLException: Parameter at position '1' is not set for a query the server executes fine.
This is the Java counterpart of ClickHouse/clickhouse-connect#925, where the same comment-stripping gap misclassified queries.
Note: nested /* /* */ */ and # comments are handled correctly here, so this is narrower than the Python issue. It is also distinct from #2835 (which is about -- / /* */; those cases return argCount=0 today).
ClickHouse server version
26.7.2.59 (verified the server accepts both queries):
$ curl -s --data-binary 'SELECT number FROM numbers(3) WHERE number = 1 // ?' http://localhost:8123/
1
$ curl -s --data-binary 'SELECT $$?$$ AS v' http://localhost:8123/
?
Driver: current main (0.10.0-rc1-SNAPSHOT).
Reproduction
jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/CommentScanTest.java:
package com.clickhouse.jdbc.internal;
import org.testng.annotations.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import static org.testng.Assert.assertEquals;
public class CommentScanTest {
static { com.clickhouse.jdbc.Driver.load(); }
private static int argCount(String parser, String sql) throws Exception {
SqlParserFacade p = SqlParserFacade.getParser(parser,
new JdbcConfiguration("jdbc:ch:http://localhost:8123", new Properties()));
return p.parsePreparedStatement(sql).getArgCount();
}
@Test
public void testPlaceholderInsideSlashSlashCommentAndHeredoc() throws Exception {
// control cases that already work
assertEquals(argCount("JAVACC", "SELECT 1 -- ?"), 0);
assertEquals(argCount("JAVACC", "SELECT 1 # ?"), 0);
assertEquals(argCount("JAVACC", "SELECT 1 /* a /* ? */ ? */"), 0);
// failing cases
assertEquals(argCount("JAVACC", "SELECT 1 // ?"), 0); // actual: 1
assertEquals(argCount("JAVACC", "SELECT $$?$$"), 0); // actual: 1
assertEquals(argCount("JAVACC", "SELECT $tag$?$tag$"), 0); // actual: 1
}
@Test
public void testEndToEndSlashSlashComment() throws Exception {
try (Connection c = DriverManager.getConnection("jdbc:clickhouse://localhost:8123");
PreparedStatement ps = c.prepareStatement("SELECT number FROM numbers(3) WHERE number = 1 // ?");
ResultSet rs = ps.executeQuery()) {
int n = 0;
while (rs.next()) { n++; }
assertEquals(n, 1);
}
}
@Test
public void testEndToEndHeredoc() throws Exception {
try (Connection c = DriverManager.getConnection("jdbc:clickhouse://localhost:8123");
PreparedStatement ps = c.prepareStatement("SELECT $$?$$ AS v");
ResultSet rs = ps.executeQuery()) {
rs.next();
assertEquals(rs.getString(1), "?");
}
}
}Expected: 1 row / ?, no exception.
Actual (both end-to-end tests):
java.sql.SQLException: Parameter at position '1' is not set
Measured getArgCount() per parser backend (jdbc_sql_parser), 0 expected everywhere:
| query | JAVACC (default) | ANTLR4 | ANTLR4_PARAMS_PARSER |
|---|---|---|---|
SELECT 1 -- ? |
0 | 0 | 0 |
SELECT 1 # ? |
0 | 0 | 0 |
SELECT 1 /* a /* ? */ ? */ |
0 | 0 | 0 |
SELECT 1 // ? |
1 | 1 | 0 |
SELECT $$?$$ |
1 | 1 | 1 |
SELECT $tag$?$tag$ |
1 | 1 | 0 |
JAVACC and ANTLR4 share parseParameters, hence the identical columns. ANTLR4_PARAMS_PARSER takes placeholders from the ANTLR token stream instead, so it survives // (the lexer errors out and the ? is not reached as a placeholder in this case) and $tag$, but still misreads $$?$$.
Suggested fix
In SqlParserFacade.parseParameters (SqlParserFacade.java:483-489), treat / followed by / as a line comment in the same branch as --, and add a $tag$ ... $tag$ heredoc skip before the quote handling ($ is currently not special, so a heredoc body is scanned as ordinary SQL and any '/`/" inside it can also derail the scan or make skipQuotedString throw IllegalArgumentException: Missing quote).
The ANTLR lexer has the same gaps and may be worth aligning: ClickHouseLexer.g4:493-494 has no // alternative in SINGLE_LINE_COMMENT, no heredoc token, and MULTI_LINE_COMMENT : '/*' .*? '*/' is non-nesting (the server nests /* a /* b */ still comment */).
Link
Related: ClickHouse/clickhouse-connect#925 · adjacent to #2835
Activity
Fix jdbc-v2: skip // comments and heredocs when scanning for ? placeh…