Skip to content

Commit a107d2d

Browse files
committed
fixed passing properties to client and driver
1 parent 218b124 commit a107d2d

File tree

10 files changed

+82
-22
lines changed

10 files changed

+82
-22
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ public boolean hasValue(int colIndex) {
523523

524524
@Override
525525
public boolean hasValue(String colName) {
526+
getSchema().getColumnByName(colName);
526527
return currentRecord.containsKey(colName);
527528
}
528529

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public final class ServerSettings {
2222
*/
2323
public static final String OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING = "output_format_binary_write_json_as_string";
2424

25+
/**
26+
* Limit number of rows in a result set
27+
*/
28+
public static final String MAX_RESULT_ROWS = "max_result_rows";
29+
30+
/**
31+
* Defines server response if result set exceeded a limit set by {@code max_result_rows}.
32+
* Possible values are 'throw' or 'break'. Default is 'throw'
33+
*/
34+
public static final String RESULT_OVERFLOW_MODE = "result_overflow_mode";
2535
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.clickhouse.client.api.metadata;
2+
3+
import com.clickhouse.client.api.ClientException;
4+
5+
public class NoSuchColumnException extends ClientException {
6+
7+
public NoSuchColumnException(String message) {
8+
super(message);
9+
}
10+
}

client-v2/src/main/java/com/clickhouse/client/api/metadata/TableSchema.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.clickhouse.client.api.metadata;
22

3+
import com.clickhouse.client.api.ClientException;
34
import com.clickhouse.data.ClickHouseColumn;
45

56
import java.util.ArrayList;
@@ -87,20 +88,22 @@ public void addColumn(String name, String type, String defaultType) {
8788
}
8889

8990
public ClickHouseColumn getColumnByName(String name) {
90-
for (ClickHouseColumn column : columns) {
91-
if (column.getColumnName().equalsIgnoreCase(name)) {
92-
return column;//TODO: Try to deep clone the column rather than reference pass
93-
}
94-
}
95-
96-
return null;
91+
return columns.get(nameToIndex(name));
9792
}
9893

9994
public String indexToName(int index) {
100-
return columns.get(index).getColumnName();
95+
try {
96+
return columns.get(index).getColumnName();
97+
} catch (IndexOutOfBoundsException e) {
98+
throw new NoSuchColumnException("Result has no column with index = " + index);
99+
}
101100
}
102101

103102
public int nameToIndex(String name) {
103+
Integer index = colIndex.get(name);
104+
if (index == null) {
105+
throw new NoSuchColumnException("Result has no column with name '" + name + "'");
106+
}
104107
return colIndex.get(name).intValue();
105108
}
106109

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.clickhouse.jdbc;
22

33

4+
import com.clickhouse.client.api.ClientConfigProperties;
45
import com.clickhouse.jdbc.internal.JdbcConfiguration;
56
import com.clickhouse.jdbc.internal.ExceptionUtils;
67
import org.slf4j.Logger;
@@ -156,6 +157,10 @@ public boolean jdbcCompliant() {
156157
return false;
157158
}
158159

160+
public static String chSettingKey(String key) {
161+
return ClientConfigProperties.serverSetting(key);
162+
}
163+
159164
@Override
160165
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
161166
throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/DriverProperties.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
/**
77
* JDBC driver specific properties. Should not include any of ClientConfigProperties.
8-
* All driver specific properties must have {@code "driver."} prefix to isolate them from everything else
8+
* Processing logic should be the follows
9+
* 1. If property is among DriverProperties then Driver handles it specially and will not pass to a client
10+
* 2. If property is not among DriverProperties then it is passed to a client
911
*/
1012
public enum DriverProperties {
1113

1214
/**
1315
* query settings to be passed along with query operation.
1416
* {@see com.clickhouse.client.api.query.QuerySettings}
1517
*/
16-
DEFAULT_QUERY_SETTINGS("driver.query_settings", null);
18+
DEFAULT_QUERY_SETTINGS("default_query_settings", null);
1719

1820
private final String key;
1921

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ private static String stripUrlPrefix(String url) {
102102
List<DriverPropertyInfo> listOfProperties;
103103

104104
private void initProperties(String url, Properties providedProperties) {
105-
log.debug("Parsing url: " + url + ", properties: " + providedProperties);
106105
Map<String, String> props = new HashMap<>();
107106
for (Map.Entry<Object, Object> entry : providedProperties.entrySet()) {
108107
if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
@@ -111,21 +110,17 @@ private void initProperties(String url, Properties providedProperties) {
111110
throw new RuntimeException("Cannot apply non-String properties");
112111
}
113112
}
114-
Map<String, String> mergedProperties = ClickHouseUtils.extractParameters(url, props);
113+
115114
Map<String, DriverPropertyInfo> propertyInfos = new HashMap<>();
116-
for (Map.Entry<String, String> prop : mergedProperties.entrySet()) {
115+
// create initial list of properties that will be passed to a client
116+
for (Map.Entry<String, String> prop : ClickHouseUtils.extractParameters(url, props).entrySet()) {
117117
DriverPropertyInfo propertyInfo = new DriverPropertyInfo(prop.getKey(), prop.getValue());
118118
propertyInfo.description = "(User Defined)";
119119
propertyInfos.put(prop.getKey(), propertyInfo);
120-
if (prop.getValue() != null) {
121-
if (prop.getKey().contains("driver.")) {
122-
driverProperties.put(prop.getKey(), prop.getValue());
123-
} else {
124-
clientProperties.put(prop.getKey(), prop.getValue());
125-
}
126-
}
120+
clientProperties.put(prop.getKey(), prop.getValue());
127121
}
128-
// set know properties
122+
123+
// Fill list of client properties information, add not specified properties (doesn't affect client properties)
129124
for (ClientConfigProperties clientProp : ClientConfigProperties.values()) {
130125
DriverPropertyInfo propertyInfo = propertyInfos.get(clientProp.getKey());
131126
if (propertyInfo == null) {
@@ -135,12 +130,18 @@ private void initProperties(String url, Properties providedProperties) {
135130
}
136131
}
137132

133+
// Fill list of driver properties information, add not specified properties, copy know driver properties from client properties
138134
for (DriverProperties driverProp : DriverProperties.values()) {
139135
DriverPropertyInfo propertyInfo = propertyInfos.get(driverProp.getKey());
140136
if (propertyInfo == null) {
141137
propertyInfo = new DriverPropertyInfo(driverProp.getKey(), driverProp.getDefaultValue());
142138
propertyInfos.put(driverProp.getKey(), propertyInfo);
143139
}
140+
141+
String value = clientProperties.get(driverProp.getKey());
142+
if (value != null) {
143+
driverProperties.put(driverProp.getKey(), value);
144+
}
144145
}
145146

146147
listOfProperties = propertyInfos.values().stream().sorted(Comparator.comparing(o -> o.name)).toList();

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package com.clickhouse.jdbc;
22

33
import java.sql.*;
4+
import java.util.Properties;
5+
6+
import com.clickhouse.client.api.ClientConfigProperties;
7+
import com.clickhouse.client.api.ServerException;
8+
import com.clickhouse.client.api.internal.ServerSettings;
49
import com.clickhouse.jdbc.internal.ClientInfoProperties;
510
import org.testng.Assert;
611
import org.testng.annotations.Test;
712

13+
import static org.testng.Assert.assertEquals;
814
import static org.testng.Assert.assertThrows;
15+
import static org.testng.Assert.fail;
916

1017

1118
public class ConnectionTest extends JdbcIntegrationTest {
@@ -284,4 +291,19 @@ public void setShardingKeyTest() throws SQLException {
284291
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKey(null));
285292
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKey(null, null));
286293
}
294+
295+
@Test
296+
public void testMaxResultRowsProperty() throws Exception {
297+
Properties properties = new Properties();
298+
properties.setProperty(Driver.chSettingKey(ServerSettings.MAX_RESULT_ROWS), "5");
299+
try (Connection conn = getJdbcConnection(properties)) {
300+
try (Statement stmt = conn.createStatement()) {
301+
ResultSet rs = stmt.executeQuery("SELECT toInt32(number) FROM system.numbers LIMIT 20");
302+
fail("Exception expected");
303+
} catch (SQLException e) {
304+
Assert.assertTrue(e.getCause() instanceof ServerException);
305+
Assert.assertEquals(((ServerException)e.getCause()).getCode(), 396);
306+
}
307+
}
308+
}
287309
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ public String getEndpointString(boolean includeDbName) {
2525
}
2626

2727
public Connection getJdbcConnection() throws SQLException {
28+
return getJdbcConnection(null);
29+
}
30+
public Connection getJdbcConnection(Properties addProperties) throws SQLException {
2831
Properties info = new Properties();
2932
info.setProperty("user", "default");
3033
info.setProperty("password", ClickHouseServerForTest.getPassword());
3134
LOGGER.info("Connecting to {}", getEndpointString());
32-
35+
if (addProperties != null) {
36+
info.putAll(addProperties);
37+
}
3338
return new ConnectionImpl(getEndpointString(), info);
3439
//return DriverManager.getConnection(getEndpointString(), "default", ClickHouseServerForTest.getPassword());
3540
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcConfigurationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.testng.Assert.assertEquals;
1515

1616
public class JdbcConfigurationTest {
17+
1718
@Test(dataProvider = "testConnectionUrlDataProvider")
1819
public void testConnectionUrl(String jdbcUrl, String connectionUrl, Properties properties) throws Exception {
1920
JdbcConfiguration configuration = new JdbcConfiguration(jdbcUrl, properties);

0 commit comments

Comments
 (0)