Search in sources :

Example 1 with CalciteRowMetaData

use of io.mycat.calcite.resultset.CalciteRowMetaData in project Mycat2 by MyCATApache.

the class PlanImpl method getMetaData.

@Override
public MycatRowMetaData getMetaData() {
    MycatRel mycatRel = (MycatRel) relNode;
    List<RelDataTypeField> fieldList = mycatRel.getRowType().getFieldList();
    return new CalciteRowMetaData(fieldList, aliasList);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) MycatRel(io.mycat.calcite.MycatRel) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData)

Example 2 with CalciteRowMetaData

use of io.mycat.calcite.resultset.CalciteRowMetaData in project Mycat2 by MyCATApache.

the class MycatSQLTableLookup method dispatchRightObservable.

public static Observable<Object[]> dispatchRightObservable(NewMycatDataContext context, MycatSQLTableLookup tableLookup, Observable<Object[]> leftInput) {
    MycatView rightView = (MycatView) tableLookup.getRight();
    CopyMycatRowMetaData rightRowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(rightView.getRowType().getFieldList()));
    AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = (AsyncMycatDataContextImpl.SqlMycatDataContextImpl) context;
    Observable<@NonNull List<Object[]>> buffer;
    if (tableLookup.getType() == Type.BACK) {
        // semi 可以分解
        buffer = leftInput.buffer(300, 50);
    } else {
        // NONE 该运算不能分解
        buffer = leftInput.toList().toObservable();
    }
    Observable<Object[]> rightObservable = buffer.flatMap(argsList -> {
        if (argsList.isEmpty()) {
            return Observable.empty();
        }
        RexShuttle rexShuttle = argSolver(argsList);
        RelNode mycatInnerRelNode = rightView.getRelNode().accept(new RelShuttleImpl() {

            @Override
            public RelNode visit(RelNode other) {
                if (other instanceof Filter) {
                    Filter filter = (Filter) other;
                    return filter.accept(rexShuttle);
                } else if (other instanceof MycatTableLookupValues) {
                    MycatTableLookupValues mycatTableLookupValues = (MycatTableLookupValues) other;
                    return mycatTableLookupValues.apply(argsList);
                }
                return super.visit(other);
            }
        });
        MycatView newRightView = new MycatView(rightView.getTraitSet(), mycatInnerRelNode, rightView.getDistribution(), rightView.getCondition().map(c -> c.accept(rexShuttle)).orElse(null));
        DrdsSqlWithParams drdsSql = context.getDrdsSql();
        SqlNode sqlTemplate = newRightView.getSQLTemplate(DrdsSqlWithParams.isForUpdate(drdsSql.getParameterizedSQL()));
        ImmutableMultimap<String, SqlString> apply1 = newRightView.apply(context.getContext().getMergeUnionSize(), sqlTemplate, sqlMycatDataContext.getSqlMap(Collections.emptyMap(), newRightView, drdsSql, drdsSql.getHintDataNodeFilter()), drdsSql.getParams());
        return Observable.merge(sqlMycatDataContext.getObservables(apply1, rightRowMetaData, (sessionConnection, sql, extractParams, calciteRowMetaData) -> VertxExecuter.runQuery(sessionConnection, sql, extractParams, calciteRowMetaData)));
    });
    return rightObservable;
}
Also used : VertxExecuter(io.mycat.vertx.VertxExecuter) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) Getter(lombok.Getter) NewMycatDataContext(org.apache.calcite.runtime.NewMycatDataContext) SqlShuttle(org.apache.calcite.sql.util.SqlShuttle) RelOptCost(org.apache.calcite.plan.RelOptCost) NonNull(io.reactivex.rxjava3.annotations.NonNull) Filter(org.apache.calcite.rel.core.Filter) RelMdUtil(org.apache.calcite.rel.metadata.RelMdUtil) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) RexUtil(org.apache.calcite.rex.RexUtil) CorrelationId(org.apache.calcite.rel.core.CorrelationId) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) ImmutableList(com.google.common.collect.ImmutableList) RexNode(org.apache.calcite.rex.RexNode) Observable(io.reactivex.rxjava3.core.Observable) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) NewMycatConnection(io.mycat.newquery.NewMycatConnection) io.mycat.calcite(io.mycat.calcite) org.apache.calcite.adapter.enumerable(org.apache.calcite.adapter.enumerable) MycatView(io.mycat.calcite.logical.MycatView) Future(io.vertx.core.Future) JoinInfo(org.apache.calcite.rel.core.JoinInfo) Collectors(java.util.stream.Collectors) org.apache.calcite.sql(org.apache.calcite.sql) List(java.util.List) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) MYCAT_SQL_LOOKUP_IN(io.mycat.calcite.MycatImplementor.MYCAT_SQL_LOOKUP_IN) org.apache.calcite.linq4j.tree(org.apache.calcite.linq4j.tree) JoinRelType(org.apache.calcite.rel.core.JoinRelType) org.apache.calcite.rel(org.apache.calcite.rel) SqlString(org.apache.calcite.sql.util.SqlString) RexShuttle(org.apache.calcite.rex.RexShuttle) Util(org.apache.calcite.util.Util) NotNull(org.jetbrains.annotations.NotNull) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Collections(java.util.Collections) RexCall(org.apache.calcite.rex.RexCall) RexShuttle(org.apache.calcite.rex.RexShuttle) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) SqlString(org.apache.calcite.sql.util.SqlString) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) SqlString(org.apache.calcite.sql.util.SqlString) MycatView(io.mycat.calcite.logical.MycatView) Filter(org.apache.calcite.rel.core.Filter) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with CalciteRowMetaData

use of io.mycat.calcite.resultset.CalciteRowMetaData in project Mycat2 by MyCATApache.

the class MycatViewPlan method execute.

@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
    RootAllocator rootAllocator = new RootAllocator();
    NewMycatDataContext context = (NewMycatDataContext) rootContext.getContext();
    DrdsSqlWithParams drdsSql = context.getDrdsSql();
    MycatView view = (MycatView) mycatView;
    CopyMycatRowMetaData rowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(view.getRowType().getFieldList()));
    MycatRelDatasourceSourceInfo mycatRelDatasourceSourceInfo = new MycatRelDatasourceSourceInfo(rowMetaData, view.getSQLTemplate(DrdsSql.isForUpdate(drdsSql.getParameterizedStatement())), view);
    SqlNode sqlTemplate = mycatRelDatasourceSourceInfo.getSqlTemplate();
    List<PartitionGroup> partitionGroups = AsyncMycatDataContextImpl.getSqlMap(Collections.emptyMap(), view, drdsSql, drdsSql.getHintDataNodeFilter());
    ImmutableMultimap<String, SqlString> stringSqlStringImmutableMultimap = view.apply(-1, sqlTemplate, partitionGroups, drdsSql.getParams());
    List<Observable<VectorSchemaRoot>> observableList = new ArrayList<>();
    for (Map.Entry<String, SqlString> entry : stringSqlStringImmutableMultimap.entries()) {
        String key = entry.getKey();
        SqlString sqlString = entry.getValue();
        observableList.add(Observable.create(emitter -> {
            Future<NewMycatConnection> connectionFuture = context.getConnection(context.getContext().resolveDatasourceTargetName(key));
            Future<Observable<VectorSchemaRoot>> observableFuture = connectionFuture.map(connection -> {
                Observable<VectorSchemaRoot> observable = connection.prepareQuery(sqlString.getSql(), MycatPreparedStatementUtil.extractParams(drdsSql.getParams(), sqlString.getDynamicParameters()), MycatRelDataType.getMycatRelType(MycatViewPlan.this.schema()), rootAllocator);
                return observable.doOnComplete(() -> context.recycleConnection(context.getContext().resolveDatasourceTargetName(key), Future.succeededFuture(connection)));
            });
            observableFuture.onFailure(event -> emitter.tryOnError(event));
            observableFuture.onSuccess(event -> {
                event = event.doOnComplete(() -> emitter.onComplete());
                event = event.doOnError(throwable -> emitter.tryOnError(throwable));
                event.forEach(vectorSchemaRoot -> emitter.onNext(vectorSchemaRoot));
            });
        }));
    }
    return Observable.fromIterable(observableList).flatMap(i -> i);
}
Also used : PartitionGroup(io.mycat.PartitionGroup) Schema(org.apache.arrow.vector.types.pojo.Schema) PartitionGroup(io.mycat.PartitionGroup) PhysicalSortProperty(io.ordinate.engine.builder.PhysicalSortProperty) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) CalciteCompiler(io.ordinate.engine.builder.CalciteCompiler) ObservableOnSubscribe(io.reactivex.rxjava3.core.ObservableOnSubscribe) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) RootAllocator(org.apache.arrow.memory.RootAllocator) Map(java.util.Map) BufferAllocator(org.apache.arrow.memory.BufferAllocator) MycatMergeSort(io.mycat.calcite.physical.MycatMergeSort) SqlKind(org.apache.calcite.sql.SqlKind) NewMycatConnection(io.mycat.newquery.NewMycatConnection) MycatView(io.mycat.calcite.logical.MycatView) RexLiteral(org.apache.calcite.rex.RexLiteral) Field(org.apache.arrow.vector.types.pojo.Field) FactoryUtil(io.ordinate.engine.factory.FactoryUtil) RootContext(io.ordinate.engine.record.RootContext) Future(io.vertx.core.Future) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Collectors(java.util.stream.Collectors) Action(io.reactivex.rxjava3.functions.Action) List(java.util.List) RelCollation(org.apache.calcite.rel.RelCollation) Sort(org.apache.calcite.rel.core.Sort) MycatPreparedStatementUtil(io.mycat.calcite.executor.MycatPreparedStatementUtil) SqlString(org.apache.calcite.sql.util.SqlString) RexCall(org.apache.calcite.rex.RexCall) VertxExecuter(io.mycat.vertx.VertxExecuter) Distribution(io.mycat.calcite.rewriter.Distribution) NewMycatDataContext(org.apache.calcite.runtime.NewMycatDataContext) NonNull(io.reactivex.rxjava3.annotations.NonNull) ArrayList(java.util.ArrayList) Observable(io.reactivex.rxjava3.core.Observable) IntFunction(io.ordinate.engine.function.IntFunction) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) MycatRelDatasourceSourceInfo(io.mycat.calcite.MycatRelDatasourceSourceInfo) Consumer(io.reactivex.rxjava3.functions.Consumer) ObservableEmitter(io.reactivex.rxjava3.core.ObservableEmitter) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) DrdsSql(io.mycat.DrdsSql) Function(io.reactivex.rxjava3.functions.Function) Handler(io.vertx.core.Handler) Collections(java.util.Collections) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) ArrayList(java.util.ArrayList) SqlString(org.apache.calcite.sql.util.SqlString) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) Observable(io.reactivex.rxjava3.core.Observable) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) SqlString(org.apache.calcite.sql.util.SqlString) MycatView(io.mycat.calcite.logical.MycatView) RootAllocator(org.apache.arrow.memory.RootAllocator) MycatRelDatasourceSourceInfo(io.mycat.calcite.MycatRelDatasourceSourceInfo) NewMycatDataContext(org.apache.calcite.runtime.NewMycatDataContext) Future(io.vertx.core.Future) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) Map(java.util.Map) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with CalciteRowMetaData

use of io.mycat.calcite.resultset.CalciteRowMetaData 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

CalciteRowMetaData (io.mycat.calcite.resultset.CalciteRowMetaData)4 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)3 MycatView (io.mycat.calcite.logical.MycatView)3 Future (io.vertx.core.Future)3 RexNode (org.apache.calcite.rex.RexNode)3 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 AsyncMycatDataContextImpl (io.mycat.AsyncMycatDataContextImpl)2 DrdsSqlWithParams (io.mycat.DrdsSqlWithParams)2 io.mycat.calcite (io.mycat.calcite)2 MycatMergeSort (io.mycat.calcite.physical.MycatMergeSort)2 NewMycatConnection (io.mycat.newquery.NewMycatConnection)2 VertxExecuter (io.mycat.vertx.VertxExecuter)2 NonNull (io.reactivex.rxjava3.annotations.NonNull)2 Observable (io.reactivex.rxjava3.core.Observable)2 SqlNode (org.apache.calcite.sql.SqlNode)2 SqlString (org.apache.calcite.sql.util.SqlString)2 ImmutableList (com.google.common.collect.ImmutableList)1 DrdsSql (io.mycat.DrdsSql)1 PartitionGroup (io.mycat.PartitionGroup)1 MycatRelDataType (io.mycat.beans.mycat.MycatRelDataType)1