Java Code Examples for org.apache.calcite.rel.core.Aggregate#getIndicatorCount()

The following examples show how to use org.apache.calcite.rel.core.Aggregate#getIndicatorCount() . 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 File: RelMdColumnOrigins.java    From Bats with 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 2
Source File: AggregateReduceFunctionsRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Reduces all calls to AVG, STDDEV_POP, STDDEV_SAMP, VAR_POP, VAR_SAMP in
 * the aggregates list to.
 *
 * <p>It handles newly generated common subexpressions since this was done
 * at the sql2rel stage.
 */
private void reduceAggs(
    RelOptRuleCall ruleCall,
    Aggregate oldAggRel) {
  RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();

  List<AggregateCall> oldCalls = oldAggRel.getAggCallList();
  final int groupCount = oldAggRel.getGroupCount();
  final int indicatorCount = oldAggRel.getIndicatorCount();

  final List<AggregateCall> newCalls = new ArrayList<>();
  final Map<AggregateCall, RexNode> aggCallMapping = new HashMap<>();

  final List<RexNode> projList = new ArrayList<>();

  // pass through group key (+ indicators if present)
  for (int i = 0; i < groupCount + indicatorCount; ++i) {
    projList.add(
        rexBuilder.makeInputRef(
            getFieldType(oldAggRel, i),
            i));
  }

  // List of input expressions. If a particular aggregate needs more, it
  // will add an expression to the end, and we will create an extra
  // project.
  final RelBuilder relBuilder = ruleCall.builder();
  relBuilder.push(oldAggRel.getInput());
  final List<RexNode> inputExprs = new ArrayList<>(relBuilder.fields());

  // create new agg function calls and rest of project list together
  for (AggregateCall oldCall : oldCalls) {
    projList.add(
        reduceAgg(
            oldAggRel, oldCall, newCalls, aggCallMapping, inputExprs));
  }

  final int extraArgCount =
      inputExprs.size() - relBuilder.peek().getRowType().getFieldCount();
  if (extraArgCount > 0) {
    relBuilder.project(inputExprs,
        CompositeList.of(
            relBuilder.peek().getRowType().getFieldNames(),
            Collections.nCopies(extraArgCount, null)));
  }
  newAggregateRel(relBuilder, oldAggRel, newCalls);
  newCalcRel(relBuilder, oldAggRel.getRowType(), projList);
  ruleCall.transformTo(relBuilder.build());
}