Skip to content

Commit 7881639

Browse files
committed
SQL: Relax StackOverflow circuit breaker for constants (#38572)
Constant numbers (of any form: integers, decimals, negatives, scientific) and strings shouldn't increase the depth counters as they don't contribute to the increment of the stack depth. Fixes: #38571
1 parent 7db33fd commit 7881639

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlParser.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package org.elasticsearch.xpack.sql.parser;
77

88
import com.carrotsearch.hppc.ObjectShortHashMap;
9-
109
import org.antlr.v4.runtime.BaseErrorListener;
1110
import org.antlr.v4.runtime.CharStream;
1211
import org.antlr.v4.runtime.CommonToken;
@@ -37,8 +36,6 @@
3736
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StatementContext;
3837
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StatementDefaultContext;
3938
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.UnquoteIdentifierContext;
40-
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ValueExpressionContext;
41-
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ValueExpressionDefaultContext;
4239
import org.elasticsearch.xpack.sql.plan.logical.LogicalPlan;
4340
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
4441

@@ -242,7 +239,6 @@ static class CircuitBreakerListener extends SqlBaseBaseListener {
242239
ENTER_EXIT_RULE_MAPPING.put(StatementDefaultContext.class.getSimpleName(), StatementContext.class.getSimpleName());
243240
ENTER_EXIT_RULE_MAPPING.put(QueryPrimaryDefaultContext.class.getSimpleName(), QueryTermContext.class.getSimpleName());
244241
ENTER_EXIT_RULE_MAPPING.put(BooleanDefaultContext.class.getSimpleName(), BooleanExpressionContext.class.getSimpleName());
245-
ENTER_EXIT_RULE_MAPPING.put(ValueExpressionDefaultContext.class.getSimpleName(), ValueExpressionContext.class.getSimpleName());
246242
}
247243

248244
private boolean insideIn = false;
@@ -265,6 +261,9 @@ public void enterEveryRule(ParserRuleContext ctx) {
265261
if (ctx.getClass() != UnquoteIdentifierContext.class &&
266262
ctx.getClass() != QuoteIdentifierContext.class &&
267263
ctx.getClass() != BackQuotedIdentifierContext.class &&
264+
ctx.getClass() != SqlBaseParser.ConstantContext.class &&
265+
ctx.getClass() != SqlBaseParser.NumberContext.class &&
266+
ctx.getClass() != SqlBaseParser.ValueExpressionContext.class &&
268267
(insideIn == false || ctx.getClass() != PrimaryExpressionContext.class)) {
269268

270269
int currentDepth = depthCounts.putOrAdd(ctx.getClass().getSimpleName(), (short) 1, (short) 1);

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/SqlParserTests.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,18 @@ public void testLimitToPreventStackOverflowFromLargeComplexSubselectTree() {
294294
}
295295

296296
public void testLimitStackOverflowForInAndLiteralsIsNotApplied() {
297-
int noChildren = 100_000;
297+
int noChildren = 10_000;
298298
LogicalPlan plan = parseStatement("SELECT * FROM t WHERE a IN(" +
299-
Joiner.on(",").join(nCopies(noChildren, "a + b")) + ")");
299+
Joiner.on(",").join(nCopies(noChildren, "a + 10")) + "," +
300+
Joiner.on(",").join(nCopies(noChildren, "-(-a - 10)")) + "," +
301+
Joiner.on(",").join(nCopies(noChildren, "20")) + "," +
302+
Joiner.on(",").join(nCopies(noChildren, "-20")) + "," +
303+
Joiner.on(",").join(nCopies(noChildren, "20.1234")) + "," +
304+
Joiner.on(",").join(nCopies(noChildren, "-20.4321")) + "," +
305+
Joiner.on(",").join(nCopies(noChildren, "1.1234E56")) + "," +
306+
Joiner.on(",").join(nCopies(noChildren, "-1.4321E-65")) + "," +
307+
Joiner.on(",").join(nCopies(noChildren, "'foo'")) + "," +
308+
Joiner.on(",").join(nCopies(noChildren, "'bar'")) + ")");
300309

301310
assertEquals(With.class, plan.getClass());
302311
assertEquals(Project.class, ((With) plan).child().getClass());
@@ -305,8 +314,17 @@ public void testLimitStackOverflowForInAndLiteralsIsNotApplied() {
305314
assertEquals(In.class, filter.condition().getClass());
306315
In in = (In) filter.condition();
307316
assertEquals("?a", in.value().toString());
308-
assertEquals(noChildren, in.list().size());
309-
assertThat(in.list().get(0).toString(), startsWith("Add[?a,?b]"));
317+
assertEquals(noChildren * 2 + 8, in.list().size());
318+
assertThat(in.list().get(0).toString(), startsWith("Add[?a,10]#"));
319+
assertThat(in.list().get(noChildren).toString(), startsWith("Neg[Sub[Neg[?a]#"));
320+
assertEquals("20", in.list().get(noChildren * 2).toString());
321+
assertEquals("-20", in.list().get(noChildren * 2 + 1).toString());
322+
assertEquals("20.1234", in.list().get(noChildren * 2 + 2).toString());
323+
assertEquals("-20.4321", in.list().get(noChildren * 2 + 3).toString());
324+
assertEquals("1.1234E56", in.list().get(noChildren * 2 + 4).toString());
325+
assertEquals("-1.4321E-65", in.list().get(noChildren * 2 + 5).toString());
326+
assertEquals("'foo'=foo", in.list().get(noChildren * 2 + 6).toString());
327+
assertEquals("'bar'=bar", in.list().get(noChildren * 2 + 7).toString());
310328
}
311329

312330
public void testDecrementOfDepthCounter() {

0 commit comments

Comments
 (0)