This is an automated email from the ASF dual-hosted git repository.

morrysnow pushed a commit to branch branch-3.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 4fb8403ff8e [opt](nereids) optimize rewrite of synchronize materialize 
view (#45748) (#45816)
4fb8403ff8e is described below

commit 4fb8403ff8e51f789ad9bee764c6c362c1aa1038
Author: 924060929 <lanhuaj...@selectdb.com>
AuthorDate: Tue Dec 24 13:30:33 2024 +0800

    [opt](nereids) optimize rewrite of synchronize materialize view (#45748) 
(#45816)
    
    cherry pick from #45748
---
 .../doris/common/profile/SummaryProfile.java       |  4 +-
 .../apache/doris/nereids/analyzer/MappingSlot.java |  2 +-
 .../doris/nereids/analyzer/UnboundAlias.java       |  2 +-
 .../doris/nereids/analyzer/UnboundFunction.java    |  2 +-
 .../apache/doris/nereids/analyzer/UnboundSlot.java | 14 +++++-
 .../apache/doris/nereids/analyzer/UnboundStar.java |  2 +-
 .../apache/doris/nereids/parser/NereidsParser.java | 53 ++++++++++++++++++++++
 .../mv/AbstractSelectMaterializedIndexRule.java    |  5 +-
 .../mv/SelectMaterializedIndexWithAggregate.java   |  6 ++-
 .../trees/expressions/AggregateExpression.java     |  2 +-
 .../doris/nereids/trees/expressions/Alias.java     |  2 +-
 .../trees/expressions/ArrayItemReference.java      |  2 +-
 .../nereids/trees/expressions/BinaryOperator.java  |  2 +-
 .../doris/nereids/trees/expressions/BoundStar.java |  2 +-
 .../doris/nereids/trees/expressions/CaseWhen.java  |  2 +-
 .../doris/nereids/trees/expressions/Cast.java      |  2 +-
 .../doris/nereids/trees/expressions/Exists.java    |  4 +-
 .../nereids/trees/expressions/Expression.java      | 10 ++++
 .../nereids/trees/expressions/InPredicate.java     |  2 +-
 .../nereids/trees/expressions/InSubquery.java      |  4 +-
 .../doris/nereids/trees/expressions/IsNull.java    |  2 +-
 .../doris/nereids/trees/expressions/ListQuery.java |  4 +-
 .../doris/nereids/trees/expressions/Match.java     |  2 +-
 .../doris/nereids/trees/expressions/Not.java       |  2 +-
 .../nereids/trees/expressions/OrderExpression.java |  2 +-
 .../nereids/trees/expressions/Placeholder.java     |  2 +-
 .../nereids/trees/expressions/Properties.java      |  2 +-
 .../nereids/trees/expressions/ScalarSubquery.java  |  4 +-
 .../nereids/trees/expressions/SlotReference.java   |  2 +-
 .../trees/expressions/StringRegexPredicate.java    |  2 +-
 .../nereids/trees/expressions/SubqueryExpr.java    |  2 +-
 .../trees/expressions/TimestampArithmetic.java     |  2 +-
 .../nereids/trees/expressions/UnaryOperator.java   |  2 +-
 .../doris/nereids/trees/expressions/Variable.java  |  2 +-
 .../nereids/trees/expressions/VariableDesc.java    |  2 +-
 .../trees/expressions/VirtualSlotReference.java    |  2 +-
 .../nereids/trees/expressions/WhenClause.java      |  2 +-
 .../trees/expressions/WindowExpression.java        |  2 +-
 .../nereids/trees/expressions/WindowFrame.java     |  2 +-
 .../trees/expressions/functions/BoundFunction.java |  2 +-
 .../functions/agg/AggregateFunction.java           |  2 +-
 .../trees/expressions/functions/agg/Count.java     |  4 +-
 .../functions/scalar/CryptoFunction.java           |  2 +-
 .../trees/expressions/functions/scalar/Lambda.java |  2 +-
 .../functions/table/TableValuedFunction.java       |  2 +-
 .../trees/expressions/literal/ArrayLiteral.java    |  2 +-
 .../trees/expressions/literal/DateLiteral.java     |  2 +-
 .../trees/expressions/literal/DateTimeLiteral.java |  2 +-
 .../trees/expressions/literal/DecimalLiteral.java  |  2 +-
 .../expressions/literal/DecimalV3Literal.java      |  2 +-
 .../nereids/trees/expressions/literal/Literal.java |  2 +-
 .../trees/expressions/literal/MapLiteral.java      |  2 +-
 .../trees/expressions/literal/MaxLiteral.java      |  2 +-
 .../trees/expressions/literal/StructLiteral.java   |  2 +-
 .../java/org/apache/doris/nereids/util/Utils.java  | 12 ++++-
 55 files changed, 149 insertions(+), 61 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/common/profile/SummaryProfile.java 
b/fe/fe-core/src/main/java/org/apache/doris/common/profile/SummaryProfile.java
index 2cc8eff6df9..a37070962f7 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/common/profile/SummaryProfile.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/common/profile/SummaryProfile.java
@@ -599,7 +599,9 @@ public class SummaryProfile {
     }
 
     public void setQueryPlanFinishTime() {
-        this.queryPlanFinishTime = TimeUtils.getStartTimeMs();
+        if (queryPlanFinishTime == -1) {
+            this.queryPlanFinishTime = TimeUtils.getStartTimeMs();
+        }
     }
 
     public void setQueryScheduleFinishTime() {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/MappingSlot.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/MappingSlot.java
index c7a020fd2ab..2e9e8419550 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/MappingSlot.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/MappingSlot.java
@@ -77,7 +77,7 @@ public class MappingSlot extends Slot {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return slot.toSql();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java
index 2be2130aba7..25d40dd5981 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java
@@ -59,7 +59,7 @@ public class UnboundAlias extends NamedExpression implements 
UnaryExpression, Un
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         StringBuilder stringBuilder = new StringBuilder();
         stringBuilder.append("(" + child() + ")");
         alias.ifPresent(name -> stringBuilder.append(" AS " + name));
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundFunction.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundFunction.java
index a53917f08cd..b4b21e40dcd 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundFunction.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundFunction.java
@@ -115,7 +115,7 @@ public class UnboundFunction extends Function implements 
Unbound, PropagateNulla
     }
 
     @Override
-    public String toSql() throws UnboundException {
+    public String computeToSql() throws UnboundException {
         String params = children.stream()
                 .map(Expression::toSql)
                 .collect(Collectors.joining(", "));
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundSlot.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundSlot.java
index f8581256980..fdcb9547837 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundSlot.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundSlot.java
@@ -81,8 +81,18 @@ public class UnboundSlot extends Slot implements Unbound, 
PropagateNullable {
     }
 
     @Override
-    public String toSql() {
-        return nameParts.stream().map(Utils::quoteIfNeeded).reduce((left, 
right) -> left + "." + right).orElse("");
+    public String computeToSql() {
+        switch (nameParts.size()) {
+            case 1: return Utils.quoteIfNeeded(nameParts.get(0));
+            case 2: return Utils.quoteIfNeeded(nameParts.get(0)) + "." + 
Utils.quoteIfNeeded(nameParts.get(1));
+            case 3: return Utils.quoteIfNeeded(nameParts.get(0)) + "." + 
Utils.quoteIfNeeded(nameParts.get(1))
+                    + "." + Utils.quoteIfNeeded(nameParts.get(2));
+            default: {
+                return nameParts.stream().map(Utils::quoteIfNeeded)
+                        .reduce((left, right) -> left + "." + right)
+                        .orElse("");
+            }
+        }
     }
 
     public static UnboundSlot quoted(String name) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundStar.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundStar.java
index 2875036eb07..0a0c3031f4e 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundStar.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundStar.java
@@ -52,7 +52,7 @@ public class UnboundStar extends NamedExpression implements 
LeafExpression, Unbo
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return Utils.qualifiedName(qualifier, "*");
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java
index 34646c1d657..4ed71bbbc14 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java
@@ -23,7 +23,9 @@ import org.apache.doris.catalog.Env;
 import org.apache.doris.common.Pair;
 import org.apache.doris.nereids.DorisLexer;
 import org.apache.doris.nereids.DorisParser;
+import org.apache.doris.nereids.DorisParser.NonReservedContext;
 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;
@@ -35,6 +37,8 @@ import org.apache.doris.plugin.PluginMgr;
 import org.apache.doris.qe.ConnectContext;
 import org.apache.doris.qe.SessionVariable;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import org.antlr.v4.runtime.CharStreams;
@@ -45,14 +49,17 @@ import org.antlr.v4.runtime.Token;
 import org.antlr.v4.runtime.TokenSource;
 import org.antlr.v4.runtime.atn.PredictionMode;
 import org.antlr.v4.runtime.misc.ParseCancellationException;
+import org.antlr.v4.runtime.tree.TerminalNode;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+import java.lang.reflect.Method;
 import java.util.BitSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import java.util.function.Function;
 import javax.annotation.Nullable;
 
@@ -66,6 +73,9 @@ public class NereidsParser {
 
     private static final BitSet EXPLAIN_TOKENS = new BitSet();
 
+    private static final Set<String> NON_RESERVED_KEYWORDS;
+    private static final Map<String, Integer> LITERAL_TOKENS;
+
     static {
         EXPLAIN_TOKENS.set(DorisLexer.EXPLAIN);
         EXPLAIN_TOKENS.set(DorisLexer.PARSED);
@@ -77,6 +87,25 @@ public class NereidsParser {
         EXPLAIN_TOKENS.set(DorisLexer.PLAN);
         EXPLAIN_TOKENS.set(DorisLexer.PROCESS);
 
+        ImmutableSet.Builder<String> nonReserveds = ImmutableSet.builder();
+        for (Method declaredMethod : 
NonReservedContext.class.getDeclaredMethods()) {
+            if (TerminalNode.class.equals(declaredMethod.getReturnType())
+                    && 
declaredMethod.getName().toUpperCase().equals(declaredMethod.getName())
+                    && declaredMethod.getParameterTypes().length == 0) {
+                String nonReserved = declaredMethod.getName();
+                nonReserveds.add(nonReserved);
+            }
+        }
+        NON_RESERVED_KEYWORDS = nonReserveds.build();
+
+        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();
     }
 
     /**
@@ -256,9 +285,33 @@ public class NereidsParser {
     }
 
     public Expression parseExpression(String expression) {
+        if (isSimpleIdentifier(expression)) {
+            return new UnboundSlot(expression);
+        }
         return parse(expression, DorisParser::expression);
     }
 
+    private static 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;
+            }
+        }
+        if (!hasLetter) {
+            return false;
+        }
+        String upperCase = expression.toUpperCase();
+        return (NON_RESERVED_KEYWORDS.contains(upperCase) || 
!LITERAL_TOKENS.containsKey(upperCase));
+    }
+
     public DataType parseDataType(String dataType) {
         return parse(dataType, DorisParser::dataType);
     }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/AbstractSelectMaterializedIndexRule.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/AbstractSelectMaterializedIndexRule.java
index e8679eaba9c..ceb496bc5c3 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/AbstractSelectMaterializedIndexRule.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/AbstractSelectMaterializedIndexRule.java
@@ -219,8 +219,9 @@ public abstract class AbstractSelectMaterializedIndexRule {
     }
 
     protected static boolean containsAllColumn(Expression expression, 
Set<String> mvColumnNames) {
-        if (mvColumnNames.contains(expression.toSql()) || mvColumnNames
-                
.contains(org.apache.doris.analysis.CreateMaterializedViewStmt.mvColumnBreaker(expression.toSql())))
 {
+        String sql = expression.toSql();
+        if (mvColumnNames.contains(sql) || mvColumnNames
+                
.contains(org.apache.doris.analysis.CreateMaterializedViewStmt.mvColumnBreaker(sql)))
 {
             return true;
         }
         if (expression.children().isEmpty()) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithAggregate.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithAggregate.java
index 468b9cf659c..fa03165b37c 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithAggregate.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/mv/SelectMaterializedIndexWithAggregate.java
@@ -625,9 +625,13 @@ public class SelectMaterializedIndexWithAggregate extends 
AbstractSelectMaterial
                         aggFuncsDiff(aggregateFunctions, aggRewriteResult), 
groupingExprs).isOn())
                 .collect(Collectors.toSet());
 
+        Set<MaterializedIndex> candidatesWithRewritingIndexes = 
candidatesWithRewriting.stream()
+                .map(result -> result.index)
+                .collect(Collectors.toSet());
+
         Set<MaterializedIndex> candidatesWithoutRewriting = 
indexesGroupByIsBaseOrNot
                 .getOrDefault(false, ImmutableList.of()).stream()
-                .filter(index -> !candidatesWithRewriting.contains(index))
+                .filter(index -> 
!candidatesWithRewritingIndexes.contains(index))
                 .filter(index -> preAggEnabledByHint(scan)
                         || checkPreAggStatus(scan, index.getId(), predicates, 
aggregateFunctions, groupingExprs).isOn())
                 .collect(Collectors.toSet());
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/AggregateExpression.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/AggregateExpression.java
index 2e20dd05180..86d7eb72382 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/AggregateExpression.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/AggregateExpression.java
@@ -100,7 +100,7 @@ public class AggregateExpression extends Expression 
implements UnaryExpression {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         if (aggregateParam.aggMode.productAggregateBuffer) {
             return "partial_" + function.toSql();
         } else {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java
index 9eea3afd879..53a82011ac4 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java
@@ -124,7 +124,7 @@ public class Alias extends NamedExpression implements 
UnaryExpression {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return child().toSql() + " AS `" + name.get() + "`";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ArrayItemReference.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ArrayItemReference.java
index c54ad358561..edc074af2b5 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ArrayItemReference.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ArrayItemReference.java
@@ -92,7 +92,7 @@ public class ArrayItemReference extends NamedExpression 
implements ExpectsInputT
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return child(0).toSql();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BinaryOperator.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BinaryOperator.java
index 750f3a77881..f699e7531f6 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BinaryOperator.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BinaryOperator.java
@@ -49,7 +49,7 @@ public abstract class BinaryOperator extends Expression 
implements BinaryExpress
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "(" + left().toSql() + " " + symbol + " " + right().toSql() + 
")";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BoundStar.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BoundStar.java
index 8b4bffad3fc..0789d9a6527 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BoundStar.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/BoundStar.java
@@ -35,7 +35,7 @@ public class BoundStar extends NamedExpression implements 
PropagateNullable {
         );
     }
 
-    public String toSql() {
+    public String computeToSql() {
         return 
children.stream().map(Expression::toSql).collect(Collectors.joining(", "));
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/CaseWhen.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/CaseWhen.java
index bd48b648a73..0c3687f5715 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/CaseWhen.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/CaseWhen.java
@@ -111,7 +111,7 @@ public class CaseWhen extends Expression {
     }
 
     @Override
-    public String toSql() throws UnboundException {
+    public String computeToSql() throws UnboundException {
         StringBuilder output = new StringBuilder("CASE");
         for (Expression child : children()) {
             if (child instanceof WhenClause) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Cast.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Cast.java
index 9122f0f4adb..20f8079bd9f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Cast.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Cast.java
@@ -95,7 +95,7 @@ public class Cast extends Expression implements 
UnaryExpression {
     }
 
     @Override
-    public String toSql() throws UnboundException {
+    public String computeToSql() throws UnboundException {
         return "cast(" + child().toSql() + " as " + targetType.toSql() + ")";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Exists.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Exists.java
index 3d3bd17c70e..8d097d0faa6 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Exists.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Exists.java
@@ -65,8 +65,8 @@ public class Exists extends SubqueryExpr {
     }
 
     @Override
-    public String toSql() {
-        return "EXISTS (SUBQUERY) " + super.toSql();
+    public String computeToSql() {
+        return "EXISTS (SUBQUERY) " + super.computeToSql();
     }
 
     @Override
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java
index 6063ad2b1cd..ab33ccfbfe4 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java
@@ -21,6 +21,7 @@ import org.apache.doris.common.Config;
 import org.apache.doris.nereids.analyzer.Unbound;
 import org.apache.doris.nereids.analyzer.UnboundVariable;
 import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.exceptions.UnboundException;
 import org.apache.doris.nereids.trees.AbstractTreeNode;
 import 
org.apache.doris.nereids.trees.expressions.ArrayItemReference.ArrayItemSlot;
 import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
@@ -69,6 +70,7 @@ public abstract class Expression extends 
AbstractTreeNode<Expression> implements
     private final Supplier<Set<Slot>> inputSlots = Suppliers.memoize(
             () -> collect(e -> e instanceof Slot && !(e instanceof 
ArrayItemSlot)));
     private final int fastChildrenHashCode;
+    private final Supplier<String> toSqlCache = 
Suppliers.memoize(this::computeToSql);
 
     protected Expression(Expression... children) {
         super(children);
@@ -226,6 +228,10 @@ public abstract class Expression extends 
AbstractTreeNode<Expression> implements
         return fastChildrenHashCode;
     }
 
+    protected String computeToSql() {
+        throw new UnboundException("sql");
+    }
+
     protected TypeCheckResult checkInputDataTypesInternal() {
         return TypeCheckResult.SUCCESS;
     }
@@ -317,6 +323,10 @@ public abstract class Expression extends 
AbstractTreeNode<Expression> implements
         return inferred;
     }
 
+    public final String toSql() {
+        return toSqlCache.get();
+    }
+
     @Override
     public Expression withChildren(List<Expression> children) {
         throw new RuntimeException();
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/InPredicate.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/InPredicate.java
index 53a753c4535..b8c0cf54471 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/InPredicate.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/InPredicate.java
@@ -122,7 +122,7 @@ public class InPredicate extends Expression {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return compareExpr.toSql() + " IN " + options.stream()
             .map(Expression::toSql).sorted()
             .collect(Collectors.joining(", ", "(", ")"));
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/InSubquery.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/InSubquery.java
index 8b7d0518181..71dc1f5eb4f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/InSubquery.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/InSubquery.java
@@ -77,8 +77,8 @@ public class InSubquery extends SubqueryExpr {
     }
 
     @Override
-    public String toSql() {
-        return this.compareExpr.toSql() + " IN (" + super.toSql() + ")";
+    public String computeToSql() {
+        return this.compareExpr.toSql() + " IN (" + super.computeToSql() + ")";
     }
 
     @Override
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/IsNull.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/IsNull.java
index 7bb8538fc75..22216a84baf 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/IsNull.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/IsNull.java
@@ -55,7 +55,7 @@ public class IsNull extends Expression implements 
UnaryExpression, AlwaysNotNull
     }
 
     @Override
-    public String toSql() throws UnboundException {
+    public String computeToSql() throws UnboundException {
         return child().toSql() + " IS NULL";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ListQuery.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ListQuery.java
index 214525d2594..16dade740b9 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ListQuery.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ListQuery.java
@@ -48,8 +48,8 @@ public class ListQuery extends SubqueryExpr {
     }
 
     @Override
-    public String toSql() {
-        return " (LISTQUERY) " + super.toSql();
+    public String computeToSql() {
+        return " (LISTQUERY) " + super.computeToSql();
     }
 
     @Override
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Match.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Match.java
index d9dcde287d3..405e3cb8fe4 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Match.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Match.java
@@ -76,7 +76,7 @@ public abstract class Match extends BinaryOperator implements 
PropagateNullable
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "(" + left().toSql() + " " + symbol + " " + right().toSql() + 
")";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Not.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Not.java
index 5061cab5ac9..b001da9118f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Not.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Not.java
@@ -102,7 +102,7 @@ public class Not extends Expression implements 
UnaryExpression, ExpectsInputType
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "( not " + child().toSql() + ")";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/OrderExpression.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/OrderExpression.java
index d09fe2c0a00..7e33d4315d8 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/OrderExpression.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/OrderExpression.java
@@ -81,7 +81,7 @@ public class OrderExpression extends Expression implements 
UnaryExpression, Prop
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return orderKey.toSql();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Placeholder.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Placeholder.java
index c79c2d9db6d..3ce8cdb017f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Placeholder.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Placeholder.java
@@ -66,7 +66,7 @@ public class Placeholder extends Expression implements 
LeafExpression {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "?";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Properties.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Properties.java
index db0c78c1f78..d604e919e31 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Properties.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Properties.java
@@ -56,7 +56,7 @@ public class Properties extends Expression implements 
LeafExpression {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return getMap()
                 .entrySet()
                 .stream()
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ScalarSubquery.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ScalarSubquery.java
index 178debe7db8..25a7052a4ac 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ScalarSubquery.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/ScalarSubquery.java
@@ -84,8 +84,8 @@ public class ScalarSubquery extends SubqueryExpr {
     }
 
     @Override
-    public String toSql() {
-        return " (SCALARSUBQUERY) " + super.toSql();
+    public String computeToSql() {
+        return " (SCALARSUBQUERY) " + super.computeToSql();
     }
 
     @Override
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java
index 679c8ab73bd..fb7d0a32303 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java
@@ -183,7 +183,7 @@ public class SlotReference extends Slot {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         if (subPath.isEmpty()) {
             return name.get();
         } else {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/StringRegexPredicate.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/StringRegexPredicate.java
index 8900ac92859..5a62be54f93 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/StringRegexPredicate.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/StringRegexPredicate.java
@@ -55,7 +55,7 @@ public abstract class StringRegexPredicate extends 
ScalarFunction
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return '(' + left().toSql() + ' ' + getName() + ' ' + right().toSql() 
+ ')';
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SubqueryExpr.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SubqueryExpr.java
index 35d0e566476..c08fda1dc6b 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SubqueryExpr.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SubqueryExpr.java
@@ -80,7 +80,7 @@ public abstract class SubqueryExpr extends Expression 
implements LeafExpression
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "(" + queryPlan + ")";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java
index 888a1a0869c..737778a82d1 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java
@@ -129,7 +129,7 @@ public class TimestampArithmetic extends Expression 
implements BinaryExpression,
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         StringBuilder strBuilder = new StringBuilder();
         if (funcName != null) {
             // Function-call like version.
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/UnaryOperator.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/UnaryOperator.java
index ace2c648dae..61efa91f262 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/UnaryOperator.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/UnaryOperator.java
@@ -46,7 +46,7 @@ public abstract class UnaryOperator extends Expression 
implements UnaryExpressio
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "(" + symbol + " " + child().toSql() + ")";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Variable.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Variable.java
index fd16b84b183..5944ec08744 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Variable.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Variable.java
@@ -85,7 +85,7 @@ public class Variable extends Expression implements 
LeafExpression {
     }
 
     @Override
-    public String toSql() throws UnboundException {
+    public String computeToSql() throws UnboundException {
         return toString();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VariableDesc.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VariableDesc.java
index 38f23ee40fa..3a16b38f9e1 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VariableDesc.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VariableDesc.java
@@ -50,7 +50,7 @@ public class VariableDesc extends Expression implements 
LeafExpression {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return toString();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VirtualSlotReference.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VirtualSlotReference.java
index 43f48537581..1b46a8552ba 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VirtualSlotReference.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VirtualSlotReference.java
@@ -82,7 +82,7 @@ public class VirtualSlotReference extends SlotReference 
implements SlotNotFromCh
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return getName();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WhenClause.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WhenClause.java
index 4ce77f22df1..adb862bb2f1 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WhenClause.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WhenClause.java
@@ -56,7 +56,7 @@ public class WhenClause extends Expression implements 
BinaryExpression, ExpectsI
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return " WHEN " + left().toSql() + " THEN " + right().toSql();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowExpression.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowExpression.java
index 5bea07fff00..7f26298c700 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowExpression.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowExpression.java
@@ -179,7 +179,7 @@ public class WindowExpression extends Expression {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         StringBuilder sb = new StringBuilder();
         sb.append(function.toSql()).append(" OVER(");
         if (!partitionKeys.isEmpty()) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowFrame.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowFrame.java
index 5cbb93ce374..58ed4f15f9b 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowFrame.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowFrame.java
@@ -95,7 +95,7 @@ public class WindowFrame extends Expression implements 
PropagateNullable, LeafEx
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         StringBuilder sb = new StringBuilder();
         sb.append(frameUnits + " ");
         if (rightBoundary != null) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/BoundFunction.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/BoundFunction.java
index 5ccc64a34bb..13d4b515ad7 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/BoundFunction.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/BoundFunction.java
@@ -85,7 +85,7 @@ public abstract class BoundFunction extends Function 
implements ComputeSignature
     }
 
     @Override
-    public String toSql() throws UnboundException {
+    public String computeToSql() throws UnboundException {
         StringBuilder sql = new StringBuilder(getName()).append("(");
         int arity = arity();
         for (int i = 0; i < arity; i++) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/AggregateFunction.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/AggregateFunction.java
index 90df2f531da..777c9c4cc7a 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/AggregateFunction.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/AggregateFunction.java
@@ -107,7 +107,7 @@ public abstract class AggregateFunction extends 
BoundFunction implements Expects
     }
 
     @Override
-    public String toSql() throws UnboundException {
+    public String computeToSql() throws UnboundException {
         StringBuilder sql = new StringBuilder(getName()).append("(");
         if (distinct) {
             sql.append("DISTINCT ");
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java
index e86e90974da..21e6ee1cba6 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java
@@ -119,11 +119,11 @@ public class Count extends NotNullableAggregateFunction
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         if (isStar) {
             return "count(*)";
         }
-        return super.toSql();
+        return super.computeToSql();
     }
 
     @Override
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/CryptoFunction.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/CryptoFunction.java
index 151f7ffc773..1e4a866ecdc 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/CryptoFunction.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/CryptoFunction.java
@@ -42,7 +42,7 @@ public abstract class CryptoFunction extends ScalarFunction
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         List<String> args = Lists.newArrayList();
         for (int i = 0; i < arity(); i++) {
             if (i == 1) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Lambda.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Lambda.java
index e8261f6391d..2ecab6090d8 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Lambda.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Lambda.java
@@ -126,7 +126,7 @@ public class Lambda extends Expression {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         StringBuilder builder = new StringBuilder();
         String argStr = argumentNames.get(0);
         if (argumentNames.size() > 1) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/table/TableValuedFunction.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/table/TableValuedFunction.java
index 837edf27ab1..4a4257e6760 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/table/TableValuedFunction.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/table/TableValuedFunction.java
@@ -130,7 +130,7 @@ public abstract class TableValuedFunction extends 
BoundFunction
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         String args = getTVFProperties()
                 .getMap()
                 .entrySet()
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/ArrayLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/ArrayLiteral.java
index 486eeddabd7..be84a5b32e3 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/ArrayLiteral.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/ArrayLiteral.java
@@ -94,7 +94,7 @@ public class ArrayLiteral extends Literal {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         String items = this.items.stream()
                 .map(Literal::toSql)
                 .collect(Collectors.joining(", "));
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java
index 6ea1d2af725..ed99e3025e8 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java
@@ -423,7 +423,7 @@ public class DateLiteral extends Literal {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "'" + getStringValue() + "'";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java
index 27470187eae..17c5678b051 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java
@@ -267,7 +267,7 @@ public class DateTimeLiteral extends DateLiteral {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "'" + getStringValue() + "'";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalLiteral.java
index 4ffc92c634d..1f0aa788cdc 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalLiteral.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalLiteral.java
@@ -112,7 +112,7 @@ public class DecimalLiteral extends FractionalLiteral {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return value.toPlainString();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java
index d8be4faf0c9..045da28bdb3 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java
@@ -152,7 +152,7 @@ public class DecimalV3Literal extends FractionalLiteral {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return value.toPlainString();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
index e8e37aaf697..69e61b03c82 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
@@ -135,7 +135,7 @@ public abstract class Literal extends Expression implements 
LeafExpression, Comp
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return toString();
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/MapLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/MapLiteral.java
index c57bd3a0487..dbcf74c971e 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/MapLiteral.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/MapLiteral.java
@@ -114,7 +114,7 @@ public class MapLiteral extends Literal {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         StringBuilder sb = new StringBuilder();
         sb.append("map(");
         if (!keys.isEmpty()) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/MaxLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/MaxLiteral.java
index ce1278a9ad4..763fdfb1f4f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/MaxLiteral.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/MaxLiteral.java
@@ -38,7 +38,7 @@ public class MaxLiteral extends Literal {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         return "MAX_VALUE";
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/StructLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/StructLiteral.java
index 3a46f1f5b83..f44aa663c9e 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/StructLiteral.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/StructLiteral.java
@@ -124,7 +124,7 @@ public class StructLiteral extends Literal {
     }
 
     @Override
-    public String toSql() {
+    public String computeToSql() {
         StringBuilder sb = new StringBuilder();
         sb.append("STRUCT(");
         for (int i = 0; i < fields.size(); i++) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/Utils.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/Utils.java
index 42b99f6effd..c111839fc50 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/Utils.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/Utils.java
@@ -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();
     }
 
     /**


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to