From 3ee51a871eb5b6d817ca586afcfc6752d3f8c462 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Sat, 15 Mar 2025 20:37:13 -0700 Subject: [PATCH 1/2] ESQL: Catch parsing exception (#124958) When an invalid popMode occurs (due to an invalid query), ANTLR throws an out-of-channel exception, bypassing the existing checks. This PR extends the checks and properly reports the error back to the user Fix #119025 (cherry picked from commit dc15462ca4f97cf622a284afa8ac0e797f02f8f1) # Conflicts: # x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlParser.java # x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java --- docs/changelog/124958.yaml | 6 ++++++ .../org/elasticsearch/xpack/esql/parser/EsqlParser.java | 4 ++++ .../xpack/esql/parser/StatementParserTests.java | 7 +++++++ 3 files changed, 17 insertions(+) create mode 100644 docs/changelog/124958.yaml diff --git a/docs/changelog/124958.yaml b/docs/changelog/124958.yaml new file mode 100644 index 0000000000000..be7f646b7dcae --- /dev/null +++ b/docs/changelog/124958.yaml @@ -0,0 +1,6 @@ +pr: 124958 +summary: Catch parsing exception +area: ES|QL +type: bug +issues: + - 119025 \ No newline at end of file diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlParser.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlParser.java index 620a25e0170ea..526599d366280 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlParser.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlParser.java @@ -21,6 +21,7 @@ import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; import java.util.BitSet; +import java.util.EmptyStackException; import java.util.function.BiFunction; import java.util.function.Function; import java.util.regex.Matcher; @@ -91,6 +92,9 @@ private T invokeParser( return result.apply(new AstBuilder(params), tree); } catch (StackOverflowError e) { throw new ParsingException("ESQL statement is too large, causing stack overflow when generating the parsing tree: [{}]", query); + // likely thrown by an invalid popMode (such as extra closing parenthesis) + } catch (EmptyStackException ese) { + throw new ParsingException("Invalid query [{}]", query); } } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java index 11ffe587155e2..75a6ec71d4d27 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java @@ -2346,4 +2346,11 @@ public void testInvalidMatchOperator() { "line 1:37: mismatched input ':' expecting {, '|', 'and', '::', 'or', '+', '-', '*', '/', '%'}" ); } + + public void testUnclosedParenthesis() { + String[] queries = { "row a = )", "row ]", "from source | eval x = [1,2,3]]" }; + for (String q : queries) { + expectError(q, "Invalid query"); + } + } } From b4892b165ec888399a53ed18a314e0fac4b889b4 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Sun, 16 Mar 2025 19:55:11 -0700 Subject: [PATCH 2/2] Update StatementParserTests.java Fix test for 8.x branch --- .../elasticsearch/xpack/esql/parser/StatementParserTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java index 75a6ec71d4d27..ed0727e38ecf1 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java @@ -2348,7 +2348,7 @@ public void testInvalidMatchOperator() { } public void testUnclosedParenthesis() { - String[] queries = { "row a = )", "row ]", "from source | eval x = [1,2,3]]" }; + String[] queries = { "row ]", "from source | eval x = [1,2,3]]" }; for (String q : queries) { expectError(q, "Invalid query"); }