Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@

### Bug Fixes

- **[jdbc-v2]** Fixed `PreparedStatement.getMetaData()` losing the result-set schema for a statement whose SQL
contains a comment. The `DESCRIBE` query used to resolve the metadata was built by re-scanning the SQL with a
regex that knew only quoted tokens, so a `?` inside a `--` / `#` / `/* */` comment was rewritten to `NULL` and
an odd `'` inside a comment mis-paired the quote alternative, leaving a real placeholder unreplaced — the
`DESCRIBE` then failed and the driver silently returned untyped metadata. The metadata query is now built from
the placeholder positions the statement parser already computed, so it always matches the SQL that a
parameterized execution produces. (https://github.com/ClickHouse/clickhouse-java/issues/3011)
- **[client-v2]** Fixed reading a `UInt64` column into a **primitive** POJO field (e.g. `long`) always failing with
`ClassCastException: BinaryStreamReader cannot be cast to java.math.BigInteger`. The compiled setter for that
combination never emitted a read call, so it cast the reader itself instead of a value and consumed nothing from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ private String buildSQL() throws SQLException {
return compiledSql.toString();
}

private String substituteParameters(String value) {
StringBuilder compiledSql = new StringBuilder(originalSql);
int posOffset = 0;
int[] positions = parsedPreparedStatement.getParamPositions();
for (int i = 0; i < argCount; i++) {
int p = positions[i] + posOffset;
compiledSql.replace(p, p + 1, value);
posOffset += value.length() - 1;
}
return compiledSql.toString();
}

@Override
public ResultSet executeQuery() throws SQLException {
ensureOpen();
Expand Down Expand Up @@ -411,7 +423,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
if (parsedPreparedStatement.isHasResultSet()) {
try {
// Replace '?' with NULL to make SQL valid for DESCRIBE
String sql = replaceQuestionMarks(originalSql, NULL_LITERAL);
String sql = substituteParameters(NULL_LITERAL);
TableSchema tSchema = connection.getClient().getTableSchemaFromQuery(sql);
resultSetMetaData = new ResultSetMetaDataImpl(tSchema.getColumns(),
connection.getSchema(), connection.getCatalog(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,30 @@ public void testParameterCount() throws Exception {
}
}

@Test(dataProvider = "testGetMetadataIgnoresCommentsDataProvider")
public void testGetMetadataIgnoresComments(String sql) throws Exception {
try (Connection conn = getJdbcConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
ResultSetMetaData metaData = stmt.getMetaData();
assertEquals(metaData.getColumnCount(), 3);
assertEquals(metaData.getColumnName(1), "x");
assertEquals(metaData.getColumnName(2), "y");
assertEquals(metaData.getColumnName(3), "z");
assertEquals(metaData.getColumnType(1), Types.VARCHAR);
assertEquals(metaData.getColumnType(3), Types.VARCHAR);
}
}

@DataProvider(name = "testGetMetadataIgnoresCommentsDataProvider")
static Object[][] testGetMetadataIgnoresCommentsDataProvider() {
return new Object[][] {
{"SELECT 'a' AS x -- it's ?\n, ? AS y, 'z' AS z"},
{"SELECT 'a' AS x # it's ?\n, ? AS y, 'z' AS z"},
{"SELECT 'a' AS x /* it's ? */, ? AS y, 'z' AS z"},
{"SELECT 'a?b' AS x, ? AS y, 'z' AS z"}
};
}

@Test
public void testEncodingArray() throws Exception {
try (Connection conn = getJdbcConnection();) {
Expand Down
Loading