Search in sources :

Example 1 with ColumnTypes

use of io.questdb.cairo.ColumnTypes in project Mycat2 by MyCATApache.

the class MapFactory method createMap.

public static Map createMap(IntInnerType[] keys, InnerType[] values) {
    ColumnTypes keyTypes = getKeyTypes(keys);
    int[] valueTypes = Arrays.stream(values).mapToInt(i -> toQuestDbType(i)).toArray();
    int MB = 1024 * 1024;
    return new FastMap(16 * MB, keyTypes, new ColumnTypes() {

        @Override
        public int getColumnCount() {
            return valueTypes.length;
        }

        @Override
        public int getColumnType(int columnIndex) {
            return valueTypes[columnIndex];
        }
    }, 128, 0.5, 64);
}
Also used : ColumnType(io.questdb.cairo.ColumnType) ColumnTypes(io.questdb.cairo.ColumnTypes) Arrays(java.util.Arrays) InnerType(io.ordinate.engine.schema.InnerType) IntInnerType(io.ordinate.engine.schema.IntInnerType) FastMap(io.questdb.cairo.map.FastMap) MapValue(io.questdb.cairo.map.MapValue) NotNull(org.jetbrains.annotations.NotNull) MapKey(io.questdb.cairo.map.MapKey) Map(io.questdb.cairo.map.Map) FastMap(io.questdb.cairo.map.FastMap) ColumnTypes(io.questdb.cairo.ColumnTypes)

Example 2 with ColumnTypes

use of io.questdb.cairo.ColumnTypes in project Mycat2 by MyCATApache.

the class GroupByKeyWithAggPlan 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]);
    if (groupByKeys.length > 0) {
        ColumnTypes arrayColumnTypes = RecordUtil.getArrayColumnTypes(accumulators);
        Map map = MapFactory.createMap2(innerTypes, arrayColumnTypes);
        RecordSink[] recordSinks = buildRecordSink(fields);
        return physicalPlan.execute(rootContext).reduce(map, (mapKey, input) -> {
            int rowCount = input.getRowCount();
            VectorBatchRecord record = new VectorBatchRecord(input);
            for (RecordSink recordSink : recordSinks) {
                for (int rowId = 0; rowId < rowCount; rowId++) {
                    record.setPosition(rowId);
                    MapKey key = mapKey.withKey();
                    RecordSetter recordSinkSPI = RecordSinkFactory.INSTANCE.getRecordSinkSPI(key);
                    recordSink.copy(record, recordSinkSPI);
                    MapValue value = key.createValue();
                    if (value.isNew()) {
                        for (AccumulatorFunction accumulator : accumulators) {
                            accumulator.computeFirst(value, record);
                        }
                    } else {
                        for (AccumulatorFunction accumulator : accumulators) {
                            accumulator.computeNext(value, record);
                        }
                    }
                }
            }
            physicalPlan.eachFree(input);
            return mapKey;
        }).map(map1 -> {
            int size = (int) map1.size();
            VectorSchemaRoot output = rootContext.getVectorSchemaRoot(schema(), size);
            RecordCursor cursor = map1.getCursor();
            cursor.toTop();
            int index = 0;
            while (cursor.hasNext()) {
                Record record = cursor.getRecord();
                functionSink.copy(accumulators, RecordUtil.wrapAsAggRecord(record), index++, output);
            }
            output.setRowCount(index);
            map1.clear();
            return output;
        }).toObservable().doOnComplete(() -> map.close());
    } else {
        SimpleMapValue mapValue = new SimpleMapValue(RecordUtil.getContextSize(accumulators));
        return physicalPlan.execute(rootContext).reduce(mapValue, new BiFunction<SimpleMapValue, VectorSchemaRoot, SimpleMapValue>() {

            AtomicBoolean first = new AtomicBoolean(true);

            @Override
            public SimpleMapValue apply(SimpleMapValue simpleMapValue, VectorSchemaRoot root) throws Throwable {
                int rowCount = root.getRowCount();
                VectorBatchRecord record = new VectorBatchRecord(root);
                if (first.compareAndSet(true, false)) {
                    record.setPosition(0);
                    for (AccumulatorFunction accumulator : accumulators) {
                        accumulator.computeFirst(mapValue, record);
                    }
                    for (int i = 1; i < rowCount; i++) {
                        record.setPosition(i);
                        for (AccumulatorFunction accumulator : accumulators) {
                            accumulator.computeNext(mapValue, record);
                        }
                    }
                } else {
                    for (int i = 0; i < rowCount; i++) {
                        record.setPosition(i);
                        for (AccumulatorFunction accumulator : accumulators) {
                            accumulator.computeNext(mapValue, record);
                        }
                    }
                }
                root.close();
                return simpleMapValue;
            }
        }).map(new Function<SimpleMapValue, VectorSchemaRoot>() {

            @Override
            public VectorSchemaRoot apply(SimpleMapValue simpleMapValue) throws Throwable {
                VectorSchemaRoot vectorSchemaRoot = rootContext.getVectorSchemaRoot(schema(), 1);
                vectorSchemaRoot.setRowCount(1);
                functionSink.copy(accumulators, RecordUtil.wrapAsAggRecord(simpleMapValue), 0, vectorSchemaRoot);
                return vectorSchemaRoot;
            }
        }).toObservable();
    }
}
Also used : MapFactory(io.ordinate.engine.structure.MapFactory) Schema(org.apache.arrow.vector.types.pojo.Schema) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecordCursor(io.questdb.cairo.sql.RecordCursor) Map(io.questdb.cairo.map.Map) GroupKeys(io.ordinate.engine.builder.GroupKeys) IntInnerType(io.ordinate.engine.schema.IntInnerType) io.ordinate.engine.record(io.ordinate.engine.record) SimpleMapValue(io.questdb.griffin.engine.groupby.SimpleMapValue) Observable(io.reactivex.rxjava3.core.Observable) InnerType(io.ordinate.engine.schema.InnerType) Logger(org.slf4j.Logger) MapValue(io.questdb.cairo.map.MapValue) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) Field(org.apache.arrow.vector.types.pojo.Field) MapKey(io.questdb.cairo.map.MapKey) ColumnTypes(io.questdb.cairo.ColumnTypes) List(java.util.List) Function(io.reactivex.rxjava3.functions.Function) BiFunction(io.reactivex.rxjava3.functions.BiFunction) Record(io.questdb.cairo.sql.Record) AccumulatorFunction(io.ordinate.engine.function.aggregate.AccumulatorFunction) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) RecordCursor(io.questdb.cairo.sql.RecordCursor) IntInnerType(io.ordinate.engine.schema.IntInnerType) InnerType(io.ordinate.engine.schema.InnerType) SimpleMapValue(io.questdb.griffin.engine.groupby.SimpleMapValue) MapValue(io.questdb.cairo.map.MapValue) ColumnTypes(io.questdb.cairo.ColumnTypes) Field(org.apache.arrow.vector.types.pojo.Field) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.rxjava3.functions.Function) BiFunction(io.reactivex.rxjava3.functions.BiFunction) AccumulatorFunction(io.ordinate.engine.function.aggregate.AccumulatorFunction) MapKey(io.questdb.cairo.map.MapKey) SimpleMapValue(io.questdb.griffin.engine.groupby.SimpleMapValue) AccumulatorFunction(io.ordinate.engine.function.aggregate.AccumulatorFunction) Record(io.questdb.cairo.sql.Record) Map(io.questdb.cairo.map.Map)

Aggregations

InnerType (io.ordinate.engine.schema.InnerType)2 IntInnerType (io.ordinate.engine.schema.IntInnerType)2 ColumnTypes (io.questdb.cairo.ColumnTypes)2 Map (io.questdb.cairo.map.Map)2 MapKey (io.questdb.cairo.map.MapKey)2 MapValue (io.questdb.cairo.map.MapValue)2 NotNull (org.jetbrains.annotations.NotNull)2 GroupKeys (io.ordinate.engine.builder.GroupKeys)1 AccumulatorFunction (io.ordinate.engine.function.aggregate.AccumulatorFunction)1 io.ordinate.engine.record (io.ordinate.engine.record)1 MapFactory (io.ordinate.engine.structure.MapFactory)1 ColumnType (io.questdb.cairo.ColumnType)1 FastMap (io.questdb.cairo.map.FastMap)1 Record (io.questdb.cairo.sql.Record)1 RecordCursor (io.questdb.cairo.sql.RecordCursor)1 SimpleMapValue (io.questdb.griffin.engine.groupby.SimpleMapValue)1 Observable (io.reactivex.rxjava3.core.Observable)1 BiFunction (io.reactivex.rxjava3.functions.BiFunction)1 Function (io.reactivex.rxjava3.functions.Function)1 Arrays (java.util.Arrays)1