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

jdbc版本升级到0.4.2之后,出现UnsignedLong cannot cast to long的报错 #1422

Closed
Lqinhao opened this issue Aug 16, 2023 · 14 comments
Closed
Labels
module-jdbc JDBC driver

Comments

@Lqinhao
Copy link

Lqinhao commented Aug 16, 2023

SQL查询结果使用Map作为resultMap,count函数使用Java中Long类型接收对应的value,SQL查询报错:clickhouse.value.UnsignedLong cannot cast to Long

@zhicwu
Copy link
Contributor

zhicwu commented Aug 16, 2023

Hi @Lqinhao, have you tried 0.4.6? Besides, could you provide sample code to reproduce the issue as well as stack trace of the error?

@Lqinhao
Copy link
Author

Lqinhao commented Aug 16, 2023

0.4.6也是一样的报错。我的工程是springboot + mybatis + clickhouse,通过mybatis封装SQL。代码样例:
select count(*), countIf(id=1) from table

@Lqinhao
Copy link
Author

Lqinhao commented Aug 16, 2023

clickhouse.value.UnsignedLong这个类在0.4.1版本还不存在,我将jdbc的版本切换到0.4.1的时候找不到对应的导包。0.4.2才有了这个类

@zhicwu
Copy link
Contributor

zhicwu commented Aug 16, 2023

Could you post stack trace? The change was added in #1124 to ensure the Java lib can handle large numbers correctly.

If you don't want to use ResultHandler, you may try two workarounds: 1) explicitly specifying type java.lang.Long for UInt64 columns in mybatis, which could be tedious to do; 2) set typeMappings to UInt64=java.lang.Long in connection properties to use Java long instead of the default com.clickhouse.value.UnsignedLong.

@Lqinhao
Copy link
Author

Lqinhao commented Aug 16, 2023

因为我查询的结果是两个值,所以我使用了Map作为resultMap接收mybatis查询的结果,查询到结果在代码中就直接get对应的value,所以具体的堆栈打印就只有一句ClassCastException:com.clickhouse.data.value.UnsignedLong cannot be cast to java.lang.Long。debug中可以看到Map接收到的结果中value对应的类型已经变为UnsignedLong了,你们这种改变是完全出乎我们使用的习惯的

@Lqinhao
Copy link
Author

Lqinhao commented Aug 16, 2023

"The change was added in #1124 to ensure the Java lib can handle large”numbers correctly." 这个改变只是clickhouse中的数据类型与Java中数据类型的映射,但是我出错的是count函数而不是单个字段

@zhicwu
Copy link
Contributor

zhicwu commented Aug 16, 2023

你们这种改变是完全出乎我们使用的习惯的

Apologies for any inconvenience, but ensuring the accuracy of the value is crucial. It's important to note that Java's long data type cannot accurately represent UInt64. As I understand it, MyBatis employs ResultSet.getObject() rather than getLong() or getBigDecimal() to fetch the value, although this behavior could be altered. Have you considered testing the second workaround?

但是我出错的是count函数而不是单个字段

By default, the return type of the count* function is UInt64.

@Lqinhao
Copy link
Author

Lqinhao commented Aug 17, 2023

第二种方法我试过了是可以解决我出现的问题的,感谢

@zhicwu
Copy link
Contributor

zhicwu commented Aug 17, 2023

Thank you for confirming the workaround. I'd like to emphasize that Java's long data type cannot accurately represent UInt64. Consequently, if you don't want to use UnsignedLong provided by this lib, you may want to consider using BigInteger or BigDecimal for accurate representation.

I'm not familiar with MyBatis, but it's possible that we require a dialect or a similar component in this driver to enhance its compatibility with the existing persistence framework.

@Lqinhao
Copy link
Author

Lqinhao commented Sep 20, 2023

我现在又发现新的报错UnsignedInteger can not cast to long,我如果想将UnsignedLong和UnsignedInteger同时映射给long,应该怎么修改?

@zhicwu
Copy link
Contributor

zhicwu commented Sep 20, 2023

It's comma separated key-value pairs, so you may try UInt64=java.lang.Long,UInt32=java.lang.Long or UInt64=java.math.BigInteger,UInt32=java.lang.Long.

@Lqinhao
Copy link
Author

Lqinhao commented Oct 7, 2023

我又发现了另一个问题,我在升级版本之前,clickhouse表中有一列属性为Float32 default 0,在插入null值时是OK的,但是在新的版本这个操作会直接报错

@StoneForests
Copy link

第二种方法我试过了是可以解决我出现的问题的,感谢

请问咋解决的呀

@chernser chernser added the module-jdbc JDBC driver label Jun 18, 2024
@chernser
Copy link
Contributor

chernser commented Jan 9, 2025

Not applicable for v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module-jdbc JDBC driver
Projects
None yet
Development

No branches or pull requests

4 participants