Java Code Examples for org.apache.calcite.rel.logical.LogicalAggregate#getAggCallList()

The following examples show how to use org.apache.calcite.rel.logical.LogicalAggregate#getAggCallList() . 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: MongoRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  final RelTraitSet traitSet =
      agg.getTraitSet().replace(out);
  try {
    return new MongoAggregate(
        rel.getCluster(),
        traitSet,
        convert(agg.getInput(), traitSet.simplify()),
        agg.getGroupSet(),
        agg.getGroupSets(),
        agg.getAggCallList());
  } catch (InvalidRelException e) {
    LOGGER.warn(e.toString());
    return null;
  }
}
 
Example 2
Source File: EnumerableSortedAggregateRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  if (!Aggregate.isSimple(agg)) {
    return null;
  }
  final RelTraitSet inputTraits = rel.getCluster()
      .traitSet().replace(EnumerableConvention.INSTANCE)
      .replace(
          RelCollations.of(
              ImmutableIntList.copyOf(
          agg.getGroupSet().asList())));
  final RelTraitSet selfTraits = inputTraits.replace(
      RelCollations.of(
      ImmutableIntList.identity(agg.getGroupSet().cardinality())));
  return new EnumerableSortedAggregate(
      rel.getCluster(),
      selfTraits,
      convert(agg.getInput(), inputTraits),
      agg.getGroupSet(),
      agg.getGroupSets(),
      agg.getAggCallList());
}
 
Example 3
Source File: OLAPAggregateRule.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode convert(RelNode rel) {
    LogicalAggregate agg = (LogicalAggregate) rel;

    // AVG() will be transformed into SUM()/COUNT() by AggregateReduceFunctionsRule.
    // Here only let the transformed plan pass.
    if (containsAvg(agg)) {
        return null;
    }

    RelTraitSet traitSet = agg.getTraitSet().replace(OLAPRel.CONVENTION);
    try {
        return new OLAPAggregateRel(agg.getCluster(), traitSet, convert(agg.getInput(), OLAPRel.CONVENTION), agg.indicator, agg.getGroupSet(), agg.getGroupSets(), agg.getAggCallList());
    } catch (InvalidRelException e) {
        throw new IllegalStateException("Can't create OLAPAggregateRel!", e);
    }
}
 
Example 4
Source File: EnumerableAggregateRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  final RelTraitSet traitSet = rel.getCluster()
      .traitSet().replace(EnumerableConvention.INSTANCE);
  try {
    return new EnumerableAggregate(
        rel.getCluster(),
        traitSet,
        convert(agg.getInput(), traitSet),
        agg.getGroupSet(),
        agg.getGroupSets(),
        agg.getAggCallList());
  } catch (InvalidRelException e) {
    EnumerableRules.LOGGER.debug(e.toString());
    return null;
  }
}
 
Example 5
Source File: ElasticsearchAggregateRule.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
public RelNode convert(RelNode relNode) {
    LogicalAggregate aggregate = (LogicalAggregate) relNode;
    RelTraitSet traitSet = aggregate.getTraitSet().replace(getOutTrait());
    for (AggregateCall call : aggregate.getAggCallList())
    {
        switch (call.getAggregation().getKind())
        {
            case MIN:
            case MAX:
            case COUNT:
            case SUM:
            case AVG:break;
            default:return null;//doesn't match. aggregate rule doesn't fire
        }
    }
    return new ElasticsearchAggregate(aggregate.getCluster(), traitSet,
            convert(aggregate.getInput(), getOutTrait()), aggregate.indicator,
            aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList());
}
 
Example 6
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
protected boolean hasNestedAggregations(LogicalAggregate rel) {
  List<AggregateCall> aggCallList = rel.getAggCallList();
  HashSet<Integer> aggregatesArgs = new HashSet<>();
  for (AggregateCall aggregateCall : aggCallList) {
    aggregatesArgs.addAll(aggregateCall.getArgList());
  }
  for (Integer aggregatesArg : aggregatesArgs) {
    SqlNode selectNode = ((SqlSelect) node).getSelectList().get(aggregatesArg);
    if (!(selectNode instanceof SqlBasicCall)) {
      continue;
    }
    for (SqlNode operand : ((SqlBasicCall) selectNode).getOperands()) {
      if (operand instanceof SqlCall) {
        final SqlOperator operator = ((SqlCall) operand).getOperator();
        if (operator instanceof SqlAggFunction) {
          return true;
        }
      }
    }
  }
  return false;
}
 
Example 7
Source File: SqlImplementor.java    From Bats with Apache License 2.0 6 votes vote down vote up
private boolean hasNestedAggregations(LogicalAggregate rel) {
    if (node instanceof SqlSelect) {
        final SqlNodeList selectList = ((SqlSelect) node).getSelectList();
        if (selectList != null) {
            final Set<Integer> aggregatesArgs = new HashSet<>();
            for (AggregateCall aggregateCall : rel.getAggCallList()) {
                aggregatesArgs.addAll(aggregateCall.getArgList());
            }
            for (int aggregatesArg : aggregatesArgs) {
                if (selectList.get(aggregatesArg) instanceof SqlBasicCall) {
                    final SqlBasicCall call = (SqlBasicCall) selectList.get(aggregatesArg);
                    for (SqlNode operand : call.getOperands()) {
                        if (operand instanceof SqlCall
                                && ((SqlCall) operand).getOperator() instanceof SqlAggFunction) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example 8
Source File: GeodeRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelNode convert(RelNode rel) {
  final LogicalAggregate aggregate = (LogicalAggregate) rel;
  final RelTraitSet traitSet = aggregate.getTraitSet().replace(out);
  return new GeodeAggregate(
      aggregate.getCluster(),
      traitSet,
      convert(aggregate.getInput(), traitSet.simplify()),
      aggregate.getGroupSet(),
      aggregate.getGroupSets(),
      aggregate.getAggCallList());
}
 
Example 9
Source File: OLAPAggregateRule.java    From kylin with Apache License 2.0 5 votes vote down vote up
private boolean containsAvg(LogicalAggregate agg) {
    for (AggregateCall call : agg.getAggCallList()) {
        SqlAggFunction func = call.getAggregation();
        if (func instanceof SqlAvgAggFunction)
            return true;
    }
    return false;
}
 
Example 10
Source File: SolrRules.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  final RelTraitSet traitSet = agg.getTraitSet().replace(out);
  return new SolrAggregate(
      rel.getCluster(),
      traitSet,
      convert(agg.getInput(), traitSet.simplify()),
      agg.indicator,
      agg.getGroupSet(),
      agg.getGroupSets(),
      agg.getAggCallList());
}
 
Example 11
Source File: PreProcessRel.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalAggregate aggregate) {
  for(AggregateCall aggregateCall : aggregate.getAggCallList()) {
    if(aggregateCall.getAggregation() instanceof SqlSingleValueAggFunction) {
      // see DRILL-1937
      unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION,
          "Dremio doesn't currently support non-scalar sub-queries used in an expression");
      throw new UnsupportedOperationException();
    }
  }
  return visitChild(aggregate, 0, aggregate.getInput());
}
 
Example 12
Source File: DistinctFinder.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public RelNode visit(LogicalAggregate aggregate) {
  List<AggregateCall> aggCallList = aggregate.getAggCallList();

  for (int i = 0; i < aggCallList.size(); i++) {
    if (aggCallList.get(i).isDistinct()) {
      foundDistinct = true;
      return aggregate;
    }
  }

  return visitChildren(aggregate);
}
 
Example 13
Source File: Bindables.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  final RelTraitSet traitSet =
      agg.getTraitSet().replace(BindableConvention.INSTANCE);
  try {
    return new BindableAggregate(rel.getCluster(), traitSet,
        convert(agg.getInput(), traitSet), false, agg.getGroupSet(),
        agg.getGroupSets(), agg.getAggCallList());
  } catch (InvalidRelException e) {
    RelOptPlanner.LOGGER.debug(e.toString());
    return null;
  }
}
 
Example 14
Source File: OLAPAggregateRule.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private boolean containsAvg(LogicalAggregate agg) {
    for (AggregateCall call : agg.getAggCallList()) {
        SqlAggFunction func = call.getAggregation();
        if (func instanceof SqlAvgAggFunction)
            return true;
    }
    return false;
}
 
Example 15
Source File: ElasticsearchRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  final RelTraitSet traitSet = agg.getTraitSet().replace(out);
  try {
    return new ElasticsearchAggregate(
        rel.getCluster(),
        traitSet,
        convert(agg.getInput(), traitSet.simplify()),
        agg.getGroupSet(),
        agg.getGroupSets(),
        agg.getAggCallList());
  } catch (InvalidRelException e) {
    return null;
  }
}
 
Example 16
Source File: PigRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  final RelTraitSet traitSet = agg.getTraitSet().replace(PigRel.CONVENTION);
  return new PigAggregate(agg.getCluster(), traitSet, agg.getInput(),
      agg.getGroupSet(), agg.getGroupSets(), agg.getAggCallList());
}
 
Example 17
Source File: ConvertCountDistinctToHll.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final LogicalAggregate agg = call.rel(0);
  final RelNode input = agg.getInput();

  boolean distinctReplaced = false;
  List<AggregateCall> calls = new ArrayList<>();

  RelMetadataQuery query = null;

  final Boolean[] memo = new Boolean[agg.getInput().getRowType().getFieldCount()];
  for (AggregateCall c : agg.getAggCallList()) {
    final boolean candidate = c.isDistinct() && c.getArgList().size() == 1 && "COUNT".equals(c.getAggregation().getName());

    if(!candidate) {
      calls.add(c);
      continue;
    }

    final int inputOrdinal = c.getArgList().get(0);
    boolean allowed = false;
    if(memo[inputOrdinal] != null) {
      allowed = memo[inputOrdinal];
    } else {
      if(query == null) {
        query = agg.getCluster().getMetadataQuery();
      }

      Set<RelColumnOrigin> origins = query.getColumnOrigins(input, inputOrdinal);

      // see if any column origin allowed a transformation.
      for(RelColumnOrigin o : origins) {
        RelOptTable table = o.getOriginTable();
        NamespaceTable namespaceTable = table.unwrap(NamespaceTable.class);
        if(namespaceTable == null) {
          // unable to decide, no way to transform.
          return;
        }

        if(namespaceTable.isApproximateStatsAllowed()) {
          allowed = true;
        }
      }

      memo[inputOrdinal] = allowed;

    }


    if(allowed) {
      calls.add(AggregateCall.create(HyperLogLog.NDV, false, c.getArgList(), -1, c.getType(), c.getName()));
      distinctReplaced = true;
    } else {
      calls.add(c);
    }

  }

  if(!distinctReplaced) {
    return;
  }

  final RelBuilder builder = relBuilderFactory.create(agg.getCluster(), null);
  builder.push(agg.getInput());
  builder.aggregate(builder.groupKey(agg.getGroupSet().toArray()), calls);
  call.transformTo(builder.build());
}
 
Example 18
Source File: RewriteNdvAsHll.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final LogicalAggregate agg = call.rel(0);
  final RelDataTypeFactory typeFactory = agg.getCluster().getTypeFactory();

  List<AggregateCall> calls = new ArrayList<>();
  Set<Integer> hllApplications = new HashSet<>();

  int i = agg.getGroupCount();
  for (AggregateCall c : agg.getAggCallList()) {

    final int location = i;
    i++;

    if(!"NDV".equals(c.getAggregation().getName())) {
      calls.add(c);
      continue;
    }

    hllApplications.add(location);
    calls.add(AggregateCall.create(HyperLogLog.HLL, false, c.getArgList(), -1, typeFactory.createSqlType(SqlTypeName.VARBINARY, HyperLogLog.HLL_VARBINARY_SIZE), c.getName()));
  }

  if(hllApplications.isEmpty()) {
    return;
  }

  final RelBuilder builder = relBuilderFactory.create(agg.getCluster(), null);
  builder.push(agg.getInput());
  builder.aggregate(builder.groupKey(agg.getGroupSet().toArray()), calls);

  // add the hll application project.
  final List<RexNode> nodes = new ArrayList<>();
  for(int field = 0; field < agg.getRowType().getFieldCount(); field++) {
    if(!hllApplications.contains(field)) {
      nodes.add(builder.field(field));
      continue;
    }

    nodes.add(builder.call(HyperLogLog.HLL_DECODE, builder.field(field)));
  }
  builder.project(nodes);
  call.transformTo(builder.build());
}
 
Example 19
Source File: RelDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
private void onMatch2(
    RelOptRuleCall call,
    LogicalCorrelate correlate,
    RelNode leftInput,
    LogicalProject aggOutputProject,
    LogicalAggregate aggregate) {
  if (generatedCorRels.contains(correlate)) {
    // This Correlate was generated by a previous invocation of
    // this rule. No further work to do.
    return;
  }

  setCurrent(call.getPlanner().getRoot(), correlate);

  // check for this pattern
  // The pattern matching could be simplified if rules can be applied
  // during decorrelation,
  //
  // CorrelateRel(left correlation, condition = true)
  //   leftInput
  //   Project-A (a RexNode)
  //     Aggregate (groupby (0), agg0(), agg1()...)

  // check aggOutputProj projects only one expression
  List<RexNode> aggOutputProjExprs = aggOutputProject.getProjects();
  if (aggOutputProjExprs.size() != 1) {
    return;
  }

  JoinRelType joinType = correlate.getJoinType();
  // corRel.getCondition was here, however Correlate was updated so it
  // never includes a join condition. The code was not modified for brevity.
  RexNode joinCond = relBuilder.literal(true);
  if ((joinType != JoinRelType.LEFT)
      || (joinCond != relBuilder.literal(true))) {
    return;
  }

  // check that the agg is on the entire input
  if (!aggregate.getGroupSet().isEmpty()) {
    return;
  }

  List<AggregateCall> aggCalls = aggregate.getAggCallList();
  Set<Integer> isCount = new HashSet<>();

  // remember the count() positions
  int i = -1;
  for (AggregateCall aggCall : aggCalls) {
    ++i;
    if (aggCall.getAggregation() instanceof SqlCountAggFunction) {
      isCount.add(i);
    }
  }

  // now rewrite the plan to
  //
  // Project-A' (all LHS plus transformed original projections,
  //             replacing references to count() with case statement)
  //   Correlate(left correlation, condition = true)
  //     leftInput
  //     Aggregate(groupby (0), agg0(), agg1()...)
  //
  LogicalCorrelate newCorrelate =
      LogicalCorrelate.create(leftInput, aggregate,
          correlate.getCorrelationId(), correlate.getRequiredColumns(),
          correlate.getJoinType());

  // remember this rel so we don't fire rule on it again
  // REVIEW jhyde 29-Oct-2007: rules should not save state; rule
  // should recognize patterns where it does or does not need to do
  // work
  generatedCorRels.add(newCorrelate);

  // need to update the mapCorToCorRel Update the output position
  // for the corVars: only pass on the corVars that are not used in
  // the join key.
  if (cm.mapCorToCorRel.get(correlate.getCorrelationId()) == correlate) {
    cm.mapCorToCorRel.put(correlate.getCorrelationId(), newCorrelate);
  }

  RelNode newOutput =
      aggregateCorrelatorOutput(newCorrelate, aggOutputProject, isCount);

  call.transformTo(newOutput);
}
 
Example 20
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void onMatch2(RelOptRuleCall call, LogicalCorrelate correlate, RelNode leftInput,
        LogicalProject aggOutputProject, LogicalAggregate aggregate) {
    if (generatedCorRels.contains(correlate)) {
        // This Correlate was generated by a previous invocation of
        // this rule. No further work to do.
        return;
    }

    setCurrent(call.getPlanner().getRoot(), correlate);

    // check for this pattern
    // The pattern matching could be simplified if rules can be applied
    // during decorrelation,
    //
    // CorrelateRel(left correlation, condition = true)
    // leftInput
    // Project-A (a RexNode)
    // Aggregate (groupby (0), agg0(), agg1()...)

    // check aggOutputProj projects only one expression
    List<RexNode> aggOutputProjExprs = aggOutputProject.getProjects();
    if (aggOutputProjExprs.size() != 1) {
        return;
    }

    JoinRelType joinType = correlate.getJoinType().toJoinType();
    // corRel.getCondition was here, however Correlate was updated so it
    // never includes a join condition. The code was not modified for brevity.
    RexNode joinCond = relBuilder.literal(true);
    if ((joinType != JoinRelType.LEFT) || (joinCond != relBuilder.literal(true))) {
        return;
    }

    // check that the agg is on the entire input
    if (!aggregate.getGroupSet().isEmpty()) {
        return;
    }

    List<AggregateCall> aggCalls = aggregate.getAggCallList();
    Set<Integer> isCount = new HashSet<>();

    // remember the count() positions
    int i = -1;
    for (AggregateCall aggCall : aggCalls) {
        ++i;
        if (aggCall.getAggregation() instanceof SqlCountAggFunction) {
            isCount.add(i);
        }
    }

    // now rewrite the plan to
    //
    // Project-A' (all LHS plus transformed original projections,
    // replacing references to count() with case statement)
    // Correlate(left correlation, condition = true)
    // leftInput
    // Aggregate(groupby (0), agg0(), agg1()...)
    //
    LogicalCorrelate newCorrelate = LogicalCorrelate.create(leftInput, aggregate, correlate.getCorrelationId(),
            correlate.getRequiredColumns(), correlate.getJoinType());

    // remember this rel so we don't fire rule on it again
    // REVIEW jhyde 29-Oct-2007: rules should not save state; rule
    // should recognize patterns where it does or does not need to do
    // work
    generatedCorRels.add(newCorrelate);

    // need to update the mapCorToCorRel Update the output position
    // for the corVars: only pass on the corVars that are not used in
    // the join key.
    if (cm.mapCorToCorRel.get(correlate.getCorrelationId()) == correlate) {
        cm.mapCorToCorRel.put(correlate.getCorrelationId(), newCorrelate);
    }

    RelNode newOutput = aggregateCorrelatorOutput(newCorrelate, aggOutputProject, isCount);

    call.transformTo(newOutput);
}