diff --git a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java index 4d7db386f..47420cfc6 100644 --- a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java +++ b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java @@ -78,17 +78,19 @@ private static String describe(Object value) { * escapes. */ void render(TsvOutput out, Object value, boolean raw) { - if (raw && value != null && isStringType(type)) { - byte[] bytes = stringBytes(type, value); - if (bytes != null) { - out.write(bytes); - return; - } - } - renderValue(out, type, value, false); + renderValue(out, type, value, false, raw); } static void renderValue(TsvOutput out, ChType t, Object v, boolean nested) { + renderValue(out, t, v, nested, false); + } + + /** + * With raw, a string that is not nested is written without escapes. {@code Nullable}, {@code LowCardinality}, + * {@code SimpleAggregateFunction} and {@code Variant} write the text of their inner type, so they keep raw; the + * values in a container are nested, and stay escaped and quoted. + */ + private static void renderValue(TsvOutput out, ChType t, Object v, boolean nested, boolean raw) { switch (t.base) { case "Nullable": case "LowCardinality": { @@ -96,7 +98,7 @@ static void renderValue(TsvOutput out, ChType t, Object v, boolean nested) { if (inner == null) { throw new UnsupportedTypeException(t.toString()); } - renderValue(out, inner, v, nested); + renderValue(out, inner, v, nested, raw); return; } case "SimpleAggregateFunction": { @@ -104,7 +106,7 @@ static void renderValue(TsvOutput out, ChType t, Object v, boolean nested) { if (inner == null) { throw new UnsupportedTypeException(t.toString()); } - renderValue(out, inner, v, nested); + renderValue(out, inner, v, nested, raw); return; } default: @@ -161,7 +163,11 @@ static void renderValue(TsvOutput out, ChType t, Object v, boolean nested) { if (bytes == null) { throw new ValueMismatchException(t, v); } - writeEscaped(out, bytes, nested); + if (raw && !nested) { + out.write(bytes); + } else { + writeEscaped(out, bytes, nested); + } return; } case "Decimal": @@ -267,7 +273,7 @@ static void renderValue(TsvOutput out, ChType t, Object v, boolean nested) { renderGeometry(out, t, v); return; case "Variant": - renderVariant(out, t, v, nested); + renderVariant(out, t, v, nested, raw); return; case "QBit": { // QBit(T, dimension) is written as an array of its element type. @@ -288,22 +294,6 @@ static void renderValue(TsvOutput out, ChType t, Object v, boolean nested) { } } - static boolean isStringType(ChType t) { - while ((t.base.equals("Nullable") || t.base.equals("LowCardinality")) && t.typeArg(0) != null) { - t = t.typeArg(0); - } - switch (t.base) { - case "String": - case "FixedString": - case "Enum8": - case "Enum16": - case "Enum": - return true; - default: - return false; - } - } - /** Returns the bytes of a string-like value, or null when v is not one. */ private static byte[] stringBytes(ChType t, Object v) { if (v instanceof String) { @@ -689,7 +679,7 @@ private static void renderGeometry(TsvOutput out, ChType t, Object v) { * Writes a {@code Variant} value. The client gives the value of the active member only, so the member type is * the first one that accepts the Java value. */ - private static void renderVariant(TsvOutput out, ChType t, Object v, boolean nested) { + private static void renderVariant(TsvOutput out, ChType t, Object v, boolean nested, boolean raw) { int mark = out.size(); RuntimeException last = null; for (ChType.Arg a : t.args) { @@ -697,7 +687,7 @@ private static void renderVariant(TsvOutput out, ChType t, Object v, boolean nes throw new UnsupportedTypeException(t.toString()); } try { - renderValue(out, a.type, v, nested); + renderValue(out, a.type, v, nested, raw); return; } catch (ValueMismatchException | UnsupportedTypeException e) { out.truncate(mark); diff --git a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java index 39e50b720..b9461cde2 100644 --- a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java +++ b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java @@ -119,6 +119,14 @@ private ShimResult runShim(String backend, Map env, String query "1 AS format FROM numbers(1)", "1 FORMAT TSVWithNamesAndTypes", "'a\\tb' AS `x y` FORMAT TSVRawWithNames", + // A Raw format writes a top-level string without escapes, also in a wrapper type. + "CAST('a\\tb' AS SimpleAggregateFunction(any, String)), CAST('c\\\\d' AS SimpleAggregateFunction(anyLast, Nullable(String))), CAST('e\\nf' AS SimpleAggregateFunction(max, LowCardinality(String))), CAST('g\\th' AS SimpleAggregateFunction(any, FixedString(3))) FORMAT TSVRaw", + "CAST('a\\'b' AS Nullable(Enum8('a\\'b' = 1))), CAST('c\\'d' AS Nullable(Enum16('c\\'d' = 300))), CAST('a\\'b' AS SimpleAggregateFunction(any, Enum8('a\\'b' = 1))) FORMAT TabSeparatedRaw", + "CAST('a\\tb' AS Variant(String, UInt8)), CAST(toFixedString('c\\\\d', 3) AS Variant(FixedString(3), UInt8)), CAST(1 AS Variant(String, UInt8)) FORMAT TSVRawWithNames", + "CAST(NULL AS SimpleAggregateFunction(any, Nullable(String))), CAST(NULL AS Nullable(Enum8('a' = 1))), CAST(NULL AS Variant(String, UInt8)) FORMAT TabSeparatedRawWithNamesAndTypes", + // Contrast: values in a container stay escaped and quoted, and TSV escapes the same values. + "[CAST('a\\tb' AS SimpleAggregateFunction(any, String))], tuple(CAST('a\\'b' AS Nullable(Enum8('a\\'b' = 1)))), [CAST('a\\tb' AS Variant(String, UInt8))] FORMAT TSVRaw", + "CAST('a\\tb' AS SimpleAggregateFunction(any, String)), CAST('a\\'b' AS Nullable(Enum8('a\\'b' = 1))), CAST('a\\tb' AS Variant(String, UInt8))", }; /** diff --git a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java index 3f38465b4..af4a5551c 100644 --- a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java +++ b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java @@ -245,6 +245,45 @@ public void testRenderRaw() { assertEquals(out.toString(), "a\tb"); } + @DataProvider + public Object[][] rawValues() { + // The expected text is what ClickHouse 26.10 writes for FORMAT TSVRaw and for FORMAT TSV. + BinaryStreamReader.EnumValue quote = new BinaryStreamReader.EnumValue("a'b", 1); + return new Object[][] { + {"SimpleAggregateFunction(any, String)", "a\tb", "a\tb", "a\\tb"}, + {"SimpleAggregateFunction(anyLast, Nullable(String))", "c\\d", "c\\d", "c\\\\d"}, + {"SimpleAggregateFunction(any, LowCardinality(String))", "e\nf", "e\nf", "e\\nf"}, + {"SimpleAggregateFunction(max, FixedString(3))", "g\th", "g\th", "g\\th"}, + {"SimpleAggregateFunction(any, LowCardinality(Nullable(String)))", "i'j", "i'j", "i\\'j"}, + {"Nullable(Enum8('a\\'b' = 1))", quote, "a'b", "a\\'b"}, + {"Nullable(Enum16('a\\'b' = 300))", new BinaryStreamReader.EnumValue("a'b", 300), "a'b", "a\\'b"}, + {"SimpleAggregateFunction(any, Enum8('a\\'b' = 1))", quote, "a'b", "a\\'b"}, + // jdbc-v2 gives the label of an Enum as a String. + {"SimpleAggregateFunction(any, Enum8('a\\'b' = 1))", "a'b", "a'b", "a\\'b"}, + {"Variant(String, UInt8)", "a\tb", "a\tb", "a\\tb"}, + {"Variant(FixedString(3), UInt8)", "c\\d", "c\\d", "c\\\\d"}, + // Contrast: the values that are not top-level strings are written as in TSV. + {"Variant(String, UInt8)", (short) 1, "1", "1"}, + {"SimpleAggregateFunction(sum, UInt64)", BigInteger.valueOf(5), "5", "5"}, + {"SimpleAggregateFunction(any, Nullable(String))", null, "\\N", "\\N"}, + {"Nullable(Enum8('a' = 1))", null, "\\N", "\\N"}, + {"Array(SimpleAggregateFunction(any, String))", Arrays.asList("a\tb"), "['a\\tb']", "['a\\tb']"}, + {"Tuple(Nullable(Enum8('a\\'b' = 1)))", new Object[] {quote}, "('a\\'b')", "('a\\'b')"}, + {"Array(Variant(String, UInt8))", Arrays.asList("a\tb", (short) 1), "['a\\tb',1]", "['a\\tb',1]"}, + {"Map(String, Nullable(String))", new LinkedHashMap<>(Collections.singletonMap("k\t", "v\n")), + "{'k\\t':'v\\n'}", "{'k\\t':'v\\n'}"}, + }; + } + + /** A Raw format writes a top-level string without escapes, also in a wrapper type. Nested values are escaped. */ + @Test(dataProvider = "rawValues") + public void testRenderRawWrapped(String type, Object value, String raw, String escaped) { + TsvOutput out = new TsvOutput(); + new TsvRenderer(type).render(out, value, true); + assertEquals(out.toString(), raw); + assertEquals(render(type, value), escaped); + } + @Test public void testRenderUnsupported() { // A map without order cannot be written.