Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@

### Bug Fixes

- **[client-v2]** Fixed reading a `Variant`, `Nested`, `Decimal` or `Enum` value held in a `Dynamic` column. The
concrete type rebuilt from the binary type encoding did not match what the server encoded: `Variant` was wrapped
twice (so the discriminator selected the wrong element), `Nested` read only the element names and left the element
type encodings in the stream, and `Decimal`/`Enum` lost their precision and scale / their constants whenever the
value sat inside another type, so a decimal read back unscaled (`1.2500` as `12500`) and every enum value read back
as `<unknown>`. The constant width of an enum is now taken from the type tag rather than from the number of
constants, which also fixes reading an `Enum16` with fewer than 128 constants and negative `Enum8` constants.
(https://github.com/ClickHouse/clickhouse-java/issues/3003)
- **[client-v2]** Fixed a `Nullable(T)` column bound to a **primitive** POJO field silently corrupting a row on the
POJO read path. The compiled setter went straight to a primitive read method without consuming the `Nullable`
null-marker byte, which is on the wire for every value of a nullable column regardless of the value, so the stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseDataType;
import com.clickhouse.data.ClickHouseEnum;
import com.clickhouse.data.ClickHouseUtils;
import com.clickhouse.data.value.ClickHouseBitmap;
import org.slf4j.Logger;
import org.slf4j.helpers.NOPLogger;
Expand Down Expand Up @@ -1568,7 +1569,9 @@ private ClickHouseColumn readDynamicData() throws IOException {
case Decimal256: {
int precision = readByte();
int scale = readByte();
return ClickHouseColumn.of("v", ClickHouseDataType.binTag2Type.get(tag), false, precision, scale);
// Rendered the way the server renders a decimal type, so that a parent type encoding
// (array, tuple, map, ...) can append it and keep the precision and the scale.
return ClickHouseColumn.of("v", "Decimal(" + precision + ", " + scale + ")");
}
case Dynamic: {
int maxTypes = readVarInt(input);
Expand All @@ -1580,16 +1583,24 @@ private ClickHouseColumn readDynamicData() throws IOException {
int constants = readVarInt(input);
int[] values = new int[constants];
String[] names = new String[constants];
ClickHouseDataType enumType = constants > 127 ? ClickHouseDataType.Enum16 : ClickHouseDataType.Enum8;
// The width of the constant values is defined by the type tag, not by their number:
// Enum8 encodes them as Int8 and Enum16 as Int16.
ClickHouseDataType enumType = type == ClickHouseDataType.Enum16 ?
ClickHouseDataType.Enum16 : ClickHouseDataType.Enum8;
StringBuilder enumDef = new StringBuilder(SB_INIT_SIZE);
enumDef.append(enumType.name()).append('(');
for (int i = 0; i < constants; i++) {
names[i] = readString(input);
if (enumType == ClickHouseDataType.Enum8) {
values[i] = readUnsignedByte();
} else {
values[i] = readUnsignedShortLE();
values[i] = enumType == ClickHouseDataType.Enum8 ? readByte() : readShortLE();
if (i > 0) {
enumDef.append(", ");
}
enumDef.append('\'').append(ClickHouseUtils.escape(names[i], '\'')).append("' = ").append(values[i]);
}
return new ClickHouseColumn(enumType, "v", enumType.name(), false, false, Collections.emptyList(), Collections.emptyList(),
enumDef.append(')');
// The constants are a part of the type: a parent type encoding appends this type name,
// so it has to carry them to keep the names of the values readable.
return new ClickHouseColumn(enumType, "v", enumDef.toString(), false, false, Collections.emptyList(), Collections.emptyList(),
new ClickHouseEnum(names, values));
}
case FixedString: {
Expand Down Expand Up @@ -1652,8 +1663,10 @@ private ClickHouseColumn readDynamicData() throws IOException {
StringBuilder nested = new StringBuilder(SB_INIT_SIZE);
nested.append("Nested(");
for (int i = 0; i < size; i++) {
String name = readString(input);
nested.append(name).append(',');
// Nested is encoded as a named tuple: every element name is followed by the
// type encoding of that element, which has to be consumed here as well.
nested.append(readString(input)).append(' ');
nested.append(readDynamicData().getOriginalTypeName()).append(',');
}
nested.setLength(nested.length() - 1);
nested.append(')');
Expand Down Expand Up @@ -1708,7 +1721,7 @@ private ClickHouseColumn readDynamicData() throws IOException {
}
variant.setLength(variant.length() - 1);
variant.append(")");
return ClickHouseColumn.of("v", "Variant(" + variant + ")");
return ClickHouseColumn.of("v", variant.toString());
}
case AggregateFunction:
throw new ClientException("Aggregate functions are not supported yet");
Expand Down
Loading
Loading