Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Dec 20, 2024
1 parent 034085a commit 61c18a6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.doris.nereids.DorisLexer;
import org.apache.doris.nereids.DorisParser;
import org.apache.doris.nereids.StatementContext;
import org.apache.doris.nereids.analyzer.UnboundSlot;
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
import org.apache.doris.nereids.parser.plsql.PLSqlLogicalPlanBuilder;
import org.apache.doris.nereids.trees.expressions.Expression;
Expand All @@ -35,6 +36,7 @@
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.SessionVariable;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.antlr.v4.runtime.CharStreams;
Expand Down Expand Up @@ -66,6 +68,8 @@ public class NereidsParser {

private static final BitSet EXPLAIN_TOKENS = new BitSet();

private static final Map<String, Integer> LITERAL_TOKENS;

static {
EXPLAIN_TOKENS.set(DorisLexer.EXPLAIN);
EXPLAIN_TOKENS.set(DorisLexer.PARSED);
Expand All @@ -77,6 +81,14 @@ public class NereidsParser {
EXPLAIN_TOKENS.set(DorisLexer.PLAN);
EXPLAIN_TOKENS.set(DorisLexer.PROCESS);

ImmutableMap.Builder<String, Integer> literalToTokenType = ImmutableMap.builder();
for (int tokenType = 0; tokenType <= DorisLexer.VOCABULARY.getMaxTokenType(); tokenType++) {
String literalName = DorisLexer.VOCABULARY.getLiteralName(tokenType);
if (literalName != null) {
literalToTokenType.put(literalName.substring(1, literalName.length() - 1), tokenType);
}
}
LITERAL_TOKENS = literalToTokenType.build();
}

/**
Expand Down Expand Up @@ -256,9 +268,29 @@ public List<Pair<LogicalPlan, StatementContext>> parseMultiple(String sql,
}

public Expression parseExpression(String expression) {
if (isSimpleIdentifier(expression)) {
return new UnboundSlot(expression);
}
return parse(expression, DorisParser::expression);
}

private boolean isSimpleIdentifier(String expression) {
if (expression == null || expression.isEmpty()) {
return false;
}

boolean hasLetter = false;
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if ((('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_' || c == '$')) {
hasLetter = true;
} else if (!('0' <= c && c <= '9')) {
return false;
}
}
return hasLetter && !LITERAL_TOKENS.containsKey(expression.toUpperCase());
}

public DataType parseDataType(String dataType) {
return parse(dataType, DorisParser::dataType);
}
Expand Down
12 changes: 10 additions & 2 deletions fe/fe-core/src/main/java/org/apache/doris/nereids/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,16 @@ public class Utils {
*/
public static String quoteIfNeeded(String part) {
// We quote strings except the ones which consist of digits only.
return part.matches("\\w*[\\w&&[^\\d]]+\\w*")
? part : part.replace("`", "``");
StringBuilder quote = new StringBuilder(part.length());
for (int i = 0; i < part.length(); i++) {
char c = part.charAt(i);
if (c == '`') {
quote.append("``");
} else {
quote.append(c);
}
}
return quote.toString();
}

/**
Expand Down

0 comments on commit 61c18a6

Please sign in to comment.