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();
}
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());
}
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);
}
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));
}
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());
}
}
}
}
Aggregations