Search in sources :

Example 6 with RootContext

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

the class GroupByKeyPlan method execute.

@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
    List<Field> fields = schema().getFields();
    InnerType[] innerTypes = schema().getFields().stream().map(i -> InnerType.from(i.getType())).toArray(n -> new InnerType[n]);
    Map map = MapFactory.createMap(innerTypes);
    RecordSink[] recordSinks = new RecordSink[groupByKeys.length];
    int groupIndex = 0;
    for (GroupKeys groupByKey : groupByKeys) {
        IntInnerType[] intPairs = new IntInnerType[groupByKey.getKeys().length];
        int[] keys = groupByKey.getKeys();
        int index = 0;
        for (int key : keys) {
            Field field = fields.get(key);
            intPairs[index] = IntInnerType.of(index, InnerType.from(field.getType()));
            index++;
        }
        recordSinks[groupIndex] = RecordSinkFactory.INSTANCE.buildRecordSink(intPairs);
        groupIndex++;
    }
    return inputPlan.execute(rootContext).flatMap(new Function<VectorSchemaRoot, ObservableSource<? extends VectorSchemaRoot>>() {

        @Override
        public ObservableSource<? extends VectorSchemaRoot> apply(VectorSchemaRoot input) throws Throwable {
            int rowCount = input.getRowCount();
            VectorBatchRecord record = new VectorBatchRecord(input);
            VectorSchemaRoot output = rootContext.getVectorSchemaRoot(schema, rowCount * recordSinks.length);
            int outputRowId = 0;
            for (int i = 0; i < recordSinks.length; i++) {
                RecordSink recordSink = recordSinks[i];
                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);
            inputPlan.eachFree(input);
            return Observable.fromArray(output);
        }
    }).doOnComplete(new Action() {

        @Override
        public void run() throws Throwable {
            map.close();
        }
    });
}
Also used : MapFactory(io.ordinate.engine.structure.MapFactory) ObservableSource(io.reactivex.rxjava3.core.ObservableSource) Schema(org.apache.arrow.vector.types.pojo.Schema) InnerType(io.ordinate.engine.schema.InnerType) 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) Field(org.apache.arrow.vector.types.pojo.Field) RootContext(io.ordinate.engine.record.RootContext) MapKey(io.questdb.cairo.map.MapKey) Action(io.reactivex.rxjava3.functions.Action) Map(io.questdb.cairo.map.Map) GroupKeys(io.ordinate.engine.builder.GroupKeys) List(java.util.List) IntInnerType(io.ordinate.engine.schema.IntInnerType) Function(io.reactivex.rxjava3.functions.Function) 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) InnerType(io.ordinate.engine.schema.InnerType) IntInnerType(io.ordinate.engine.schema.IntInnerType) IntInnerType(io.ordinate.engine.schema.IntInnerType) Field(org.apache.arrow.vector.types.pojo.Field) Function(io.reactivex.rxjava3.functions.Function) MapKey(io.questdb.cairo.map.MapKey) GroupKeys(io.ordinate.engine.builder.GroupKeys) RecordSink(io.ordinate.engine.record.RecordSink) Map(io.questdb.cairo.map.Map) VectorBatchRecord(io.ordinate.engine.record.VectorBatchRecord)

Example 7 with RootContext

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

the class HeapTopNPlan method execute.

@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
    Comparator<Record> recordComparator;
    if (physicalSortProperties.size() == 1) {
        recordComparator = physicalSortProperties.get(0).evaluateToSortComparator();
    } else {
        recordComparator = physicalSortProperties.get(0).evaluateToSortComparator();
        for (PhysicalSortProperty physicalSortProperty : physicalSortProperties.subList(1, physicalSortProperties.size())) {
            recordComparator = recordComparator.thenComparing(physicalSortProperty.evaluateToSortComparator());
        }
    }
    OutputLinq4jPhysicalPlan midPlan = OutputLinq4jPhysicalPlan.create(input);
    Observable<Object[]> observable = midPlan.executeToObject(rootContext);
    @NonNull Iterable<Record> objects = MycatRxJavaUtl.blockingIterable(observable.map(i -> RecordImpl.create(i)));
    Enumerable<Record> records = EnumerableDefaults.orderBy(Linq4j.asEnumerable(objects), i -> i, recordComparator, offset.getInt(null), fetch.getInt(null));
    return InputRecordPhysicalPlan.create(schema(), Observable.fromIterable(records)).execute(rootContext);
}
Also used : Schema(org.apache.arrow.vector.types.pojo.Schema) Linq4j(org.apache.calcite.linq4j.Linq4j) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) PhysicalSortProperty(io.ordinate.engine.builder.PhysicalSortProperty) Enumerable(org.apache.calcite.linq4j.Enumerable) MycatRxJavaUtl(io.mycat.MycatRxJavaUtl) NonNull(io.reactivex.rxjava3.annotations.NonNull) RootContext(io.ordinate.engine.record.RootContext) RecordImpl(io.ordinate.engine.record.RecordImpl) Record(io.ordinate.engine.record.Record) List(java.util.List) EnumerableDefaults(org.apache.calcite.linq4j.EnumerableDefaults) Observable(io.reactivex.rxjava3.core.Observable) IntFunction(io.ordinate.engine.function.IntFunction) Comparator(java.util.Comparator) Collections(java.util.Collections) PhysicalSortProperty(io.ordinate.engine.builder.PhysicalSortProperty) NonNull(io.reactivex.rxjava3.annotations.NonNull) Record(io.ordinate.engine.record.Record)

Example 8 with RootContext

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

the class OutputLinq4jPhysicalPlan method executeToObject.

public Observable<Object[]> executeToObject(RootContext rootContext) {
    return input.execute(rootContext).flatMap(c -> {
        int rowCount = c.getRowCount();
        int columnCount = c.getFieldVectors().size();
        Object[][] array = IntStream.range(0, rowCount).mapToObj(i -> new Object[columnCount]).toArray(n -> new Object[rowCount][]);
        for (int columnId = 0; columnId < columnCount; columnId++) {
            FieldVector vector = c.getVector(columnId);
            for (int rowId = 0; rowId < rowCount; rowId++) {
                Object object = vector.getObject(rowId);
                array[rowId][columnId] = object;
            }
        }
        return Observable.fromArray(array);
    });
}
Also used : IntStream(java.util.stream.IntStream) Schema(org.apache.arrow.vector.types.pojo.Schema) List(java.util.List) FieldVector(org.apache.arrow.vector.FieldVector) Logger(org.slf4j.Logger) Observable(io.reactivex.rxjava3.core.Observable) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) LoggerFactory(org.slf4j.LoggerFactory) RootContext(io.ordinate.engine.record.RootContext) Collections(java.util.Collections) FieldVector(org.apache.arrow.vector.FieldVector)

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