Search in sources :

Example 1 with DrdsSql

use of io.mycat.DrdsSql in project Mycat2 by MyCATApache.

the class QueryPlanner method innerComputeMinCostCodeExecuterContext.

public CodeExecuterContext innerComputeMinCostCodeExecuterContext(DrdsSql sqlSelectStatement) {
    RelOptCluster relOptCluster = DrdsSqlCompiler.newCluster();
    List<CodeExecuterContext> codeExecuterContexts = getAcceptedMycatRelList(sqlSelectStatement);
    int size = codeExecuterContexts.size();
    switch(size) {
        case 0:
            {
                throw new IllegalArgumentException();
            }
        case 1:
            {
                return codeExecuterContexts.get(0);
            }
        default:
            class SortObject implements Comparable<SortObject> {

                final CodeExecuterContext context;

                final RelOptCost cost;

                public SortObject(CodeExecuterContext context, RelOptCost cost) {
                    this.context = context;
                    this.cost = cost;
                }

                @Override
                public int compareTo(@NotNull SortObject o) {
                    return (this.cost.isLt(o.cost) ? 0 : 1);
                }
            }
            return codeExecuterContexts.stream().filter(i -> i != null).map(i -> new SortObject(i, i.getMycatRel().computeSelfCost(relOptCluster.getPlanner(), getRelMetadataQuery()))).min(SortObject::compareTo).map(i -> i.context).orElse(null);
    }
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelOptCluster(org.apache.calcite.plan.RelOptCluster) List(java.util.List) DrdsSql(io.mycat.DrdsSql) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) RelOptCost(org.apache.calcite.plan.RelOptCost) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) DrdsSqlCompiler(io.mycat.DrdsSqlCompiler) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) RelOptCost(org.apache.calcite.plan.RelOptCost) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DrdsSql

use of io.mycat.DrdsSql 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)

Aggregations

DrdsSql (io.mycat.DrdsSql)2 Collections (java.util.Collections)2 List (java.util.List)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 AsyncMycatDataContextImpl (io.mycat.AsyncMycatDataContextImpl)1 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)1 DrdsSqlWithParams (io.mycat.DrdsSqlWithParams)1 PartitionGroup (io.mycat.PartitionGroup)1 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)1 MycatRelDataType (io.mycat.beans.mycat.MycatRelDataType)1 CodeExecuterContext (io.mycat.calcite.CodeExecuterContext)1 MycatRelDatasourceSourceInfo (io.mycat.calcite.MycatRelDatasourceSourceInfo)1 MycatPreparedStatementUtil (io.mycat.calcite.executor.MycatPreparedStatementUtil)1 MycatView (io.mycat.calcite.logical.MycatView)1 MycatMergeSort (io.mycat.calcite.physical.MycatMergeSort)1 CalciteRowMetaData (io.mycat.calcite.resultset.CalciteRowMetaData)1 Distribution (io.mycat.calcite.rewriter.Distribution)1 NewMycatConnection (io.mycat.newquery.NewMycatConnection)1 VertxExecuter (io.mycat.vertx.VertxExecuter)1 CalciteCompiler (io.ordinate.engine.builder.CalciteCompiler)1