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] ClassCastException in AbstractBinaryFormatReader#readNumberValue for Boolean column #1908

Closed
voytektr opened this issue Nov 5, 2024 · 2 comments · Fixed by #1913
Assignees
Milestone

Comments

@voytektr
Copy link

voytektr commented Nov 5, 2024

Describe the bug

The method com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader#readNumberValue performs class cast to java.lang.Number. For a Bool column this result in a class cast exception

Steps to reproduce

  1. Create table with Bool column
  2. read Boolean values

Expected behaviour

corrected bool values are read by client

Code example

        var reader = client.newBinaryFormatReader(response);

        while (reader.next() != null) {
             //...
             reader.getBoolean("bool_col");
             //...
        }

Problematic code in com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader#readNumberValue

    @Override
    public boolean getBoolean(String colName) {
        return readNumberValue(colName, NumberType.Boolean);
    }
//...

    private <T> T readNumberValue(String colName, NumberType targetType) {
        int colIndex = schema.nameToIndex(colName);
        Function<Number, Number> converter = (Function<Number, Number>) convertions[colIndex].get(targetType);
        if (converter != null) {
            Number value = readValue(colName);
    //..

Configuration

Environment

  • Client version: 0.7.1
  • Language version: java 21 / kotlin
@voytektr voytektr added the bug label Nov 5, 2024
@chernser chernser self-assigned this Nov 5, 2024
@chernser chernser added this to the 0.7.2 milestone Nov 5, 2024
@chernser chernser modified the milestones: 0.7.2, 0.7.1-patch Nov 5, 2024
@chernser
Copy link
Contributor

chernser commented Nov 6, 2024

Here is another example:

java.sql.SQLException: java.lang.ClassCastException: class java.lang.Boolean cannot be cast to class java.lang.Number (java.lang.Boolean and java.lang.Number are in module java.base of loader 'bootstrap')
	at com.clickhouse.jdbc.ResultSetImpl.getBoolean(ResultSetImpl.java:95)
	at com.clickhouse.jdbc.PreparedStatementTest.testSetBoolean(PreparedStatementTest.java:41)
	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:568)
	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)
Caused by: java.lang.ClassCastException: class java.lang.Boolean cannot be cast to class java.lang.Number (java.lang.Boolean and java.lang.Number are in module java.base of loader 'bootstrap')
	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.readNumberValue(AbstractBinaryFormatReader.java:305)
	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.getBoolean(AbstractBinaryFormatReader.java:349)
	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.getBoolean(AbstractBinaryFormatReader.java:550)
	at com.clickhouse.jdbc.ResultSetImpl.getBoolean(ResultSetImpl.java:93)
	... 28 more

@chernser
Copy link
Contributor

chernser commented Nov 7, 2024

@voytektr new version with fix is release (0.7.1-patch). It should be soon be in Maven central.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants