Skip to content
Open
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 @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Loading