Skip to content

Commit 983a527

Browse files
committed
add support for SET stmt and debug
1 parent e2d1478 commit 983a527

File tree

7 files changed

+91
-19
lines changed

7 files changed

+91
-19
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,10 @@ public void setDBRoles(Collection<String> dbRoles) {
21902190
this.configuration.get(ClientConfigProperties.SESSION_DB_ROLES.getKey())));
21912191
}
21922192

2193+
public void setProfile(String name) {
2194+
this.configuration.put(ClientConfigProperties.PROFILE.getKey(), name);
2195+
}
2196+
21932197
public void updateClientName(String name) {
21942198
this.configuration.put(ClientConfigProperties.CLIENT_NAME.getKey(), name);
21952199
}

client-v2/src/main/java/com/clickhouse/client/api/ClientConfigProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public enum ClientConfigProperties {
2222

2323
PASSWORD("password", ""),
2424

25+
PROFILE("profile"),
26+
2527
/**
2628
* Maximum number of active connection in internal connection pool.
2729
*/

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,15 @@ private void addQueryParams(URIBuilder req, Map<String, String> chConfig, Map<St
556556
Collection<String> sessionRoles = (Collection<String>) requestConfig.getOrDefault(ClientConfigProperties.SESSION_DB_ROLES.getKey(),
557557
ClientConfigProperties.valuesFromCommaSeparated(chConfiguration.getOrDefault(ClientConfigProperties.SESSION_DB_ROLES.getKey(), "")));
558558
if (!sessionRoles.isEmpty()) {
559-
560559
sessionRoles.forEach(r -> req.addParameter(ClickHouseHttpProto.QPARAM_ROLE, r));
561560
}
562561

562+
String profile = (String) requestConfig.getOrDefault(ClientConfigProperties.PROFILE.getKey(),
563+
chConfiguration.getOrDefault(ClientConfigProperties.PROFILE.getKey(), ""));
564+
if (!profile.isEmpty()) {
565+
req.addParameter(ClientConfigProperties.PROFILE.getKey(), profile);
566+
}
567+
563568
for (String key : requestConfig.keySet()) {
564569
if (key.startsWith(ClientConfigProperties.SERVER_SETTING_PREFIX)) {
565570
Object val = requestConfig.get(key);

jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,27 @@ public boolean execute(String sql, QuerySettings settings) throws SQLException {
376376
connection.client.setDBRoles(roles);
377377
}
378378
}
379+
380+
} else if (JdbcUtils.containsIgnoresCase(tokens, "=")){
381+
String key = tokens.get(1);
382+
String value = tokens.get(3).replace("'", "");
383+
//SET profile
384+
if (key.equals("profile")) {
385+
connection.client.setProfile(value);
386+
LOG.debug("Set profile to {}", value);
387+
} else {
388+
//SET session settings
389+
connection.getDefaultQuerySettings().serverSetting(key, value);
390+
LOG.debug("Set session server setting {} to {}", key, value);
391+
}
379392
}
380393
return false;
381394
} else if (type == StatementType.USE) {
382395
executeUpdate(sql, settings);
383396
//USE Database
384397
List<String> tokens = JdbcUtils.tokenizeSQL(sql);
385398
this.schema = tokens.get(1).replace("\"", "");
399+
connection.getDefaultQuerySettings().setDatabase(schema);
386400
LOG.debug("Changed statement schema {}", schema);
387401
return false;
388402
} else {

jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ public void testFloatTypes() throws SQLException {
608608

609609
assertTrue(rs.next());
610610

611-
DecimalFormat df = new DecimalFormat("#.########");
611+
DecimalFormat df = new DecimalFormat("#.######");
612612
assertEquals(df.format(rs.getObject("float32")), df.format(float32));
613613
assertEquals(rs.getObject("float64"), float64);
614614

@@ -916,9 +916,7 @@ public void testNestedTypeSimpleStatement() throws SQLException {
916916
runQuery("CREATE TABLE test_nested (order Int8, "
917917
+ "nested Nested (int8 Int8, int16 Int16, int32 Int32, int64 Int64, int128 Int128, int256 Int256)"
918918
+ ") ENGINE = MergeTree ORDER BY ()");
919-
runQuery("CREATE TABLE test_nested_not_flatten (order Int8, "
920-
+ "nested Nested (int8 Int8, int16 Int16, int32 Int32, int64 Int64, int128 Int128, int256 Int256)"
921-
+ ") ENGINE = MergeTree ORDER BY () SETTINGS flatten_nested = 0");
919+
922920
// Insert random (valid) values
923921
long seed = System.currentTimeMillis();
924922
Random rand = new Random(seed);
@@ -936,11 +934,6 @@ public void testNestedTypeSimpleStatement() throws SQLException {
936934
log.info("SQL: {}", sql);
937935
insertData(sql);
938936

939-
String nsql = String.format("INSERT INTO test_nested_not_flatten VALUES ( 1, [(%s,%s,%s,%s,%s,%s)])",
940-
int8, int16, int32, int64, int128, int256);
941-
log.info("SQL: {}", nsql);
942-
insertData(nsql);
943-
944937
// Check the results
945938
try (Connection conn = getConnection()) {
946939
try (Statement stmt = conn.createStatement()) {
@@ -958,9 +951,36 @@ public void testNestedTypeSimpleStatement() throws SQLException {
958951
}
959952
}
960953

961-
// Check the results
954+
}
955+
956+
@Test(groups = { "integration" })
957+
public void testNestedTypeNonFlatten() throws SQLException {
962958
try (Connection conn = getConnection()) {
963959
try (Statement stmt = conn.createStatement()) {
960+
stmt.execute("SET flatten_nested = 0");
961+
stmt.execute("CREATE TABLE test_nested_not_flatten (order Int8, "
962+
+ "nested Nested (int8 Int8, int16 Int16, int32 Int32, int64 Int64, int128 Int128, int256 Int256)"
963+
+ ") ENGINE = MergeTree ORDER BY ()");
964+
// Insert random (valid) values
965+
long seed = System.currentTimeMillis();
966+
Random rand = new Random(seed);
967+
log.info("Random seed was: {}", seed);
968+
969+
int int8 = rand.nextInt(256) - 128;
970+
int int16 = rand.nextInt(65536) - 32768;
971+
int int32 = rand.nextInt();
972+
long int64 = rand.nextLong();
973+
BigInteger int128 = new BigInteger(127, rand);
974+
BigInteger int256 = new BigInteger(255, rand);
975+
976+
977+
String nsql = String.format("INSERT INTO test_nested_not_flatten VALUES ( 1, [(%s,%s,%s,%s,%s,%s)])",
978+
int8, int16, int32, int64, int128, int256);
979+
log.info("SQL: {}", nsql);
980+
stmt.executeUpdate(nsql);
981+
982+
// Check the results
983+
964984
try (ResultSet rs = stmt.executeQuery("SELECT * FROM test_nested_not_flatten ORDER BY order")) {
965985
assertTrue(rs.next());
966986
assertEquals((Object[])((Object[])((java.sql.Array) rs.getObject("nested")).getArray())[0],

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,33 @@ record = conn.client.queryAll("SELECT currentRoles()").get(0);
412412
}
413413
}
414414

415+
@Test(groups = { "integration" })
416+
public void testSetStatement() throws SQLException {
417+
try (ConnectionImpl conn = (ConnectionImpl) getJdbcConnection()) {
418+
try (Statement stmt = conn.createStatement()) {
419+
420+
stmt.execute("SET session_timezone = 'Europe/Berlin'");
421+
try (ResultSet rs = stmt.executeQuery("SELECT timezone()")){
422+
assertTrue(rs.next());
423+
assertEquals(rs.getString(1),"Europe/Berlin");
424+
assertFalse(rs.next());
425+
}
426+
stmt.execute("SET network_compression_method = 'GZIP'");
427+
try (ResultSet rs = stmt.executeQuery("SELECT getSetting('network_compression_method')")){
428+
assertTrue(rs.next());
429+
assertEquals(rs.getString(1),"GZIP");
430+
assertFalse(rs.next());
431+
}
432+
stmt.execute("SET profile = 'readonly'");
433+
try (ResultSet rs = stmt.executeQuery("SELECT currentProfiles()")){
434+
assertTrue(rs.next());
435+
assertEquals(((Object[]) rs.getArray(1).getArray())[0],"readonly");
436+
assertFalse(rs.next());
437+
}
438+
}
439+
}
440+
}
441+
415442
@Test
416443
public void testGettingArrays() throws Exception {
417444
try (ConnectionImpl conn = (ConnectionImpl) getJdbcConnection();

jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public void testGetColumns() throws Exception {
3232
conn.createStatement().execute("DROP TABLE IF EXISTS " + tableName);
3333

3434
StringBuilder createTableStmt = new StringBuilder("CREATE TABLE " + tableName + " (");
35-
List<String> columnNames = Arrays.asList("id", "name", "float1", "fixed_string1", "decimal_1", "nullable_column", "date", "datetime");
36-
List<String> columnTypes = Arrays.asList("UInt64", "String", "Float32", "FixedString(10)", "Decimal(10, 2)", "Nullable(Decimal(5, 4))", "Date", "DateTime");
37-
List<Integer> columnSizes = Arrays.asList(8, 0, 4, 10, 10, 5, 2, 0);
38-
List<Integer> columnJDBCDataTypes = Arrays.asList(Types.BIGINT, Types.VARCHAR, Types.FLOAT, Types.VARCHAR, Types.DECIMAL, Types.DECIMAL, Types.DATE, Types.TIMESTAMP);
39-
List<String> columnTypeNames = Arrays.asList("UInt64", "String", "Float32", "FixedString(10)", "Decimal(10, 2)", "Nullable(Decimal(5, 4))", "Date", "DateTime");
40-
List<Boolean> columnNullable = Arrays.asList(false, false, false, false, false, true, false, false);
41-
List<Integer> columnDecimalDigits = Arrays.asList(null, null, null, null, 2, 4, null, null);
42-
List<Integer> columnRadix = Arrays.asList(2, null, null, null, 10, 10, null, null);
35+
List<String> columnNames = Arrays.asList("id", "huge_integer", "name", "float1", "fixed_string1", "decimal_1", "nullable_column", "date", "datetime");
36+
List<String> columnTypes = Arrays.asList("Int64", "UInt128", "String", "Float32", "FixedString(10)", "Decimal(10, 2)", "Nullable(Decimal(5, 4))", "Date", "DateTime");
37+
List<Integer> columnSizes = Arrays.asList(8, 16, 0, 4, 10, 10, 5, 2, 0);
38+
List<Integer> columnJDBCDataTypes = Arrays.asList(Types.BIGINT, Types.OTHER, Types.VARCHAR, Types.FLOAT, Types.VARCHAR, Types.DECIMAL, Types.DECIMAL, Types.DATE, Types.TIMESTAMP);
39+
List<String> columnTypeNames = Arrays.asList("Int64", "UInt128", "String", "Float32", "FixedString(10)", "Decimal(10, 2)", "Nullable(Decimal(5, 4))", "Date", "DateTime");
40+
List<Boolean> columnNullable = Arrays.asList(false, false, false, false, false, false, true, false, false);
41+
List<Integer> columnDecimalDigits = Arrays.asList(null, null, null, null, null, 2, 4, null, null);
42+
List<Integer> columnRadix = Arrays.asList(2, 2, null, null, null, 10, 10, null, null);
4343

4444
for (int i = 0; i < columnNames.size(); i++) {
4545
createTableStmt.append(columnNames.get(i)).append(" ").append(columnTypes.get(i)).append(',');

0 commit comments

Comments
 (0)