Skip to content

Commit

Permalink
[enhancement](Nereids) add post porcessor and error listener to prser (
Browse files Browse the repository at this point in the history
…apache#10306)

add parser error listener and post processor to parser

error listener:
- throw exception when parser find unexpected syntax

post processor:
- throw exception when find error indent
- replace '``' with '`' in quoted identifier
- replace non reserved key word with normal identifier
  • Loading branch information
morrySnow authored Jul 1, 2022
1 parent 3b3debf commit 632ff01
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.errors;

import org.apache.doris.nereids.DorisParser.ErrorIdentContext;
import org.apache.doris.nereids.exceptions.ParseException;

/**
* Exception packaging to improve code readability.
*/
public class QueryParsingErrors {
public static ParseException unquotedIdentifierError(String ident, ErrorIdentContext ctx) {
return new ParseException(String.format("Possibly unquoted identifier %s detected. "
+ "Please consider quoting it with back-quotes as `%s`", ident, ident), ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,58 @@

package org.apache.doris.nereids.exceptions;

import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;

import java.util.Optional;

/** Nereids's AnalysisException. */
public class AnalysisException extends RuntimeException {
private final String message;
private final Optional<Integer> line;
private final Optional<Integer> startPosition;
private final Optional<LogicalPlan> plan;

public AnalysisException(String msg, Throwable cause) {
super(msg, cause);
public AnalysisException(String message, Throwable cause, Optional<Integer> line,
Optional<Integer> startPosition, Optional<LogicalPlan> plan) {
super(message, cause);
this.message = message;
this.line = line;
this.startPosition = startPosition;
this.plan = plan;
}

public AnalysisException(String message) {
public AnalysisException(String message, Optional<Integer> line,
Optional<Integer> startPosition, Optional<LogicalPlan> plan) {
super(message);
this.message = message;
this.line = line;
this.startPosition = startPosition;
this.plan = plan;
}


public AnalysisException(String message, Throwable cause) {
this(message, cause, Optional.empty(), Optional.empty(), Optional.empty());
}

public AnalysisException(String message) {
this(message, Optional.empty(), Optional.empty(), Optional.empty());
}

@Override
public String getMessage() {
String planAnnotation = plan.map(p -> ";\n" + p.treeString()).orElse("");
return getSimpleMessage() + planAnnotation;
}

private String getSimpleMessage() {
if (line.isPresent() || startPosition.isPresent()) {
String lineAnnotation = line.map(l -> "line " + l).orElse("");
String positionAnnotation = startPosition.map(s -> " pos " + s).orElse("");
return message + ";" + lineAnnotation + positionAnnotation;
} else {
return message;
}
}

// TODO: support ErrorCode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.exceptions;

import org.apache.doris.nereids.parser.Origin;
import org.apache.doris.nereids.parser.ParserUtils;

import org.antlr.v4.runtime.ParserRuleContext;

import java.util.Optional;

/**
* sql parsing exception.
*/
public class ParseException extends AnalysisException {
private final String message;
private final Origin start;
private final Optional<String> command;

public ParseException(String message, Origin start, Optional<String> command) {
super(message, start.line, start.startPosition, Optional.empty());
this.message = message;
this.start = start;
this.command = command;
}

public ParseException(String message, ParserRuleContext ctx) {
this(message, ParserUtils.position(ctx.getStart()), Optional.of(ParserUtils.command(ctx)));
}

@Override
public String getMessage() {
StringBuilder sb = new StringBuilder();
sb.append("\n").append(message);
if (start.line.isPresent() && start.startPosition.isPresent()) {
int line = start.line.get();
int startPosition = start.startPosition.get();
sb.append("(line ").append(line).append(", pos").append(startPosition).append(")").append("\n");
if (command.isPresent()) {
sb.append("\n== SQL ==\n");
String cmd = command.get();
String[] splitCmd = cmd.split("\n");
for (int i = 0; i < line; i++) {
sb.append(splitCmd[i]).append("\n");
}
for (int i = 0; i < startPosition; i++) {
sb.append("-");
}
sb.append("^^^\n");
for (int i = line; i < splitCmd.length; i++) {
sb.append(splitCmd[i]).append("\n");
}
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.doris.analysis.StatementBase;
import org.apache.doris.nereids.DorisLexer;
import org.apache.doris.nereids.DorisParser;
import org.apache.doris.nereids.exceptions.ParsingException;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlanAdapter;
Expand All @@ -39,6 +38,8 @@
* Sql parser, convert sql DSL to logical plan.
*/
public class NereidsParser {
private static final ParseErrorListener PARSE_ERROR_LISTENER = new ParseErrorListener();
private static final PostProcessor POST_PROCESSOR = new PostProcessor();

/**
* In MySQL protocol, client could send multi-statement in.
Expand All @@ -61,31 +62,33 @@ public List<StatementBase> parseSQL(String originStr) throws Exception {
* @param sql sql string
* @return logical plan
*/
public LogicalPlan parseSingle(String sql) throws Exception {
public LogicalPlan parseSingle(String sql) {
return (LogicalPlan) parse(sql, DorisParser::singleStatement);
}

public List<LogicalPlan> parseMultiple(String sql) throws Exception {
public List<LogicalPlan> parseMultiple(String sql) {
return (List<LogicalPlan>) parse(sql, DorisParser::multiStatements);
}

public Expression parseExpression(String expression) {
return (Expression) parse(expression, DorisParser::expression);
}

private Object parse(String sql, Function<DorisParser, ParserRuleContext> parseFunction) {
try {
ParserRuleContext tree = toAst(sql, parseFunction);
LogicalPlanBuilder logicalPlanBuilder = new LogicalPlanBuilder();
return logicalPlanBuilder.visit(tree);
} catch (StackOverflowError e) {
throw new ParsingException(e.getMessage());
}
ParserRuleContext tree = toAst(sql, parseFunction);
LogicalPlanBuilder logicalPlanBuilder = new LogicalPlanBuilder();
return logicalPlanBuilder.visit(tree);
}

private ParserRuleContext toAst(String sql, Function<DorisParser, ParserRuleContext> parseFunction) {
DorisLexer lexer = new DorisLexer(new CaseInsensitiveStream(CharStreams.fromString(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
DorisParser parser = new DorisParser(tokenStream);
// parser.addParseListener(PostProcessor)
// parser.removeErrorListeners()
// parser.addErrorListener(ParseErrorListener)

parser.addParseListener(POST_PROCESSOR);
parser.removeErrorListeners();
parser.addErrorListener(PARSE_ERROR_LISTENER);

ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
Expand All @@ -101,8 +104,4 @@ private ParserRuleContext toAst(String sql, Function<DorisParser, ParserRuleCont
}
return tree;
}

public Expression createExpression(String expression) {
return (Expression) parse(expression, DorisParser::expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.exceptions;
package org.apache.doris.nereids.parser;

import java.util.Optional;

/**
* sql parsing exception.
* Record for token's line number and position in line.
*/
public class ParsingException extends RuntimeException {
public class Origin {
public final Optional<Integer> line;
public final Optional<Integer> startPosition;

public ParsingException(String message) {
super(message);
public Origin(int line, int startPosition) {
this(Optional.of(line), Optional.of(startPosition));
}

public Origin(Optional<Integer> line, Optional<Integer> startPosition) {
this.line = line;
this.startPosition = startPosition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.parser;

import org.apache.doris.nereids.exceptions.ParseException;

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;

import java.util.Optional;

/**
* Listen parse error, and throw {@link ParseException} with reasonable message.
*/
public class ParseErrorListener extends BaseErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException e) {
Origin start;
if (offendingSymbol instanceof CommonToken) {
CommonToken token = (CommonToken) offendingSymbol;
start = new Origin(line, token.getCharPositionInLine());
} else {
start = new Origin(line, charPositionInLine);
}
throw new ParseException(msg, start, Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.apache.doris.nereids.parser;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.Interval;

import java.util.function.Supplier;

Expand All @@ -28,4 +31,13 @@ public class ParserUtils {
public static <T> T withOrigin(ParserRuleContext ctx, Supplier<T> f) {
return f.get();
}

public static String command(ParserRuleContext ctx) {
CharStream stream = ctx.getStart().getInputStream();
return stream.getText(Interval.of(0, stream.size() - 1));
}

public static Origin position(Token token) {
return new Origin(token.getLine(), token.getCharPositionInLine());
}
}
Loading

0 comments on commit 632ff01

Please sign in to comment.