Search in sources :

Example 6 with Function

use of io.ordinate.engine.function.Function 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 Function

use of io.ordinate.engine.function.Function in project Mycat2 by MyCATApache.

the class ProjectFactory method create.

@Override
public PhysicalPlan create(ComplierContext context) {
    PhysicalPlan physicalPlan = inputFactory.create(context);
    ImmutableList.Builder<Function> builder = ImmutableList.builder();
    for (RexNode project : mycatRel.getProjects()) {
        Function function = context.convertRex(project);
        builder.add(function);
    }
    return ExecuteCompiler.project(physicalPlan, builder.build());
}
Also used : Function(io.ordinate.engine.function.Function) PhysicalPlan(io.ordinate.engine.physicalplan.PhysicalPlan) ImmutableList(com.google.common.collect.ImmutableList) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with Function

use of io.ordinate.engine.function.Function in project Mycat2 by MyCATApache.

the class CalciteCompiler method convertValues.

public PhysicalPlan convertValues(Values values) {
    ImmutableList<ImmutableList<RexLiteral>> tuples = values.getTuples();
    ArrayList<Function[]> rowList = new ArrayList<>();
    for (ImmutableList<RexLiteral> tuple : tuples) {
        int size = tuple.size();
        Function[] functions = new Function[size];
        int index = 0;
        for (RexLiteral rexLiteral : tuple) {
            Function function = rexConverter.convertToFunction(rexLiteral);
            functions[index] = function;
            index++;
        }
        rowList.add(functions);
    }
    RelDataType rowType = values.getRowType();
    MycatRelDataType mycatRelDataType = MycatRelDataTypeUtil.getMycatRelDataType(rowType);
    Schema schema = FactoryUtil.toArrowSchema(mycatRelDataType);
    return executeCompiler.values(rowList, schema);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) 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) ImmutableList(com.google.common.collect.ImmutableList) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) Schema(org.apache.arrow.vector.types.pojo.Schema) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 9 with Function

use of io.ordinate.engine.function.Function 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)

Example 10 with Function

use of io.ordinate.engine.function.Function in project Mycat2 by MyCATApache.

the class VerSetterImpl method set.

@Override
public void set(int rowId, VectorSchemaRoot leftInput) {
    for (Map.Entry<Integer, List<VariableParameterFunction>> e : variableParameterFunctionMap.entrySet()) {
        Integer key = e.getKey();
        for (VariableParameterFunction value : e.getValue()) {
            Function base = value.getBase();
            FieldVector vector = leftInput.getVector(key);
            InnerType type = value.getType();
            switch(type) {
                case BOOLEAN_TYPE:
                    {
                        BooleanBindVariable booleanBindVariable = (BooleanBindVariable) base;
                        if (vector.isNull(rowId)) {
                            booleanBindVariable.isNull = true;
                        } else {
                            booleanBindVariable.isNull = false;
                            booleanBindVariable.value = ((BitVector) vector).get(rowId);
                        }
                        break;
                    }
                case INT8_TYPE:
                    {
                        ByteBindVariable byteBindVariable = (ByteBindVariable) base;
                        if (vector.isNull(rowId)) {
                            byteBindVariable.isNull = true;
                        } else {
                            byteBindVariable.isNull = false;
                            byteBindVariable.value = ((TinyIntVector) vector).get(rowId);
                        }
                        break;
                    }
                case INT16_TYPE:
                case CHAR_TYPE:
                    {
                        ShortBindVariable shortBindVariable = (ShortBindVariable) base;
                        if (vector.isNull(rowId)) {
                            shortBindVariable.isNull = true;
                        } else {
                            shortBindVariable.isNull = false;
                            shortBindVariable.value = ((SmallIntVector) vector).get(rowId);
                        }
                        break;
                    }
                case INT32_TYPE:
                    {
                        IntBindVariable intBindVariable = (IntBindVariable) base;
                        if (vector.isNull(rowId)) {
                            intBindVariable.isNull = true;
                        } else {
                            intBindVariable.isNull = false;
                            intBindVariable.value = ((IntVector) vector).get(rowId);
                        }
                        break;
                    }
                case INT64_TYPE:
                    {
                        LongBindVariable longBindVariable = (LongBindVariable) base;
                        if (vector.isNull(rowId)) {
                            longBindVariable.isNull = true;
                        } else {
                            longBindVariable.isNull = false;
                            longBindVariable.value = ((BigIntVector) vector).get(rowId);
                        }
                        break;
                    }
                case FLOAT_TYPE:
                    {
                        FloatBindVariable floatBindVariable = (FloatBindVariable) base;
                        if (vector.isNull(rowId)) {
                            floatBindVariable.isNull = true;
                        } else {
                            floatBindVariable.isNull = false;
                            floatBindVariable.value = ((Float4Vector) vector).get(rowId);
                        }
                        break;
                    }
                case DOUBLE_TYPE:
                    {
                        DoubleBindVariable doubleBindVariable = (DoubleBindVariable) base;
                        if (vector.isNull(rowId)) {
                            doubleBindVariable.isNull = true;
                        } else {
                            doubleBindVariable.isNull = false;
                            doubleBindVariable.value = ((Float8Vector) vector).get(rowId);
                        }
                        break;
                    }
                case SYMBOL_TYPE:
                case STRING_TYPE:
                    {
                        StringBindVariable strBindVariable = (StringBindVariable) base;
                        if (vector.isNull(rowId)) {
                            strBindVariable.isNull = true;
                        } else {
                            strBindVariable.isNull = false;
                            byte[] bytes = ((VarCharVector) vector).get(rowId);
                            strBindVariable.value = new String(bytes, StandardCharsets.UTF_8);
                        }
                        break;
                    }
                case BINARY_TYPE:
                    {
                        BinaryBindVariable binBindVariable = (BinaryBindVariable) base;
                        if (vector.isNull(rowId)) {
                            binBindVariable.isNull = true;
                        } else {
                            binBindVariable.isNull = false;
                            byte[] bytes = ((VarBinaryVector) vector).get(rowId);
                            binBindVariable.value = BinarySequence.of(bytes);
                        }
                        break;
                    }
                case UINT8_TYPE:
                case UINT16_TYPE:
                case UINT32_TYPE:
                case UINT64_TYPE:
                    throw new UnsupportedOperationException();
                case TIME_MILLI_TYPE:
                    {
                        TimeBindVariable timeBindVariable = (TimeBindVariable) base;
                        if (vector.isNull(rowId)) {
                            timeBindVariable.isNull = true;
                        } else {
                            timeBindVariable.isNull = false;
                            timeBindVariable.value = ((TimeMilliVector) vector).get(rowId);
                        }
                        break;
                    }
                case DATE_TYPE:
                    {
                        DateBindVariable dateBindVariable = (DateBindVariable) base;
                        if (vector.isNull(rowId)) {
                            dateBindVariable.isNull = true;
                        } else {
                            dateBindVariable.isNull = false;
                            dateBindVariable.value = ((DateMilliVector) vector).get(rowId);
                        }
                        break;
                    }
                case DATETIME_MILLI_TYPE:
                    {
                        TimestampBindVariable timestampBindVariable = (TimestampBindVariable) base;
                        if (vector.isNull(rowId)) {
                            timestampBindVariable.isNull = true;
                        } else {
                            timestampBindVariable.isNull = false;
                            timestampBindVariable.value = ((TimeStampVector) vector).get(rowId);
                        }
                        break;
                    }
                case OBJECT_TYPE:
                case NULL_TYPE:
                    throw new UnsupportedOperationException();
                default:
                    throw new IllegalStateException("Unexpected value: " + value.getType());
            }
        }
    }
}
Also used : Function(io.ordinate.engine.function.Function) List(java.util.List) InnerType(io.ordinate.engine.schema.InnerType) Map(java.util.Map)

Aggregations

Function (io.ordinate.engine.function.Function)15 VariableParameterFunction (io.ordinate.engine.function.bind.VariableParameterFunction)5 ImmutableList (com.google.common.collect.ImmutableList)4 IntFunction (io.ordinate.engine.function.IntFunction)4 AccumulatorFunction (io.ordinate.engine.function.aggregate.AccumulatorFunction)4 ColumnFunction (io.ordinate.engine.function.column.ColumnFunction)4 InnerType (io.ordinate.engine.schema.InnerType)4 Schema (org.apache.arrow.vector.types.pojo.Schema)4 BinarySequence (io.ordinate.engine.function.BinarySequence)3 RexNode (org.apache.calcite.rex.RexNode)3 MycatRelDataType (io.mycat.beans.mycat.MycatRelDataType)2 PhysicalPlan (io.ordinate.engine.physicalplan.PhysicalPlan)2 Record (io.ordinate.engine.record.Record)2 RootContext (io.ordinate.engine.record.RootContext)2 List (java.util.List)2 SneakyThrows (lombok.SneakyThrows)2 VectorSchemaRoot (org.apache.arrow.vector.VectorSchemaRoot)2 JoinType (org.apache.calcite.linq4j.JoinType)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 RexLiteral (org.apache.calcite.rex.RexLiteral)2