Search in sources :

Example 1 with RootContext

use of io.ordinate.engine.record.RootContext in project Mycat2 by MyCATApache.

the class CsvScanTest method baseTest.

// @Test
@SneakyThrows
public void baseTest() {
    Path path = Paths.get("D:\\testcsv.csv");
    // 
    // CsvWriter writer = new CsvWriter(path.toFile(), new CsvWriterSettings());
    // for (int i = 0; i < 800_0000; i++) {
    // writer.writeRow(Arrays.asList(i,i));
    // }
    // writer.close();
    StopWatch stopWatch = new StopWatch();
    ExecuteCompiler executeCompiler = new ExecuteCompiler();
    CsvScanPlan csvScan = new ValuesCsvScanPlan(path.toString(), SchemaBuilder.ofArrowType(ArrowTypes.INT64_TYPE, ArrowTypes.STRING_TYPE).toArrow(), executeCompiler.createRootContext());
    Function column = executeCompiler.column(0, csvScan.schema());
    Function add = executeCompiler.call("+", Arrays.asList(column, column));
    RootContext rootContext = executeCompiler.createRootContext();
    PhysicalPlan projection = executeCompiler.project(csvScan, Arrays.asList(add));
    for (int i = 0; i < 100; i++) {
        stopWatch.reset();
        stopWatch.start();
        Observable<VectorSchemaRoot> execute = projection.execute(rootContext);
        AtomicLong count = new AtomicLong(0);
        execute.blockingLatest().forEach(c -> {
            count.getAndAdd(c.getRowCount());
            c.close();
        });
        stopWatch.stop();
        System.out.println("count:" + count);
        Duration duration = Duration.ofMillis(stopWatch.getTime());
        System.out.println(duration.getSeconds());
        System.out.println(stopWatch.toString());
    }
// 
// Thread.sleep(100000000);
}
Also used : Path(java.nio.file.Path) ValuesCsvScanPlan(io.ordinate.engine.physicalplan.ValuesCsvScanPlan) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) PhysicalPlan(io.ordinate.engine.physicalplan.PhysicalPlan) ValuesCsvScanPlan(io.ordinate.engine.physicalplan.ValuesCsvScanPlan) CsvScanPlan(io.ordinate.engine.physicalplan.CsvScanPlan) Duration(java.time.Duration) StopWatch(org.apache.commons.lang3.time.StopWatch) RootContext(io.ordinate.engine.record.RootContext) Function(io.ordinate.engine.function.Function) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExecuteCompiler(io.ordinate.engine.builder.ExecuteCompiler) SneakyThrows(lombok.SneakyThrows)

Example 2 with RootContext

use of io.ordinate.engine.record.RootContext in project Mycat2 by MyCATApache.

the class ExecutorProviderImpl method prepare.

@Override
public PrepareExecutor prepare(Plan plan) {
    CodeExecuterContext codeExecuterContext = plan.getCodeExecuterContext();
    PrepareExecutor bindable = codeExecuterContext.bindable;
    if (bindable != null)
        return bindable;
    try {
        return codeExecuterContext.bindable = PrepareExecutor.of((newMycatDataContext, mycatRowMetaData) -> {
            DrdsSqlWithParams drdsSql = newMycatDataContext.getDrdsSql();
            CalciteCompiler mycatCalciteCompiler = new CalciteCompiler();
            PhysicalPlan factory = mycatCalciteCompiler.convert(plan.getMycatRel());
            factory = fixCast(factory);
            RexConverter rexConverter = mycatCalciteCompiler.getRexConverter();
            Map<Integer, IndexedParameterLinkFunction> indexedMap = rexConverter.getIndexedParameterLinkFunctionMap();
            List<Object> params = drdsSql.getParams();
            if (!indexedMap.isEmpty()) {
                for (int i = 0; i < params.size(); i++) {
                    Object o = params.get(i);
                    IndexedParameterLinkFunction indexedParameterLinkFunction = indexedMap.get(i);
                    if (indexedParameterLinkFunction != null) {
                        BindVariable base = (BindVariable) indexedParameterLinkFunction.getBase();
                        base.setObject(o);
                    }
                }
            }
            List<SessionVariable> sessionMap = rexConverter.getSessionVariableFunctionMap();
            for (SessionVariable sessionVariable : sessionMap) {
                sessionVariable.setSession(newMycatDataContext.getContext());
            }
            AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(newMycatDataContext.getContext(), plan.getCodeExecuterContext(), drdsSql);
            RootContext rootContext = new RootContext(sqlMycatDataContext);
            Observable<VectorSchemaRoot> schemaRootObservable = factory.execute(rootContext);
            return PrepareExecutor.ArrowObservable.of(mycatRowMetaData, schemaRootObservable);
        }, getArrayBindable(codeExecuterContext));
    } catch (Exception exception) {
        LOGGER.error("", exception);
    }
    return null;
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SneakyThrows(lombok.SneakyThrows) NewMycatDataContext(org.apache.calcite.runtime.NewMycatDataContext) LoggerFactory(org.slf4j.LoggerFactory) SessionVariable(io.ordinate.engine.function.bind.SessionVariable) CalciteCompiler(io.ordinate.engine.builder.CalciteCompiler) Utilities(org.apache.calcite.runtime.Utilities) Observable(io.reactivex.rxjava3.core.Observable) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) Map(java.util.Map) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) EnumerableInterpretable(org.apache.calcite.adapter.enumerable.EnumerableInterpretable) CalciteSystemProperty(org.apache.calcite.config.CalciteSystemProperty) IClassBodyEvaluator(org.codehaus.commons.compiler.IClassBodyEvaluator) FieldVector(org.apache.arrow.vector.FieldVector) Logger(org.slf4j.Logger) PhysicalPlan(io.ordinate.engine.physicalplan.PhysicalPlan) io.mycat.calcite(io.mycat.calcite) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) RexConverter(io.ordinate.engine.builder.RexConverter) Enumerable(org.apache.calcite.linq4j.Enumerable) BindVariable(io.ordinate.engine.function.bind.BindVariable) RootContext(io.ordinate.engine.record.RootContext) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) List(java.util.List) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) CompilerFactoryFactory(org.codehaus.commons.compiler.CompilerFactoryFactory) StringReader(java.io.StringReader) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) IndexedParameterLinkFunction(io.ordinate.engine.function.bind.IndexedParameterLinkFunction) MycatDataContext(io.mycat.MycatDataContext) NotNull(org.jetbrains.annotations.NotNull) ICompilerFactory(org.codehaus.commons.compiler.ICompilerFactory) Plan(io.mycat.calcite.spm.Plan) PhysicalPlan(io.ordinate.engine.physicalplan.PhysicalPlan) CalciteCompiler(io.ordinate.engine.builder.CalciteCompiler) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) SessionVariable(io.ordinate.engine.function.bind.SessionVariable) Observable(io.reactivex.rxjava3.core.Observable) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) RootContext(io.ordinate.engine.record.RootContext) RexConverter(io.ordinate.engine.builder.RexConverter) List(java.util.List) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) BindVariable(io.ordinate.engine.function.bind.BindVariable) Map(java.util.Map) IndexedParameterLinkFunction(io.ordinate.engine.function.bind.IndexedParameterLinkFunction)

Example 3 with RootContext

use of io.ordinate.engine.record.RootContext in project Mycat2 by MyCATApache.

the class LimitPlan method execute.

@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
    MutableLong offsetBox = new MutableLong(offsetExpr.getInt(null));
    MutableLong fetchBox = new MutableLong(fetchExpr.getInt(null));
    Observable<VectorSchemaRoot> observable = input.execute(rootContext).subscribeOn(Schedulers.single());
    if (offsetBox.longValue() > 0) {
        observable = observable.map(root -> {
            long offset = offsetBox.longValue();
            if (offset == 0) {
                return root;
            }
            if (offset > 0) {
                if (root.getRowCount() > offset) {
                    try {
                        return root.slice((int) offset);
                    } finally {
                        offsetBox.setValue(0);
                    }
                }
                offsetBox.subtract(root.getRowCount());
                root.clear();
                return root;
            }
            throw new IllegalArgumentException();
        });
    }
    if (fetchBox.longValue() > 0) {
        observable = observable.map(root -> {
            long fetch = fetchBox.longValue();
            if (fetch == 0) {
                root.clear();
                return root;
            }
            int rowCount = root.getRowCount();
            if (fetch > 0) {
                if (fetch > rowCount) {
                    fetchBox.subtract(rowCount);
                    return root;
                }
                fetchBox.setValue(0);
                return root.slice(0, (int) fetch);
            }
            throw new IllegalArgumentException();
        });
    }
    return observable;
}
Also used : Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) Schema(org.apache.arrow.vector.types.pojo.Schema) List(java.util.List) Logger(org.slf4j.Logger) ImmutableList(com.google.common.collect.ImmutableList) Observable(io.reactivex.rxjava3.core.Observable) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) MutableLong(org.apache.commons.lang3.mutable.MutableLong) LoggerFactory(org.slf4j.LoggerFactory) IntFunction(io.ordinate.engine.function.IntFunction) RootContext(io.ordinate.engine.record.RootContext) IntBindVariable(io.ordinate.engine.function.bind.IntBindVariable) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) MutableLong(org.apache.commons.lang3.mutable.MutableLong)

Example 4 with RootContext

use of io.ordinate.engine.record.RootContext 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 5 with RootContext

use of io.ordinate.engine.record.RootContext in project Mycat2 by MyCATApache.

the class DistinctPlan method execute.

@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
    // DistinctContext distinctContext = new DistinctContext();
    IntInnerType[] intPairs = getIntTypes();
    Map map = MapFactory.createMap(intPairs);
    RecordSink recordSink = RecordSinkFactory.INSTANCE.buildRecordSink(intPairs);
    return input.execute(rootContext).flatMap(input -> {
        int rowCount = input.getRowCount();
        int columnCount = input.getFieldVectors().size();
        VectorSchemaRoot output = rootContext.getVectorSchemaRoot(schema(), rowCount);
        VectorBatchRecord record = new VectorBatchRecord(input);
        int outputRowId = 0;
        for (int rowId = 0; rowId < rowCount; rowId++) {
            record.setPosition(rowId);
            MapKey key = map.withKey();
            RecordSetter recordSinkSPI = RecordSinkFactory.INSTANCE.getRecordSinkSPI(key);
            recordSink.copy(record, recordSinkSPI);
            if (key.create()) {
                recordSink.copy(record, outputRowId, output);
                outputRowId++;
            // output
            } else {
            // skip
            }
        }
        if (outputRowId == 0) {
            output.close();
            return Observable.empty();
        }
        output.setRowCount(outputRowId);
        return Observable.fromArray(output);
    }).doOnComplete(new Action() {

        @Override
        public void run() throws Throwable {
            map.close();
        }
    });
}
Also used : MapFactory(io.ordinate.engine.structure.MapFactory) Schema(org.apache.arrow.vector.types.pojo.Schema) Logger(org.slf4j.Logger) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) LoggerFactory(org.slf4j.LoggerFactory) RecordSinkFactory(io.ordinate.engine.record.RecordSinkFactory) RecordSetter(io.ordinate.engine.record.RecordSetter) RootContext(io.ordinate.engine.record.RootContext) MapKey(io.questdb.cairo.map.MapKey) Action(io.reactivex.rxjava3.functions.Action) Map(io.questdb.cairo.map.Map) List(java.util.List) IntInnerType(io.ordinate.engine.schema.IntInnerType) Observable(io.reactivex.rxjava3.core.Observable) VectorBatchRecord(io.ordinate.engine.record.VectorBatchRecord) Collections(java.util.Collections) RecordSink(io.ordinate.engine.record.RecordSink) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) RecordSetter(io.ordinate.engine.record.RecordSetter) Action(io.reactivex.rxjava3.functions.Action) MapKey(io.questdb.cairo.map.MapKey) RecordSink(io.ordinate.engine.record.RecordSink) Map(io.questdb.cairo.map.Map) VectorBatchRecord(io.ordinate.engine.record.VectorBatchRecord) IntInnerType(io.ordinate.engine.schema.IntInnerType)

Aggregations

RootContext (io.ordinate.engine.record.RootContext)8 VectorSchemaRoot (org.apache.arrow.vector.VectorSchemaRoot)8 Observable (io.reactivex.rxjava3.core.Observable)7 List (java.util.List)7 Schema (org.apache.arrow.vector.types.pojo.Schema)6 Collections (java.util.Collections)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 IntFunction (io.ordinate.engine.function.IntFunction)3 Action (io.reactivex.rxjava3.functions.Action)3 AsyncMycatDataContextImpl (io.mycat.AsyncMycatDataContextImpl)2 DrdsSqlWithParams (io.mycat.DrdsSqlWithParams)2 CalciteCompiler (io.ordinate.engine.builder.CalciteCompiler)2 PhysicalSortProperty (io.ordinate.engine.builder.PhysicalSortProperty)2 RecordSetter (io.ordinate.engine.record.RecordSetter)2 RecordSink (io.ordinate.engine.record.RecordSink)2 RecordSinkFactory (io.ordinate.engine.record.RecordSinkFactory)2 VectorBatchRecord (io.ordinate.engine.record.VectorBatchRecord)2 IntInnerType (io.ordinate.engine.schema.IntInnerType)2 MapFactory (io.ordinate.engine.structure.MapFactory)2