use of org.apache.calcite.rel.type.RelDataTypeFactory in project hive by apache.
the class HiveAggregateReduceFunctionsRule method reduceAvg.
private RexNode reduceAvg(Aggregate oldAggRel, AggregateCall oldCall, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) {
final int nGroups = oldAggRel.getGroupCount();
final RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
final RelDataTypeFactory typeFactory = oldAggRel.getCluster().getTypeFactory();
final int iAvgInput = oldCall.getArgList().get(0);
final RelDataType avgInputType = typeFactory.createTypeWithNullability(getFieldType(oldAggRel.getInput(), iAvgInput), true);
final RelDataType sumReturnType = getSumReturnType(rexBuilder.getTypeFactory(), avgInputType);
final AggregateCall sumCall = AggregateCall.create(new HiveSqlSumAggFunction(oldCall.isDistinct(), ReturnTypes.explicit(sumReturnType), oldCall.getAggregation().getOperandTypeInference(), // SqlStdOperatorTable.SUM,
oldCall.getAggregation().getOperandTypeChecker()), oldCall.isDistinct(), oldCall.isApproximate(), oldCall.getArgList(), oldCall.filterArg, oldAggRel.getGroupCount(), oldAggRel.getInput(), null, null);
RelDataType countRetType = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BIGINT), true);
final AggregateCall countCall = AggregateCall.create(new HiveSqlCountAggFunction(oldCall.isDistinct(), ReturnTypes.explicit(countRetType), oldCall.getAggregation().getOperandTypeInference(), // SqlStdOperatorTable.COUNT,
oldCall.getAggregation().getOperandTypeChecker()), oldCall.isDistinct(), oldCall.isApproximate(), oldCall.getArgList(), oldCall.filterArg, oldAggRel.getGroupCount(), oldAggRel.getInput(), countRetType, null);
// NOTE: these references are with respect to the output
// of newAggRel
RexNode numeratorRef = rexBuilder.addAggCall(sumCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(avgInputType));
final RexNode denominatorRef = rexBuilder.addAggCall(countCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(avgInputType));
if (numeratorRef.getType().getSqlTypeName() != SqlTypeName.DECIMAL) {
// If type is not decimal, we enforce the same type as the avg to comply with
// Hive semantics
numeratorRef = rexBuilder.ensureType(oldCall.getType(), numeratorRef, true);
}
final RexNode divideRef = rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, numeratorRef, denominatorRef);
return rexBuilder.makeCast(oldCall.getType(), divideRef);
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project hive by apache.
the class HiveAggregateReduceFunctionsRule method reduceSum0.
private RexNode reduceSum0(Aggregate oldAggRel, AggregateCall oldCall, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) {
final int nGroups = oldAggRel.getGroupCount();
final RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
final RelDataTypeFactory typeFactory = oldAggRel.getCluster().getTypeFactory();
final int iAvgInput = oldCall.getArgList().get(0);
final RelDataType sum0InputType = typeFactory.createTypeWithNullability(getFieldType(oldAggRel.getInput(), iAvgInput), true);
final RelDataType sumReturnType = getSumReturnType(rexBuilder.getTypeFactory(), sum0InputType);
final AggregateCall sumCall = AggregateCall.create(new HiveSqlSumAggFunction(oldCall.isDistinct(), ReturnTypes.explicit(sumReturnType), oldCall.getAggregation().getOperandTypeInference(), // SqlStdOperatorTable.SUM,
oldCall.getAggregation().getOperandTypeChecker()), oldCall.isDistinct(), oldCall.isApproximate(), oldCall.getArgList(), oldCall.filterArg, oldAggRel.getGroupCount(), oldAggRel.getInput(), null, null);
RexNode refSum = rexBuilder.addAggCall(sumCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(sum0InputType));
refSum = rexBuilder.ensureType(oldCall.getType(), refSum, true);
final RexNode coalesce = rexBuilder.makeCall(SqlStdOperatorTable.COALESCE, refSum, rexBuilder.makeZeroLiteral(refSum.getType()));
return rexBuilder.makeCast(oldCall.getType(), coalesce);
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project hive by apache.
the class HiveAggregate method deriveRowType.
public static RelDataType deriveRowType(RelDataTypeFactory typeFactory, final RelDataType inputRowType, boolean indicator, ImmutableBitSet groupSet, final List<AggregateCall> aggCalls) {
final List<Integer> groupList = groupSet.asList();
assert groupList.size() == groupSet.cardinality();
final RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
final List<RelDataTypeField> fieldList = inputRowType.getFieldList();
final Set<String> containedNames = Sets.newHashSet();
for (int groupKey : groupList) {
containedNames.add(fieldList.get(groupKey).getName());
builder.add(fieldList.get(groupKey));
}
if (indicator) {
for (int groupKey : groupList) {
final RelDataType booleanType = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BOOLEAN), false);
String name = "i$" + fieldList.get(groupKey).getName();
int i = 0;
StringBuilder nameBuilder = new StringBuilder(name);
while (containedNames.contains(name)) {
nameBuilder.append('_').append(i++);
}
containedNames.add(nameBuilder.toString());
builder.add(name, booleanType);
}
}
for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
String name;
if (aggCall.e.name != null) {
name = aggCall.e.name;
} else {
name = "$f" + (groupList.size() + aggCall.i);
}
int i = 0;
while (containedNames.contains(name)) {
name += "_" + i++;
}
containedNames.add(name);
builder.add(name, aggCall.e.type);
}
return builder.build();
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project beam by apache.
the class UdafImpl method getParameters.
@Override
public List<FunctionParameter> getParameters() {
List<FunctionParameter> para = new ArrayList<>();
para.add(new FunctionParameter() {
@Override
public int getOrdinal() {
// up to one parameter is supported in UDAF.
return 0;
}
@Override
public String getName() {
// not used as Beam SQL uses its own execution engine
return null;
}
@Override
public RelDataType getType(RelDataTypeFactory typeFactory) {
Type inputType = getInputType();
if (inputType instanceof TypeVariable) {
throw new IllegalArgumentException("Unable to infer SQL type from type variable " + inputType + ". This usually means you are trying to use a generic type whose type information " + "is not known at runtime. You can wrap your CombineFn into typed subclass" + " by 'new TypedCombineFnDelegate<...>(combineFn) {}'");
}
return CalciteUtils.sqlTypeWithAutoCast(typeFactory, inputType);
}
@Override
public boolean isOptional() {
// not used as Beam SQL uses its own execution engine
return false;
}
});
return para;
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project apex-malhar by apache.
the class CSVMessageFormat method getRowType.
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
String schema = (String) operands.get(CSV_SCHEMA);
RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
DelimitedSchema delimitedSchema = new DelimitedSchema(schema);
for (DelimitedSchema.Field field : delimitedSchema.getFields()) {
builder.add(field.getName(), convertField(typeFactory, field.getType()));
}
return builder.build();
}
Aggregations