Search in sources :

Example 11 with InnerType

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

the class ExecuteCompiler method call.

public static Function call(String name, List<Function> argExprs) {
    List<FunctionFactoryDescriptor> functions = functionFactoryCache.getListByName(name);
    if (functions.isEmpty()) {
        return null;
    }
    if (functions.size() == 1) {
        FunctionFactoryDescriptor functionFactoryDescriptor = functions.get(0);
        FunctionFactory factory = functionFactoryDescriptor.getFactory();
        int index = 0;
        List<Function> targetFunctions = new ArrayList<>(functions.size());
        if (argExprs.size() > functionFactoryDescriptor.getArgTypes().size()) {
            targetFunctions.addAll(argExprs);
        } else {
            for (InnerType argType : functionFactoryDescriptor.getArgTypes()) {
                targetFunctions.add(cast(argExprs.get(index), argType));
                index++;
            }
        }
        return factory.newInstance(targetFunctions);
    }
    boolean match = false;
    FunctionFactoryDescriptor matchVExpression = null;
    for (FunctionFactoryDescriptor function : functions) {
        List<InnerType> targetTypes = function.getArgTypes();
        int size = targetTypes.size();
        if (size != argExprs.size()) {
            continue;
        }
        boolean argMatch = true;
        for (int i = 0; i < size; i++) {
            argMatch = argMatch && targetTypes.get(i).equals(argExprs.get(i).getType());
        }
        if (argMatch) {
            match = true;
            matchVExpression = function;
            break;
        }
    }
    if (match) {
        return matchVExpression.getFactory().newInstance(argExprs);
    }
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    int lowestCastScore = Integer.MAX_VALUE;
    FunctionFactoryDescriptor bestVExpression = null;
    functionBreak: for (FunctionFactoryDescriptor function : functions) {
        List<InnerType> targetTypes = function.getArgTypes();
        int size = targetTypes.size();
        if (size != argExprs.size()) {
            continue;
        }
        int score = 0;
        for (int i = 0; i < size; i++) {
            InnerType targetArrowType = targetTypes.get(i);
            InnerType argArrowType = argExprs.get(i).getType();
            if (!targetArrowType.equals(argArrowType)) {
                if (canCast(argArrowType, targetArrowType)) {
                    score++;
                } else {
                    break functionBreak;
                }
            }
        }
        if (score < lowestCastScore) {
            lowestCastScore = score;
            bestVExpression = function;
        }
    }
    if (bestVExpression != null) {
        Function[] newArgExprs = new Function[argExprs.size()];
        List<InnerType> targetTypes = bestVExpression.getArgTypes();
        int size = targetTypes.size();
        for (int i = 0; i < size; i++) {
            InnerType targetArrowType = targetTypes.get(i);
            InnerType argArrowType = argExprs.get(i).getType();
            if (!targetArrowType.equals(argArrowType)) {
                if (canCast(argArrowType, targetArrowType)) {
                    newArgExprs[i] = cast(argExprs.get(i), targetArrowType);
                } else {
                    newArgExprs[i] = argExprs.get(i);
                }
            } else {
                newArgExprs[i] = argExprs.get(i);
            }
        }
        return call(name, Arrays.asList(newArgExprs));
    }
    // 对于变参函数,使用函数原型扩展参数类型到新的长度,然后再匹配
    throw new UnsupportedOperationException();
}
Also used : InnerType(io.ordinate.engine.schema.InnerType) CountDistinctDoubleColumnAggregateFunction(io.ordinate.engine.function.aggregate.count.CountDistinctDoubleColumnAggregateFunction) CountColumnAggregateFunction(io.ordinate.engine.function.aggregate.count.CountColumnAggregateFunction) CountDistinctLongColumnAggregateFunction(io.ordinate.engine.function.aggregate.count.CountDistinctLongColumnAggregateFunction) AvgAggregateFunction(io.ordinate.engine.function.aggregate.avg.AvgAggregateFunction) AccumulatorFunction(io.ordinate.engine.function.aggregate.AccumulatorFunction)

Example 12 with InnerType

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

the class SchemaBuilder method ofInnerTypes.

public static org.apache.arrow.vector.types.pojo.Schema ofInnerTypes(List<InnerType> innerTypes) {
    ArrayList<ArrowType> types = new ArrayList<>();
    for (InnerType innerType : innerTypes) {
        ArrowType arrowType = innerType.getArrowType();
        types.add(arrowType);
    }
    return ofArrowType(types).toArrow();
}
Also used : ArrowType(org.apache.arrow.vector.types.pojo.ArrowType) ArrayList(java.util.ArrayList) InnerType(io.ordinate.engine.schema.InnerType)

Example 13 with InnerType

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

Example 14 with InnerType

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

the class FactoryUtil method toArrowSchema.

public static Schema toArrowSchema(MycatRelDataType mycatRelDataTypeByCalcite) {
    List<MycatField> fieldList = mycatRelDataTypeByCalcite.getFieldList();
    int columnCount = fieldList.size();
    ImmutableList.Builder<org.apache.arrow.vector.types.pojo.Field> builder = ImmutableList.builder();
    for (int i = 0; i < columnCount; i++) {
        MycatField mycatField = fieldList.get(i);
        String columnName = mycatField.getName();
        MycatDataType columnType = mycatField.getMycatDataType();
        boolean nullable = mycatField.isNullable();
        InnerType innerType;
        switch(columnType) {
            case BOOLEAN:
                innerType = InnerType.BOOLEAN_TYPE;
                break;
            case BIT:
            case UNSIGNED_LONG:
                innerType = InnerType.UINT64_TYPE;
                break;
            case TINYINT:
                innerType = InnerType.INT8_TYPE;
                break;
            case UNSIGNED_TINYINT:
                innerType = InnerType.UINT8_TYPE;
                break;
            case SHORT:
                innerType = InnerType.INT16_TYPE;
                break;
            case UNSIGNED_SHORT:
            case YEAR:
                innerType = InnerType.UINT16_TYPE;
                break;
            case INT:
                innerType = InnerType.INT32_TYPE;
                break;
            case UNSIGNED_INT:
                innerType = InnerType.UINT32_TYPE;
                break;
            case LONG:
                innerType = InnerType.INT64_TYPE;
                break;
            case DOUBLE:
                innerType = InnerType.DOUBLE_TYPE;
                break;
            case DECIMAL:
                innerType = InnerType.DECIMAL_TYPE;
                break;
            case DATE:
                innerType = InnerType.DATE_TYPE;
                break;
            case DATETIME:
                innerType = InnerType.DATETIME_MILLI_TYPE;
                break;
            case TIME:
                innerType = InnerType.TIME_MILLI_TYPE;
                break;
            case CHAR:
            case VARCHAR:
                innerType = InnerType.STRING_TYPE;
                break;
            case BINARY:
                innerType = InnerType.BINARY_TYPE;
                break;
            case FLOAT:
                innerType = InnerType.FLOAT_TYPE;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + columnType);
        }
        org.apache.arrow.vector.types.pojo.Field field = FieldBuilder.of(columnName, innerType.getArrowType(), nullable).toArrow();
        builder.add(field);
    }
    return new Schema(builder.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Schema(org.apache.arrow.vector.types.pojo.Schema) MycatDataType(io.mycat.beans.mycat.MycatDataType) InnerType(io.ordinate.engine.schema.InnerType) MycatField(io.mycat.beans.mycat.MycatField) MycatField(io.mycat.beans.mycat.MycatField)

Example 15 with InnerType

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

the class AccumulatorFunction method allocContext.

public default void allocContext(InnerType[] columnTypes) {
    ArrayColumnTypes arrayColumnTypes = new ArrayColumnTypes();
    for (int i = 0; i < columnTypes.length; i++) {
        InnerType columnType = columnTypes[i];
        switch(columnType) {
            case BOOLEAN_TYPE:
                arrayColumnTypes.add(ColumnType.BOOLEAN);
                break;
            case INT8_TYPE:
                arrayColumnTypes.add(ColumnType.INT);
                break;
            case INT16_TYPE:
                arrayColumnTypes.add(ColumnType.SHORT);
                break;
            case CHAR_TYPE:
                arrayColumnTypes.add(ColumnType.CHAR);
                break;
            case INT32_TYPE:
                arrayColumnTypes.add(ColumnType.INT);
                break;
            case INT64_TYPE:
                arrayColumnTypes.add(ColumnType.LONG);
                break;
            case FLOAT_TYPE:
                arrayColumnTypes.add(ColumnType.FLOAT);
                break;
            case DOUBLE_TYPE:
                arrayColumnTypes.add(ColumnType.DOUBLE);
                break;
            case STRING_TYPE:
                arrayColumnTypes.add(ColumnType.STRING);
                break;
            case BINARY_TYPE:
                arrayColumnTypes.add(ColumnType.BINARY);
                break;
            case UINT8_TYPE:
                arrayColumnTypes.add(ColumnType.BYTE);
                break;
            case UINT16_TYPE:
                arrayColumnTypes.add(ColumnType.SHORT);
                break;
            case UINT32_TYPE:
                arrayColumnTypes.add(ColumnType.INT);
                break;
            case UINT64_TYPE:
                arrayColumnTypes.add(ColumnType.LONG);
                break;
            case TIME_MILLI_TYPE:
                arrayColumnTypes.add(ColumnType.LONG);
                break;
            case DATE_TYPE:
                arrayColumnTypes.add(ColumnType.DATE);
                break;
            case DATETIME_MILLI_TYPE:
                arrayColumnTypes.add(ColumnType.TIMESTAMP);
                break;
            case SYMBOL_TYPE:
            case OBJECT_TYPE:
            case NULL_TYPE:
            default:
                throw new IllegalStateException("Unexpected value: " + columnType);
        }
    }
    allocContext(arrayColumnTypes);
}
Also used : ArrayColumnTypes(io.questdb.cairo.ArrayColumnTypes) InnerType(io.ordinate.engine.schema.InnerType)

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