Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,9 @@ private ClickHouseColumn readDynamicData() throws IOException {
byte tag = readByte();

ClickHouseDataType type = ClickHouseDataType.binTag2Type.get(tag);
if (type == null) {
// The tag of an unnamed tuple is also the tag of ClickHouseDataType.Tuple, so the lookup finds it.
// Both tuple tags are decoded below, where the number of elements and their types are read.
if (type == null || type == ClickHouseDataType.Tuple) {
if (tag == ClickHouseDataType.DateTime64.getBinTag() - 1) {
// without timezone
byte scale = readByte();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,37 @@ public void testDynamicVariantIsNotWrappedTwice() throws Exception {
Assert.assertEquals(reader.readValue(ClickHouseColumn.of("guard", "Int32")), Integer.valueOf(4242));
}

@Test
public void testDynamicUnnamedTupleConsumesElementTypeEncodings() throws Exception {
// Array(Tuple(Tuple(Int32, String), Nullable(Int8))) with unnamed tuples
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(ClickHouseDataType.Array.getBinTag());
baos.write(ClickHouseDataType.TUPLE_WITHOUT_NAMES_BIN_TAG);
BinaryStreamUtils.writeVarInt(baos, 2);
baos.write(ClickHouseDataType.TUPLE_WITHOUT_NAMES_BIN_TAG);
BinaryStreamUtils.writeVarInt(baos, 2);
baos.write(ClickHouseDataType.Int32.getBinTag());
baos.write(ClickHouseDataType.String.getBinTag());
baos.write(ClickHouseDataType.Nullable.getBinTag());
baos.write(ClickHouseDataType.Int8.getBinTag());
BinaryStreamUtils.writeVarInt(baos, 2);
BinaryStreamUtils.writeInt32(baos, 1);
BinaryStreamUtils.writeString(baos, "a");
BinaryStreamUtils.writeNonNull(baos);
BinaryStreamUtils.writeInt8(baos, 2);
BinaryStreamUtils.writeInt32(baos, 3);
BinaryStreamUtils.writeString(baos, "b");
BinaryStreamUtils.writeNull(baos);
BinaryStreamUtils.writeInt32(baos, 4242);

BinaryStreamReader reader = dynamicReader(baos);
Object[] values = ((BinaryStreamReader.ArrayValue) reader.readValue(ClickHouseColumn.of("v", "Dynamic"))).getArrayOfObjects();
Assert.assertEquals(values, new Object[]{
new Object[]{new Object[]{1, "a"}, (byte) 2},
new Object[]{new Object[]{3, "b"}, null}});
Assert.assertEquals(reader.readValue(ClickHouseColumn.of("guard", "Int32")), Integer.valueOf(4242));
}

private static void assertEnumValue(Object actual, String expectedName, int expectedValue) {
BinaryStreamReader.EnumValue value = (BinaryStreamReader.EnumValue) actual;
Assert.assertEquals(value.getName(), expectedName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,59 @@ public void testDynamicWithNestedTypesWithQuotedNames() throws Exception {
Assert.assertEquals(records.get(0).getInteger("num"), 10);
}

@DataProvider(name = "dynamicUnnamedTuples")
public static Object[][] dynamicUnnamedTuples() {
return new Object[][]{
{"Tuple(Int32, String)", "(1, 'a')", Arrays.asList(1, "a")},
{"Tuple(Nullable(Int32), Int8)", "(1, 2)", Arrays.asList(1, (byte) 2)},
{"Tuple(Nullable(Int32), Int8)", "(NULL, 2)", Arrays.asList(null, (byte) 2)},
{"Tuple(Tuple(Int32, String), Int8)", "((1, 'a'), 2)", Arrays.asList(Arrays.asList(1, "a"), (byte) 2)},
{"Tuple(a Tuple(Int32, String), b Int8)", "((1, 'a'), 2)", Arrays.asList(Arrays.asList(1, "a"), (byte) 2)},
{"Array(Tuple(Int32, String))", "[(1, 'a'), (2, 'b')]",
Arrays.asList(Arrays.asList(1, "a"), Arrays.asList(2, "b"))},
{"Array(Array(Tuple(Int32, String)))", "[[(1, 'a')], [(2, 'b'), (3, 'c')]]",
Arrays.asList(Arrays.asList(Arrays.asList(1, "a")), Arrays.asList(Arrays.asList(2, "b"), Arrays.asList(3, "c")))},
{"Map(String, Tuple(Int32, String))", "map('k', (1, 'a'))", Collections.singletonMap("k", Arrays.asList(1, "a"))},
{"Array(Variant(String, Tuple(Int32, String)))", "[(1, 'a'), 'x']", Arrays.asList(Arrays.asList(1, "a"), "x")},
{"JSON(a Tuple(Int32, String))", "'{\"a\": [1, \"x\"]}'", Collections.singletonMap("a", Arrays.asList(1, "x"))},
};
}

@Test(groups = {"integration"}, dataProvider = "dynamicUnnamedTuples")
public void testDynamicWithUnnamedTuple(String type, String value, Object expected) throws Exception {
if (isVersionMatch("(,24.8]")) {
return;
}

List<GenericRecord> records = client.queryAll("SELECT " + value + "::" + type + "::Dynamic AS d, 7::Int64 AS tail, dynamicType(d) AS type");
Assert.assertEquals(records.size(), 1);
GenericRecord row = records.get(0);
Assert.assertEquals(plainValue(row.getObject("d")), expected);
Assert.assertEquals(row.getLong("tail"), 7L);
Assert.assertEquals(row.getString("type"), type);
}

private static Object plainValue(Object value) {
if (value instanceof BinaryStreamReader.ArrayValue) {
value = ((BinaryStreamReader.ArrayValue) value).getArrayOfObjects();
}
if (value instanceof Object[]) {
List<Object> items = new ArrayList<>();
for (Object item : (Object[]) value) {
items.add(plainValue(item));
}
return items;
}
if (value instanceof Map) {
Map<Object, Object> entries = new HashMap<>();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
entries.put(plainValue(entry.getKey()), plainValue(entry.getValue()));
}
return entries;
}
return value;
}

@Test(groups = {"integration"})
public void testDynamicWithFixedString() throws Exception {
if (isVersionMatch("(,24.8]")) {
Expand Down
6 changes: 6 additions & 0 deletions history/latest/3384.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- **[client-v2, jdbc-v2]** Fixed reading a `Dynamic` value whose type has a tuple without element names, such as
`Tuple(Int32, String)`, `Array(Tuple(Int32, String))` or `Map(String, Tuple(Int32, String))`. The reader did not
read the element types of such a tuple, so the rest of the row was read from the wrong bytes. Depending on the data,
the read failed with `Missing nested data type`, `Unsupported data type with tag 97` or `End of stream reached
before reading all data`, or it returned wrong values or no rows. Now the element types are read and the value is
returned as `Object[]`, the same as a tuple with element names. (https://github.com/ClickHouse/clickhouse-java/issues/3384)
Loading