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 @@ -733,6 +733,20 @@ protected static int readColumn(String args, int startIndex, int len, String nam
column = new ClickHouseColumn(ClickHouseDataType.of(builder.toString()), name,
args.substring(startIndex, i), nullable, lowCardinality, params, null);
builder.setLength(0);
} else if (brackets > 0) {
// The branches above end the type name at the closing bracket of their own type. Read the
// closing brackets of the Nullable and LowCardinality wrappers after it, so that the type
// name and the returned index include them.
for (int j = startIndex + column.originalTypeName.length(); brackets > 0 && j < len; j++) {
char ch = args.charAt(j);
if (ch == ')') {
brackets--;
i = j + 1;
column.originalTypeName = args.substring(startIndex, i);
} else if (!Character.isWhitespace(ch)) {
break;
}
}
}

if (nullable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,76 @@ public void testParse() {
Assert.assertEquals(list.size(), 2);
}

@DataProvider(name = "nullableTypesProvider")
private Object[][] getNullableTypes() {
return new Object[][] {
{ "Nullable(Tuple(Int32, String))", ClickHouseDataType.Tuple,
new String[] { "", "" }, new String[] { "Int32", "String" } },
{ "Nullable(Tuple(a Int32, b String))", ClickHouseDataType.Tuple,
new String[] { "a", "b" }, new String[] { "Int32", "String" } },
{ "Nullable(Tuple(a Nullable(Tuple(Int8)), b Int8))", ClickHouseDataType.Tuple,
new String[] { "a", "b" }, new String[] { "Nullable(Tuple(Int8))", "Int8" } },
{ "Nullable(JSON(a Int32))", ClickHouseDataType.JSON,
new String[] { "a" }, new String[] { "Int32" } },
{ "Nullable(QBit(Float32, 8))", ClickHouseDataType.QBit,
new String[] { "" }, new String[] { "Float32" } },
{ "LowCardinality(Nullable(String))", ClickHouseDataType.String, new String[0], new String[0] },
{ "Nullable(String)", ClickHouseDataType.String, new String[0], new String[0] },
};
}

@Test(groups = { "unit" }, dataProvider = "nullableTypesProvider")
public void testNullableTypeName(String typeName, ClickHouseDataType dataType, String[] nestedNames,
String[] nestedTypes) {
assertNullableColumn(ClickHouseColumn.of("c", typeName), "c", typeName, dataType, nestedNames, nestedTypes);
assertNullableColumn(ClickHouseColumn.of("c", "Array(" + typeName + ")").getArrayBaseColumn(), "", typeName,
dataType, nestedNames, nestedTypes);
assertNullableColumn(ClickHouseColumn.of("c", "Map(String, " + typeName + ")").getValueInfo(), "", typeName,
dataType, nestedNames, nestedTypes);

List<ClickHouseColumn> elements = ClickHouseColumn.of("c", "Tuple(x " + typeName + ", y Int8)")
.getNestedColumns();
Assert.assertEquals(elements.size(), 2);
assertNullableColumn(elements.get(0), "x", typeName, dataType, nestedNames, nestedTypes);
Assert.assertEquals(elements.get(1).getColumnName(), "y");
Assert.assertEquals(elements.get(1).getOriginalTypeName(), "Int8");
}

private static void assertNullableColumn(ClickHouseColumn column, String name, String typeName,
ClickHouseDataType dataType, String[] nestedNames, String[] nestedTypes) {
Assert.assertEquals(column.getColumnName(), name);
Assert.assertEquals(column.getOriginalTypeName(), typeName);
Assert.assertEquals(column.getDataType(), dataType);
Assert.assertTrue(column.isNullable());
List<ClickHouseColumn> nested = column.getNestedColumns();
Assert.assertEquals(nested.size(), nestedNames.length);
for (int i = 0; i < nested.size(); i++) {
Assert.assertEquals(nested.get(i).getColumnName(), nestedNames[i]);
Assert.assertEquals(nested.get(i).getOriginalTypeName(), nestedTypes[i]);
}
}

@Test(groups = { "unit" })
public void testNullableTupleInColumnList() {
List<ClickHouseColumn> columns = ClickHouseColumn.parse("a Nullable(Tuple(Int32, String)) DEFAULT (1, 'x'), "
+ "b Nullable(Tuple(c Int8)) CODEC(ZSTD(1)), d Int8");
Assert.assertEquals(columns.size(), 3);
Assert.assertEquals(columns.get(0).getColumnName(), "a");
Assert.assertEquals(columns.get(0).getOriginalTypeName(), "Nullable(Tuple(Int32, String))");
Assert.assertEquals(columns.get(1).getColumnName(), "b");
Assert.assertEquals(columns.get(1).getOriginalTypeName(), "Nullable(Tuple(c Int8))");
Assert.assertEquals(columns.get(2).getColumnName(), "d");
Assert.assertEquals(columns.get(2).getOriginalTypeName(), "Int8");

String args = "Nullable(Tuple(Int32, String)), b Int8";
Assert.assertEquals(ClickHouseColumn.readColumn(args, 0, args.length(), "a", new LinkedList<>()),
args.lastIndexOf(','));

IllegalArgumentException e = Assert.expectThrows(IllegalArgumentException.class,
() -> ClickHouseColumn.parse("a Array(Nullable(Tuple(Int32, String)), b Int8"));
Assert.assertEquals(e.getMessage(), "Missing ')' for '(' at position 7");
}

@Test(groups = { "unit" })
public void testAggregationFunction() {
ClickHouseColumn column = ClickHouseColumn.of("aggFunc", "AggregateFunction(groupBitmap, UInt32)");
Expand Down
11 changes: 6 additions & 5 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ public void loadServerInfo() {
if (tmpDbUser != null && !tmpDbUser.isEmpty()) {
this.dbUser = tmpDbUser;
}
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(),
ClientConfigProperties.SERVER_TIMEZONE.parseValue(reader.getString("timezone")));
serverVersion = reader.getString("version");
}
}
Expand Down Expand Up @@ -1167,10 +1168,10 @@ public Builder setClientName(String clientName) {
* Sets the client name and the client metadata, which identify the application to the server. See
* {@link #setClientName(String)} for the client name. The client adds the metadata to the comment group of the
* {@code User-Agent} header, in the iteration order of {@code metadata}, and then its own {@code lv},
* {@code os} and {@code hc} (HTTP client) entries if the metadata does not set these keys. For example, the
* client name {@code my-app/1.0} and the metadata {@code fv=flink/2.0.0} and {@code api=table} give
* {@code my-app/1.0 clickhouse-java-v2/<version> (fv:flink/2.0.0; api:table; lv:java/<java.version>;
* os:<os.name>; hc:ahc_<Apache HttpClient version>)}.
* {@code os}, {@code arch} (CPU architecture) and {@code hc} (HTTP client) entries if the metadata does not set
* these keys. For example, the client name {@code my-app/1.0} and the metadata {@code fv=flink/2.0.0} and
* {@code api=table} give {@code my-app/1.0 clickhouse-java-v2/<version> (fv:flink/2.0.0; api:table;
* lv:java/<java.version>; os:<os.name>; arch:<os.arch>; hc:ahc_<Apache HttpClient version>)}.
* <p>
* Keys and values are trimmed, and entries with a {@code null} or empty key or value are ignored. Keys and
* values must be printable ASCII, a key must not contain spaces or any of {@code :;(),=}, and a value must not
Expand Down
Loading
Loading