Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Client-v2] The newBinaryFormatReader can't correctly read SimpleAggregate values #2127

Open
Am-phi opened this issue Feb 1, 2025 · 0 comments · May be fixed by #2128
Open

[Client-v2] The newBinaryFormatReader can't correctly read SimpleAggregate values #2127

Am-phi opened this issue Feb 1, 2025 · 0 comments · May be fixed by #2128
Labels

Comments

@Am-phi
Copy link
Contributor

Am-phi commented Feb 1, 2025

Describe the bug

It's a reopening of #2125 that i closed by mistake.
The #2022 hasn't brought full support for simple aggregate functions. When using ClickHouseBinaryFormatReader to read query response. If the table contains a column with SimpleAggregateFunctions using functions like getLong returns an error message saying that:

Column counter SimpleAggregateFunction cannot be converted to long

This error seems to be also present for jdbc:

Caused by: com.clickhouse.client.api.ClientException: Column counter SimpleAggregateFunction cannot be converted to long
	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.readNumberValue(AbstractBinaryFormatReader.java:312) ~[jdbc-v2-0.8.0.jar:jdbc-v2 0.8.0 (revision: 5b5ac2e)]
	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.getLong(AbstractBinaryFormatReader.java:333) ~[jdbc-v2-0.8.0.jar:jdbc-v2 0.8.0 (revision: 5b5ac2e)]
	at com.clickhouse.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:293) ~[jdbc-v2-0.8.0.jar:clickhouse-jdbc 0.8.0 (revision: 5b5ac2e)]

The issue seems to be in the AbstractBinaryFormatReader.java class. A lots of functions are using column.getDataType() to determine what type of object is being read. But the implementation make that for a simpleAggregateFunction the column.getDataType always returns SimpleAggregateFunction, which makes that all functions using this are ending up in the default case.

Code example

This test is just adapted from the test case added in the #2022

    public void testReadingSimpleAggregateFunction2() throws Exception {
        final String tableName = "simple_aggregate_function_test_table";
        client.execute("DROP TABLE IF EXISTS " + tableName).get();
        client.execute("CREATE TABLE `" + tableName + "` " +
                "(idx UInt8, lowest_value SimpleAggregateFunction(min, UInt8), count SimpleAggregateFunction(sum, Int64), mp SimpleAggregateFunction(maxMap, Map(UInt8, UInt8))) " +
                "ENGINE Memory;").get();


        try (InsertResponse response = client.insert(tableName, new ByteArrayInputStream("1\t2\t3\t{1:2}".getBytes(StandardCharsets.UTF_8)), ClickHouseFormat.TSV).get(30, TimeUnit.SECONDS)) {
            Assert.assertEquals(response.getWrittenRows(), 1);
        }

        try (QueryResponse queryResponse = client.query("SELECT * FROM " + tableName + " LIMIT 1").get(30, TimeUnit.SECONDS)) {

            ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(queryResponse);
            Assert.assertNotNull(reader.next());
            Assert.assertEquals(reader.getByte("idx"), Byte.valueOf("1"));
            Assert.assertEquals((Short) reader.getShort("lowest_value"), Short.parseShort("2"));
            Assert.assertEquals((Long) reader.getLong("count"), Long.parseLong("3"));
            Assert.assertEquals(String.valueOf((LinkedHashMap) reader.readValue("mp")), "{1=2}");
            Assert.assertFalse(reader.hasNext());
        }
    }

Error log

com.clickhouse.client.api.ClientException: Column lowest_value SimpleAggregateFunction cannot be converted to short

	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.readNumberValue(AbstractBinaryFormatReader.java:312)
	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.getShort(AbstractBinaryFormatReader.java:323)
	at com.clickhouse.client.query.QueryTests.testReadingSimpleAggregateFunction2(QueryTests.java:1980)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:136)
	at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:658)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:219)
	at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
	at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:923)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:192)
	at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
	at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.testng.TestRunner.privateRun(TestRunner.java:808)
	at org.testng.TestRunner.run(TestRunner.java:603)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:429)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:423)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:383)
	at org.testng.SuiteRunner.run(SuiteRunner.java:326)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1249)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
	at org.testng.TestNG.runSuites(TestNG.java:1092)
	at org.testng.TestNG.run(TestNG.java:1060)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:65)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant