Search in sources :

Example 11 with Function

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

the class FooFunctionSink method copy.

@Override
public void copy(Function[] functions, Record inputRecord, int rowId, VectorSchemaRoot output) {
    int columnId = 0;
    for (Function function : functions) {
        FieldVector vector = output.getVector(columnId);
        switch(function.getType()) {
            case BOOLEAN_TYPE:
                {
                    int value = function.getInt(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    BitVector bitVector = (BitVector) vector;
                    if (isNull) {
                        bitVector.setNull(rowId);
                    } else {
                        bitVector.set(rowId, value);
                    }
                    break;
                }
            case INT8_TYPE:
                {
                    int value = function.getByte(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case UINT8_TYPE:
                {
                    int value = function.getInt(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case CHAR_TYPE:
                {
                    int value = function.getInt(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case INT16_TYPE:
                {
                    int value = function.getInt(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case UINT16_TYPE:
                {
                    int value = function.getInt(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case INT32_TYPE:
                {
                    int value = function.getInt(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case UINT32_TYPE:
                {
                    int value = function.getInt(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case INT64_TYPE:
                {
                    long value = function.getLong(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    BigIntVector bigIntVector = (BigIntVector) output.getVector(columnId);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case UINT64_TYPE:
                {
                    long value = function.getLong(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    setInt(rowId, output, columnId, value, isNull);
                    break;
                }
            case FLOAT_TYPE:
                {
                    float value = function.getFloat(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    Float4Vector float4Vector = (Float4Vector) output.getVector(columnId);
                    if (isNull) {
                        float4Vector.setNull(rowId);
                    } else {
                        float4Vector.set(rowId, value);
                    }
                    break;
                }
            case DOUBLE_TYPE:
                {
                    double value = function.getDouble(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    Float8Vector float8Vector = (Float8Vector) output.getVector(columnId);
                    if (isNull) {
                        float8Vector.setNull(rowId);
                    } else {
                        float8Vector.set(rowId, value);
                    }
                    break;
                }
            case SYMBOL_TYPE:
            case STRING_TYPE:
                {
                    CharSequence value = function.getString(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    VarCharVector varCharVector = (VarCharVector) output.getVector(columnId);
                    if (isNull) {
                        varCharVector.setNull(rowId);
                    } else {
                        varCharVector.set(rowId, new Text(value.toString()));
                    }
                    break;
                }
            case BINARY_TYPE:
                {
                    BinarySequence value = function.getBinary(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    VarBinaryVector varBinaryVector = (VarBinaryVector) output.getVector(columnId);
                    if (isNull) {
                        varBinaryVector.setNull(rowId);
                    } else {
                        varBinaryVector.set(rowId, value.getBytes());
                    }
                    break;
                }
            case TIME_MILLI_TYPE:
                {
                    long value = function.getTime(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    TimeMilliVector timeMilliVector = (TimeMilliVector) output.getVector(columnId);
                    if (isNull) {
                        timeMilliVector.setNull(rowId);
                    } else {
                        timeMilliVector.set(rowId, (int) value);
                    }
                    break;
                }
            case DATE_TYPE:
                {
                    long value = function.getDate(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    DateMilliVector dateMilliVector = (DateMilliVector) output.getVector(columnId);
                    if (isNull) {
                        dateMilliVector.setNull(rowId);
                    } else {
                        dateMilliVector.set(rowId, value);
                    }
                    break;
                }
            case DATETIME_MILLI_TYPE:
                {
                    long value = function.getDatetime(inputRecord);
                    boolean isNull = function.isNull(inputRecord);
                    TimeStampVector dateMilliVector = (TimeStampVector) output.getVector(columnId);
                    if (isNull) {
                        dateMilliVector.setNull(rowId);
                    } else {
                        dateMilliVector.set(rowId, value);
                    }
                    break;
                }
            case OBJECT_TYPE:
            case NULL_TYPE:
                throw new UnsupportedOperationException();
        }
        columnId++;
    }
}
Also used : BinarySequence(io.ordinate.engine.function.BinarySequence) Text(org.apache.arrow.vector.util.Text) Function(io.ordinate.engine.function.Function)

Example 12 with Function

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

the class TableFunctionListPlan method execute.

@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
    VectorSchemaRoot vectorSchemaRoot = rootContext.getVectorSchemaRoot(schema, functionList.size());
    int index = 0;
    for (Function[] functions : functionList) {
        functionSink.copy(functions, null, index, vectorSchemaRoot);
        index++;
    }
    vectorSchemaRoot.setRowCount(functionList.size());
    return Observable.fromArray(vectorSchemaRoot);
}
Also used : VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) Function(io.ordinate.engine.function.Function)

Example 13 with Function

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

the class CodeGenRecordSinkFactoryImpl method buildFunctionSink.

@Override
@SneakyThrows
public FunctionSink buildFunctionSink(Function[] functions) {
    StringBuilder sb = new StringBuilder();
    for (Function function : functions) {
        sb.append(function.getType().getAlias());
    }
    sb.append(FunctionSink.class.getSimpleName());
    String className = sb.toString();
    MethodDeclaration methodDeclaration = generateFunctionToVectorBatchMethod(functions);
    ClassDeclaration classDeclaration = Expressions.classDecl(java.lang.reflect.Modifier.PUBLIC, className, null, Collections.singletonList(FunctionSink.class), Arrays.asList(methodDeclaration));
    String s1 = Expressions.toString(classDeclaration);
    Class cookClass = CodeGenerator.cookClass(RecordSink.class.getClassLoader(), className, s1);
    return (FunctionSink) cookClass.newInstance();
}
Also used : Function(io.ordinate.engine.function.Function) SneakyThrows(lombok.SneakyThrows)

Example 14 with Function

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

the class NLJoinPlan method createRecord.

private Record createRecord(VectorSchemaRoot leftInput, int leftIndex, VectorSchemaRoot rightBatch, int rightIndex) {
    int leftCount = leftInput.getFieldVectors().size();
    IntUnaryOperator function = columnIndex -> {
        if (columnIndex < leftCount) {
            return leftIndex;
        } else {
            return rightIndex;
        }
    };
    return new Record() {

        boolean isNull;

        @Override
        public void setPosition(int value) {
        }

        private int getPosition(int columnIndex) {
            return function.applyAsInt(columnIndex);
        }

        private <T extends FieldVector> T getFieldVector(int columnIndex) {
            FieldVector vector;
            if (columnIndex < leftCount) {
                vector = leftInput.getVector(columnIndex);
            } else {
                vector = rightBatch.getVector(columnIndex - leftCount);
            }
            return (T) vector;
        }

        @Override
        public int getInt(int columnIndex) {
            IntVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position);
            }
        }

        @Override
        public long getLong(int columnIndex) {
            BigIntVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position);
            }
        }

        @Override
        public byte getByte(int columnIndex) {
            TinyIntVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position);
            }
        }

        @Override
        public short getShort(int columnIndex) {
            SmallIntVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position);
            }
        }

        @Override
        public BinarySequence getBinary(int columnIndex) {
            VarBinaryVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return null;
            } else {
                isNull = false;
                return BinarySequence.of(vector.get(position));
            }
        }

        @Override
        public char getChar(int columnIndex) {
            SmallIntVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return (char) vector.get(position);
            }
        }

        @Override
        public long getDate(int columnIndex) {
            DateMilliVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position);
            }
        }

        @Override
        public long getDatetime(int columnIndex) {
            TimeStampVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position) * 1000L;
            }
        }

        @Override
        public CharSequence getString(int columnIndex) {
            VarCharVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return null;
            } else {
                isNull = false;
                return new String(vector.get(position));
            }
        }

        @Override
        public CharSequence getSymbol(int columnIndex) {
            return getString(columnIndex);
        }

        @Override
        public float getFloat(int columnIndex) {
            Float4Vector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position);
            }
        }

        @Override
        public double getDouble(int columnIndex) {
            Float8Vector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position);
            }
        }

        @Override
        public long getTime(int columnIndex) {
            TimeStampVector vector = getFieldVector(columnIndex);
            int position = getPosition(columnIndex);
            if (vector.isNull(position)) {
                isNull = true;
                return 0;
            } else {
                isNull = false;
                return vector.get(position);
            }
        }

        @Override
        public boolean isNull(int columnIndex) {
            return false;
        }

        @Override
        public short getUInt16(int columnIndex) {
            return 0;
        }

        @Override
        public long getUInt64(int columnIndex) {
            return 0;
        }

        @Override
        public Object getObject(int columnIndex) {
            return null;
        }

        @Override
        public int getUInt32(int i) {
            return 0;
        }

        @Override
        public String toString() {
            int columnCount = leftInput.getSchema().getFields().size() + rightBatch.getSchema().getFields().size();
            ArrayList arrayList = new ArrayList();
            for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
                FieldVector vector = getFieldVector(columnIndex);
                Object object = vector.getObject(function.applyAsInt(columnIndex));
                arrayList.add(object);
            }
            return arrayList.toString();
        }
    };
}
Also used : Schema(org.apache.arrow.vector.types.pojo.Schema) Logger(org.slf4j.Logger) IntUnaryOperator(java.util.function.IntUnaryOperator) LoggerFactory(org.slf4j.LoggerFactory) Function(io.ordinate.engine.function.Function) RootContext(io.ordinate.engine.record.RootContext) org.apache.arrow.vector(org.apache.arrow.vector) ArrayList(java.util.ArrayList) BinarySequence(io.ordinate.engine.function.BinarySequence) Record(io.ordinate.engine.record.Record) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) JoinType(org.apache.calcite.linq4j.JoinType) Observable(io.reactivex.rxjava3.core.Observable) NotNull(org.jetbrains.annotations.NotNull) ArrayList(java.util.ArrayList) IntUnaryOperator(java.util.function.IntUnaryOperator) Record(io.ordinate.engine.record.Record)

Example 15 with Function

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

the class IfNullFunctionFactory method newInstance.

@Override
public Function newInstance(List<Function> args, EngineConfiguration configuration) {
    return new Function() {

        Function left;

        Function right;

        boolean isNull;

        @Override
        public InnerType getType() {
            return left.getType();
        }

        @Override
        public List<Function> getArgs() {
            return args;
        }

        @Override
        public BinarySequence getBinary(Record rec) {
            isNull = false;
            BinarySequence binary = left.getBinary(rec);
            if (!left.isNull(rec)) {
                return binary;
            }
            binary = right.getBinary(rec);
            if (!right.isNull(rec)) {
                return binary;
            }
            isNull = true;
            return null;
        }

        @Override
        public byte getByte(Record rec) {
            isNull = false;
            byte aByte = left.getByte(rec);
            if (!left.isNull(rec)) {
                return aByte;
            }
            aByte = right.getByte(rec);
            if (!right.isNull(rec)) {
                return aByte;
            }
            isNull = true;
            return 0;
        }

        @Override
        public char getChar(Record rec) {
            isNull = false;
            char aChar = left.getChar(rec);
            if (!left.isNull(rec)) {
                return aChar;
            }
            aChar = right.getChar(rec);
            if (!right.isNull(rec)) {
                return aChar;
            }
            isNull = true;
            return 0;
        }

        @Override
        public long getDate(Record rec) {
            isNull = false;
            long date = left.getDate(rec);
            if (!left.isNull(rec)) {
                return date;
            }
            date = right.getDate(rec);
            if (!right.isNull(rec)) {
                return date;
            }
            isNull = true;
            return 0;
        }

        @Override
        public double getDouble(Record rec) {
            isNull = false;
            double aDouble = left.getDouble(rec);
            if (!left.isNull(rec)) {
                return aDouble;
            }
            aDouble = right.getDouble(rec);
            if (!right.isNull(rec)) {
                return aDouble;
            }
            isNull = true;
            return 0;
        }

        @Override
        public float getFloat(Record rec) {
            isNull = false;
            float aFloat = left.getFloat(rec);
            if (!left.isNull(rec)) {
                return aFloat;
            }
            aFloat = right.getFloat(rec);
            if (!right.isNull(rec)) {
                return aFloat;
            }
            isNull = true;
            return 0;
        }

        @Override
        public int getInt(Record rec) {
            isNull = false;
            int anInt = left.getInt(rec);
            if (!left.isNull(rec)) {
                return anInt;
            }
            anInt = right.getInt(rec);
            if (!right.isNull(rec)) {
                return anInt;
            }
            isNull = true;
            return 0;
        }

        @Override
        public long getLong(Record rec) {
            isNull = false;
            long aLong = left.getLong(rec);
            if (!left.isNull(rec)) {
                return aLong;
            }
            aLong = right.getLong(rec);
            if (!right.isNull(rec)) {
                return aLong;
            }
            isNull = true;
            return 0;
        }

        @Override
        public short getShort(Record rec) {
            isNull = false;
            short aLong = left.getShort(rec);
            if (!left.isNull(rec)) {
                return aLong;
            }
            aLong = right.getShort(rec);
            if (!right.isNull(rec)) {
                return aLong;
            }
            isNull = true;
            return 0;
        }

        @Override
        public CharSequence getString(Record rec) {
            isNull = false;
            CharSequence charSequence = left.getString(rec);
            if (!left.isNull(rec)) {
                return charSequence;
            }
            charSequence = right.getString(rec);
            if (!right.isNull(rec)) {
                return charSequence;
            }
            isNull = true;
            return null;
        }

        @Override
        public long getDatetime(Record rec) {
            isNull = false;
            long datetime = left.getDatetime(rec);
            if (!left.isNull(rec)) {
                return datetime;
            }
            datetime = right.getDatetime(rec);
            if (!right.isNull(rec)) {
                return datetime;
            }
            isNull = true;
            return 0;
        }

        @Override
        public long getTime(Record rec) {
            isNull = false;
            long time = left.getTime(rec);
            if (!left.isNull(rec)) {
                return time;
            }
            time = right.getTime(rec);
            if (!right.isNull(rec)) {
                return time;
            }
            isNull = true;
            return 0;
        }

        @Override
        public CharSequence getSymbol(Record rec) {
            isNull = false;
            CharSequence symbol = left.getSymbol(rec);
            if (!left.isNull(rec)) {
                return symbol;
            }
            symbol = right.getSymbol(rec);
            if (!right.isNull(rec)) {
                return symbol;
            }
            isNull = true;
            return null;
        }

        @Override
        public boolean isNull(Record rec) {
            return isNull;
        }
    };
}
Also used : Function(io.ordinate.engine.function.Function) BinarySequence(io.ordinate.engine.function.BinarySequence) Record(io.ordinate.engine.record.Record)

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