ExprToExternalSqlVisitor.java

// 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.analysis;

import org.apache.doris.common.util.ToSqlContext;
import org.apache.doris.qe.ConnectContext;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Visitor that produces the external SQL string for any {@link Expr}.
 * Replaces the 4-arg {@code toSqlImpl(boolean, boolean, TableType, TableIf)} body in every subclass.
 *
 * <p>Usage examples:
 * <pre>
 *   // equivalent to expr.toSqlWithoutTbl()
 *   expr.accept(ExprToExternalSqlVisitor.INSTANCE, ToSqlParams.WITHOUT_TABLE)
 *
 *   // equivalent to expr.toExternalSql(tableType, table)
 *   expr.accept(ExprToExternalSqlVisitor.INSTANCE, new ToSqlParams(false, true, tableType, table))
 * </pre>
 */
public class ExprToExternalSqlVisitor extends ExprVisitor<String, ToSqlParams> {

    public static final ExprToExternalSqlVisitor INSTANCE = new ExprToExternalSqlVisitor();

    private ExprToExternalSqlVisitor() {
        // singleton
    }

    @Override
    public String visit(Expr expr, ToSqlParams context) {
        throw new UnsupportedOperationException("ExprToExternalSqlVisitor does not support Expr type: "
                + expr.getClass().getSimpleName());
    }

    // -----------------------------------------------------------------------
    // Literals (context-independent)
    // -----------------------------------------------------------------------

    @Override
    public String visitBoolLiteral(BoolLiteral expr, ToSqlParams context) {
        return expr.getValue() ? "TRUE" : "FALSE";
    }

    @Override
    public String visitStringLiteral(StringLiteral expr, ToSqlParams context) {
        return "'" + expr.getValue().replaceAll("'", "''") + "'";
    }

    @Override
    public String visitIntLiteral(IntLiteral expr, ToSqlParams context) {
        return expr.getStringValue();
    }

    @Override
    public String visitLargeIntLiteral(LargeIntLiteral expr, ToSqlParams context) {
        return expr.getStringValue();
    }

    @Override
    public String visitFloatLiteral(FloatLiteral expr, ToSqlParams context) {
        return expr.getStringValue();
    }

    @Override
    public String visitDecimalLiteral(DecimalLiteral expr, ToSqlParams context) {
        return expr.getStringValue();
    }

    @Override
    public String visitDateLiteral(DateLiteral expr, ToSqlParams context) {
        return "'" + expr.getStringValue() + "'";
    }

    @Override
    public String visitTimeV2Literal(TimeV2Literal expr, ToSqlParams context) {
        return "\"" + expr.getStringValue() + "\"";
    }

    @Override
    public String visitNullLiteral(NullLiteral expr, ToSqlParams context) {
        return expr.getStringValue();
    }

    @Override
    public String visitMaxLiteral(MaxLiteral expr, ToSqlParams context) {
        return "MAXVALUE";
    }

    @Override
    public String visitJsonLiteral(JsonLiteral expr, ToSqlParams context) {
        return "'" + expr.getValue().replaceAll("'", "''") + "'";
    }

    @Override
    public String visitIPv4Literal(IPv4Literal expr, ToSqlParams context) {
        return "\"" + expr.getStringValue() + "\"";
    }

    @Override
    public String visitIPv6Literal(IPv6Literal expr, ToSqlParams context) {
        return "\"" + expr.getStringValue() + "\"";
    }

    @Override
    public String visitVarBinaryLiteral(VarBinaryLiteral expr, ToSqlParams context) {
        return expr.toHexLiteral();
    }

    @Override
    public String visitArrayLiteral(ArrayLiteral expr, ToSqlParams context) {
        List<String> list = new ArrayList<>(expr.getChildren().size());
        expr.getChildren().forEach(v -> list.add(v.accept(this, context)));
        return "[" + StringUtils.join(list, ", ") + "]";
    }

    @Override
    public String visitMapLiteral(MapLiteral expr, ToSqlParams context) {
        List<String> list = new ArrayList<>(expr.getChildren().size());
        for (int i = 0; i < expr.getChildren().size() && i + 1 < expr.getChildren().size(); i += 2) {
            list.add(expr.getChild(i).accept(this, context) + ":" + expr.getChild(i + 1).accept(this, context));
        }
        return "MAP{" + StringUtils.join(list, ", ") + "}";
    }

    @Override
    public String visitStructLiteral(StructLiteral expr, ToSqlParams context) {
        List<String> list = new ArrayList<>(expr.getChildren().size());
        expr.getChildren().forEach(v -> list.add(v.accept(this, context)));
        return "STRUCT(" + StringUtils.join(list, ", ") + ")";
    }

    @Override
    public String visitPlaceHolderExpr(PlaceHolderExpr expr, ToSqlParams context) {
        if (expr.getLiteral() == null) {
            return "?";
        }
        return "_placeholder_(" + expr.getLiteral().accept(this, context) + ")";
    }

    // -----------------------------------------------------------------------
    // Reference / slot expressions
    // -----------------------------------------------------------------------

    @Override
    public String visitSlotRef(SlotRef expr, ToSqlParams context) {
        if (context.needExternalSql) {
            return expr.toExternalSqlImpl(context.tableType, context.table);
        }

        if (context.disableTableName && expr.label != null) {
            return expr.label;
        }

        StringBuilder sb = new StringBuilder();
        String subColumnPaths = "";
        if (expr.subColPath != null && !expr.subColPath.isEmpty()) {
            subColumnPaths = "." + String.join(".", expr.subColPath);
        }
        if (expr.tableNameInfo != null) {
            return expr.tableNameInfo.toSql() + "." + expr.label + subColumnPaths;
        } else if (expr.label != null) {
            if (ConnectContext.get() != null
                    && ConnectContext.get().getState().isNereids()
                    && !ConnectContext.get().getState().isQuery()
                    && ConnectContext.get().getSessionVariable() != null
                    && expr.desc != null) {
                return expr.label + "[#" + expr.desc.getId().asInt() + "]";
            } else {
                return expr.label;
            }
        } else if (expr.desc == null) {
            return "`" + expr.col + "`";
        } else if (expr.desc.getSourceExprs() != null) {
            if (!context.disableTableName && (ToSqlContext.get() == null || ToSqlContext.get().isNeedSlotRefId())) {
                if (expr.desc.getId().asInt() != 1) {
                    sb.append("<slot " + expr.desc.getId().asInt() + ">");
                }
            }
            for (Expr srcExpr : expr.desc.getSourceExprs()) {
                if (!context.disableTableName) {
                    sb.append(" ");
                }
                sb.append(context.disableTableName
                        ? srcExpr.accept(INSTANCE, ToSqlParams.WITHOUT_TABLE)
                        : srcExpr.accept(this, context));
            }
            return sb.toString();
        } else {
            return "<slot " + expr.desc.getId().asInt() + ">" + sb.toString();
        }
    }

    @Override
    public String visitColumnRefExpr(ColumnRefExpr expr, ToSqlParams context) {
        return expr.getName();
    }

    @Override
    public String visitInformationFunction(InformationFunction expr, ToSqlParams context) {
        return expr.getFuncType() + "()";
    }

    @Override
    public String visitEncryptKeyRef(EncryptKeyRef expr, ToSqlParams context) {
        return expr.encryptKeyName.toSql();
    }

    @Override
    public String visitVariableExpr(VariableExpr expr, ToSqlParams context) {
        StringBuilder sb = new StringBuilder();
        if (expr.getSetType() == SetType.USER) {
            sb.append("@");
        } else {
            sb.append("@@");
            if (expr.getSetType() == SetType.GLOBAL) {
                sb.append("GLOBAL.");
            }
        }
        sb.append(expr.getName());
        return sb.toString();
    }

    // -----------------------------------------------------------------------
    // Predicates
    // -----------------------------------------------------------------------

    @Override
    public String visitBinaryPredicate(BinaryPredicate expr, ToSqlParams context) {
        return "(" + expr.getChild(0).accept(this, context)
                + " " + expr.getOp().toString()
                + " " + expr.getChild(1).accept(this, context) + ")";
    }

    @Override
    public String visitIsNullPredicate(IsNullPredicate expr, ToSqlParams context) {
        return expr.getChild(0).accept(this, context) + (expr.isNotNull ? " IS NOT NULL" : " IS NULL");
    }

    @Override
    public String visitCompoundPredicate(CompoundPredicate expr, ToSqlParams context) {
        if (expr.getChildren().size() == 1) {
            Preconditions.checkState(expr.getOp() == CompoundPredicate.Operator.NOT);
            return "(NOT " + expr.getChild(0).accept(this, context) + ")";
        } else {
            return "(" + expr.getChild(0).accept(this, context)
                    + " " + expr.getOp().toString()
                    + " " + expr.getChild(1).accept(this, context) + ")";
        }
    }

    @Override
    public String visitInPredicate(InPredicate expr, ToSqlParams context) {
        StringBuilder strBuilder = new StringBuilder();
        String notStr = expr.isNotIn ? "NOT " : "";
        strBuilder.append(expr.getChild(0).accept(this, context) + " " + notStr + "IN (");
        for (int i = 1; i < expr.getChildren().size(); ++i) {
            strBuilder.append(expr.getChild(i).accept(this, context));
            strBuilder.append((i + 1 != expr.getChildren().size()) ? ", " : "");
        }
        strBuilder.append(")");
        return strBuilder.toString();
    }

    @Override
    public String visitLikePredicate(LikePredicate expr, ToSqlParams context) {
        return expr.getChild(0).accept(this, context)
                + " " + expr.op.toString()
                + " " + expr.getChild(1).accept(this, context);
    }

    @Override
    public String visitMatchPredicate(MatchPredicate expr, ToSqlParams context) {
        return expr.getChild(0).accept(this, context)
                + " " + expr.op.toString()
                + " " + expr.getChild(1).accept(this, context)
                + expr.analyzerSqlFragment();
    }

    @Override
    public String visitBetweenPredicate(BetweenPredicate expr, ToSqlParams context) {
        String notStr = expr.isNotBetween ? "NOT " : "";
        return expr.getChild(0).accept(this, context) + " " + notStr + "BETWEEN "
                + expr.getChild(1).accept(this, context) + " AND " + expr.getChild(2).accept(this, context);
    }

    @Override
    public String visitSearchPredicate(SearchPredicate expr, ToSqlParams context) {
        return expr.buildSqlForExplain();
    }

    // -----------------------------------------------------------------------
    // Arithmetic / cast
    // -----------------------------------------------------------------------

    @Override
    public String visitArithmeticExpr(ArithmeticExpr expr, ToSqlParams context) {
        if (expr.getChildren().size() == 1) {
            return expr.op.toString() + " " + expr.getChild(0).accept(this, context);
        } else {
            return "(" + expr.getChild(0).accept(this, context)
                    + " " + expr.op.toString()
                    + " " + expr.getChild(1).accept(this, context) + ")";
        }
    }

    @Override
    public String visitCastExpr(CastExpr expr, ToSqlParams context) {
        if (context.needExternalSql) {
            return expr.getChild(0).accept(this, context);
        }
        return "CAST(" + expr.getChild(0).accept(this, context) + " AS " + expr.getType().toSql() + ")";
    }

    @Override
    public String visitTryCastExpr(TryCastExpr expr, ToSqlParams context) {
        if (context.needExternalSql) {
            return expr.getChild(0).accept(this, context);
        }
        return "TRY_CAST(" + expr.getChild(0).accept(this, context) + " AS " + expr.getType().toSql() + ")";
    }

    @Override
    public String visitTimestampArithmeticExpr(TimestampArithmeticExpr expr, ToSqlParams context) {
        StringBuilder strBuilder = new StringBuilder();
        if (expr.funcName != null) {
            if (expr.funcName.equalsIgnoreCase("TIMESTAMPDIFF") || expr.funcName.equalsIgnoreCase("TIMESTAMPADD")) {
                strBuilder.append(expr.funcName).append("(");
                strBuilder.append(expr.timeUnitIdent).append(", ");
                strBuilder.append(expr.getChild(1).accept(this, context)).append(", ");
                strBuilder.append(expr.getChild(0).accept(this, context)).append(")");
                return strBuilder.toString();
            }
            strBuilder.append(expr.funcName).append("(");
            strBuilder.append(expr.getChild(0).accept(this, context)).append(", ");
            strBuilder.append("INTERVAL ");
            strBuilder.append(expr.getChild(1).accept(this, context));
            strBuilder.append(" ").append(expr.timeUnitIdent);
            strBuilder.append(")");
            return strBuilder.toString();
        }
        if (expr.intervalFirst) {
            strBuilder.append("INTERVAL ");
            strBuilder.append(expr.getChild(1).accept(this, context) + " ");
            strBuilder.append(expr.timeUnitIdent);
            strBuilder.append(" ").append(expr.op.toString()).append(" ");
            strBuilder.append(expr.getChild(0).accept(this, context));
        } else {
            strBuilder.append(expr.getChild(0).accept(this, context));
            strBuilder.append(" " + expr.op.toString() + " ");
            strBuilder.append("INTERVAL ");
            strBuilder.append(expr.getChild(1).accept(this, context) + " ");
            strBuilder.append(expr.timeUnitIdent);
        }
        return strBuilder.toString();
    }

    // -----------------------------------------------------------------------
    // Functions / lambda / case
    // -----------------------------------------------------------------------

    @Override
    public String visitFunctionCallExpr(FunctionCallExpr expr, ToSqlParams context) {
        FunctionCallExpr resolvedExpr = expr.originStmtFnExpr != null
                ? (FunctionCallExpr) expr.originStmtFnExpr : expr;
        StringBuilder sb = new StringBuilder();

        if (expr.getFnName().getFunction().equalsIgnoreCase("like")
                || expr.getFnName().getFunction().equalsIgnoreCase("regexp")) {
            sb.append(expr.getChild(0).accept(this, context));
            sb.append(" ");
            sb.append(resolvedExpr.getFnName());
            sb.append(" ");
            sb.append(expr.getChild(1).accept(this, context));
        } else if (expr.getFnName().getFunction().equalsIgnoreCase("encryptkeyref")) {
            sb.append("key ");
            for (int i = 0; i < expr.getChildren().size(); i++) {
                String str = ((StringLiteral) expr.getChild(i)).getValue();
                if (str.isEmpty()) {
                    continue;
                }
                sb.append(str);
                sb.append(".");
            }
            sb.deleteCharAt(sb.length() - 1);
        } else {
            sb.append(resolvedExpr.getFnName());
            sb.append(expr.paramsToSql(context.disableTableName, context.needExternalSql,
                    context.tableType, context.table));
            if (expr.getFnName().getFunction().equalsIgnoreCase("json_quote")
                    || expr.getFnName().getFunction().equalsIgnoreCase("json_array")
                    || expr.getFnName().getFunction().equalsIgnoreCase("json_object")
                    || expr.getFnName().getFunction().equalsIgnoreCase("json_insert")
                    || expr.getFnName().getFunction().equalsIgnoreCase("json_replace")
                    || expr.getFnName().getFunction().equalsIgnoreCase("json_set")) {
                return expr.forJSON(sb.toString());
            }
        }
        return sb.toString();
    }

    @Override
    public String visitLambdaFunctionCallExpr(LambdaFunctionCallExpr expr, ToSqlParams context) {
        StringBuilder sb = new StringBuilder();
        String fnName = expr.getFnName().getFunction();
        if (expr.fn != null) {
            fnName = expr.fn.getFunctionName().getFunction();
        }
        sb.append(fnName);
        sb.append("(");
        int childSize = expr.getChildren().size();
        Expr lastExpr = expr.getChild(childSize - 1);
        boolean lastIsLambdaExpr = (lastExpr instanceof LambdaFunctionExpr);
        if (lastIsLambdaExpr) {
            sb.append(lastExpr.accept(this, context));
            sb.append(", ");
        }
        for (int i = 0; i < childSize - 1; ++i) {
            sb.append(expr.getChild(i).accept(this, context));
            if (i != childSize - 2) {
                sb.append(", ");
            }
        }
        if (!lastIsLambdaExpr) {
            if (childSize > 1) {
                sb.append(", ");
            }
            sb.append(lastExpr.accept(this, context));
        }
        sb.append(")");
        return sb.toString();
    }

    @Override
    public String visitLambdaFunctionExpr(LambdaFunctionExpr expr, ToSqlParams context) {
        String nameStr = "";
        Expr lambdaExpr = expr.slotExprs.get(0);
        int exprSize = expr.names.size();
        for (int i = 0; i < exprSize; ++i) {
            nameStr = nameStr + expr.names.get(i);
            if (i != exprSize - 1) {
                nameStr = nameStr + ",";
            }
        }
        if (exprSize > 1) {
            nameStr = "(" + nameStr + ")";
        }
        return String.format("%s -> %s", nameStr, lambdaExpr.accept(this, context));
    }

    @Override
    public String visitCaseExpr(CaseExpr expr, ToSqlParams context) {
        StringBuilder output = new StringBuilder("CASE");
        int childIdx = 0;
        if (expr.hasCaseExpr) {
            output.append(' ').append(expr.getChild(childIdx++).accept(this, context));
        }
        while (childIdx + 2 <= expr.getChildren().size()) {
            output.append(" WHEN " + expr.getChild(childIdx++).accept(this, context));
            output.append(" THEN " + expr.getChild(childIdx++).accept(this, context));
        }
        if (expr.hasElseExpr) {
            output.append(" ELSE " + expr.getChild(expr.getChildren().size() - 1).accept(this, context));
        }
        output.append(" END");
        return output.toString();
    }
}