Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Support queries end with semi colon #609

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/user/dql/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The syntax of ``SELECT`` statement is as follows::
[ORDER BY expression [IS [NOT] NULL] [ASC | DESC] [, ...]]
[LIMIT [offset, ] size]

Although multiple query statements to execute in batch is not supported, ending with semicolon ``;`` is still allowed. For example, you can run ``SELECT * FROM accounts;`` without issue. This is useful to support queries generated by other tool, such as Microsoft Excel or BI tool.

Fundamentals
------------

Expand Down
1 change: 1 addition & 0 deletions integ-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ task docTest(type: RestIntegTestTask) {
include 'com/amazon/opendistroforelasticsearch/sql/doctest/**/*IT.class'
exclude 'com/amazon/opendistroforelasticsearch/sql/correctness/**/*IT.class'
exclude 'com/amazon/opendistroforelasticsearch/sql/ppl/**/*IT.class'
exclude 'com/amazon/opendistroforelasticsearch/sql/sql/**/*IT.class'
exclude 'com/amazon/opendistroforelasticsearch/sql/legacy/**/*IT.class'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ protected void init() throws Exception {
loadIndex(Index.BANK_WITH_NULL_VALUES);
}

@Test
public void queryEndWithSemiColonTest() {
executeQuery(StringUtils.format("SELECT * FROM %s;", TEST_INDEX_BANK), "jdbc");
}

@Test
public void searchTypeTest() throws IOException {
JSONObject response = executeQuery(String.format(Locale.ROOT, "SELECT * FROM %s LIMIT 1000",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The syntax of ``SELECT`` statement is as follows::
[ORDER BY expression [IS [NOT] NULL] [ASC | DESC] [, ...]]
[LIMIT [offset, ] size]

Although multiple query statements to execute in batch is not supported, ending with semicolon ``;`` is still allowed. For example, you can run ``SELECT * FROM accounts;`` without issue. This is useful to support queries generated by other tool, such as Microsoft Excel or BI tool.

Fundamentals
------------

Expand Down
2 changes: 1 addition & 1 deletion legacy/src/main/antlr/OpenDistroSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ options { tokenVocab=OpenDistroSqlLexer; }

// Root rule
root
: sqlStatement? EOF
: sqlStatement? SEMI? EOF
;

// Only SELECT, DELETE, SHOW and DSCRIBE are supported for now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ public static QueryAction create(Client client, String sql)
public static QueryAction create(Client client, QueryActionRequest request)
throws SqlParseException, SQLFeatureNotSupportedException {
String sql = request.getSql();
// Linebreak matcher
// Remove line breaker anywhere and semicolon at the end
sql = sql.replaceAll("\\R", " ").trim();
if (sql.endsWith(";")) {
sql = sql.substring(0, sql.length() - 1);
}

switch (getFirstWord(sql)) {
case "SELECT":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void missingWhereKeywordShouldThrowException() {
expectValidationFailWithErrorMessage(
"SELECT * FROM accounts age = 1",
"offending symbol [=]", // parser thought 'age' is alias of 'accounts' and failed at '='
"Expecting", "'WHERE'" // "Expecting tokens in {<EOF>, 'INNER', 'JOIN', ... 'WHERE', ','}"
"Expecting", ";" // "Expecting tokens in {<EOF>, ';'}"
);
}

Expand Down Expand Up @@ -130,6 +130,11 @@ public void arithmeticExpressionInWhereClauseShouldPass() {
validate("SELECT * FROM accounts WHERE age + 1 = 10");
}

@Test
public void queryEndWithSemiColonShouldPass() {
validate("SELECT * FROM accounts;");
}

private void expectValidationFailWithErrorMessage(String query, String... messages) {
exception.expect(SyntaxAnalysisException.class);
exception.expectMessage(allOf(Arrays.stream(messages).
Expand Down
2 changes: 1 addition & 1 deletion sql/src/main/antlr/OpenDistroSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ options { tokenVocab=OpenDistroSQLLexer; }

// Root rule
root
: sqlStatement? EOF
: sqlStatement? SEMI? EOF
;

// Only SELECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class SQLSyntaxParserTest {

private final SQLSyntaxParser parser = new SQLSyntaxParser();

@Test
public void canParseQueryEndWithSemiColon() {
assertNotNull(parser.parse("SELECT 123;"));
}

@Test
public void canParseSelectLiterals() {
assertNotNull(parser.parse("SELECT 123, 'hello'"));
Expand Down