Search in sources :

Example 6 with InnerType

use of io.ordinate.engine.schema.InnerType in project Mycat2 by MyCATApache.

the class CodeGenRecordSinkFactoryImpl method generateFunctionToVectorBatchBody.

public static BlockStatement generateFunctionToVectorBatchBody(Function[] functions) {
    int size = functions.length;
    BlockBuilder statements = new BlockBuilder();
    for (int varIndex = 0; varIndex < functions.length; varIndex++) {
        Function function = functions[varIndex];
        InnerType type = function.getType();
        ParameterExpression functionVariable = Expressions.parameter(Function.class);
        ConstantExpression arrayIndex = Expressions.constant(varIndex);
        statements.add(Expressions.declare(0, functionVariable, Expressions.convert_(Expressions.arrayIndex(functionsExpression, arrayIndex), Function.class)));
        ParameterExpression vectorVariable = Expressions.parameter(type.getFieldVector());
        ParameterExpression returnValue = Expressions.parameter(function.getType().getJavaClass());
        statements.add(Expressions.declare(0, returnValue, Expressions.call(functionVariable, "get" + type.name(), from)));
        ParameterExpression isNull = Expressions.parameter(boolean.class);
        statements.add(Expressions.declare(0, isNull, Expressions.call(functionVariable, "isNull", from)));
        statements.add(Expressions.declare(0, vectorVariable, Expressions.convert_(Expressions.call(input, "getVector", arrayIndex), type.getFieldVector())));
        MethodCallExpression ifTrue = Expressions.call(RecordSinkFactoryImpl.class, "set" + type.getFieldVector().getSimpleName() + "Null", vectorVariable, rowId);
        MethodCallExpression ifFalse = Expressions.call(RecordSinkFactoryImpl.class, "set" + type.getFieldVector().getSimpleName(), vectorVariable, rowId, returnValue);
        statements.add(Expressions.ifThenElse(isNull, Expressions.statement(ifTrue), Expressions.statement(ifFalse)));
    }
    return statements.toBlock();
}
Also used : Function(io.ordinate.engine.function.Function) InnerType(io.ordinate.engine.schema.InnerType) IntInnerType(io.ordinate.engine.schema.IntInnerType)

Example 7 with InnerType

use of io.ordinate.engine.schema.InnerType in project Mycat2 by MyCATApache.

the class MapFactory method getIntPairs.

@NotNull
private static IntInnerType[] getIntPairs(InnerType[] pairs) {
    int index = 0;
    IntInnerType[] intPairs = new IntInnerType[pairs.length];
    for (InnerType pair : pairs) {
        intPairs[index] = IntInnerType.of(index, pair);
        index++;
    }
    return intPairs;
}
Also used : InnerType(io.ordinate.engine.schema.InnerType) IntInnerType(io.ordinate.engine.schema.IntInnerType) IntInnerType(io.ordinate.engine.schema.IntInnerType) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with InnerType

use of io.ordinate.engine.schema.InnerType 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 9 with InnerType

use of io.ordinate.engine.schema.InnerType in project Mycat2 by MyCATApache.

the class ResultWriterUtil method vectorRowBatchToResultSetColumn.

public static MycatRowMetaData vectorRowBatchToResultSetColumn(Schema schema) {
    ResultSetBuilder writer = ResultSetBuilder.create();
    List<Field> fields = schema.getFields();
    for (Field field : fields) {
        InnerType innerType = InnerType.from(field.getType());
        writer.addColumnInfo(field.getName(), innerType.getJdbcType(), field.isNullable(), innerType.isSigned());
    }
    return writer.build().getMetaData();
}
Also used : Field(org.apache.arrow.vector.types.pojo.Field) InnerType(io.ordinate.engine.schema.InnerType)

Example 10 with InnerType

use of io.ordinate.engine.schema.InnerType in project Mycat2 by MyCATApache.

the class CalciteCompiler method convertAggregate.

public PhysicalPlan convertAggregate(Aggregate aggregate) {
    PhysicalPlan input = convert(aggregate.getInput());
    List<Integer> groupSet = aggregate.getGroupSet().asList();
    List<AggregateCall> aggCallList = aggregate.getAggCallList();
    GroupKeys[] groupSets = aggregate.getGroupSets().stream().map(i -> GroupKeys.of(i.toArray())).toArray(n -> new GroupKeys[n]);
    AccumulatorFunction[] accumulatorFunctions = new AccumulatorFunction[groupSet.size() + aggCallList.size()];
    int index = 0;
    for (Integer integer : groupSet) {
        accumulatorFunctions[index] = ExecuteCompiler.anyValue(input, integer);
        index++;
    }
    index = groupSet.size();
    for (AggregateCall aggregateCall : aggCallList) {
        List<Integer> argList = aggregateCall.getArgList();
        SqlKind kind = aggregateCall.getAggregation().kind;
        AccumulatorFunction accumulatorFunction = null;
        switch(kind) {
            case SUM:
            case SUM0:
                {
                    Integer integer = argList.get(0);
                    accumulatorFunction = executeCompiler.sum(input, integer);
                    break;
                }
            case AVG:
                {
                    accumulatorFunction = executeCompiler.avg(argList.get(0));
                    break;
                }
            case COUNT:
                {
                    boolean distinct = aggregateCall.isDistinct();
                    if (distinct) {
                        // todo check
                        accumulatorFunction = executeCompiler.countDistinct(input, argList.get(0));
                    } else {
                        if (argList.size() == 0) {
                            accumulatorFunction = executeCompiler.count();
                        } else {
                            accumulatorFunction = executeCompiler.count(argList.get(0));
                        }
                    }
                    break;
                }
            case ANY_VALUE:
                {
                    accumulatorFunction = executeCompiler.anyValue(input, argList.get(0));
                    break;
                }
            case MAX:
                {
                    accumulatorFunction = executeCompiler.max(input, argList.get(0));
                    break;
                }
            case MIN:
                {
                    accumulatorFunction = executeCompiler.min(input, argList.get(0));
                    break;
                }
        }
        Objects.requireNonNull(accumulatorFunction);
        accumulatorFunctions[index] = accumulatorFunction;
        ++index;
    }
    int slotOriginalSize = input.schema().getFields().size();
    int slotInc = slotOriginalSize;
    Map<Integer, Map<InnerType, Integer>> indexToTypeMap = new HashMap<>();
    List<Field> fieldList = input.schema().getFields();
    for (AccumulatorFunction accumulatorFunction : accumulatorFunctions) {
        InnerType aggInputType = accumulatorFunction.getInputType();
        InnerType aggOutputType = accumulatorFunction.getOutputType();
        int aggInputIndex = accumulatorFunction.getInputColumnIndex();
        InnerType aggInputSourceType = InnerType.from(fieldList.get(aggInputIndex).getType());
        Function column = ExecuteCompiler.column(aggInputIndex, input.schema());
        Map<InnerType, Integer> indexSlot = indexToTypeMap.computeIfAbsent(aggInputIndex, integer -> {
            HashMap<InnerType, Integer> map = new HashMap<>();
            map.put(aggInputSourceType, integer);
            return map;
        });
        if (aggInputType != null) {
            if (!indexSlot.containsValue(aggInputIndex)) {
                indexSlot.put(aggInputType, aggInputIndex);
            } else {
                accumulatorFunction.setInputColumnIndex(slotInc);
                indexSlot.put(aggInputType, slotInc);
                slotInc++;
            }
        }
    }
    Function[] exprs = new Function[slotInc];
    for (int i = 0; i < slotOriginalSize; i++) {
        exprs[i] = ExecuteCompiler.column(i, input.schema());
    }
    if (slotInc > slotOriginalSize) {
        for (Map.Entry<Integer, Map<InnerType, Integer>> e1 : indexToTypeMap.entrySet()) {
            for (Map.Entry<InnerType, Integer> e2 : e1.getValue().entrySet()) {
                Integer inIndex = e1.getKey();
                Integer outIndex = e2.getValue();
                if (exprs[outIndex] == null) {
                    exprs[outIndex] = (ExecuteCompiler.cast(ExecuteCompiler.column(inIndex, input.schema()), e2.getKey()));
                }
            }
        }
    }
    input = executeCompiler.project(input, exprs);
    return executeCompiler.agg(input, ExecuteCompiler.AggImpl.HASH, Arrays.asList(groupSets), Arrays.asList(accumulatorFunctions));
}
Also used : MycatRelDataTypeUtil(io.mycat.calcite.MycatRelDataTypeUtil) Schema(org.apache.arrow.vector.types.pojo.Schema) java.util(java.util) AnyAccumulator(io.ordinate.engine.function.aggregate.any.AnyAccumulator) ColumnFunction(io.ordinate.engine.function.column.ColumnFunction) Getter(lombok.Getter) ImmutableList(com.google.common.collect.ImmutableList) RexNode(org.apache.calcite.rex.RexNode) IntFunction(io.ordinate.engine.function.IntFunction) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) VariableParameterFunction(io.ordinate.engine.function.bind.VariableParameterFunction) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataType(org.apache.calcite.rel.type.RelDataType) org.apache.calcite.rel.core(org.apache.calcite.rel.core) InnerType(io.ordinate.engine.schema.InnerType) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) VectorExpression(io.ordinate.engine.vector.VectorExpression) MycatView(io.mycat.calcite.logical.MycatView) RexLiteral(org.apache.calcite.rex.RexLiteral) Function(io.ordinate.engine.function.Function) Field(org.apache.arrow.vector.types.pojo.Field) FactoryUtil(io.ordinate.engine.factory.FactoryUtil) RelNode(org.apache.calcite.rel.RelNode) MycatTableScan(io.mycat.calcite.table.MycatTableScan) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Collectors(java.util.stream.Collectors) JoinType(org.apache.calcite.linq4j.JoinType) RelCollation(org.apache.calcite.rel.RelCollation) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) AccumulatorFunction(io.ordinate.engine.function.aggregate.AccumulatorFunction) IntConstant(io.ordinate.engine.function.constant.IntConstant) io.ordinate.engine.physicalplan(io.ordinate.engine.physicalplan) RexCall(org.apache.calcite.rex.RexCall) InnerType(io.ordinate.engine.schema.InnerType) SqlKind(org.apache.calcite.sql.SqlKind) Field(org.apache.arrow.vector.types.pojo.Field) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ColumnFunction(io.ordinate.engine.function.column.ColumnFunction) IntFunction(io.ordinate.engine.function.IntFunction) VariableParameterFunction(io.ordinate.engine.function.bind.VariableParameterFunction) Function(io.ordinate.engine.function.Function) AccumulatorFunction(io.ordinate.engine.function.aggregate.AccumulatorFunction) AccumulatorFunction(io.ordinate.engine.function.aggregate.AccumulatorFunction)

Aggregations

InnerType (io.ordinate.engine.schema.InnerType)23 IntInnerType (io.ordinate.engine.schema.IntInnerType)10 Schema (org.apache.arrow.vector.types.pojo.Schema)6 Field (org.apache.arrow.vector.types.pojo.Field)5 Function (io.ordinate.engine.function.Function)4 ImmutableList (com.google.common.collect.ImmutableList)3 AccumulatorFunction (io.ordinate.engine.function.aggregate.AccumulatorFunction)3 Map (io.questdb.cairo.map.Map)3 MapKey (io.questdb.cairo.map.MapKey)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 VectorSchemaRoot (org.apache.arrow.vector.VectorSchemaRoot)3 GroupKeys (io.ordinate.engine.builder.GroupKeys)2 VariableParameterFunction (io.ordinate.engine.function.bind.VariableParameterFunction)2 MapFactory (io.ordinate.engine.structure.MapFactory)2 SimpleMapValue (io.questdb.griffin.engine.groupby.SimpleMapValue)2 Observable (io.reactivex.rxjava3.core.Observable)2 Function (io.reactivex.rxjava3.functions.Function)2 Collections (java.util.Collections)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2