Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Relax StackOverflow circuit breaker for constants #38572

Merged
merged 4 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.elasticsearch.xpack.sql.parser;

import com.carrotsearch.hppc.ObjectShortHashMap;

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
Expand Down Expand Up @@ -37,8 +36,6 @@
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StatementContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StatementDefaultContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.UnquoteIdentifierContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ValueExpressionContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ValueExpressionDefaultContext;
import org.elasticsearch.xpack.sql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;

Expand Down Expand Up @@ -242,7 +239,6 @@ static class CircuitBreakerListener extends SqlBaseBaseListener {
ENTER_EXIT_RULE_MAPPING.put(StatementDefaultContext.class.getSimpleName(), StatementContext.class.getSimpleName());
ENTER_EXIT_RULE_MAPPING.put(QueryPrimaryDefaultContext.class.getSimpleName(), QueryTermContext.class.getSimpleName());
ENTER_EXIT_RULE_MAPPING.put(BooleanDefaultContext.class.getSimpleName(), BooleanExpressionContext.class.getSimpleName());
ENTER_EXIT_RULE_MAPPING.put(ValueExpressionDefaultContext.class.getSimpleName(), ValueExpressionContext.class.getSimpleName());
}

private boolean insideIn = false;
Expand All @@ -265,6 +261,9 @@ public void enterEveryRule(ParserRuleContext ctx) {
if (ctx.getClass() != UnquoteIdentifierContext.class &&
ctx.getClass() != QuoteIdentifierContext.class &&
ctx.getClass() != BackQuotedIdentifierContext.class &&
ctx.getClass() != SqlBaseParser.ConstantContext.class &&
ctx.getClass() != SqlBaseParser.NumberContext.class &&
ctx.getClass() != SqlBaseParser.ValueExpressionContext.class &&
(insideIn == false || ctx.getClass() != PrimaryExpressionContext.class)) {

int currentDepth = depthCounts.putOrAdd(ctx.getClass().getSimpleName(), (short) 1, (short) 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,16 @@ public void testLimitToPreventStackOverflowFromLargeComplexSubselectTree() {
public void testLimitStackOverflowForInAndLiteralsIsNotApplied() {
int noChildren = 100_000;
LogicalPlan plan = parseStatement("SELECT * FROM t WHERE a IN(" +
Joiner.on(",").join(nCopies(noChildren, "a + b")) + ")");
Joiner.on(",").join(nCopies(noChildren, "a + 10")) + "," +
Joiner.on(",").join(nCopies(noChildren, "-(-a - 10)")) + "," +
Joiner.on(",").join(nCopies(noChildren, "20")) + "," +
Joiner.on(",").join(nCopies(noChildren, "-20")) + "," +
Joiner.on(",").join(nCopies(noChildren, "20.1234")) + "," +
Joiner.on(",").join(nCopies(noChildren, "-20.4321")) + "," +
Joiner.on(",").join(nCopies(noChildren, "1.1234E56")) + "," +
Joiner.on(",").join(nCopies(noChildren, "-1.4321E-65")) + "," +
Joiner.on(",").join(nCopies(noChildren, "'foo'")) + "," +
Joiner.on(",").join(nCopies(noChildren, "'bar'")) + ")");

assertEquals(With.class, plan.getClass());
assertEquals(Project.class, ((With) plan).child().getClass());
Expand All @@ -305,8 +314,17 @@ public void testLimitStackOverflowForInAndLiteralsIsNotApplied() {
assertEquals(In.class, filter.condition().getClass());
In in = (In) filter.condition();
assertEquals("?a", in.value().toString());
assertEquals(noChildren, in.list().size());
assertThat(in.list().get(0).toString(), startsWith("Add[?a,?b]"));
assertEquals(noChildren * 2 + 8, in.list().size());
assertThat(in.list().get(0).toString(), startsWith("Add[?a,10]#"));
assertThat(in.list().get(noChildren).toString(), startsWith("Neg[Sub[Neg[?a]#"));
assertEquals("20", in.list().get(noChildren * 2).toString());
assertEquals("-20", in.list().get(noChildren * 2 + 1).toString());
assertEquals("20.1234", in.list().get(noChildren * 2 + 2).toString());
assertEquals("-20.4321", in.list().get(noChildren * 2 + 3).toString());
assertEquals("1.1234E56", in.list().get(noChildren * 2 + 4).toString());
assertEquals("-1.4321E-65", in.list().get(noChildren * 2 + 5).toString());
assertEquals("'foo'=foo", in.list().get(noChildren * 2 + 6).toString());
assertEquals("'bar'=bar", in.list().get(noChildren * 2 + 7).toString());
}

public void testDecrementOfDepthCounter() {
Expand Down