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.catalog.JdbcTable;
import org.apache.doris.catalog.OdbcTable;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.util.ToSqlContext;
import org.apache.doris.qe.ConnectContext;

/**
 * Visitor that produces the external SQL string for any {@link Expr}.
 * Extends {@link ExprToSqlVisitor} and overrides only the methods that differ
 * for external SQL generation.
 *
 * <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 ExprToSqlVisitor {

    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());
    }

    @Override
    public String visitSlotRef(SlotRef expr, ToSqlParams context) {
        if (context.needExternalSql) {
            if (expr.getCol() != null) {
                if (context.tableType.equals(TableIf.TableType.JDBC_EXTERNAL_TABLE)
                        || context.tableType.equals(TableIf.TableType.JDBC)
                        || context.tableType.equals(TableIf.TableType.ODBC)) {
                    if (context.table instanceof JdbcTable) {
                        return ((JdbcTable) context.table).getProperRemoteColumnName(
                                ((JdbcTable) context.table).getJdbcTableType(), expr.getCol());
                    } else if (context.table instanceof OdbcTable) {
                        return JdbcTable.databaseProperName(
                                ((OdbcTable) context.table).getOdbcTableType(), expr.getCol());
                    } else {
                        return expr.getCol();
                    }
                } else {
                    return expr.getCol();
                }
            } else {
                return "<slot " + expr.desc.getId().asInt() + ">";
            }
        }

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

        StringBuilder sb = new StringBuilder();
        String subColumnPaths = "";
        if (expr.getSubColPath() != null && !expr.getSubColPath().isEmpty()) {
            subColumnPaths = "." + String.join(".", expr.getSubColPath());
        }
        if (expr.getTableNameInfo() != null) {
            return expr.getTableNameInfo().toSql() + "." + expr.getLabel() + subColumnPaths;
        } else if (expr.getLabel() != null) {
            if (ConnectContext.get() != null
                    && ConnectContext.get().getState().isNereids()
                    && !ConnectContext.get().getState().isQuery()
                    && ConnectContext.get().getSessionVariable() != null
                    && expr.desc != null) {
                return expr.getLabel() + "[#" + expr.desc.getId().asInt() + "]";
            } else {
                return expr.getLabel();
            }
        } else if (expr.desc == null) {
            return "`" + expr.getCol() + "`";
        } else if (expr.desc.getSourceExprs() != null) {
            if (!context.disableTableName && (ToSqlContext.get() == null || ToSqlContext.get().isNeedSlotRefId())) {
                if (expr.desc.getId().asInt() != 1) {
                    sb.append("<slot ").append(expr.desc.getId().asInt()).append(">");
                }
            }
            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;
        }
    }

    @Override
    public String visitCastExpr(CastExpr expr, ToSqlParams context) {
        if (context.needExternalSql) {
            return expr.getChild(0).accept(this, context);
        }
        return super.visitCastExpr(expr, context);
    }

    @Override
    public String visitTryCastExpr(TryCastExpr expr, ToSqlParams context) {
        if (context.needExternalSql) {
            return expr.getChild(0).accept(this, context);
        }
        return super.visitTryCastExpr(expr, context);
    }
}