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 @@ -1544,7 +1544,12 @@ public static int readParameters(String args, int startIndex, int len, List<Stri
char c = args.charAt(j);
i = j;
builder.append(c);
if (c == ch && args.charAt(j - 1) != '\\') {
if (c == '\\') {
if (j + 1 < len) {
builder.append(args.charAt(++j));
i = j;
}
} else if (c == ch) {
if (j + 1 < len && args.charAt(j + 1) == ch) {
builder.append(ch);
i = ++j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
Expand Down Expand Up @@ -264,6 +265,37 @@ public void testEnum(String typeName, ClickHouseDataType expectedType) {
Assert.assertEquals(column.getEstimatedLength(), column.getDataType().getByteLength());
}

@DataProvider(name = "enumLabelBackslashProvider")
private Object[][] enumLabelBackslashProvider() {
Function<ClickHouseColumn, ClickHouseColumn> self = c -> c;
Function<ClickHouseColumn, ClickHouseColumn> first = c -> c.getNestedColumns().get(0);
return new Object[][] {
{ "Enum8('a\\\\' = 1, 'z' = 2)", self, new String[] { "a\\", "z" }, new int[] { 1, 2 } },
{ "Enum8('z' = 2, 'a\\\\' = 1)", self, new String[] { "z", "a\\" }, new int[] { 2, 1 } },
{ "Enum16('a\\\\\\\\' = 1, 'z' = 2)", self, new String[] { "a\\\\", "z" }, new int[] { 1, 2 } },
{ "Enum8('\\\\\\'' = 1, 'z' = 2)", self, new String[] { "\\'", "z" }, new int[] { 1, 2 } },
{ "Nullable(Enum8('a\\\\' = 1, 'z' = 2))", self, new String[] { "a\\", "z" }, new int[] { 1, 2 } },
{ "Array(Enum8('a\\\\' = 1, 'z' = 2))",
(Function<ClickHouseColumn, ClickHouseColumn>) ClickHouseColumn::getArrayBaseColumn,
new String[] { "a\\", "z" }, new int[] { 1, 2 } },
{ "Map(String, Enum8('a\\\\' = 1, 'z' = 2))",
(Function<ClickHouseColumn, ClickHouseColumn>) ClickHouseColumn::getValueInfo,
new String[] { "a\\", "z" }, new int[] { 1, 2 } },
{ "Tuple(Enum8('a\\\\' = 1, 'z' = 2), String)", first, new String[] { "a\\", "z" },
new int[] { 1, 2 } },
{ "Variant(Enum8('a\\\\' = 1, 'z' = 2), String)", first, new String[] { "a\\", "z" },
new int[] { 1, 2 } },
};
}

@Test(groups = { "unit" }, dataProvider = "enumLabelBackslashProvider")
public void testEnumLabelEndsWithBackslash(String typeName,
Function<ClickHouseColumn, ClickHouseColumn> enumColumn, String[] names, int[] values) {
ClickHouseColumn column = enumColumn.apply(ClickHouseColumn.of("e", typeName));
Assert.assertEquals(column.getEnumConstants().getNames(), names);
Assert.assertEquals(column.getEnumConstants().getValues(), values);
}

@DataProvider(name = "enumWidthProvider")
private Object[][] getEnumWidths() {
return new Object[][] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ public void testReadParameters() {
Assert.assertEquals(ClickHouseUtils.readParameters(args, args.indexOf('('), args.length(), params),
args.lastIndexOf(')') + 1);
Assert.assertEquals(params, Arrays.asList("anyLast", "Nested(a string,b string)"));

params.clear();
args = "Enum8('a\\\\' = 1, 'b\\\\\\'' = 2, 'c' = 3)";
Assert.assertEquals(ClickHouseUtils.readParameters(args, args.indexOf('('), args.length(), params),
args.length());
Assert.assertEquals(params, Arrays.asList("'a\\\\'=1", "'b\\\\\\''=2", "'c'=3"));
}

@Test(groups = { "unit" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ private Object[][] enumTypesInSchema() {
{"Enum('a' = 1, 'b' = 1000)", "a"},
{"enum('a' = 1, 'b' = 1000)", "b"},
{"ENUM('a' = -128, 'b' = 127)", "b"},
{"Enum8('a\\\\' = 1, 'z' = 2)", "a\\"},
{"Variant(Bool, Enum('a' = 1, 'b' = 1000))", "b"},
{"Nullable(Enum('a' = 1, 'b' = 1000))", "a"},
};
Expand Down
6 changes: 6 additions & 0 deletions history/latest/3371.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- **[client-v2, jdbc-v2]** Fixed type names with an Enum label that ends with a backslash, such as
`Enum8('a\\' = 1, 'z' = 2)`. The type parser treated the closing quote as escaped because the character before it
was a backslash, so it failed with `NumberFormatException`. A query that returned such a column failed before the
first row, and so did a `TableSchema` built from the type name. The parser now skips the character after each
backslash in a quoted string, so `\\` is one escaped backslash and the next quote closes the string. Escaped quotes
such as `\'` are read as before. (https://github.com/ClickHouse/clickhouse-java/issues/3371)
Loading