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 @@ -39,17 +39,66 @@ protected ClickHouseEnum(Collection<String> params) {
names = new String[size];
values = new int[size];

boolean[] hasValue = new boolean[size];
int i = 0;
for (String p : params) {
int index = p.lastIndexOf('=');
if (index > 0) {
names[i] = ClickHouseUtils.unescape(p.substring(0, index));
values[i] = Integer.parseInt(p.substring(index + 1));
int nameEnd = quotedNameEnd(p);
if (nameEnd == p.length()) {
names[i] = ClickHouseUtils.unescape(p);
} else {
throw new IllegalArgumentException("Invalid enum entry: " + p);
int index = nameEnd > 0 ? nameEnd : p.lastIndexOf('=');
if (index > 0 && p.charAt(index) == '=') {
names[i] = ClickHouseUtils.unescape(p.substring(0, index));
values[i] = Integer.parseInt(p.substring(index + 1));
hasValue[i] = true;
} else {
throw new IllegalArgumentException("Invalid enum entry: " + p);
}
}
i++;
}

// The server gives an entry without a value the number base + position. The base is the value of the
// first entry, or 1 when the first entry has no value. All entries after the first one need a value, or
// none of them.
int valuesAfterFirst = 0;
for (i = 1; i < size; i++) {
if (hasValue[i]) {
valuesAfterFirst++;
}
}
if (valuesAfterFirst != 0 && valuesAfterFirst != size - 1) {
throw new IllegalArgumentException(
"Either all or none of the enum entries after the first one must have a value, got " + params);
}
int base = size > 0 && hasValue[0] ? values[0] : 1;
for (i = 0; i < size; i++) {
if (!hasValue[i]) {
values[i] = base + i;
}
}
}

private static int quotedNameEnd(String entry) {
int len = entry.length();
if (len == 0 || !ClickHouseUtils.isQuote(entry.charAt(0))) {
return -1;
}

char quote = entry.charAt(0);
for (int i = 1; i < len; i++) {
char ch = entry.charAt(i);
if (ch == '\\') {
i++;
} else if (ch == quote) {
if (i + 1 < len && entry.charAt(i + 1) == quote) {
i++;
} else {
return i + 1;
}
}
}
return -1;
}

public ClickHouseEnum(String[] names, int[] values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,51 @@ public void testEnumWidth(String typeName, ClickHouseDataType expectedType) {
Assert.assertEquals(variant.getNestedColumns().get(1).getDataType(), expectedType);
}

@DataProvider(name = "enumImplicitValuesProvider")
private Object[][] getEnumImplicitValues() {
return new Object[][] {
{ "Enum8('a', 'b')", ClickHouseDataType.Enum8, new String[] { "a", "b" }, new int[] { 1, 2 } },
{ "Enum16('a', 'b')", ClickHouseDataType.Enum16, new String[] { "a", "b" }, new int[] { 1, 2 } },
{ "Enum('a', 'b')", ClickHouseDataType.Enum8, new String[] { "a", "b" }, new int[] { 1, 2 } },
{ "Enum8('a' = 5, 'b', 'c')", ClickHouseDataType.Enum8, new String[] { "a", "b", "c" },
new int[] { 5, 6, 7 } },
{ "Enum8('a' = -3, 'b')", ClickHouseDataType.Enum8, new String[] { "a", "b" }, new int[] { -3, -2 } },
{ "Enum8('a', 'b' = 5, 'c' = 9)", ClickHouseDataType.Enum8, new String[] { "a", "b", "c" },
new int[] { 1, 5, 9 } },
{ "Enum('a' = 127, 'b')", ClickHouseDataType.Enum16, new String[] { "a", "b" },
new int[] { 127, 128 } },
{ "Enum8('x=y', 'b')", ClickHouseDataType.Enum8, new String[] { "x=y", "b" }, new int[] { 1, 2 } },
{ "Enum8('it''s', 'b')", ClickHouseDataType.Enum8, new String[] { "it's", "b" }, new int[] { 1, 2 } },
};
}

@Test(dataProvider = "enumImplicitValuesProvider", groups = { "unit" })
public void testEnumImplicitValues(String typeName, ClickHouseDataType expectedType, String[] names,
int[] values) {
ClickHouseColumn column = ClickHouseColumn.of("e", typeName);
Assert.assertEquals(column.getDataType(), expectedType);
Assert.assertEquals(column.getEnumConstants().getNames(), names);
Assert.assertEquals(column.getEnumConstants().getValues(), values);

ClickHouseColumn tuple = ClickHouseColumn.of("e", "Tuple(Int32, Nullable(" + typeName + "), Float64)");
Assert.assertEquals(tuple.getNestedColumns().get(1).getEnumConstants(), column.getEnumConstants());
Assert.assertEquals(tuple.getNestedColumns().get(2).getDataType(), ClickHouseDataType.Float64);
}

@DataProvider(name = "enumMixedValuesProvider")
private Object[][] getEnumMixedValues() {
return new Object[][] {
{ "Enum8('a' = 5, 'b', 'c' = 10, 'd')" },
{ "Enum8('a' = 1, 'b' = 2, 'c')" },
{ "Enum8('a', 'b' = 5, 'c')" },
};
}

@Test(dataProvider = "enumMixedValuesProvider", groups = { "unit" })
public void testEnumMixedValues(String typeName) {
Assert.assertThrows(IllegalArgumentException.class, () -> ClickHouseColumn.of("e", typeName));
}

@Test(groups = { "unit" })
public void testBareEnumWidthFromDataType() {
Assert.assertEquals(ClickHouseColumn.of("e", ClickHouseDataType.Enum, false, false, "'a'=1").getDataType(),
Expand Down
8 changes: 4 additions & 4 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -1167,10 +1167,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
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
* Builds the identification of the client and of the application that uses it. The identification is the value of
* the {@code User-Agent} header, in the standard ClickHouse client format:
* {@code <application> clickhouse-java-v2/<version> (<key>:<value>; ...; lv:java/<java.version>; os:<os.name>;
* hc:ahc_<Apache HttpClient version>)}.
* arch:<os.arch>; hc:ahc_<Apache HttpClient version>)}.
* <p>
* The application part is the client name ({@link ClientConfigProperties#CLIENT_NAME}), or the custom
* {@code User-Agent} header if the application set one. The key/value pairs are the client metadata
* ({@link ClientConfigProperties#CLIENT_ID_METADATA}). The {@code hc} entry is the HTTP client that sends the requests:
* {@code ahc_} (Apache HttpClient) and its version.
* ({@link ClientConfigProperties#CLIENT_ID_METADATA}). The {@code arch} entry is the CPU architecture that the JVM
* reports. The {@code hc} entry is the HTTP client that sends the requests: {@code ahc_} (Apache HttpClient) and its
* version.
* <p>
* The client metadata is validated when {@link Client.Builder#build()} parses the configuration. {@link Client}
* creates one instance and updates it when the client name changes. The instance composes the value for the client
Expand All @@ -35,6 +36,8 @@ public final class WorkloadIdentitySupport {

private static final String METADATA_KEY_OS = "os";

private static final String METADATA_KEY_ARCHITECTURE = "arch";

private static final String METADATA_KEY_HTTP_CLIENT = "hc";

/**
Expand Down Expand Up @@ -147,8 +150,8 @@ private static boolean isClientMetadataText(String str, String forbiddenChars) {

/**
* Composes the client part: {@code clickhouse-java-v2/<version> (<key>:<value>; ...)}. The key/value pairs are the
* client metadata, if it is set, then {@code lv:java/<java.version>}, {@code os:<os.name>} and
* {@code hc:ahc_<Apache HttpClient version>} if the client metadata does not set these keys (ignoring case). The
* client metadata, if it is set, then {@code lv:java/<java.version>}, {@code os:<os.name>}, {@code arch:<os.arch>}
* and {@code hc:ahc_<Apache HttpClient version>} if the client metadata does not set these keys (ignoring case). The
* client sends requests over HTTP with Apache HttpClient ({@code ahc}).
*/
private static String composeClientPart(Map<String, Object> configuration) {
Expand All @@ -157,6 +160,7 @@ private static String composeClientPart(Map<String, Object> configuration) {
ClientUtils.putIfAbsentIgnoreCase(metadata, METADATA_KEY_LANGUAGE_VERSION,
"java/" + System.getProperty("java.version"));
ClientUtils.putIfAbsentIgnoreCase(metadata, METADATA_KEY_OS, System.getProperty("os.name"));
ClientUtils.putIfAbsentIgnoreCase(metadata, METADATA_KEY_ARCHITECTURE, System.getProperty("os.arch"));
ClientUtils.putIfAbsentIgnoreCase(metadata, METADATA_KEY_HTTP_CLIENT, "ahc_" + Versions.APACHE_HTTP_CLIENT);

StringJoiner comment = new StringJoiner("; ", Client.CLIENT_USER_AGENT + Client.clientVersion + " (", ")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,9 +1271,9 @@ public void testUserAgentHasCompleteProductName(String clientName, Pattern userA
@DataProvider(name = "testUserAgentHasCompleteProductName_dataProvider")
public static Object[][] testUserAgentHasCompleteProductName_dataProvider() {
return new Object[][] {
{ "", Pattern.compile("clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; hc:ahc_[\\d.]+\\)") },
{ "test-client/1.0", Pattern.compile("test-client/1.0 clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; hc:ahc_[\\d.]+\\)")},
{ "test-client/", Pattern.compile("test-client/ clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; hc:ahc_[\\d.]+\\)")}};
{ "", Pattern.compile("clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; arch:[^;()]+; hc:ahc_[\\d.]+\\)") },
{ "test-client/1.0", Pattern.compile("test-client/1.0 clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; arch:[^;()]+; hc:ahc_[\\d.]+\\)")},
{ "test-client/", Pattern.compile("test-client/ clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; arch:[^;()]+; hc:ahc_[\\d.]+\\)")}};
}

@Test(dataProvider = "testClientNameDataProvider")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static Object[][] userAgentData() {
Map<String, String> osMetadata = new LinkedHashMap<>();
osMetadata.put("os", "Android 14");
osMetadata.put("api", "table");
osMetadata.put("arch", "arm64-v8a");

Map<String, String> withIgnoredEntries = new LinkedHashMap<>();
withIgnoredEntries.put("api", "");
Expand Down Expand Up @@ -103,7 +104,8 @@ public static Object[][] userAgentData() {
{(Consumer<Client.Builder>) b -> b.setClientName(" my-app/1.0 "),
userAgent("my-app/1.0", null)},
{(Consumer<Client.Builder>) b -> b.setClientName("my-app/1.0", osMetadata),
"my-app/1.0 clickhouse-java-v2/" + Client.clientVersion + " (os:Android 14; api:table; lv:java/"
"my-app/1.0 clickhouse-java-v2/" + Client.clientVersion
+ " (os:Android 14; api:table; arch:arm64-v8a; lv:java/"
+ System.getProperty("java.version") + "; hc:ahc_" + Versions.APACHE_HTTP_CLIENT
+ ")"},
};
Expand Down Expand Up @@ -165,6 +167,7 @@ private static String userAgent(String application, String metadata) {
+ " (" + (metadata == null ? "" : metadata + "; ")
+ "lv:java/" + System.getProperty("java.version")
+ "; os:" + System.getProperty("os.name")
+ "; arch:" + System.getProperty("os.arch")
+ "; hc:ahc_" + Versions.APACHE_HTTP_CLIENT + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ private static String userAgentHeader(HttpAPIClientHelper helper, String clientN

private static String clientPart() {
return "clickhouse-java-v2/" + Client.clientVersion + " (lv:java/" + System.getProperty("java.version")
+ "; os:" + System.getProperty("os.name") + "; hc:ahc_" + Versions.APACHE_HTTP_CLIENT + ")";
+ "; os:" + System.getProperty("os.name") + "; arch:" + System.getProperty("os.arch") + "; hc:ahc_"
+ Versions.APACHE_HTTP_CLIENT + ")";
}

private static String headerValue(HttpPost req, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class WorkloadIdentitySupportTest {

private static final String OS = "os:" + System.getProperty("os.name");

private static final String ARCHITECTURE = "arch:" + System.getProperty("os.arch");

private static final String HTTP_CLIENT = "hc:ahc_" + Versions.APACHE_HTTP_CLIENT;

@Test(dataProvider = "requestUserAgents")
Expand Down Expand Up @@ -102,12 +104,15 @@ public void testClientMetadataReplacesClientEntries(String metadata, String expe
@DataProvider(name = "clientEntriesMetadata")
public static Object[][] clientEntriesMetadata() {
return new Object[][]{
{"os=Android 14", "os:Android 14; " + LANGUAGE_VERSION + "; " + HTTP_CLIENT},
{"api=table,lv=kotlin/2.0", "api:table; lv:kotlin/2.0; " + OS + "; " + HTTP_CLIENT},
{"hc=okhttp_4.12", "hc:okhttp_4.12; " + LANGUAGE_VERSION + "; " + OS},
{"OS=Android 14,LV=kotlin/2.0", "OS:Android 14; LV:kotlin/2.0; " + HTTP_CLIENT},
{"os=Android 14,hc=okhttp_4.12,api=table,lv=kotlin/2.0",
"os:Android 14; hc:okhttp_4.12; api:table; lv:kotlin/2.0"},
{"os=Android 14", "os:Android 14; " + LANGUAGE_VERSION + "; " + ARCHITECTURE + "; " + HTTP_CLIENT},
{"api=table,lv=kotlin/2.0",
"api:table; lv:kotlin/2.0; " + OS + "; " + ARCHITECTURE + "; " + HTTP_CLIENT},
{"hc=okhttp_4.12", "hc:okhttp_4.12; " + LANGUAGE_VERSION + "; " + OS + "; " + ARCHITECTURE},
{"arch=arm64-v8a", "arch:arm64-v8a; " + LANGUAGE_VERSION + "; " + OS + "; " + HTTP_CLIENT},
{"OS=Android 14,LV=kotlin/2.0,ARCH=arm64-v8a",
"OS:Android 14; LV:kotlin/2.0; ARCH:arm64-v8a; " + HTTP_CLIENT},
{"os=Android 14,hc=okhttp_4.12,api=table,arch=arm64-v8a,lv=kotlin/2.0",
"os:Android 14; hc:okhttp_4.12; api:table; arch:arm64-v8a; lv:kotlin/2.0"},
};
}

Expand Down Expand Up @@ -146,6 +151,7 @@ private static String userAgent(String application) {

private static String userAgent(String application, String metadata) {
return (application == null ? "" : application + " ") + "clickhouse-java-v2/" + Client.clientVersion + " ("
+ (metadata == null ? "" : metadata + "; ") + LANGUAGE_VERSION + "; " + OS + "; " + HTTP_CLIENT + ")";
+ (metadata == null ? "" : metadata + "; ") + LANGUAGE_VERSION + "; " + OS + "; " + ARCHITECTURE + "; "
+ HTTP_CLIENT + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ private Object[][] enumTypesInSchema() {
{"ENUM('a' = -128, 'b' = 127)", "b"},
{"Variant(Bool, Enum('a' = 1, 'b' = 1000))", "b"},
{"Nullable(Enum('a' = 1, 'b' = 1000))", "a"},
{"Enum8('a', 'b')", "b"},
{"Enum8('a' = 5, 'b', 'c')", "c"},
{"Enum('a' = 127, 'b')", "b"},
{"Nullable(Enum8('x=y', 'b'))", "x=y"},
};
}

Expand Down
Loading
Loading