Skip to content

Commit 0d0b9bb

Browse files
authored
Merge pull request #2757 from ClickHouse/02/24/26/jdbc_add_old_parameters
[JDBC-v2] Port some old properties
2 parents 13535b2 + a4e32ca commit 0d0b9bb

File tree

9 files changed

+235
-51
lines changed

9 files changed

+235
-51
lines changed

jdbc-v2/src/main/antlr4/com/clickhouse/jdbc/internal/parser/antlr4/ClickHouseParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ setStmt
668668
// SET ROLE statement
669669

670670
setRoleStmt
671-
: SET (DEFAULT)? ROLE (setRolesList | NONE | ALL (EXCEPT setRolesList)) (TO identifier | CURRENT_USER (COMMA identifier | CURRENT_USER)*)?
671+
: SET (DEFAULT)? ROLE ( NONE | setRolesList | ALL (EXCEPT setRolesList)) (TO identifier | CURRENT_USER (COMMA identifier | CURRENT_USER)*)?
672672
;
673673

674674
setRolesList

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
119119

120120

121121
this.sqlParser = SqlParserFacade.getParser(config.getDriverProperty(DriverProperties.SQL_PARSER.getKey(),
122-
DriverProperties.SQL_PARSER.getDefaultValue()));
122+
DriverProperties.SQL_PARSER.getDefaultValue()), config);
123123
this.featureManager = new FeatureManager(this.config);
124124
} catch (SQLException e) {
125125
throw e;

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,52 @@ public enum DriverProperties {
7878
*/
7979
QUERY_ID_GENERATOR("jdbc_query_id_generator", null),
8080

81+
/**
82+
* Controls logic of saving roles that were set using {@code SET <role>} statement.
83+
* Default: true - save roles
84+
*/
85+
REMEMBER_LAST_SET_ROLES("remember_last_set_roles", String.valueOf(Boolean.TRUE)),
86+
87+
/**
88+
* Deprecated and will be removed.
89+
* This property is here to keep backward compatibility with {@code com.clickhouse.client.http.config.ClickHouseHttpOption#CUSTOM_PARAMS}.
90+
* Original property is deprecated for {@code com.clickhouse.client.config.ClickHouseClientOption#CUSTOM_SETTINGS}
91+
* This property is expected to be a comma separated list of key-value pair that should be sent to server.
92+
* Pairs will be converted to new properties for client config.
93+
* Use {@link ClientConfigProperties#serverSetting(String)} instead
94+
*
95+
*/
96+
@Deprecated
97+
CUSTOM_HTTP_PARAMS("custom_http_params", null),
98+
99+
100+
/**
101+
* Deprecated and will be removed.
102+
* This property is here to keep backward compatibility with {@code com.clickhouse.client.config.ClickHouseClientOption#CUSTOM_SETTINGS}.
103+
* This property is expected to be a comma separated list of key-value pair that should be sent to server.
104+
* Pairs will be converted to new properties for client config.
105+
* Use {@link ClientConfigProperties#serverSetting(String)} instead
106+
*
107+
*/
108+
@Deprecated
109+
CUSTOM_SETTINGS("custom_settings", null),
110+
111+
/**
112+
* Deprecated as driver do not convert Date values to any timezone. Dates are sent as is.
113+
* Problem having this setting on connection level is that affects all child statements and
114+
* there is no way to control on column basis.
115+
* Driver ignores this setting but will throw exception when it is completely removed.
116+
*/
117+
@Deprecated
118+
USE_TZ_FOR_DATES("use_server_time_zone_for_dates", null),
119+
120+
/**
121+
* Current driver implementation uses only one http provider. So setting it has no effect.
122+
* Deprecated will be removed soon
123+
*/
124+
@Deprecated
125+
HTTP_CONNECTION_PROVIDER("http_connection_provider", null),
126+
81127
;
82128

83129

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected long executeUpdateImpl(String sql, QuerySettings settings) throws SQLE
235235
return updateCount;
236236
}
237237

238-
protected void postUpdateActions() throws SQLException {
238+
private void postUpdateActions() throws SQLException {
239239
if (parsedStatement.getUseDatabase() != null) {
240240
this.localSettings.setDatabase(parsedStatement.getUseDatabase());
241241
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ private void initProperties(Map<String, String> urlProperties, Properties provid
299299
propertyInfos.put(prop.getKey(), propertyInfo);
300300

301301
if (DRIVER_PROP_KEYS.contains(prop.getKey())) {
302+
if (prop.getKey().equalsIgnoreCase(DriverProperties.CUSTOM_HTTP_PARAMS.getKey()) ||
303+
prop.getKey().equalsIgnoreCase(DriverProperties.CUSTOM_SETTINGS.getKey())) {
304+
ClientConfigProperties.toKeyValuePairs(prop.getValue())
305+
.forEach((k, v) -> clientProperties.put(ClientConfigProperties.serverSetting(k), v));
306+
}
302307
driverProperties.put(prop.getKey(), prop.getValue());
303308
} else {
304309
clientProperties.put(prop.getKey(), prop.getValue());

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

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.clickhouse.client.api.sql.SQLUtils;
44
import com.clickhouse.data.ClickHouseUtils;
5+
import com.clickhouse.jdbc.DriverProperties;
56
import com.clickhouse.jdbc.internal.parser.antlr4.ClickHouseLexer;
67
import com.clickhouse.jdbc.internal.parser.antlr4.ClickHouseParser;
78
import com.clickhouse.jdbc.internal.parser.antlr4.ClickHouseParserBaseListener;
@@ -25,6 +26,7 @@
2526
import java.util.ArrayList;
2627
import java.util.Collections;
2728
import java.util.List;
29+
import java.util.Map;
2830
import java.util.stream.Collectors;
2931

3032
public abstract class SqlParserFacade {
@@ -37,6 +39,12 @@ public abstract class SqlParserFacade {
3739

3840
private static class JavaCCParser extends SqlParserFacade {
3941

42+
private final boolean processUseRolesExpr;
43+
44+
public JavaCCParser(boolean saveRoles) {
45+
this.processUseRolesExpr = saveRoles;
46+
}
47+
4048
@Override
4149
public ParsedStatement parsedStatement(String sql) {
4250
ParsedStatement stmt = new ParsedStatement();
@@ -45,22 +53,11 @@ public ParsedStatement parsedStatement(String sql) {
4553
stmt.setUseDatabase(parsedStmt.getDatabase());
4654
}
4755

48-
String rolesCount = parsedStmt.getSettings().get("_ROLES_COUNT");
49-
if (rolesCount != null) {
50-
int rolesCountInt = Integer.parseInt(rolesCount);
51-
ArrayList<String> roles = new ArrayList<>(rolesCountInt);
52-
boolean resetRoles = false;
53-
for (int i = 0; i < rolesCountInt; i++) {
54-
String role = parsedStmt.getSettings().get("_ROLE_" + i);
55-
if (role.equalsIgnoreCase("NONE")) {
56-
resetRoles = true;
57-
}
58-
roles.add(parsedStmt.getSettings().get("_ROLE_" + i));
59-
}
60-
if (resetRoles) {
61-
roles.clear();
56+
if (processUseRolesExpr) {
57+
List<String> roles = processRoles(parsedStmt.getSettings());
58+
if (roles != null) {
59+
stmt.setRoles(roles);
6260
}
63-
stmt.setRoles(roles);
6461
}
6562

6663
stmt.setInsert(parsedStmt.getStatementType() == StatementType.INSERT);
@@ -109,11 +106,39 @@ public ParsedPreparedStatement parsePreparedStatement(String sql) {
109106
}
110107
}
111108

109+
if (processUseRolesExpr) {
110+
List<String> roles = processRoles(parsedStmt.getSettings());
111+
if (roles != null) {
112+
stmt.setRoles(roles);
113+
}
114+
}
115+
112116
stmt.setUseFunction(parsedStmt.isFuncUsed());
113117
parseParameters(sql, stmt);
114118
return stmt;
115119
}
116120

121+
private List<String> processRoles(Map<String, String> settings) {
122+
String rolesCount = settings.get("_ROLES_COUNT");
123+
if (rolesCount != null) {
124+
int rolesCountInt = Integer.parseInt(rolesCount);
125+
ArrayList<String> roles = new ArrayList<>(rolesCountInt);
126+
boolean resetRoles = false;
127+
for (int i = 0; i < rolesCountInt; i++) {
128+
String role = settings.get("_ROLE_" + i);
129+
if (role.equalsIgnoreCase("NONE")) {
130+
resetRoles = true;
131+
}
132+
roles.add(settings.get("_ROLE_" + i));
133+
}
134+
if (resetRoles) {
135+
roles.clear();
136+
}
137+
return roles;
138+
}
139+
return null; // no roles present
140+
}
141+
117142

118143
public ClickHouseSqlStatement parse(String sql) {
119144
JdbcParseHandler handler = JdbcParseHandler.getInstance();
@@ -127,17 +152,23 @@ public ClickHouseSqlStatement parse(String sql) {
127152

128153
private static class ANTLR4Parser extends SqlParserFacade {
129154

155+
protected final boolean processUseRolesExpr;
156+
157+
public ANTLR4Parser(boolean saveRoles) {
158+
this.processUseRolesExpr = saveRoles;
159+
}
160+
130161
@Override
131162
public ParsedStatement parsedStatement(String sql) {
132163
ParsedStatement stmt = new ParsedStatement();
133-
parseSQL(sql, new ParsedStatementListener(stmt));
164+
parseSQL(sql, new ParsedStatementListener(stmt, processUseRolesExpr));
134165
return stmt;
135166
}
136167

137168
@Override
138169
public ParsedPreparedStatement parsePreparedStatement(String sql) {
139170
ParsedPreparedStatement stmt = new ParsedPreparedStatement();
140-
parseSQL(sql, new ParsedPreparedStatementListener(stmt));
171+
parseSQL(sql, new ParsedPreparedStatementListener(stmt, processUseRolesExpr));
141172

142173
// Combine database and table like JavaCC does
143174
String tableName = stmt.getTable();
@@ -177,12 +208,27 @@ static boolean isStmtWithResultSet(ClickHouseParser.QueryStmtContext stmtContext
177208
qCtx.existsStmt() != null || qCtx.checkStmt() != null);
178209
}
179210

211+
static List<String> processRolesExpr(ClickHouseParser.SetRoleStmtContext ctx) {
212+
if (ctx.NONE() != null) {
213+
return Collections.emptyList();
214+
} else {
215+
List<String> roles = new ArrayList<>();
216+
for (ClickHouseParser.IdentifierContext id : ctx.setRolesList().identifier()) {
217+
roles.add(SQLUtils.unquoteIdentifier(id.getText()));
218+
}
219+
return roles;
220+
}
221+
}
222+
180223
private static class ParsedStatementListener extends ClickHouseParserBaseListener {
181224

182225
private final ParsedStatement parsedStatement;
183226

184-
public ParsedStatementListener(ParsedStatement parsedStatement) {
227+
private final boolean processSetRolesExpr;
228+
229+
public ParsedStatementListener(ParsedStatement parsedStatement, boolean processSetRolesExpr) {
185230
this.parsedStatement = parsedStatement;
231+
this.processSetRolesExpr = processSetRolesExpr;
186232
}
187233

188234
@Override
@@ -206,14 +252,8 @@ public void enterUseStmt(ClickHouseParser.UseStmtContext ctx) {
206252

207253
@Override
208254
public void enterSetRoleStmt(ClickHouseParser.SetRoleStmtContext ctx) {
209-
if (ctx.NONE() != null) {
210-
parsedStatement.setRoles(Collections.emptyList());
211-
} else {
212-
List<String> roles = new ArrayList<>();
213-
for (ClickHouseParser.IdentifierContext id : ctx.setRolesList().identifier()) {
214-
roles.add(SQLUtils.unquoteIdentifier(id.getText()));
215-
}
216-
parsedStatement.setRoles(roles);
255+
if (processSetRolesExpr) {
256+
parsedStatement.setRoles(processRolesExpr(ctx));
217257
}
218258
}
219259
}
@@ -222,8 +262,11 @@ protected static class ParsedPreparedStatementListener extends ClickHouseParserB
222262

223263
protected final ParsedPreparedStatement parsedStatement;
224264

225-
public ParsedPreparedStatementListener(ParsedPreparedStatement parsedStatement) {
265+
private final boolean processSetRolesExpr;
266+
267+
public ParsedPreparedStatementListener(ParsedPreparedStatement parsedStatement, boolean processSetRolesExpr) {
226268
this.parsedStatement = parsedStatement;
269+
this.processSetRolesExpr = processSetRolesExpr;
227270
}
228271

229272
@Override
@@ -242,14 +285,8 @@ public void enterUseStmt(ClickHouseParser.UseStmtContext ctx) {
242285

243286
@Override
244287
public void enterSetRoleStmt(ClickHouseParser.SetRoleStmtContext ctx) {
245-
if (ctx.NONE() != null) {
246-
parsedStatement.setRoles(Collections.emptyList());
247-
} else {
248-
List<String> roles = new ArrayList<>();
249-
for (ClickHouseParser.IdentifierContext id : ctx.setRolesList().identifier()) {
250-
roles.add(SQLUtils.unquoteIdentifier(id.getText()));
251-
}
252-
parsedStatement.setRoles(roles);
288+
if (processSetRolesExpr) {
289+
parsedStatement.setRoles(processRolesExpr(ctx));
253290
}
254291
}
255292

@@ -349,10 +386,15 @@ public void exitInsertParameterFuncExpr(ClickHouseParser.InsertParameterFuncExpr
349386

350387
private static class ANTLR4AndParamsParser extends ANTLR4Parser {
351388

389+
390+
public ANTLR4AndParamsParser(boolean saveRoles) {
391+
super(saveRoles);
392+
}
393+
352394
@Override
353395
public ParsedPreparedStatement parsePreparedStatement(String sql) {
354396
ParsedPreparedStatement stmt = new ParsedPreparedStatement();
355-
parseSQL(sql, new ParseStatementAndParamsListener(stmt));
397+
parseSQL(sql, new ParseStatementAndParamsListener(stmt, processUseRolesExpr));
356398

357399
// Combine database and table like JavaCC does
358400
String tableName = stmt.getTable();
@@ -366,8 +408,8 @@ public ParsedPreparedStatement parsePreparedStatement(String sql) {
366408

367409
private static class ParseStatementAndParamsListener extends ParsedPreparedStatementListener {
368410

369-
public ParseStatementAndParamsListener(ParsedPreparedStatement parsedStatement) {
370-
super(parsedStatement);
411+
public ParseStatementAndParamsListener(ParsedPreparedStatement parsedStatement, boolean processSetRolesExpr) {
412+
super(parsedStatement, processSetRolesExpr);
371413
}
372414

373415
@Override
@@ -449,16 +491,18 @@ public enum SQLParser {
449491
ANTLR4
450492
}
451493

452-
public static SqlParserFacade getParser(String name) throws SQLException {
494+
public static SqlParserFacade getParser(String name, JdbcConfiguration jdbcConfiguration) throws SQLException {
453495
try {
496+
boolean saveRoles = Boolean.parseBoolean(jdbcConfiguration.getDriverProperty(DriverProperties.REMEMBER_LAST_SET_ROLES.getKey(),
497+
DriverProperties.REMEMBER_LAST_SET_ROLES.getDefaultValue()));
454498
SQLParser parserSelection = SQLParser.valueOf(name);
455499
switch (parserSelection) {
456500
case JAVACC:
457-
return new JavaCCParser();
501+
return new JavaCCParser(saveRoles);
458502
case ANTLR4_PARAMS_PARSER:
459-
return new ANTLR4AndParamsParser();
503+
return new ANTLR4AndParamsParser(saveRoles);
460504
case ANTLR4:
461-
return new ANTLR4Parser();
505+
return new ANTLR4Parser(saveRoles);
462506
}
463507
throw new SQLException("Unsupported parser: " + parserSelection);
464508
} catch (IllegalArgumentException e) {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,4 +1113,34 @@ public void testCustomParameters() throws Exception {
11131113
}
11141114
}
11151115
}
1116+
1117+
@Test(groups = {"integration"})
1118+
public void testOldCustomSettingsParameter() throws Exception {
1119+
if (isCloud()) {
1120+
return; // no custom settings on cloud instance
1121+
}
1122+
1123+
String sql = "SELECT " +
1124+
" getSetting('max_threads') AS max_threads, " +
1125+
" getSetting('session_timezone') AS tz ";
1126+
Properties properties = new Properties();
1127+
properties.put(DriverProperties.CUSTOM_SETTINGS.getKey(), "max_threads=10,session_timezone=Asia/Tokyo");
1128+
try (Connection connection = getJdbcConnection(properties)) {
1129+
try (Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql)){
1130+
assertTrue(rs.next());
1131+
assertEquals(rs.getString(1), "10");
1132+
assertEquals(rs.getString(2), "Asia/Tokyo");
1133+
}
1134+
}
1135+
1136+
properties = new Properties();
1137+
properties.put(DriverProperties.CUSTOM_HTTP_PARAMS.getKey(), "max_threads=10,session_timezone=Asia/Tokyo");
1138+
try (Connection connection = getJdbcConnection(properties)) {
1139+
try (Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql)){
1140+
assertTrue(rs.next());
1141+
assertEquals(rs.getString(1), "10");
1142+
assertEquals(rs.getString(2), "Asia/Tokyo");
1143+
}
1144+
}
1145+
}
11161146
}

0 commit comments

Comments
 (0)