Skip to content

Commit 25913a4

Browse files
committed
fixed reading type of Nullable and issue with changing user after loading server info
1 parent f062681 commit 25913a4

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public class Client implements AutoCloseable {
136136
private final Map<ClickHouseDataType, Class<?>> typeHintMapping;
137137

138138
// Server context
139+
private String dbUser;
139140
private String serverVersion;
140141
private Object metricsRegistry;
141142
private int retries;
@@ -196,7 +197,7 @@ private Client(Set<String> endpoints, Map<String,String> configuration,
196197
}
197198

198199
this.serverVersion = configuration.getOrDefault(ClientConfigProperties.SERVER_VERSION.getKey(), "unknown");
199-
200+
this.dbUser = configuration.getOrDefault(ClientConfigProperties.USER.getKey(), ClientConfigProperties.USER.getDefObjVal());
200201
this.typeHintMapping = (Map<ClickHouseDataType, Class<?>>) this.configuration.get(ClientConfigProperties.TYPE_HINT_MAPPING.getKey());
201202
}
202203

@@ -208,7 +209,10 @@ public void loadServerInfo() {
208209
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
209210
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
210211
if (reader.next() != null) {
211-
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
212+
String tmpDbUser = reader.getString("user");
213+
if (tmpDbUser != null && !tmpDbUser.isEmpty()) {
214+
this.dbUser = tmpDbUser;
215+
}
212216
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
213217
serverVersion = reader.getString("version");
214218
}
@@ -2041,7 +2045,7 @@ public Set<String> getEndpoints() {
20412045
}
20422046

20432047
public String getUser() {
2044-
return (String) this.configuration.get(ClientConfigProperties.USER.getKey());
2048+
return dbUser;
20452049
}
20462050

20472051
public String getServerVersion() {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,10 @@ public void setNetworkTimeout(long timeout, ChronoUnit unit) {
233233
* @return timeout in ms.
234234
*/
235235
public Long getNetworkTimeout() {
236-
return (Long) getOption(ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getKey(),
237-
ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getDefaultValue());
236+
// Socket operation timeout must be integer because of OS interface
237+
// Network timeout may be something else in the future. So we need to cast it to Long.
238+
return ((Number) getOption(ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getKey(),
239+
ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getDefObjVal())).longValue();
238240
}
239241

240242

client-v2/src/test/java/com/clickhouse/client/SettingsTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ void testQuerySettingsSpecific() throws Exception {
105105

106106
{
107107
final QuerySettings settings = new QuerySettings();
108+
Assert.assertEquals(settings.getNetworkTimeout().intValue(),
109+
(Integer) ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getDefObjVal());
108110
settings.setNetworkTimeout(10, ChronoUnit.SECONDS);
109111
Assert.assertEquals(settings.getNetworkTimeout(), TimeUnit.SECONDS.toMillis(10));
110112
}
@@ -164,6 +166,8 @@ public void testInsertSettingsSpecific() throws Exception {
164166

165167
{
166168
final InsertSettings settings = new InsertSettings();
169+
Assert.assertEquals(settings.getNetworkTimeout().intValue(),
170+
(Integer) ClientConfigProperties.SOCKET_OPERATION_TIMEOUT.getDefObjVal());
167171
settings.setNetworkTimeout(10, ChronoUnit.SECONDS);
168172
Assert.assertEquals(settings.getNetworkTimeout(), TimeUnit.SECONDS.toMillis(10));
169173
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataImpl.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,9 @@ private static String generateSqlTypeSizes(String columnName) {
895895
SQLType type = JdbcUtils.CLICKHOUSE_TYPE_NAME_TO_SQL_TYPE_MAP.get(typeName);
896896
if (type == null) {
897897
try {
898-
type = JdbcUtils.convertToSqlType(ClickHouseDataType.valueOf(typeName));
898+
ClickHouseColumn c = ClickHouseColumn.of("v", typeName);
899+
ClickHouseDataType dt = c.getDataType();
900+
type = JdbcUtils.convertToSqlType(dt);
899901
} catch (Exception e) {
900902
log.error("Failed to convert column data type to SQL type: {}", typeName, e);
901903
type = JDBCType.OTHER; // In case of error, return SQL type 0
@@ -1092,8 +1094,23 @@ public ResultSet getTypeInfo() throws SQLException {
10921094
row.put("NULLABLE", nullability);
10931095
};
10941096

1097+
private static final Consumer<Map<String, Object>> TYPE_INFO_VALUE_FUNCTION = row -> {
1098+
String typeName = (String) row.get("TYPE_NAME");
1099+
SQLType type = JdbcUtils.CLICKHOUSE_TYPE_NAME_TO_SQL_TYPE_MAP.get(typeName);
1100+
if (type == null) {
1101+
try {
1102+
type = JdbcUtils.convertToSqlType(ClickHouseDataType.valueOf(typeName));
1103+
} catch (Exception e) {
1104+
log.error("Failed to convert column data type to SQL type: {}", typeName, e);
1105+
type = JDBCType.OTHER; // In case of error, return SQL type 0
1106+
}
1107+
}
1108+
1109+
row.put("DATA_TYPE", type.getVendorTypeNumber());
1110+
};
1111+
10951112
private static final List<Consumer<Map<String, Object>>> GET_TYPE_INFO_MUTATORS = Arrays.asList(
1096-
DATA_TYPE_VALUE_FUNCTION,
1113+
TYPE_INFO_VALUE_FUNCTION,
10971114
NULLABILITY_VALUE_FUNCTION
10981115
);
10991116

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testGetColumns() throws Exception {
4040
final String tableName = "get_columns_metadata_test";
4141
try (Statement stmt = conn.createStatement()) {
4242
stmt.executeUpdate("" +
43-
"CREATE TABLE " + tableName + " (id Int32, name String NOT NULL, v1 Nullable(Int8)) " +
43+
"CREATE TABLE " + tableName + " (id Int32, name String NOT NULL, v1 Nullable(Int8), v2 Array(Int8)) " +
4444
"ENGINE MergeTree ORDER BY ()");
4545
}
4646

@@ -131,6 +131,15 @@ public void testGetColumns() throws Exception {
131131
assertEquals(rs.getObject("DATA_TYPE"), Types.TINYINT);
132132
assertEquals(rs.getString("TYPE_NAME"), "Nullable(Int8)");
133133
assertTrue(rs.getBoolean("NULLABLE"));
134+
135+
assertTrue(rs.next());
136+
assertEquals(rs.getString("TABLE_SCHEM"), getDatabase());
137+
assertEquals(rs.getString("TABLE_NAME"), tableName);
138+
assertEquals(rs.getString("COLUMN_NAME"), "v2");
139+
assertEquals(rs.getInt("DATA_TYPE"), Types.ARRAY);
140+
assertEquals(rs.getObject("DATA_TYPE"), Types.ARRAY);
141+
assertEquals(rs.getString("TYPE_NAME"), "Array(Int8)");
142+
assertFalse(rs.getBoolean("NULLABLE"));
134143
}
135144
}
136145
}

0 commit comments

Comments
 (0)