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

[FEATURE REQUEST] ClickHouseValue return ClickHouseValue data for nested types

Open pan3793 opened this issue 3 years ago • 3 comments

e.g. for array type

interface ClickHouseValue {
  Object[] asArray()
  <T> T[] asArray(Class<T> clazz)

  // wanted
  ClickHouseValue[] asArrayValues()
}

pan3793 avatar Sep 18 '22 07:09 pan3793

Thanks for the suggestion @pan3793. I think you can use ClickHouseArraySequence(sub-interface of ClickHouseValue just for wrapping array values), which was added in PR #1087:

ClickHouseColumn column = ClickHouseColumn.of("", "Array(Int8)");
ClickHouseArraySequence array = ClickHouseValues.newArrayValue(column);
ClickHouseValue nestedValue = ClickHouseValues.newValue(config, column.getNestedColumns().get(0));
for (int i = 0, len = array.length(); i<len; i++) {
  System.out.println(array.getValue(i, nestedValue).asObject());
}

Although the method you suggested is easier to use, I'd suggest to go with above painful approach. The major concern here is memory usage. ClickHouseValue is designed as a container object mainly for ease of type conversion. In most cases, we should reuse one single instance for all values of a column for serialization/deserialization, instead of multiple instances one for each value, or the memory usage will be too high(try compare ClickHouseByteValue and byte), and the overall performance will be decreased.

zhicwu avatar Sep 18 '22 08:09 zhicwu

Thanks for the quick reply, I think we should provide such API in ClickHouseValue and add warnings about performance and resources in Javadoc.

pan3793 avatar Sep 18 '22 08:09 pan3793

I had a rough idea of moving ClickHouseValues.new*Value methods to ClickHouseColumn for ease of use and slightly improved performance. Maybe it's better to rename ClickHouseArraySequence to ClickHouseIterableValue to cover all nested types, along with new methods ClickHouseValue[] copyValues(ClickHouseColumn) and ClickHouseValue[] getValues(ClickHouseValue[]) for convenience.

zhicwu avatar Sep 18 '22 08:09 zhicwu