clickhouse-java icon indicating copy to clipboard operation
clickhouse-java copied to clipboard

Can't set an array of doubles

Open chrix75 opened this issue 2 years ago • 1 comments

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[]

chrix75 avatar Oct 07 '22 13:10 chrix75

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.

zhicwu avatar Oct 07 '22 22:10 zhicwu

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.

zhicwu avatar May 19 '23 02:05 zhicwu