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

ClickHouseRowMetadata index should start at 0

Open murzart opened this issue 2 years ago • 2 comments

Describe the bug

ClickHouseRowMetadata is starting the index at 1, but the index should be starting at 0, according to the spec.

Expected behaviour

Should work according to spec. Index should start at 0.

Code example

    for (int i = 0; i < rowMetadata.getColumnMetadatas().size(); i++) {
      String name = rowMetadata.getColumnMetadata(i).getName(); // failing at 0.. i + 1 works
      Object value = row.get(i); // works fine

      map.put(name, value);
    }

Error log

java.lang.IllegalArgumentException: -1
        at java.util.stream.ReferencePipeline.skip(ReferencePipeline.java:575) ~[?:?]
        at com.clickhouse.r2dbc.ClickHouseRowMetadata.getColumnMetadata(ClickHouseRowMetadata.java:24) ~[clickhouse-r2dbc-0.4.6.jar:clickhouse-r2dbc 0.4.6 (revision: dd91e17)]
        ...

Configuration

Environment

  • clickhouse-jdbc version: 0.4.6

murzart avatar Jun 12 '23 10:06 murzart

Thank you @murzart for the reporting the issue. Will raise a PR to fix this.

zhicwu avatar Jun 13 '23 00:06 zhicwu

If additional information helps to find out the bug location, there's a more detailed error (I was creating a new issue, but I found out this one)

As of today in the current implementation of the jdbc driver as of 0.8.6 seems not to be working as expected, for an unknown reason the jdbc driver is using 1-index column indexes instead of 0-index that confuses the Spring DataClassRowMapper

I tried to workaround it by manually summing +1 to the index, but unfortunately, the DataClassRowMapper is not using a protected method to find out the index, so I would have to manually create a bigger wrapper that wraps the entire for loop

Error:

Image

Note that counts returns 0 index (should return 1)

One interesting thing is that the error occurs when finding out the column by name "counts" but, the TableSchema class seems to have correct indexing, on the column data instance return the

Image

my workaround fixed the method reader.getSchema().getColumnByIndex() which works like 1 index, sample, but still couldn't fix the getColumnByName("counts").getColumnIndex()

Image

KevinGuancheDarias avatar May 23 '25 10:05 KevinGuancheDarias

Fixed in 0.9.0 https://github.com/ClickHouse/clickhouse-java/pull/2376

chernser avatar Aug 13 '25 07:08 chernser