Java Code Examples for org.apache.calcite.rel.core.AggregateCall

The following examples show how to use org.apache.calcite.rel.core.AggregateCall. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: calcite   Source File: AggregateNode.java    License: Apache License 2.0 6 votes vote down vote up
public AggregateNode(Compiler compiler, Aggregate rel) {
  super(compiler, rel);
  this.dataContext = compiler.getDataContext();

  ImmutableBitSet union = ImmutableBitSet.of();

  if (rel.getGroupSets() != null) {
    for (ImmutableBitSet group : rel.getGroupSets()) {
      union = union.union(group);
      groups.add(new Grouping(group));
    }
  }

  this.unionGroups = union;
  this.outputRowLength = unionGroups.cardinality()
      + rel.getAggCallList().size();

  ImmutableList.Builder<AccumulatorFactory> builder = ImmutableList.builder();
  for (AggregateCall aggregateCall : rel.getAggCallList()) {
    builder.add(getAccumulator(aggregateCall, false));
  }
  accumulatorFactories = builder.build();
}
 
Example 2
Source Project: kylin-on-parquet-v2   Source File: OLAPWindowRel.java    License: Apache License 2.0 6 votes vote down vote up
ColumnRowType buildColumnRowType() {
    OLAPRel olapChild = (OLAPRel) getInput(0);
    ColumnRowType inputColumnRowType = olapChild.getColumnRowType();

    List<TblColRef> columns = new ArrayList<>();
    // the input col always be collected by left
    columns.addAll(inputColumnRowType.getAllColumns());

    // add window aggregate calls column
    for (Group group : groups) {
        List<TupleExpression> sourceColOuter = Lists.newArrayList();
        group.keys.asSet().stream().map(inputColumnRowType::getTupleExpressionByIndex).forEach(sourceColOuter::add);
        group.orderKeys.getFieldCollations().stream().map(RelFieldCollation::getFieldIndex)
                .map(inputColumnRowType::getTupleExpressionByIndex).forEach(sourceColOuter::add);
        for (AggregateCall aggrCall : group.getAggregateCalls(this)) {
            TblColRef aggrCallCol = TblColRef.newInnerColumn(aggrCall.getName(),
                    TblColRef.InnerDataTypeEnum.LITERAL);
            List<TupleExpression> sourceColInner = Lists.newArrayList(sourceColOuter.iterator());
            aggrCall.getArgList().stream().filter(i -> i < inputColumnRowType.size())
                    .map(inputColumnRowType::getTupleExpressionByIndex).forEach(sourceColInner::add);
            aggrCallCol.setSubTupleExps(sourceColInner);
            columns.add(aggrCallCol);
        }
    }
    return new ColumnRowType(columns);
}
 
Example 3
Source Project: dremio-oss   Source File: DremioRelToSqlConverter.java    License: Apache License 2.0 6 votes vote down vote up
protected void generateGroupBy(DremioRelToSqlConverter.Builder builder, Aggregate e) {
  List<SqlNode> groupByList = Expressions.list();
  final List<SqlNode> selectList = new ArrayList<>();
  for (int group : e.getGroupSet()) {
    final SqlNode field = builder.context.field(group);
    addSelect(selectList, field, e.getRowType());
    addGroupBy(groupByList, field);
  }

  for (AggregateCall aggCall : e.getAggCallList()) {
    SqlNode aggCallSqlNode = ((DremioContext) builder.context).toSql(aggCall, e);

    if (aggCall.getAggregation() instanceof SqlSingleValueAggFunction) {
      aggCallSqlNode = dialect.
        rewriteSingleValueExpr(aggCallSqlNode);
    }
    addSelect(selectList, aggCallSqlNode, e.getRowType());
  }
  builder.setSelect(new SqlNodeList(selectList, POS));
  if (!groupByList.isEmpty() || e.getAggCallList().isEmpty()) {
    // Some databases don't support "GROUP BY ()". We can omit it as long
    // as there is at least one aggregate function.
    builder.setGroupBy(new SqlNodeList(groupByList, POS));
  }
}
 
Example 4
Source Project: Bats   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private AggregateCall createAggregateCallWithBinding(
    RelDataTypeFactory typeFactory,
    SqlAggFunction aggFunction,
    RelDataType operandType,
    Aggregate oldAggRel,
    AggregateCall oldCall,
    int argOrdinal,
    int filter) {
  final Aggregate.AggCallBinding binding =
      new Aggregate.AggCallBinding(typeFactory, aggFunction,
          ImmutableList.of(operandType), oldAggRel.getGroupCount(),
          filter >= 0);
  return AggregateCall.create(aggFunction,
      oldCall.isDistinct(),
      oldCall.isApproximate(),
      ImmutableIntList.of(argOrdinal),
      filter,
      oldCall.collation,
      aggFunction.inferReturnType(binding),
      null);
}
 
Example 5
Source Project: Bats   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode getSumAggregatedRexNode(Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    RexBuilder rexBuilder,
    int argOrdinal,
    int filterArg) {
  final AggregateCall aggregateCall =
      AggregateCall.create(SqlStdOperatorTable.SUM,
          oldCall.isDistinct(),
          oldCall.isApproximate(),
          ImmutableIntList.of(argOrdinal),
          filterArg,
          oldCall.collation,
          oldAggRel.getGroupCount(),
          oldAggRel.getInput(),
          null,
          null);
  return rexBuilder.addAggCall(aggregateCall,
      oldAggRel.getGroupCount(),
      oldAggRel.indicator,
      newCalls,
      aggCallMapping,
      ImmutableList.of(aggregateCall.getType()));
}
 
Example 6
Source Project: calcite   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode getSumAggregatedRexNode(Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    RexBuilder rexBuilder,
    int argOrdinal,
    int filterArg) {
  final AggregateCall aggregateCall =
      AggregateCall.create(SqlStdOperatorTable.SUM,
          oldCall.isDistinct(),
          oldCall.isApproximate(),
          oldCall.ignoreNulls(),
          ImmutableIntList.of(argOrdinal),
          filterArg,
          oldCall.collation,
          oldAggRel.getGroupCount(),
          oldAggRel.getInput(),
          null,
          null);
  return rexBuilder.addAggCall(aggregateCall,
      oldAggRel.getGroupCount(),
      newCalls,
      aggCallMapping,
      ImmutableList.of(aggregateCall.getType()));
}
 
Example 7
Source Project: Bats   Source File: AggregateJoinTransposeRule.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean isAggregateSupported(Aggregate aggregate, boolean allowFunctions) {
  if (!allowFunctions && !aggregate.getAggCallList().isEmpty()) {
    return false;
  }
  if (aggregate.getGroupType() != Aggregate.Group.SIMPLE) {
    return false;
  }
  // If any aggregate functions do not support splitting, bail out
  // If any aggregate call has a filter or is distinct, bail out
  for (AggregateCall aggregateCall : aggregate.getAggCallList()) {
    if (aggregateCall.getAggregation().unwrap(SqlSplittableAggFunction.class)
        == null) {
      return false;
    }
    if (aggregateCall.filterArg >= 0 || aggregateCall.isDistinct()) {
      return false;
    }
  }
  return true;
}
 
Example 8
Source Project: lucene-solr   Source File: SolrAggregate.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"fallthrough"})
private Pair<String, String> toSolrMetric(Implementor implementor, AggregateCall aggCall, List<String> inNames) {
  SqlAggFunction aggregation = aggCall.getAggregation();
  List<Integer> args = aggCall.getArgList();
  switch (args.size()) {
    case 0:
      if (aggregation.equals(SqlStdOperatorTable.COUNT)) {
        return new Pair<>(aggregation.getName(), "*");
      }
    case 1:
      String inName = inNames.get(args.get(0));
      String name = implementor.fieldMappings.getOrDefault(inName, inName);
      if(SUPPORTED_AGGREGATIONS.contains(aggregation)) {
        return new Pair<>(aggregation.getName(), name);
      }
    default:
      throw new AssertionError("Invalid aggregation " + aggregation + " with args " + args + " with names" + inNames);
  }
}
 
Example 9
Source Project: kylin   Source File: OLAPWindowRel.java    License: Apache License 2.0 6 votes vote down vote up
ColumnRowType buildColumnRowType() {
    OLAPRel olapChild = (OLAPRel) getInput(0);
    ColumnRowType inputColumnRowType = olapChild.getColumnRowType();

    List<TblColRef> columns = new ArrayList<>();
    // the input col always be collected by left
    columns.addAll(inputColumnRowType.getAllColumns());

    // add window aggregate calls column
    for (Group group : groups) {
        List<TupleExpression> sourceColOuter = Lists.newArrayList();
        group.keys.asSet().stream().map(inputColumnRowType::getTupleExpressionByIndex).forEach(sourceColOuter::add);
        group.orderKeys.getFieldCollations().stream().map(RelFieldCollation::getFieldIndex)
                .map(inputColumnRowType::getTupleExpressionByIndex).forEach(sourceColOuter::add);
        for (AggregateCall aggrCall : group.getAggregateCalls(this)) {
            TblColRef aggrCallCol = TblColRef.newInnerColumn(aggrCall.getName(),
                    TblColRef.InnerDataTypeEnum.LITERAL);
            List<TupleExpression> sourceColInner = Lists.newArrayList(sourceColOuter.iterator());
            aggrCall.getArgList().stream().filter(i -> i < inputColumnRowType.size())
                    .map(inputColumnRowType::getTupleExpressionByIndex).forEach(sourceColInner::add);
            aggrCallCol.setSubTupleExps(sourceColInner);
            columns.add(aggrCallCol);
        }
    }
    return new ColumnRowType(columns);
}
 
Example 10
Source Project: Bats   Source File: SqlSplittableAggFunction.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>{@code COUNT(*)}, and {@code COUNT} applied to all NOT NULL arguments,
 * become {@code 1}; otherwise
 * {@code CASE WHEN arg0 IS NOT NULL THEN 1 ELSE 0 END}.
 */
public RexNode singleton(RexBuilder rexBuilder, RelDataType inputRowType,
    AggregateCall aggregateCall) {
  final List<RexNode> predicates = new ArrayList<>();
  for (Integer arg : aggregateCall.getArgList()) {
    final RelDataType type = inputRowType.getFieldList().get(arg).getType();
    if (type.isNullable()) {
      predicates.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
              rexBuilder.makeInputRef(type, arg)));
    }
  }
  final RexNode predicate =
      RexUtil.composeConjunction(rexBuilder, predicates, true);
  if (predicate == null) {
    return rexBuilder.makeExactLiteral(BigDecimal.ONE);
  } else {
    return rexBuilder.makeCall(SqlStdOperatorTable.CASE, predicate,
        rexBuilder.makeExactLiteral(BigDecimal.ONE),
        rexBuilder.makeExactLiteral(BigDecimal.ZERO));
  }
}
 
Example 11
Source Project: calcite   Source File: JdbcRules.java    License: Apache License 2.0 6 votes vote down vote up
public JdbcAggregate(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelNode input,
    ImmutableBitSet groupSet,
    List<ImmutableBitSet> groupSets,
    List<AggregateCall> aggCalls)
    throws InvalidRelException {
  super(cluster, traitSet, ImmutableList.of(), input, groupSet, groupSets, aggCalls);
  assert getConvention() instanceof JdbcConvention;
  assert this.groupSets.size() == 1 : "Grouping sets not supported";
  final SqlDialect dialect = ((JdbcConvention) getConvention()).dialect;
  for (AggregateCall aggCall : aggCalls) {
    if (!canImplement(aggCall.getAggregation(), dialect)) {
      throw new InvalidRelException("cannot implement aggregate function "
          + aggCall.getAggregation());
    }
  }
}
 
Example 12
Source Project: Bats   Source File: RexBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a reference to an aggregate call, checking for repeated calls.
 *
 * <p>Argument types help to optimize for repeated aggregates.
 * For instance count(42) is equivalent to count(*).</p>
 *
 * @param aggCall aggregate call to be added
 * @param groupCount number of groups in the aggregate relation
 * @param indicator Whether the Aggregate has indicator (GROUPING) columns
 * @param aggCalls destination list of aggregate calls
 * @param aggCallMapping the dictionary of already added calls
 * @param aggArgTypes Argument types, not null
 *
 * @return Rex expression for the given aggregate call
 */
public RexNode addAggCall(AggregateCall aggCall, int groupCount,
    boolean indicator, List<AggregateCall> aggCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    final List<RelDataType> aggArgTypes) {
  if (aggCall.getAggregation() instanceof SqlCountAggFunction
      && !aggCall.isDistinct()) {
    final List<Integer> args = aggCall.getArgList();
    final List<Integer> nullableArgs = nullableArgs(args, aggArgTypes);
    if (!nullableArgs.equals(args)) {
      aggCall = aggCall.copy(nullableArgs, aggCall.filterArg,
          aggCall.collation);
    }
  }
  RexNode rex = aggCallMapping.get(aggCall);
  if (rex == null) {
    int index = aggCalls.size() + groupCount * (indicator ? 2 : 1);
    aggCalls.add(aggCall);
    rex = makeInputRef(aggCall.getType(), index);
    aggCallMapping.put(aggCall, rex);
  }
  return rex;
}
 
Example 13
Source Project: dremio-oss   Source File: IncrementalUpdateUtils.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(LogicalAggregate aggregate) {
  RelNode input = aggregate.getInput().accept(this);
  RelDataType incomingRowType = input.getRowType();
  RelDataTypeField modField = incomingRowType.getField(UPDATE_COLUMN, false, false);
  if (modField == null) {
    return aggregate;
  }

  final AggregateCall aggCall = AggregateCall.create(SqlStdOperatorTable.MAX, false, ImmutableList.of(modField.getIndex()), -1, modField.getType(), UPDATE_COLUMN);
  final List<AggregateCall> aggCalls = FluentIterable.from(aggregate.getAggCallList())
    .append(aggCall)
    .toList();
  return aggregate.copy(
    aggregate.getTraitSet(),
    input,
    aggregate.indicator,
    aggregate.getGroupSet(),
    null,
    aggCalls
  );
}
 
Example 14
Source Project: flink   Source File: FlinkAggregateRemoveRule.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
	final Aggregate aggregate = call.rel(0);
	final RelNode input = call.rel(1);
	if (aggregate.getGroupCount() == 0 || aggregate.indicator ||
			aggregate.getGroupType() != Aggregate.Group.SIMPLE) {
		return false;
	}
	for (AggregateCall aggCall : aggregate.getAggCallList()) {
		SqlKind aggCallKind = aggCall.getAggregation().getKind();
		// TODO supports more AggregateCalls
		boolean isAllowAggCall = aggCallKind == SqlKind.SUM ||
				aggCallKind == SqlKind.MIN ||
				aggCallKind == SqlKind.MAX ||
				aggCall.getAggregation() instanceof SqlAuxiliaryGroupAggFunction;
		if (!isAllowAggCall || aggCall.filterArg >= 0 || aggCall.getArgList().size() != 1) {
			return false;
		}
	}

	final RelMetadataQuery mq = call.getMetadataQuery();
	return SqlFunctions.isTrue(mq.areColumnsUnique(input, aggregate.getGroupSet()));
}
 
Example 15
Source Project: calcite   Source File: RexBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a reference to an aggregate call, checking for repeated calls.
 *
 * <p>Argument types help to optimize for repeated aggregates.
 * For instance count(42) is equivalent to count(*).</p>
 *
 * @param aggCall aggregate call to be added
 * @param groupCount number of groups in the aggregate relation
 * @param aggCalls destination list of aggregate calls
 * @param aggCallMapping the dictionary of already added calls
 * @param aggArgTypes Argument types, not null
 *
 * @return Rex expression for the given aggregate call
 */
public RexNode addAggCall(AggregateCall aggCall, int groupCount,
    List<AggregateCall> aggCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    final List<RelDataType> aggArgTypes) {
  if (aggCall.getAggregation() instanceof SqlCountAggFunction
      && !aggCall.isDistinct()) {
    final List<Integer> args = aggCall.getArgList();
    final List<Integer> nullableArgs = nullableArgs(args, aggArgTypes);
    if (!nullableArgs.equals(args)) {
      aggCall = aggCall.copy(nullableArgs, aggCall.filterArg,
          aggCall.collation);
    }
  }
  RexNode rex = aggCallMapping.get(aggCall);
  if (rex == null) {
    int index = aggCalls.size() + groupCount;
    aggCalls.add(aggCall);
    rex = makeInputRef(aggCall.getType(), index);
    aggCallMapping.put(aggCall, rex);
  }
  return rex;
}
 
Example 16
Source Project: calcite   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode getSumAggregatedRexNodeWithBinding(Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    RelDataType operandType,
    int argOrdinal,
    int filter) {
  RelOptCluster cluster = oldAggRel.getCluster();
  final AggregateCall sumArgSquaredAggCall =
      createAggregateCallWithBinding(cluster.getTypeFactory(),
          SqlStdOperatorTable.SUM, operandType, oldAggRel, oldCall, argOrdinal, filter);

  return cluster.getRexBuilder().addAggCall(sumArgSquaredAggCall,
      oldAggRel.getGroupCount(),
      newCalls,
      aggCallMapping,
      ImmutableList.of(sumArgSquaredAggCall.getType()));
}
 
Example 17
Source Project: streamline   Source File: RelNodeCompiler.java    License: Apache License 2.0 6 votes vote down vote up
private void aggregate(AggregateCall call) {
  SqlAggFunction aggFunction = call.getAggregation();
  String aggregationName = call.getAggregation().getName();
  Type ty = typeFactory.getJavaClass(call.getType());
  if (call.getArgList().size() != 1) {
    if (aggregationName.equals("COUNT")) {
      if (call.getArgList().size() != 0) {
        throw new UnsupportedOperationException("Count with nullable fields");
      }
    }
  }
  if (aggFunction instanceof SqlUserDefinedAggFunction) {
    AggregateFunction aggregateFunction = ((SqlUserDefinedAggFunction) aggFunction).function;
    doAggregate((AggregateFunctionImpl) aggregateFunction, reserveAggVarName(call), ty, call.getArgList());
  } else {
    List<BuiltinAggregateFunctions.TypeClass> typeClasses = BuiltinAggregateFunctions.TABLE.get(aggregationName);
    if (typeClasses == null) {
      throw new UnsupportedOperationException(aggregationName + " Not implemented");
    }
    doAggregate(AggregateFunctionImpl.create(findMatchingClass(aggregationName, typeClasses, ty)),
                reserveAggVarName(call), ty, call.getArgList());
  }
}
 
Example 18
Source Project: dremio-oss   Source File: CopyWithCluster.java    License: Apache License 2.0 5 votes vote down vote up
private AggregateCall copyOf(AggregateCall call) {
  return AggregateCall.create(
    call.getAggregation(), // doesn't look we need to copy this
    call.isDistinct(),
    call.getArgList(),
    call.filterArg,
    copyOf(call.getType()),
    call.getName());
}
 
Example 19
Source Project: calcite   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether any of the aggregates are calls to AVG, STDDEV_*, VAR_*.
 *
 * @param aggCallList List of aggregate calls
 */
private boolean containsAvgStddevVarCall(List<AggregateCall> aggCallList) {
  for (AggregateCall call : aggCallList) {
    if (isReducible(call.getAggregation().getKind())) {
      return true;
    }
  }
  return false;
}
 
Example 20
Source Project: dremio-oss   Source File: HashAggPrel.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Aggregate copy(RelTraitSet traitSet, RelNode input, boolean indicator, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
  try {
    return HashAggPrel.create(getCluster(), traitSet, input, indicator, groupSet, groupSets, aggCalls,
        this.getOperatorPhase());
  } catch (InvalidRelException e) {
    throw new AssertionError(e);
  }
}
 
Example 21
Source Project: dremio-oss   Source File: AggregateRel.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Aggregate copy(RelTraitSet traitSet, RelNode input, boolean indicator, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
  try {
    return AggregateRel.create(getCluster(), traitSet, input, indicator, groupSet, groupSets, aggCalls);
  } catch (InvalidRelException e) {
    // Semantic error not possible. Must be a bug. Convert to
    // internal error.
    throw new AssertionError(e);
  }
}
 
Example 22
Source Project: calcite   Source File: Bindables.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public BindableAggregate(RelOptCluster cluster, RelTraitSet traitSet,
    RelNode input, boolean indicator, ImmutableBitSet groupSet,
    List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls)
    throws InvalidRelException {
  this(cluster, traitSet, input, groupSet, groupSets, aggCalls);
  checkIndicator(indicator);
}
 
Example 23
Source Project: kylin   Source File: OLAPAggregateRel.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private AggregateCall rewriteAggregateCall(AggregateCall aggCall, FunctionDesc func) {
    // rebuild function
    String callName = getSqlFuncName(aggCall);
    RelDataType fieldType = aggCall.getType();
    SqlAggFunction newAgg = aggCall.getAggregation();

    Map<String, Class<?>> udafMap = func.getMeasureType().getRewriteCalciteAggrFunctions();
    if (func.isCount()) {
        newAgg = SqlStdOperatorTable.SUM0;
    } else if (udafMap != null && udafMap.containsKey(callName)) {
        newAgg = createCustomAggFunction(callName, fieldType, udafMap.get(callName));
    }

    // rebuild parameters
    List<Integer> newArgList = Lists.newArrayList(aggCall.getArgList());
    if (udafMap != null && udafMap.containsKey(callName)) {
        newArgList = truncArgList(newArgList, udafMap.get(callName));
    }
    if (func.needRewriteField()) {
        RelDataTypeField field = getInput().getRowType().getField(func.getRewriteFieldName(), true, false);
        if (newArgList.isEmpty()) {
            newArgList.add(field.getIndex());
        } else {
            // TODO: only the first column got overwritten
            newArgList.set(0, field.getIndex());
        }
    }

    // rebuild aggregate call
    AggregateCall newAggCall = new AggregateCall(newAgg, false, newArgList, fieldType, callName);

    return newAggCall;
}
 
Example 24
Source Project: calcite   Source File: SqlSplittableAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
public AggregateCall merge(AggregateCall top, AggregateCall bottom) {
  if (top.getAggregation().getKind() == bottom.getAggregation().getKind()) {
    return AggregateCall.create(bottom.getAggregation(),
        bottom.isDistinct(), bottom.isApproximate(), false,
        bottom.getArgList(), bottom.filterArg, bottom.getCollation(),
        bottom.getType(), top.getName());
  } else {
    return null;
  }
}
 
Example 25
Source Project: Bats   Source File: RelMdColumnOrigins.java    License: Apache License 2.0 5 votes vote down vote up
public Set<RelColumnOrigin> getColumnOrigins(Aggregate rel,
    RelMetadataQuery mq, int iOutputColumn) {
  if (iOutputColumn < rel.getGroupCount()) {
    // Group columns pass through directly.
    return mq.getColumnOrigins(rel.getInput(), iOutputColumn);
  }

  if (rel.indicator) {
    if (iOutputColumn < rel.getGroupCount() + rel.getIndicatorCount()) {
      // The indicator column is originated here.
      return ImmutableSet.of();
    }
  }

  // Aggregate columns are derived from input columns
  AggregateCall call =
      rel.getAggCallList().get(iOutputColumn
              - rel.getGroupCount() - rel.getIndicatorCount());

  final Set<RelColumnOrigin> set = new HashSet<>();
  for (Integer iInput : call.getArgList()) {
    Set<RelColumnOrigin> inputSet =
        mq.getColumnOrigins(rel.getInput(), iInput);
    inputSet = createDerivedColumnOrigins(inputSet);
    if (inputSet != null) {
      set.addAll(inputSet);
    }
  }
  return set;
}
 
Example 26
Source Project: dremio-oss   Source File: RexToExpr.java    License: Apache License 2.0 5 votes vote down vote up
public static List<NamedExpression> aggsToExpr(
    RelDataType rowType, RelNode input, ImmutableBitSet groupSet, List<AggregateCall> aggCalls) {
  final List<String> fields = rowType.getFieldNames();
  final List<String> childFields = input.getRowType().getFieldNames();
  final List<NamedExpression> aggExprs = Lists.newArrayList();
  for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
    int aggExprOrdinal = groupSet.cardinality() + aggCall.i;
    FieldReference ref = FieldReference.getWithQuotedRef(fields.get(aggExprOrdinal));
    LogicalExpression expr = toExpr(aggCall.e, childFields);
    NamedExpression ne = new NamedExpression(expr, ref);
    aggExprs.add(ne);
  }
  return aggExprs;
}
 
Example 27
Source Project: calcite   Source File: SqlSplittableAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override public RexNode singleton(RexBuilder rexBuilder,
    RelDataType inputRowType, AggregateCall aggregateCall) {
  final int arg = aggregateCall.getArgList().get(0);
  final RelDataType type = inputRowType.getFieldList().get(arg).getType();
  final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
  final RelDataType type1 = typeFactory.getTypeSystem().deriveSumType(typeFactory, type);
  final RexNode inputRef = rexBuilder.makeInputRef(type1, arg);
  if (type.isNullable()) {
    return rexBuilder.makeCall(SqlStdOperatorTable.COALESCE, inputRef,
        rexBuilder.makeExactLiteral(BigDecimal.ZERO, type));
  } else {
    return inputRef;
  }
}
 
Example 28
/**
 * Converts an aggregate relational expression that contains just one
 * distinct aggregate function (or perhaps several over the same arguments)
 * and no non-distinct aggregate functions.
 */
private RelBuilder convertMonopole(RelBuilder relBuilder, Aggregate aggregate, List<Integer> argList,
        int filterArg) {
    // For example,
    // SELECT deptno, COUNT(DISTINCT sal), SUM(DISTINCT sal)
    // FROM emp
    // GROUP BY deptno
    //
    // becomes
    //
    // SELECT deptno, COUNT(distinct_sal), SUM(distinct_sal)
    // FROM (
    // SELECT DISTINCT deptno, sal AS distinct_sal
    // FROM EMP GROUP BY deptno)
    // GROUP BY deptno

    // Project the columns of the GROUP BY plus the arguments
    // to the agg function.
    final Map<Integer, Integer> sourceOf = new HashMap<>();
    createSelectDistinct(relBuilder, aggregate, argList, filterArg, sourceOf);

    // Create an aggregate on top, with the new aggregate list.
    final List<AggregateCall> newAggCalls = Lists.newArrayList(aggregate.getAggCallList());
    rewriteAggCalls(newAggCalls, argList, sourceOf);
    final int cardinality = aggregate.getGroupSet().cardinality();
    relBuilder.push(aggregate.copy(aggregate.getTraitSet(), relBuilder.build(), aggregate.indicator,
            ImmutableBitSet.range(cardinality), null, newAggCalls));
    return relBuilder;
}
 
Example 29
Source Project: flink   Source File: FlinkAggregateJoinTransposeRule.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert aggregate with AUXILIARY_GROUP to regular aggregate.
 * Return original aggregate and null project if the given aggregate does not contain AUXILIARY_GROUP,
 * else new aggregate without AUXILIARY_GROUP and a project to permute output columns if needed.
 */
private Pair<Aggregate, List<RexNode>> toRegularAggregate(Aggregate aggregate) {
	Tuple2<int[], Seq<AggregateCall>> auxGroupAndRegularAggCalls = AggregateUtil.checkAndSplitAggCalls(aggregate);
	final int[] auxGroup = auxGroupAndRegularAggCalls._1;
	final Seq<AggregateCall> regularAggCalls = auxGroupAndRegularAggCalls._2;
	if (auxGroup.length != 0) {
		int[] fullGroupSet = AggregateUtil.checkAndGetFullGroupSet(aggregate);
		ImmutableBitSet newGroupSet = ImmutableBitSet.of(fullGroupSet);
		List<AggregateCall> aggCalls = JavaConverters.seqAsJavaListConverter(regularAggCalls).asJava();
		final Aggregate newAgg = aggregate.copy(
				aggregate.getTraitSet(),
				aggregate.getInput(),
				aggregate.indicator,
				newGroupSet,
				com.google.common.collect.ImmutableList.of(newGroupSet),
				aggCalls);
		final List<RelDataTypeField> aggFields = aggregate.getRowType().getFieldList();
		final List<RexNode> projectAfterAgg = new ArrayList<>();
		for (int i = 0; i < fullGroupSet.length; ++i) {
			int group = fullGroupSet[i];
			int index = newGroupSet.indexOf(group);
			projectAfterAgg.add(new RexInputRef(index, aggFields.get(i).getType()));
		}
		int fieldCntOfAgg = aggFields.size();
		for (int i = fullGroupSet.length; i < fieldCntOfAgg; ++i) {
			projectAfterAgg.add(new RexInputRef(i, aggFields.get(i).getType()));
		}
		Preconditions.checkArgument(projectAfterAgg.size() == fieldCntOfAgg);
		return new Pair<>(newAgg, projectAfterAgg);
	} else {
		return new Pair<>(aggregate, null);
	}
}
 
Example 30
Source Project: Bats   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether any of the aggregates are calls to AVG, STDDEV_*, VAR_*.
 *
 * @param aggCallList List of aggregate calls
 */
private boolean containsAvgStddevVarCall(List<AggregateCall> aggCallList) {
  for (AggregateCall call : aggCallList) {
    if (isReducible(call.getAggregation().getKind())) {
      return true;
    }
  }
  return false;
}