use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.
the class Aggregate method deriveRowType.
/**
* Computes the row type of an {@code Aggregate} before it exists.
*
* @param typeFactory Type factory
* @param inputRowType Input row type
* @param indicator Whether row type should include indicator fields to
* indicate which grouping set is active; must be true if
* aggregate is not simple
* @param groupSet Bit set of grouping fields
* @param groupSets List of all grouping sets; null for just {@code groupSet}
* @param aggCalls Collection of calls to aggregate functions
* @return Row type of the aggregate
*/
public static RelDataType deriveRowType(RelDataTypeFactory typeFactory, final RelDataType inputRowType, boolean indicator, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, final List<AggregateCall> aggCalls) {
final List<Integer> groupList = groupSet.asList();
assert groupList.size() == groupSet.cardinality();
final RelDataTypeFactory.Builder builder = typeFactory.builder();
final List<RelDataTypeField> fieldList = inputRowType.getFieldList();
final Set<String> containedNames = Sets.newHashSet();
for (int groupKey : groupList) {
final RelDataTypeField field = fieldList.get(groupKey);
containedNames.add(field.getName());
builder.add(field);
if (groupSets != null && !allContain(groupSets, groupKey)) {
builder.nullable(true);
}
}
if (indicator) {
for (int groupKey : groupList) {
final RelDataType booleanType = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BOOLEAN), false);
final String base = "i$" + fieldList.get(groupKey).getName();
String name = base;
int i = 0;
while (containedNames.contains(name)) {
name = base + "_" + i++;
}
containedNames.add(name);
builder.add(name, booleanType);
}
}
for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
final String base;
if (aggCall.e.name != null) {
base = aggCall.e.name;
} else {
base = "$f" + (groupList.size() + aggCall.i);
}
String name = base;
int i = 0;
while (containedNames.contains(name)) {
name = base + "_" + i++;
}
containedNames.add(name);
builder.add(name, aggCall.e.type);
}
return builder.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.
the class Aggregate method typeMatchesInferred.
/**
* Returns whether the inferred type of an {@link AggregateCall} matches the
* type it was given when it was created.
*
* @param aggCall Aggregate call
* @param litmus What to do if an error is detected (types do not match)
* @return Whether the inferred and declared types match
*/
private boolean typeMatchesInferred(final AggregateCall aggCall, final Litmus litmus) {
SqlAggFunction aggFunction = aggCall.getAggregation();
AggCallBinding callBinding = aggCall.createBinding(this);
RelDataType type = aggFunction.inferReturnType(callBinding);
RelDataType expectedType = aggCall.type;
return RelOptUtil.eq("aggCall type", expectedType, "inferred type", type, litmus);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.
the class AggregateCall method create.
/**
* Creates an AggregateCall, inferring its type if {@code type} is null.
*/
public static AggregateCall create(SqlAggFunction aggFunction, boolean distinct, boolean approximate, List<Integer> argList, int filterArg, int groupCount, RelNode input, RelDataType type, String name) {
if (type == null) {
final RelDataTypeFactory typeFactory = input.getCluster().getTypeFactory();
final List<RelDataType> types = SqlTypeUtil.projectTypes(input.getRowType(), argList);
final Aggregate.AggCallBinding callBinding = new Aggregate.AggCallBinding(typeFactory, aggFunction, types, groupCount, filterArg >= 0);
type = aggFunction.inferReturnType(callBinding);
}
return create(aggFunction, distinct, approximate, argList, filterArg, type, name);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.
the class Collect method deriveCollectRowType.
/**
* Derives the output type of a collect relational expression.
*
* @param rel relational expression
* @param fieldName name of sole output field
* @return output type of a collect relational expression
*/
public static RelDataType deriveCollectRowType(SingleRel rel, String fieldName) {
RelDataType childType = rel.getInput().getRowType();
assert childType.isStruct();
final RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
RelDataType ret = SqlTypeUtil.createMultisetType(typeFactory, childType, false);
ret = typeFactory.builder().add(fieldName, ret).build();
return typeFactory.createTypeWithNullability(ret, false);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.
the class LogicalProject method create.
// ~ Methods ----------------------------------------------------------------
/**
* Creates a LogicalProject.
*/
public static LogicalProject create(final RelNode input, final List<? extends RexNode> projects, List<String> fieldNames) {
final RelOptCluster cluster = input.getCluster();
final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), projects, fieldNames, SqlValidatorUtil.F_SUGGESTER);
return create(input, projects, rowType);
}
Aggregations