From 648f3e90222b1a78d841a63155cc4727b0487788 Mon Sep 17 00:00:00 2001 From: Polyglot AI <293096396+polyglotAI-bot@users.noreply.github.com> Date: Tue, 4 Aug 2026 15:54:18 +0000 Subject: [PATCH] Fix jdbc-v2: report precision/scale of SimpleAggregateFunction-wrapped columns ResultSetMetaData.getPrecision()/getScale() read the outer ClickHouseColumn. SimpleAggregateFunction(func, T) parses into dataType=SimpleAggregateFunction with T kept as the nested column, and the wrapper's own data type declares no precision or scale, so both accessors returned 0 - losing the declared scale of DateTime64 and both precision and scale of Decimal. The wrapper is transparent on the read path, so the accessors now describe the nested column. AggregateFunction is left as is: its values are aggregation states, not values of the nested type. Fixes: https://github.com/ClickHouse/clickhouse-java/issues/3042 --- CHANGELOG.md | 7 +++++ .../jdbc/metadata/ResultSetMetaDataImpl.java | 19 ++++++++++-- .../metadata/ResultSetMetaDataImplTest.java | 31 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52c92a57a..2aefb7f34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,13 @@ ### Bug Fixes +- **[jdbc-v2]** Fixed `ResultSetMetaData.getPrecision()` and `getScale()` returning `0` for columns wrapped in + `SimpleAggregateFunction(func, T)`. The wrapper is transparent on the read path (values are read as plain `T`), but + both accessors described the wrapper itself, which carries no precision or scale — so a + `SimpleAggregateFunction(sum, Decimal(18, 4))` column looked like a scale-0 value and + `SimpleAggregateFunction(any, DateTime64(3, tz))` looked like second precision. They now describe the nested type. + `AggregateFunction` columns are unchanged, since their values are aggregation states rather than values of the + nested type. (https://github.com/ClickHouse/clickhouse-java/issues/3042) - **[client-v2]** Fixed LZ4 input streams not closing their underlying HTTP response stream. Closing an LZ4 stream returned by `QueryResponse.getInputStream()` now releases the wrapped transport stream, including after a partial read. (https://github.com/ClickHouse/clickhouse-java/issues/2985) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImpl.java index a0f64a17f..efba911c6 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImpl.java @@ -239,12 +239,27 @@ public String getSchemaName(int column) throws SQLException { @Override public int getPrecision(int column) throws SQLException { - return getColumn(column).getPrecision(); + return valueColumn(getColumn(column)).getPrecision(); } @Override public int getScale(int column) throws SQLException { - return getColumn(column).getScale(); + return valueColumn(getColumn(column)).getScale(); + } + + /** + * Returns the column describing the values a caller actually reads. {@code SimpleAggregateFunction(func, T)} + * stores and returns plain {@code T} values, so precision and scale are declared by the nested column - the + * wrapper itself carries none. {@code AggregateFunction} is not transparent (its values are aggregation states) + * and is therefore returned as is. + */ + private static ClickHouseColumn valueColumn(ClickHouseColumn column) { + ClickHouseColumn current = column; + while (current.getDataType() == ClickHouseDataType.SimpleAggregateFunction + && !current.getNestedColumns().isEmpty()) { + current = current.getNestedColumns().get(0); + } + return current; } @Override diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImplTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImplTest.java index e515cc7f7..41313b357 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImplTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/ResultSetMetaDataImplTest.java @@ -2,6 +2,7 @@ import com.clickhouse.jdbc.JdbcIntegrationTest; import org.testng.Assert; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.sql.Connection; @@ -237,6 +238,36 @@ public void testGetColumnScale() throws Exception { } } + @DataProvider(name = "wrappedTypePrecisionAndScale") + public static Object[][] wrappedTypePrecisionAndScaleProvider() { + return new Object[][] { + { "CAST(toDecimal64(1, 4) AS Decimal(18, 4))", 18, 4 }, + { "CAST(toDecimal64(1, 4) AS SimpleAggregateFunction(any, Decimal(18, 4)))", 18, 4 }, + { "CAST(toDecimal128(1, 10) AS SimpleAggregateFunction(sum, Decimal(38, 10)))", 38, 10 }, + { "CAST(NULL AS SimpleAggregateFunction(any, Nullable(Decimal(18, 4))))", 18, 4 }, + { "CAST(now() AS SimpleAggregateFunction(any, DateTime('Europe/Amsterdam')))", 29, 0 }, + { "CAST(now64(3, 'Europe/Amsterdam') AS " + + "SimpleAggregateFunction(any, DateTime64(3, 'Europe/Amsterdam')))", 29, 3 }, + { "CAST('abc' AS SimpleAggregateFunction(any, FixedString(16)))", 16, 0 }, + { "CAST([toDecimal64(1, 4)] AS " + + "SimpleAggregateFunction(groupArrayArray, Array(Decimal(18, 4))))", 0, 0 }, + { "toLowCardinality(toDateTime('2024-01-15 12:30:45', 'Europe/Amsterdam'))", 29, 0 }, + { "sumState(toDecimal64(1, 4))", 0, 0 }, + }; + } + + @Test(groups = { "integration" }, dataProvider = "wrappedTypePrecisionAndScale") + public void testGetPrecisionAndScaleOfWrappedTypes(String expression, int precision, int scale) throws Exception { + try (Connection conn = getJdbcConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT " + expression + " AS v")) { + ResultSetMetaData rsmd = rs.getMetaData(); + String type = rsmd.getColumnTypeName(1); + assertEquals(rsmd.getPrecision(1), precision, type); + assertEquals(rsmd.getScale(1), scale, type); + } + } + @Test(groups = { "integration" }) public void testColumnNamesStrippedFromTablePrefix() throws Exception { final String t1 = "rsmd_test_prefix_t1";