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