You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In the JDBC query result set, columns of type AggregateFunction cannot be parsed normally #2662
bugclientModule: the client (client-v2; with legacy: the v1 client)data-typesArea: type names, value encoding and decoding, row mapping, data formatsjdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)native-formatNative code: the Native format codec (blocks, type names, column bytes)
When creating a materialized view, specify the Select statement as follows:
SELECT
toDate(timestamp) ASdate,
countState(*) AS count,
quantileState(0.9)(duration) AS tp90_duration_ms,
quantileState(0.99)(duration) AS tp99_duration_ms,
avgState(duration) AS avg_duration_ms,
maxState(duration) AS max_duration_ms,
minState(duration) AS min_duration_ms
FROMtest.test_table_localGROUP BYdate
This statement is queried directly with JDBC, and the final error is:
java.sql.SQLException: Failed to read value for column count
final debug result:
BinaryStreamReader.java -> readValue method
case AggregateFunction:
return (T)this.readBitmap(actualColumn);
This issue has been automatically marked as stale because it has not had activity in the last year. It will be closed in 30 days if no further activity occurs. Please feel free to leave a comment if you believe the issue is still relevant. Thank you for your contributions!
Recommendation: Keep open — fix needed
The reader decodes every AggregateFunction state as a groupBitmap state, although the docs support only groupBitmap. So countState fails with an unclear error, and other states give wrong rows with no error. Reading the other states is the separate request #2599.
Category: Bug
Classification: impact: Data integrity · area: Data types & formats (AggregateFunction) · native: format
Status on main: Reproduced on main — the issue's SELECT fails at count; alone, quantileState gives 6 rows and avgState, maxState, minState give 0 rows, where the server returns 1 row.
Workaround: Read the states with -Merge or finalizeAggregation (docs/clickhouse-docs/jdbc.mdx:1343); the run gives 49.5 for finalizeAggregation(avgState(duration)).
Analysis:BinaryStreamReader.java:292 (client-v2, which jdbc-v2 uses) reads each AggregateFunction value with readBitmap (:1572), which takes the type of the first argument: count has no argument, so it throws IndexOutOfBoundsException, and the driver reports only "Failed to read value for column count". For other functions it reads the state bytes as a bitmap, so the stream loses synchronization; an EOFException in the first column then counts as the end of the data (AbstractBinaryFormatReader.java:203), with no error. The Native format fails with an error for the same states.
Reproduction: complete program and output
Changed from the issue's steps: added a table with data for the issue's SELECT, each state alone, groupBitmapState, finalizeAggregation, and two client-v2 reads.
importcom.clickhouse.client.api.Client;
importcom.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
importcom.clickhouse.client.api.query.QueryResponse;
importcom.clickhouse.client.api.query.QuerySettings;
importcom.clickhouse.data.ClickHouseFormat;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.Statement;
importjava.util.Properties;
publicclassRepro {
staticfinalStringURL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
staticfinalStringPASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
publicstaticvoidmain(String[] args) throwsException {
Propertiesprops = newProperties();
props.setProperty("user", "default");
props.setProperty("password", PASSWORD);
StringjdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
try (Connectionconn = DriverManager.getConnection(jdbcUrl, props); Statementst = conn.createStatement()) {
st.execute("DROP TABLE IF EXISTS test_table_local");
st.execute("CREATE TABLE test_table_local (timestamp DateTime, duration UInt64) ENGINE = MergeTree ORDER BY timestamp");
st.execute("INSERT INTO test_table_local SELECT toDateTime('2025-11-19 00:00:00') + number, number FROM numbers(100)");
// The SELECT of the materialized view in the issuejdbc(st, "SELECT toDate(timestamp) AS date, countState(*) AS count, quantileState(0.9)(duration) AS tp90_duration_ms, "
+ "quantileState(0.99)(duration) AS tp99_duration_ms, avgState(duration) AS avg_duration_ms, "
+ "maxState(duration) AS max_duration_ms, minState(duration) AS min_duration_ms FROM test_table_local GROUP BY date");
// Each state of the issue alone (the server returns 1 row), groupBitmapState, and finalizeAggregationfor (Stringf : newString[] {"countState()", "quantileState(0.9)(duration)", "avgState(duration)", "maxState(duration)",
"minState(duration)", "groupBitmapState(duration)", "finalizeAggregation(avgState(duration))"}) {
jdbc(st, "SELECT " + f + " FROM test_table_local");
}
}
try (Clientclient = newClient.Builder().addEndpoint(URL).setUsername("default").setPassword(PASSWORD).build()) {
for (ClickHouseFormatformat : newClickHouseFormat[] {ClickHouseFormat.RowBinaryWithNamesAndTypes, ClickHouseFormat.Native}) {
Stringsql = "SELECT avgState(duration) FROM test_table_local";
try (QueryResponseresponse = client.query(sql, newQuerySettings().setFormat(format)).get()) {
ClickHouseBinaryFormatReaderreader = client.newBinaryFormatReader(response);
introws = 0;
while (reader.next() != null) rows++;
System.out.println("client-v2 " + format + " " + sql + " -> rows=" + rows);
} catch (Exceptione) {
System.out.println("client-v2 " + format + " " + sql + " -> " + e.getClass().getName() + ": " + e.getMessage());
}
}
client.execute("DROP TABLE test_table_local").get().close();
}
}
staticvoidjdbc(Statementst, Stringsql) {
Stringname = sql.length() > 70 ? sql.substring(0, 70) + "…" : sql;
try (ResultSetrs = st.executeQuery(sql)) {
introws = 0;
Objectlast = null;
while (rs.next()) {
rows++;
last = rs.getObject(1);
}
System.out.println("jdbc " + name + " -> rows=" + rows
+ (last != null ? ", value " + (lastinstanceofNumber ? last : last.getClass().getSimpleName()) : ""));
} catch (Exceptione) {
System.out.println("jdbc " + name + " -> " + e.getClass().getName() + ": " + e.getMessage()
+ (e.getCause() != null && e.getCause().getCause() != null ? " | cause " + e.getCause().getCause() : ""));
}
}
}
Output on main (8480ca001), ClickHouse 26.11.4.11:
jdbc SELECT toDate(timestamp) AS date, countState(*) AS count, quantileStat… -> java.sql.SQLException: Failed to read value for column count | cause java.lang.IndexOutOfBoundsException: Index: 0
jdbc SELECT countState() FROM test_table_local -> java.sql.SQLException: Failed to read value for column countState() | cause java.lang.IndexOutOfBoundsException: Index: 0
jdbc SELECT quantileState(0.9)(duration) FROM test_table_local -> rows=6, value ClickHouseRoaring64NavigableMap
jdbc SELECT avgState(duration) FROM test_table_local -> rows=0
jdbc SELECT maxState(duration) FROM test_table_local -> rows=0
jdbc SELECT minState(duration) FROM test_table_local -> rows=0
jdbc SELECT groupBitmapState(duration) FROM test_table_local -> rows=1, value ClickHouseRoaring64NavigableMap
jdbc SELECT finalizeAggregation(avgState(duration)) FROM test_table_local -> rows=1, value 49.5
client-v2 RowBinaryWithNamesAndTypes SELECT avgState(duration) FROM test_table_local -> rows=0
client-v2 Native SELECT avgState(duration) FROM test_table_local -> com.clickhouse.client.api.ClientException: Failed to read block
Automated triage by Polyglot AI. A maintainer makes the final decision.
bugclientModule: the client (client-v2; with legacy: the v1 client)data-typesArea: type names, value encoding and decoding, row mapping, data formatsjdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)native-formatNative code: the Native format codec (blocks, type names, column bytes)
Activity
chernser commentedon Nov 19, 2025
@CCweixiao
Thank you for reporting this!
We will look into it.
github-actions commentedon Nov 20, 2026
This issue has been automatically marked as stale because it has not had activity in the last year. It will be closed in 30 days if no further activity occurs. Please feel free to leave a comment if you believe the issue is still relevant. Thank you for your contributions!
polyglotAI-bot commentedon Nov 28, 2026
Recommendation: Keep open — fix needed
The reader decodes every
AggregateFunctionstate as agroupBitmapstate, although the docs support onlygroupBitmap. SocountStatefails with an unclear error, and other states give wrong rows with no error. Reading the other states is the separate request #2599.main: Reproduced on main — the issue'sSELECTfails atcount; alone,quantileStategives 6 rows andavgState,maxState,minStategive 0 rows, where the server returns 1 row.-MergeorfinalizeAggregation(docs/clickhouse-docs/jdbc.mdx:1343); the run gives49.5forfinalizeAggregation(avgState(duration)).Analysis:
BinaryStreamReader.java:292(client-v2, which jdbc-v2 uses) reads eachAggregateFunctionvalue withreadBitmap(:1572), which takes the type of the first argument:counthas no argument, so it throwsIndexOutOfBoundsException, and the driver reports only "Failed to read value for column count". For other functions it reads the state bytes as a bitmap, so the stream loses synchronization; anEOFExceptionin the first column then counts as the end of the data (AbstractBinaryFormatReader.java:203), with no error. TheNativeformat fails with an error for the same states.Reproduction: complete program and output
Changed from the issue's steps: added a table with data for the issue's
SELECT, each state alone,groupBitmapState,finalizeAggregation, and two client-v2 reads.Output on
main(8480ca001), ClickHouse 26.11.4.11:Automated triage by Polyglot AI. A maintainer makes the final decision.