SQL: removed fallback to exact prediction in the parser (elastic/x-pack-elasticsearch#3522)

The grammar definition should not require the exact prediction on the
listener. Falling back to it hides potential ambiguities.
Moved it to a separate method so it can be enabled for debugging in
development similar to Painless picky mode.

Original commit: elastic/x-pack-elasticsearch@969cb0b5cb
This commit is contained in:
Costin Leau 2018-01-10 17:52:35 +02:00 committed by GitHub
parent ce81a34467
commit c204cc0aa3
1 changed files with 9 additions and 12 deletions

View File

@ -39,6 +39,7 @@ public class SqlParser {
* that deal with dates and times. * that deal with dates and times.
*/ */
private final DateTimeZone timeZone; private final DateTimeZone timeZone;
private final boolean DEBUG = false;
public SqlParser(DateTimeZone timeZone) { public SqlParser(DateTimeZone timeZone) {
this.timeZone = timeZone; this.timeZone = timeZone;
@ -74,27 +75,23 @@ public class SqlParser {
parser.removeErrorListeners(); parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER); parser.addErrorListener(ERROR_LISTENER);
//debug(parser); parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(parser);
} catch (Exception ex) {
// if we fail, parse with LL mode
tokenStream.reset(); // rewind input stream
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION); if (DEBUG) {
tree = parseFunction.apply(parser); debug(parser);
} }
ParserRuleContext tree = parseFunction.apply(parser);
postProcess(lexer, parser, tree); postProcess(lexer, parser, tree);
return visitor.apply(new AstBuilder(timeZone), tree); return visitor.apply(new AstBuilder(timeZone), tree);
} }
private void debug(SqlBaseParser parser) { private void debug(SqlBaseParser parser) {
// when debugging, use the exact prediction mode (needed for diagnostics as well)
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
parser.addParseListener(parser.new TraceListener()); parser.addParseListener(parser.new TraceListener());
parser.addErrorListener(new DiagnosticErrorListener() { parser.addErrorListener(new DiagnosticErrorListener() {