Skip to content

Commit 265022f

Browse files
committed
Fixed errors in parser
1 parent 0bdb8b2 commit 265022f

File tree

3 files changed

+9
-23
lines changed

3 files changed

+9
-23
lines changed

jdbc/src/main/java/tech/ydb/jdbc/query/YdbQueryParser.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ public QueryType detectQueryType() throws SQLException {
5757

5858
@SuppressWarnings("MethodLength")
5959
public String parseSQL(String origin) throws SQLException {
60-
this.statements.clear();
61-
this.batcher.clear();
62-
6360
int fragmentStart = 0;
64-
6561
boolean detectJdbcArgs = false;
6662

6763
QueryStatement currStatement = null;
@@ -78,6 +74,7 @@ public String parseSQL(String origin) throws SQLException {
7874
for (int i = 0; i < chars.length; ++i) {
7975
char ch = chars[i];
8076
boolean isInsideKeyword = false;
77+
int keywordEnd = i; // parseSingleQuotes, parseDoubleQuotes, etc move index so we keep old value
8178
switch (ch) {
8279
case '\'': // single-quotes
8380
int singleQuitesEnd = parseSingleQuotes(chars, i);
@@ -140,7 +137,7 @@ public String parseSQL(String origin) throws SQLException {
140137

141138
if (keywordStart >= 0 && (!isInsideKeyword || (i == chars.length - 1))) {
142139
lastKeywordIsOffsetLimit = false;
143-
int keywordLength = isInsideKeyword ? i - keywordStart - 1 : i - keywordStart;
140+
int keywordLength = (isInsideKeyword ? i + 1 : keywordEnd) - keywordStart;
144141

145142
if (currStatement != null) {
146143
batcher.readIdentifier(chars, keywordStart, keywordLength);
@@ -192,14 +189,14 @@ public String parseSQL(String origin) throws SQLException {
192189
currStatement = new QueryStatement(QueryType.SCAN_QUERY, QueryCmd.SELECT);
193190
// Skip SCAN prefix
194191
parsed.append(chars, fragmentStart, keywordStart - fragmentStart);
195-
fragmentStart = isInsideKeyword ? i + 1 : i;
192+
fragmentStart = isInsideKeyword ? keywordEnd + 1 : keywordEnd;
196193
}
197194
// Detect explain expression - starts with EXPLAIN
198195
if (parseExplainKeyword(chars, keywordStart)) {
199196
currStatement = new QueryStatement(QueryType.EXPLAIN_QUERY, QueryCmd.SELECT);
200197
// Skip EXPLAIN prefix
201198
parsed.append(chars, fragmentStart, keywordStart - fragmentStart);
202-
fragmentStart = isInsideKeyword ? i + 1 : i;
199+
fragmentStart = isInsideKeyword ? keywordEnd + 1 : keywordEnd;
203200
}
204201
}
205202

@@ -334,21 +331,16 @@ private static int parseBlockComment(final char[] query, int offset) {
334331
return offset;
335332
}
336333

337-
private static boolean isSpace(char c) {
338-
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
339-
}
340-
341334
private static boolean parseAlterKeyword(char[] query, int offset) {
342-
if (query.length < (offset + 6)) {
335+
if (query.length < (offset + 5)) {
343336
return false;
344337
}
345338

346339
return (query[offset] | 32) == 'a'
347340
&& (query[offset + 1] | 32) == 'l'
348341
&& (query[offset + 2] | 32) == 't'
349342
&& (query[offset + 3] | 32) == 'e'
350-
&& (query[offset + 4] | 32) == 'r'
351-
&& isSpace(query[offset + 5]);
343+
&& (query[offset + 4] | 32) == 'r';
352344
}
353345

354346
private static boolean parseCreateKeyword(char[] query, int offset) {

jdbc/src/main/java/tech/ydb/jdbc/query/YqlBatcher.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ public boolean isValidBatch() {
6060
&& !columns.isEmpty() && columns.size() == values.size();
6161
}
6262

63-
public void clear() {
64-
this.state = State.INIT;
65-
this.cmd = null;
66-
this.tableName = null;
67-
this.columns.clear();
68-
this.values.clear();
69-
}
70-
7163
public void readInsert() {
7264
if (state == State.INIT) {
7365
state = State.CMD;

jdbc/src/test/java/tech/ydb/jdbc/query/YdbQueryParserTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class YdbQueryParserTest {
2424
"'exPlain\nupsert to', '\nupsert to'",
2525
"' Explain select * from table', ' select * from table'",
2626
"'\texPlain\nupsert to', '\t\nupsert to'",
27+
"'EXPLAIN/*comment*/UPSERT INTO', '/*comment*/UPSERT INTO'",
2728
"'EXPLAIN',''",
2829
})
2930
public void explainQueryTest(String sql, String prepared) throws SQLException {
@@ -40,6 +41,7 @@ public void explainQueryTest(String sql, String prepared) throws SQLException {
4041
@ParameterizedTest(name = "[{index}] {0} is scheme query")
4142
@ValueSource(strings = {
4243
" Alter table set;",
44+
"Alter--comment\ntable set;",
4345
"drOp table 'test'",
4446
"-- comment \nCreate;",
4547
})
@@ -168,7 +170,7 @@ public void validBatchedInsertTest(String sql) throws SQLException {
168170
"Upsert into table_name(c1, c2, c3) values (?, ? , ?)",
169171
"\n upsert into `table_name` (\t`c1`, c2, c3)values(?, ? , ?)",
170172
"/* comment */ Upsert into `table_name` (`c1`, /* commect */ c2, c3)values(?, ? , ?);\n-- post comment",
171-
";;Upsert into table_name (`c1`, /* comment */ c2, c3 ) values(?, ? , ?);",
173+
";;Upsert/* comment */into table_name (`c1`, /* comment */ c2, c3 ) values(?, ? , ?);",
172174
})
173175
public void validBatchedUpsertTest(String sql) throws SQLException {
174176
YdbQueryParser parser = new YdbQueryParser(true, true);

0 commit comments

Comments
 (0)