clickhouse-java
clickhouse-java copied to clipboard
Can't set an array of doubles
I've got a table with a column that has type Array(Nullable(Float64))
.
In my Java code, I do something like:
Double[] values = {1.0};
preparedStatement.setArray(1, preparedStatement.getConnection().createArrayOf("DOUBLE", values));
When the statement is executed, I get a cast exception: class [Ljava.lang.Double; cannot be cast to class [D ([Ljava.lang.Double; and [D are in module java.base of loader 'bootstrap')
I read the code and the cause comes from the class ClickHouseRowBinaryProcessor.MappedFunctions
in the method writeArray
.
We have this line: double[] array = (double[])value.asObject();
But the result type of value.asObject()
is Double[]
, it can't be cast to double[]
Thanks @chrix75, it does look like a bug. I'm in the middle of refactoring to better support unsigned types as well as arrays and tuples. I'll definitely fix this and add tests accordingly.
Fixed in PR #1124. It is strongly recommended to use preparedStatement.setObject(<index>, new double[] {1.0D})
rather than createArrayOf
to prevent unnecessary overhead from the creation of additional objects.