Skip to content

Commit 5c1b7fa

Browse files
authored
Merge pull request #1961 from ClickHouse/testing-feedback
Testing feedback
2 parents 1a4bdba + dc168b7 commit 5c1b7fa

File tree

7 files changed

+100
-8
lines changed

7 files changed

+100
-8
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.clickhouse.client.api.internal.EnvUtils;
3030
import com.clickhouse.client.api.internal.HttpAPIClientHelper;
3131
import com.clickhouse.client.api.internal.MapUtils;
32+
import com.clickhouse.client.api.internal.ServerSettings;
3233
import com.clickhouse.client.api.internal.SettingsConverter;
3334
import com.clickhouse.client.api.internal.TableSchemaParser;
3435
import com.clickhouse.client.api.internal.ValidationUtils;
@@ -242,7 +243,16 @@ public Builder fromUrl(String url) {
242243
try {
243244
Map<String, String> urlProperties = HttpAPIClientHelper.parseUrlParameters(new java.net.URL(url));
244245
this.addEndpoint(url);
245-
this.configuration.putAll(urlProperties);//TODO: Mostly just a placeholder for now
246+
247+
for (ClientConfigProperties properties: ClientConfigProperties.values()) {
248+
String key = properties.getKey().replace(ClientConfigProperties.SERVER_SETTING_PREFIX, "");//People wouldn't pass with prefix
249+
String value = urlProperties.get(key);
250+
if (value != null) {
251+
this.configuration.put(properties.getKey(), value);//We need to keep the original key
252+
}
253+
}
254+
255+
// Set the database
246256
} catch (java.net.MalformedURLException e) {
247257
throw new IllegalArgumentException("Configuration via URL should be done with a valid URL string", e);
248258
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,15 @@ public static Map<String, String> parseUrlParameters(URL url) {
601601
Map<String, String> params = new HashMap<>();
602602

603603
try {
604+
String path = url.getPath();
605+
path = path.substring(path.indexOf('/') + 1);
606+
LOG.debug("path: {}", path);
607+
if (!path.trim().isEmpty()) {
608+
params.put("database", path);
609+
} else {
610+
params.put("database", "default");
611+
}
612+
604613
String query = url.getQuery();
605614
if (query != null) {
606615
for (String pair : query.split("&")) {

client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,15 @@ public QuerySettings logComment(String logComment) {
254254
public String getLogComment() {
255255
return logComment;
256256
}
257+
258+
public static QuerySettings merge(QuerySettings source, QuerySettings override) {
259+
QuerySettings merged = new QuerySettings();
260+
if (source != null) {
261+
merged.rawSettings.putAll(source.rawSettings);
262+
}
263+
if (override != null && override != source) {// avoid copying the literally same object
264+
merged.rawSettings.putAll(override.rawSettings);
265+
}
266+
return merged;
267+
}
257268
}

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.clickhouse.client.api.Client;
44
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
55
import com.clickhouse.client.api.query.QueryResponse;
6+
import com.clickhouse.client.api.query.QuerySettings;
67
import com.clickhouse.jdbc.internal.JdbcConfiguration;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
@@ -23,16 +24,30 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper {
2324
private boolean closed = false;
2425
private String catalog;
2526
private String schema;
27+
private QuerySettings defaultQuerySettings;
2628

2729
public ConnectionImpl(String url, Properties info) {
2830
log.debug("Creating connection to {}", url);
29-
3031
this.url = url;//Raw URL
3132
this.config = new JdbcConfiguration(url, info);
33+
String clientName = "ClickHouse JDBC Driver/" + Driver.driverVersion;
34+
35+
if (this.config.isDisableFrameworkDetection()) {
36+
log.debug("Framework detection is disabled.");
37+
} else {
38+
String detectedFrameworks = Driver.FrameworksDetection.getFrameworksDetected();
39+
log.debug("Detected frameworks: {}", detectedFrameworks);
40+
clientName += " (" + detectedFrameworks + ")";
41+
}
42+
3243
this.client = new Client.Builder()
3344
.fromUrl(this.config.getUrl())//URL without prefix
3445
.setUsername(config.getUser())
35-
.setPassword(config.getPassword()).build();
46+
.setPassword(config.getPassword())
47+
.setClientName(clientName)
48+
.build();
49+
50+
this.defaultQuerySettings = new QuerySettings();
3651
}
3752

3853
public String getUser() {
@@ -43,6 +58,14 @@ public String getURL() {
4358
return url;
4459
}
4560

61+
public QuerySettings getDefaultQuerySettings() {
62+
return defaultQuerySettings;
63+
}
64+
65+
public void setDefaultQuerySettings(QuerySettings settings) {
66+
this.defaultQuerySettings = settings;
67+
}
68+
4669
private String getServerVersion() throws SQLException {
4770
try (QueryResponse response = client.query("SELECT version()").get(30, TimeUnit.SECONDS)) {
4871
// Create a reader to access the data in a convenient way

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ public class Driver implements java.sql.Driver {
1414
private static final Logger log = LoggerFactory.getLogger(Driver.class);
1515
public static final String driverVersion;
1616

17+
public static String frameworksDetected = null;
18+
public static class FrameworksDetection {
19+
private static final List<String> FRAMEWORKS_TO_DETECT = Arrays.asList("apache.spark");
20+
static volatile String frameworksDetected = null;
21+
22+
private FrameworksDetection() {}
23+
public static String getFrameworksDetected() {
24+
if (frameworksDetected == null) {//Only detect frameworks once
25+
Set<String> inferredFrameworks = new LinkedHashSet<>();
26+
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
27+
for (String framework : FRAMEWORKS_TO_DETECT) {
28+
if (ste.toString().contains(framework)) {
29+
inferredFrameworks.add(String.format("(%s)", framework));
30+
}
31+
}
32+
}
33+
34+
frameworksDetected = String.join("; ", inferredFrameworks);
35+
}
36+
37+
return frameworksDetected;
38+
}
39+
}
40+
1741
static {
1842
log.debug("Initializing ClickHouse JDBC driver V2");
1943
String tempDriverVersion = Driver.class.getPackage().getImplementationVersion();

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,16 @@ protected static String parseJdbcEscapeSyntax(String sql) {
103103
@Override
104104
public ResultSet executeQuery(String sql) throws SQLException {
105105
checkClosed();
106-
return executeQuery(sql, new QuerySettings());
106+
return executeQuery(sql, connection.getDefaultQuerySettings());
107107
}
108108

109109
public ResultSet executeQuery(String sql, QuerySettings settings) throws SQLException {
110110
checkClosed();
111+
QuerySettings mergedSettings = QuerySettings.merge(connection.getDefaultQuerySettings(), settings);
111112

112113
try {
113114
sql = parseJdbcEscapeSyntax(sql);
114-
QueryResponse response = connection.client.query(sql, settings).get(queryTimeout, TimeUnit.SECONDS);
115+
QueryResponse response = connection.client.query(sql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS);
115116
ClickHouseBinaryFormatReader reader = connection.client.newBinaryFormatReader(response);
116117
currentResultSet = new ResultSetImpl(response, reader);
117118
metrics = response.getMetrics();
@@ -125,7 +126,7 @@ public ResultSet executeQuery(String sql, QuerySettings settings) throws SQLExce
125126
@Override
126127
public int executeUpdate(String sql) throws SQLException {
127128
checkClosed();
128-
return executeUpdate(sql, new QuerySettings());
129+
return executeUpdate(sql, connection.getDefaultQuerySettings());
129130
}
130131

131132
public int executeUpdate(String sql, QuerySettings settings) throws SQLException {
@@ -135,9 +136,11 @@ public int executeUpdate(String sql, QuerySettings settings) throws SQLException
135136
throw new SQLException("executeUpdate() cannot be called with a SELECT statement");
136137
}
137138

139+
QuerySettings mergedSettings = QuerySettings.merge(connection.getDefaultQuerySettings(), settings);
140+
138141
try {
139142
sql = parseJdbcEscapeSyntax(sql);
140-
QueryResponse response = connection.client.query(sql, settings).get(queryTimeout, TimeUnit.SECONDS);
143+
QueryResponse response = connection.client.query(sql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS);
141144
currentResultSet = null;
142145
metrics = response.getMetrics();
143146
response.close();

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class JdbcConfiguration {
2020
final String user;
2121
final String password;
2222
final String url;
23+
final String jdbcUrl;
24+
final boolean disableFrameworkDetection;
2325

2426
public String getPassword() {
2527
return password;
@@ -33,17 +35,27 @@ public String getUrl() {
3335
return url;
3436
}
3537

38+
public String getJdbcUrl() {
39+
return jdbcUrl;
40+
}
41+
42+
public boolean isDisableFrameworkDetection() {
43+
return disableFrameworkDetection;
44+
}
45+
3646
public JdbcConfiguration(String url, Properties info) {
47+
this.jdbcUrl = url;
3748
this.url = stripUrlPrefix(url);
3849
this.user = info.getProperty("user", "default");
3950
this.password = info.getProperty("password", "");
51+
this.disableFrameworkDetection = Boolean.parseBoolean(info.getProperty("disable_frameworks_detection", "false"));
4052
}
4153

4254
public static boolean acceptsURL(String url) {
4355
return url.startsWith(PREFIX_CLICKHOUSE) || url.startsWith(PREFIX_CLICKHOUSE_SHORT);
4456
}
4557

46-
public String stripUrlPrefix(String url) {
58+
private String stripUrlPrefix(String url) {
4759
if (url.startsWith(PREFIX_CLICKHOUSE)) {
4860
return url.substring(PREFIX_CLICKHOUSE.length());
4961
} else if (url.startsWith(PREFIX_CLICKHOUSE_SHORT)) {

0 commit comments

Comments
 (0)