Skip to content

Commit 520f82f

Browse files
ruanwenjunruanwenjun
authored andcommitted
[KYUUBI #7109] Ignore the ? in backticks
### Why are the changes needed? We will split the sql by `?` when we use `KyuubiPreparedStatement`. But there exist corner case when ? exist in backticks. For example, below sql contains `?`, but we shouldn't split it by `?`. ```sql SELECT `(ds|hr)?+.+` FROM sales ``` More details can find at https://hive.apache.org/docs/latest/languagemanual-select_27362043/#regex-column-specification Hive upstream fix - HIVE-29060 ### How was this patch tested? UT. ### Was this patch authored or co-authored using generative AI tooling? NO. Closes #7125 from ruanwenjun/dev_wenjun_fix7109. Closes #7109 7140980 [ruanwenjun] [KYUUBI #7109] Ignore the ? in backticks Lead-authored-by: Wenjun Ruan <wenjun@apache.org> Co-authored-by: ruanwenjun <zyb@wenjuns-MacBook-Pro-2.local> Signed-off-by: Cheng Pan <chengpan@apache.org> (cherry picked from commit 4e40f94) Signed-off-by: Cheng Pan <chengpan@apache.org>
1 parent cd83583 commit 520f82f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ static List<String> splitSqlStatement(String sql) {
124124
boolean inSingleQuote = false;
125125
boolean inDoubleQuote = false;
126126
boolean inComment = false;
127+
boolean inBackticks = false;
127128
int off = 0;
128129
boolean skip = false;
129130

@@ -148,8 +149,13 @@ static List<String> splitSqlStatement(String sql) {
148149
inDoubleQuote = !inDoubleQuote;
149150
}
150151
break;
151-
case '-':
152+
case '`':
152153
if (!inSingleQuote && !inDoubleQuote) {
154+
inBackticks = !inBackticks;
155+
}
156+
break;
157+
case '-':
158+
if (!inSingleQuote && !inDoubleQuote && !inBackticks) {
153159
if (i < sql.length() - 1 && sql.charAt(i + 1) == '-') {
154160
inComment = true;
155161
}
@@ -161,7 +167,7 @@ static List<String> splitSqlStatement(String sql) {
161167
}
162168
break;
163169
case '?':
164-
if (!inSingleQuote && !inDoubleQuote) {
170+
if (!inSingleQuote && !inDoubleQuote && !inBackticks) {
165171
parts.add(sql.substring(off, i));
166172
off = i + 1;
167173
}
@@ -191,7 +197,7 @@ public static String updateSql(final String sql, HashMap<Integer, String> parame
191197
}
192198

193199
public static JdbcConnectionParams parseURL(String uri)
194-
throws JdbcUriParseException, SQLException, ZooKeeperHiveClientException {
200+
throws JdbcUriParseException, ZooKeeperHiveClientException {
195201
return parseURL(uri, new Properties());
196202
}
197203

@@ -215,7 +221,7 @@ public static JdbcConnectionParams parseURL(String uri)
215221
* jdbc:hive2://server:10001/db;user=foo;password=bar?hive.server2.transport.mode=http;hive.server2.thrift.http.path=hs2
216222
*/
217223
public static JdbcConnectionParams parseURL(String uri, Properties info)
218-
throws JdbcUriParseException, SQLException, ZooKeeperHiveClientException {
224+
throws JdbcUriParseException, ZooKeeperHiveClientException {
219225
JdbcConnectionParams connParams = extractURLComponents(uri, info);
220226
if (ZooKeeperHiveClientHelper.isZkDynamicDiscoveryMode(connParams.getSessionVars())) {
221227
configureConnParamsFromZooKeeper(connParams);

kyuubi-hive-jdbc/src/test/java/org/apache/kyuubi/jdbc/hive/UtilsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,17 @@ public void testSplitSqlStatement() {
197197
assertEquals("--comments\n" + "select --? \n", splitSql.get(0));
198198
assertEquals(" from ", splitSql.get(1));
199199
assertEquals("", splitSql.get(2));
200+
201+
String inIdentifierQuoted =
202+
"SELECT "
203+
+ "regexp_replace(col2, '\\n|\\r|\\t', '') as col2, "
204+
+ "`(col2|col2)?+.+` "
205+
+ "FROM ?";
206+
splitSql = Utils.splitSqlStatement(inIdentifierQuoted);
207+
assertEquals(2, splitSql.size());
208+
assertEquals(
209+
"SELECT regexp_replace(col2, '\\n|\\r|\\t', '') as col2, `(col2|col2)?+.+` FROM ",
210+
splitSql.get(0));
211+
assertEquals("", splitSql.get(1));
200212
}
201213
}

0 commit comments

Comments
 (0)