Search in sources :

Example 1 with MycatTransientSQLTableScan

use of io.mycat.calcite.table.MycatTransientSQLTableScan in project Mycat2 by MyCATApache.

the class PlanImpl method specificSql.

@NotNull
public List<SpecificSql> specificSql(DrdsSqlWithParams drdsSql, int mergeUnionSize) {
    List<SpecificSql> res = new ArrayList<>();
    getMycatRel().accept(new RelShuttleImpl() {

        @Override
        protected RelNode visitChildren(RelNode relNode) {
            List<Each> sqls = new ArrayList<>();
            String parameterizedSql = "";
            if (relNode instanceof MycatView) {
                MycatView view = (MycatView) relNode;
                SqlNode sqlTemplate = view.getSQLTemplate(false);
                ImmutableMultimap<String, SqlString> apply = view.apply(mergeUnionSize, sqlTemplate, AsyncMycatDataContextImpl.getSqlMap(executerContext.getConstantMap(), view, drdsSql, drdsSql.getHintDataNodeFilter()), drdsSql.getParams());
                ImmutableMultimap<String, SqlString> stringImmutableMultimap = apply;
                for (Map.Entry<String, SqlString> entry : (stringImmutableMultimap.entries())) {
                    SqlString sqlString = new SqlString(entry.getValue().getDialect(), (Util.toLinux(entry.getValue().getSql())), entry.getValue().getDynamicParameters());
                    sqls.add(new Each(entry.getKey(), sqlString.getSql()));
                }
                if (relNode instanceof MycatView) {
                    parameterizedSql = ((MycatView) relNode).getSql();
                }
                if (relNode instanceof MycatTransientSQLTableScan) {
                    parameterizedSql = ((MycatTransientSQLTableScan) relNode).getSql();
                }
                res.add(new SpecificSql(relNode.getDigest(), parameterizedSql, sqls));
            } else if (relNode instanceof MycatSQLTableLookup) {
                MycatView right = ((MycatSQLTableLookup) relNode).getRight();
                right.accept(this);
            }
            return super.visitChildren(relNode);
        }
    });
    return res;
}
Also used : MycatTransientSQLTableScan(io.mycat.calcite.table.MycatTransientSQLTableScan) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString) MycatSQLTableLookup(io.mycat.calcite.physical.MycatSQLTableLookup) MycatView(io.mycat.calcite.logical.MycatView) RelNode(org.apache.calcite.rel.RelNode) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) SqlNode(org.apache.calcite.sql.SqlNode) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with MycatTransientSQLTableScan

use of io.mycat.calcite.table.MycatTransientSQLTableScan in project Mycat2 by MyCATApache.

the class DrdsExecutorCompiler method getCodeExecuterContext.

@NotNull
@SneakyThrows
public static CodeExecuterContext getCodeExecuterContext(Map<RexNode, RexNode> constantMap, MycatRel relNode, boolean forUpdate) {
    HashMap<String, Object> varContext = new HashMap<>(2);
    StreamMycatEnumerableRelImplementor mycatEnumerableRelImplementor = new StreamMycatEnumerableRelImplementor(varContext);
    HashMap<String, MycatRelDatasourceSourceInfo> stat = new HashMap<>();
    relNode.accept(new RelShuttleImpl() {

        @Override
        public RelNode visit(RelNode other) {
            CopyMycatRowMetaData rowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(other.getRowType().getFieldList()));
            if (other instanceof MycatView) {
                MycatView view = (MycatView) other;
                MycatRelDatasourceSourceInfo rel = stat.computeIfAbsent(other.getDigest(), s -> {
                    return new MycatRelDatasourceSourceInfo(rowMetaData, view.getSQLTemplate(forUpdate), view);
                });
                rel.refCount += 1;
            }
            if (other instanceof MycatTransientSQLTableScan) {
                MycatTransientSQLTableScan tableScan = (MycatTransientSQLTableScan) other;
                MycatRelDatasourceSourceInfo rel = stat.computeIfAbsent(other.getDigest(), s -> {
                    return new MycatRelDatasourceSourceInfo(rowMetaData, new TextSqlNode(tableScan.getSql()), tableScan);
                });
                rel.refCount += 1;
            }
            if (other instanceof MycatSQLTableLookup) {
                MycatView right = ((MycatSQLTableLookup) other).getRight();
                right.accept(this);
            }
            return super.visit(other);
        }
    });
    ClassDeclaration classDeclaration = mycatEnumerableRelImplementor.implementHybridRoot(relNode, EnumerableRel.Prefer.ARRAY);
    String code = Expressions.toString(classDeclaration.memberDeclarations, "\n", false);
    if (log.isDebugEnabled()) {
        log.debug("----------------------------------------code----------------------------------------");
        log.debug(code);
    }
    CodeContext codeContext = new CodeContext(classDeclaration.name, code);
    CodeExecuterContext executerContext = CodeExecuterContext.of(constantMap, stat, varContext, relNode, codeContext);
    return executerContext;
}
Also used : java.util(java.util) SneakyThrows(lombok.SneakyThrows) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) LoggerFactory(org.slf4j.LoggerFactory) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) BigDecimal(java.math.BigDecimal) SqlNode(org.apache.calcite.sql.SqlNode) MycatTransientSQLTableScan(io.mycat.calcite.table.MycatTransientSQLTableScan) RexNode(org.apache.calcite.rex.RexNode) Duration(java.time.Duration) PlanImplementor(io.mycat.calcite.plan.PlanImplementor) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) EnumerableRel(org.apache.calcite.adapter.enumerable.EnumerableRel) MycatMergeSort(io.mycat.calcite.physical.MycatMergeSort) PlanImpl(io.mycat.calcite.spm.PlanImpl) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) Logger(org.slf4j.Logger) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) io.mycat.calcite(io.mycat.calcite) ByteString(org.apache.calcite.avatica.util.ByteString) MySQLType(io.mycat.beans.mysql.MySQLType) MycatView(io.mycat.calcite.logical.MycatView) Timestamp(java.sql.Timestamp) Expressions(org.apache.calcite.linq4j.tree.Expressions) MycatViewDataNodeMapping(io.mycat.calcite.logical.MycatViewDataNodeMapping) RelNode(org.apache.calcite.rel.RelNode) VertxUtil(io.mycat.util.VertxUtil) Future(io.vertx.core.Future) MycatSQLTableLookup(io.mycat.calcite.physical.MycatSQLTableLookup) MycatUpdateRel(io.mycat.calcite.physical.MycatUpdateRel) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel) LocalDate(java.time.LocalDate) ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) SqlString(org.apache.calcite.sql.util.SqlString) NotNull(org.jetbrains.annotations.NotNull) Plan(io.mycat.calcite.spm.Plan) MycatTransientSQLTableScan(io.mycat.calcite.table.MycatTransientSQLTableScan) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) ByteString(org.apache.calcite.avatica.util.ByteString) SqlString(org.apache.calcite.sql.util.SqlString) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) MycatSQLTableLookup(io.mycat.calcite.physical.MycatSQLTableLookup) MycatView(io.mycat.calcite.logical.MycatView) ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) RelNode(org.apache.calcite.rel.RelNode) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) SneakyThrows(lombok.SneakyThrows) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MycatView (io.mycat.calcite.logical.MycatView)2 MycatSQLTableLookup (io.mycat.calcite.physical.MycatSQLTableLookup)2 MycatTransientSQLTableScan (io.mycat.calcite.table.MycatTransientSQLTableScan)2 RelNode (org.apache.calcite.rel.RelNode)2 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)2 SqlNode (org.apache.calcite.sql.SqlNode)2 SqlString (org.apache.calcite.sql.util.SqlString)2 NotNull (org.jetbrains.annotations.NotNull)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)1 MySQLErrorCode (io.mycat.beans.mysql.MySQLErrorCode)1 MySQLType (io.mycat.beans.mysql.MySQLType)1 io.mycat.calcite (io.mycat.calcite)1 MycatViewDataNodeMapping (io.mycat.calcite.logical.MycatViewDataNodeMapping)1 MycatInsertRel (io.mycat.calcite.physical.MycatInsertRel)1 MycatMergeSort (io.mycat.calcite.physical.MycatMergeSort)1 MycatUpdateRel (io.mycat.calcite.physical.MycatUpdateRel)1 PlanImplementor (io.mycat.calcite.plan.PlanImplementor)1 CalciteRowMetaData (io.mycat.calcite.resultset.CalciteRowMetaData)1 Plan (io.mycat.calcite.spm.Plan)1