Search in sources :

Example 6 with MycatSQLTableLookup

use of io.mycat.calcite.physical.MycatSQLTableLookup 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)6 MycatSQLTableLookup (io.mycat.calcite.physical.MycatSQLTableLookup)6 RelNode (org.apache.calcite.rel.RelNode)6 RelOptCluster (org.apache.calcite.plan.RelOptCluster)4 Join (org.apache.calcite.rel.core.Join)4 JoinRelType (org.apache.calcite.rel.core.JoinRelType)3 RexNode (org.apache.calcite.rex.RexNode)3 RelBuilder (org.apache.calcite.tools.RelBuilder)3 MycatTransientSQLTableScan (io.mycat.calcite.table.MycatTransientSQLTableScan)2 ArrayList (java.util.ArrayList)2 RelDataType (org.apache.calcite.rel.type.RelDataType)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