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
9 changes: 8 additions & 1 deletion tests/clickhouse-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,14 @@ tests read their own queries from `system.query_log`.
`TabSeparated`: escaped strings and `\N` at the top level, quoted strings
and `NULL` in `Array`, `Tuple` and `Map`, the shortest float text, decimals
without trailing zeros, `DateTime64(P)` with P digits, `Time` over 24 hours,
the server's IPv6 forms, and the map order of the server. `WITH TOTALS` and
the server's IPv6 forms, and the map order of the server. jdbc-v2 gives the
text of a `FixedString(N)` column (also in `Nullable`, `LowCardinality` and
`SimpleAggregateFunction`) without its trailing zero bytes and trailing
U+FFFD characters ([`docs/features.md`](../../docs/features.md)), so for
the `jdbc` backend the renderer adds zero bytes up to N bytes. A value that
ends in bytes that are not valid UTF-8 cannot be restored
(`[string-invalid-utf8]`). A `FixedString` value in `Array`, `Tuple`,
`Map` or `Variant` keeps its N bytes in jdbc-v2. `WITH TOTALS` and
`extremes` rows are not in the binary output over HTTP.
- **Settings.** Of the settings on the command line, only `log_comment`,
`max_insert_threads`, `max_threads`, `send_logs_level`, `async_insert` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ default byte[] rawText() throws Exception {
return null;
}

/**
* True when the client gives the text of a {@code FixedString(N)} column without its trailing zero bytes
* (jdbc-v2, {@code docs/features.md}). Then the renderer writes the zero bytes from the column type.
*/
default boolean trimsFixedString() {
return false;
}

@Override
void close() throws Exception;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public Object get(int i) throws SQLException {
return rs.getObject(i + 1);
}

@Override
public boolean trimsFixedString() {
return true;
}

@Override
public void close() throws SQLException {
rs.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void writeRows(Backend.Result rs, String sql, boolean other) throws Exce
List<String> types = rs.types();
List<TsvRenderer> renderers = new ArrayList<>(types.size());
for (String t : types) {
renderers.add(new TsvRenderer(t));
renderers.add(new TsvRenderer(t, rs.trimsFixedString()));
}
if (!discard && format.names) {
writeHeader(names, format.raw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,40 @@ private static String describe(Object value) {
}

private final ChType type;
/** N when the client gives the text of this {@code FixedString(N)} column without its zero bytes, else -1. */
private final int fixedStringLength;

TsvRenderer(String typeName) {
this(typeName, false);
}

/**
* @param trimsFixedString true when the client gives the text of a {@code FixedString(N)} column without its
* trailing zero bytes ({@link Backend.Result#trimsFixedString()})
*/
TsvRenderer(String typeName, boolean trimsFixedString) {
this.type = ChType.parse(typeName);
this.fixedStringLength = trimsFixedString ? fixedStringLength(type) : -1;
}

/**
* Writes one top-level value. With raw (the {@code TSVRaw} formats), a top-level string is written without
* escapes.
* escapes. When the client gives the text of a {@code FixedString(N)} column without its trailing zero bytes,
* a value of fewer than N bytes gets zero bytes up to N bytes, as the server writes it.
*/
void render(TsvOutput out, Object value, boolean raw) {
if (fixedStringLength >= 0 && value instanceof String) {
byte[] bytes = ((String) value).getBytes(StandardCharsets.UTF_8);
if (bytes.length < fixedStringLength) {
bytes = Arrays.copyOf(bytes, fixedStringLength);
}
if (raw) {
out.write(bytes);
} else {
writeEscaped(out, bytes, false);
}
return;
}
if (raw && value != null && isStringType(type)) {
byte[] bytes = stringBytes(type, value);
if (bytes != null) {
Expand Down Expand Up @@ -304,6 +328,28 @@ static boolean isStringType(ChType t) {
}
}

/**
* Returns N for a {@code FixedString(N)} column, also in {@code Nullable}, {@code LowCardinality} and
* {@code SimpleAggregateFunction}, or -1 for any other column. These are the columns whose text jdbc-v2 gives
* without the trailing zero bytes ({@code docs/features.md}, "{@code FixedString} text reads"). A
* {@code FixedString} value in a container or a {@code Variant} keeps its N bytes in jdbc-v2. A type without a
* valid N also gives -1, and its values are written as the client gives them.
*/
private static int fixedStringLength(ChType t) {
while (t != null && (t.base.equals("Nullable") || t.base.equals("LowCardinality")
|| t.base.equals("SimpleAggregateFunction"))) {
t = t.typeArg(t.base.equals("SimpleAggregateFunction") ? t.args.size() - 1 : 0);
}
if (t == null || !t.base.equals("FixedString")) {
return -1;
}
try {
return Integer.parseInt(t.literalArg(0));
} catch (NumberFormatException e) {
return -1;
}
}

/** 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ private ShimResult runShim(String backend, Map<String, String> env, String query
"toFloat32(0.1), toFloat32(3.4028235e38), toFloat32(-1e-7), toBFloat16(1.5)",
"toDecimal32(1.50, 2), toDecimal64(-0.001, 5), toDecimal128(100, 3), toDecimal256('1.5', 10), toDecimal32(0, 3)",
"'a\\tb\\\\c\\nd''e\\0\\b\\f\\v\\x01\\r', toFixedString('ab', 4), 'ünïcödé'",
"toNullable(toFixedString('y', 2)), toLowCardinality(toFixedString('z', 2)), toLowCardinality(toNullable(toFixedString('v', 2))), toFixedString('', 3), toFixedString('é', 3), toFixedString('abcd', 4)",
"CAST(toFixedString('w', 3) AS SimpleAggregateFunction(any, FixedString(3))), CAST(NULL AS Nullable(FixedString(2))), [toFixedString('x', 3)], (toFixedString('t', 2), 1), map(toFixedString('k', 2), toFixedString('v', 3))",
// A Variant with a FixedString and a String member: the String member gets no zero bytes.
"CAST(CAST('ab', 'Variant(String, UInt8)') AS Variant(String, FixedString(4), UInt8)), CAST(toFixedString('cd', 4) AS Variant(String, FixedString(4))) SETTINGS allow_suspicious_variant_types = 1",
"toFixedString('a\\tb', 4), toNullable(toFixedString('c', 2)), 'd\\te' FORMAT TSVRaw",
"true, false, NULL, CAST(NULL AS Nullable(String)), toNullable(1)",
"toDate('2020-01-02'), toDate32('1900-01-01'), toDate32('2299-12-31'), toDateTime('2020-01-01 10:00:00', 'UTC')",
"toDateTime('2020-01-01 10:00:00', 'Asia/Tokyo'), toDateTime64('2020-01-01 00:00:00.120', 3), toDateTime64('2020-01-01 00:00:00', 9, 'Asia/Tokyo')",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,53 @@ public void testRender(String type, Object value, String expected) {
assertEquals(render(type, value), expected);
}

@DataProvider
public Object[][] trimmedFixedStrings() {
// jdbc-v2 gives the text of a FixedString column without the trailing zero bytes. The server writes all N
// bytes.
return new Object[][] {
{"FixedString(4)", "ab", "ab\\0\\0"},
{"Nullable(FixedString(3))", "é", "é\\0"},
{"LowCardinality(FixedString(2))", "", "\\0\\0"},
{"LowCardinality(Nullable(FixedString(2)))", "z", "z\\0"},
{"SimpleAggregateFunction(any, Nullable(FixedString(3)))", "w", "w\\0\\0"},
{"Nullable(FixedString(2))", null, "\\N"},
{"FixedString(4)", "\0a", "\\0a\\0\\0"},
// A value of N bytes gets no more zero bytes.
{"FixedString(4)", "ab\0\0", "ab\\0\\0"},
// A value of more than N bytes is not cut. (Bytes that are not valid UTF-8 read as U+FFFD, 3 bytes;
// the server writes the original bytes: [string-invalid-utf8] in allowlist.txt.)
{"FixedString(2)", "\uFFFDa", "\uFFFDa"},
// A FixedString value in a container keeps its N bytes in jdbc-v2. A String member of a Variant and
// a String column get no zero bytes.
{"Array(FixedString(3))", Arrays.asList("x\0\0", null), "['x\\0\\0',NULL]"},
{"Variant(FixedString(4), String, UInt8)", "ab", "ab"},
{"String", "ab", "ab"},
};
}

@Test(dataProvider = "trimmedFixedStrings")
public void testRenderTrimmedFixedString(String type, Object value, String expected) {
TsvOutput out = new TsvOutput();
new TsvRenderer(type, true).render(out, value, false);
assertEquals(out.toString(), expected);
}

@Test
public void testRenderFixedStringOfClient() {
// client-v2 gives all N bytes. A shorter value is written as the client gives it, so that the diff shows it.
assertEquals(render("FixedString(4)", "ab\0\0"), "ab\\0\\0");
assertEquals(render("FixedString(4)", "ab"), "ab");
}

@Test
public void testRenderRaw() {
TsvOutput out = new TsvOutput();
new TsvRenderer("Nullable(String)").render(out, "a\tb", true);
assertEquals(out.toString(), "a\tb");
out = new TsvOutput();
new TsvRenderer("Nullable(FixedString(4))", true).render(out, "a\tb", true);
assertEquals(out.toString(), "a\tb\0");
}

@Test
Expand Down
Loading