forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[enhancement](Nereids) add post porcessor and error listener to prser (…
…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
Showing
11 changed files
with
334 additions
and
32 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
fe/fe-core/src/main/java/org/apache/doris/nereids/errors/QueryParsingErrors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
fe/fe-core/src/main/java/org/apache/doris/nereids/exceptions/ParseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/ParseErrorListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.