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

JSON column cant use the string type to write

Open minchowang opened this issue 1 year ago • 1 comments

Describe the bug

When we use the ps.setString("json_val") insert JSON column to the clickhouse server, we will have the below error. because the jdbc-diver uses select * from the table where 0 to read all columns from the clickhouse server, but the server returns a tuple type for this JSON type, so it used incorrect serializer com.clickhouse.data.format.ClickHouseRowBinaryProcessor$TupleSerializer.serialize(ClickHouseRowBinaryProcessor.java:243) img_v3_02c2_e5802790-e152-47ad-a05e-b49b83d07c8g

Steps to reproduce

Expected behaviour

It should use the desc table xxx to get the factly type, then it will get the correct serializer `` image

Code example

Error log

Caused by: java.lang.NumberFormatException: For input string: "{"a": 1, "b": { "c": 2, "d": [1, 2, 3] }}"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.base/java.lang.Integer.parseInt(Integer.java:652)
        at com.clickhouse.data.value.UnsignedByte.valueOf(UnsignedByte.java:78)
        at com.clickhouse.data.value.UnsignedByte.valueOf(UnsignedByte.java:61)
        at com.clickhouse.data.value.ClickHouseByteValue$UnsignedByteValue.update(ClickHouseByteValue.java:93)
        at com.clickhouse.data.value.ClickHouseByteValue$UnsignedByteValue.update(ClickHouseByteValue.java:16)
        at com.clickhouse.data.ClickHouseValue.update(ClickHouseValue.java:1236)
        at com.clickhouse.data.value.ClickHouseByteValue.update(ClickHouseByteValue.java:408)
        at com.clickhouse.data.value.ClickHouseByteValue$UnsignedByteValue.update(ClickHouseByteValue.java:16)
        at com.clickhouse.data.format.ClickHouseRowBinaryProcessor$TupleSerializer.serialize(ClickHouseRowBinaryProcessor.java:243)
        at com.clickhouse.data.ClickHouseDataProcessor.write(ClickHouseDataProcessor.java:593)
        at com.clickhouse.jdbc.internal.InputBasedPreparedStatement.addBatch(InputBasedPreparedStatement.java:350)

Configuration

Environment

  • Client version: 0.6
  • Language version:
  • OS:

ClickHouse server

  • ClickHouse Server version:
  • ClickHouse Server non-default settings, if any:
  • CREATE TABLE statements for tables involved:
  • Sample data for all these tables, use clickhouse-obfuscator if necessary

minchowang avatar Jun 26 '24 02:06 minchowang

Hi @zhicwu , How do you think it?

minchowang avatar Jun 26 '24 03:06 minchowang

Client-v2 allows writing JSON as string. https://github.com/ClickHouse/clickhouse-java/pull/1909

chernser avatar Jan 09 '25 22:01 chernser