org.apache.calcite.rel.logical.LogicalAggregate Java Examples

The following examples show how to use org.apache.calcite.rel.logical.LogicalAggregate. 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: 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 #2
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 #3
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 #4
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 #5
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void rewriteRel(LogicalAggregate rel) {
  RelNode oldInput = rel.getInput();
  RelDataType inputType = oldInput.getRowType();
  List<RelDataTypeField> inputTypeFields = inputType.getFieldList();
  if (SqlTypeUtil.isFlat(inputType) || rel.getAggCallList().stream().allMatch(
      call -> call.getArgList().isEmpty()
          || call.getArgList().stream().noneMatch(idx -> inputTypeFields.get(idx)
          .getType().isStruct()))) {
    rewriteGeneric(rel);
  } else {
    // one of aggregate calls definitely refers to field with struct type from oldInput,
    // let's restructure new input back and use restructured one as new input for aggregate node
    RelNode restructuredInput = tryRestructure(oldInput, getNewForOldRel(oldInput));
    // expected that after restructuring indexes in AggregateCalls again became relevant,
    // leave it as is but with new input
    RelNode newRel = rel.copy(rel.getTraitSet(), restructuredInput, rel.getGroupSet(),
        rel.getGroupSets(), rel.getAggCallList());
    if (!SqlTypeUtil.isFlat(rel.getRowType())) {
      newRel = coverNewRelByFlatteningProjection(rel, newRel);
    }
    setNewForOldRel(rel, newRel);
  }
}
 
Example #6
Source File: IncrementalUpdateUtils.java    From dremio-oss with 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 #7
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 #8
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 #9
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 #10
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 #11
Source File: OLAPAggregateRule.java    From kylin 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 #12
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalAggregate rel) {
    RelDataType inputType = rel.getInput().getRowType();
    for (RelDataTypeField field : inputType.getFieldList()) {
        if (field.getType().isStruct()) {
            // TODO jvs 10-Feb-2005
            throw Util.needToImplement("aggregation on structured types");
        }
    }

    rewriteGeneric(rel);
}
 
Example #13
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 #14
Source File: DrillAggregateRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final LogicalAggregate aggregate = call.rel(0);
  final RelNode input = call.rel(1);

  if (aggregate.containsDistinctCall()) {
    // currently, don't use this rule if any of the aggregates contains DISTINCT
    return;
  }

  final RelTraitSet traits = aggregate.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
  final RelNode convertedInput = convert(input, input.getTraitSet().plus(DrillRel.DRILL_LOGICAL).simplify());
  call.transformTo(new DrillAggregateRel(aggregate.getCluster(), traits, convertedInput, aggregate.indicator,
      aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList()));
}
 
Example #15
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
RemoveSingleAggregateRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(
          LogicalAggregate.class,
          operand(
              LogicalProject.class,
              operand(LogicalAggregate.class, any()))),
      relBuilderFactory, null);
}
 
Example #16
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalAggregate singleAggregate = call.rel(0);
  LogicalProject project = call.rel(1);
  LogicalAggregate aggregate = call.rel(2);

  // check singleAggRel is single_value agg
  if ((!singleAggregate.getGroupSet().isEmpty())
      || (singleAggregate.getAggCallList().size() != 1)
      || !(singleAggregate.getAggCallList().get(0).getAggregation()
      instanceof SqlSingleValueAggFunction)) {
    return;
  }

  // check projRel only projects one expression
  // check this project only projects one expression, i.e. scalar
  // sub-queries.
  List<RexNode> projExprs = project.getProjects();
  if (projExprs.size() != 1) {
    return;
  }

  // check the input to project is an aggregate on the entire input
  if (!aggregate.getGroupSet().isEmpty()) {
    return;
  }

  // singleAggRel produces a nullable type, so create the new
  // projection that casts proj expr to a nullable type.
  final RelBuilder relBuilder = call.builder();
  final RelDataType type =
      relBuilder.getTypeFactory()
          .createTypeWithNullability(projExprs.get(0).getType(), true);
  final RexNode cast =
      relBuilder.getRexBuilder().makeCast(type, projExprs.get(0));
  relBuilder.push(aggregate)
      .project(cast);
  call.transformTo(relBuilder.build());
}
 
Example #17
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
RemoveCorrelationForScalarProjectRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(LogicalCorrelate.class,
          operand(RelNode.class, any()),
          operand(LogicalAggregate.class,
              operand(LogicalProject.class,
                  operand(RelNode.class, any())))),
      relBuilderFactory, null);
}
 
Example #18
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
RemoveCorrelationForScalarAggregateRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(LogicalCorrelate.class,
          operand(RelNode.class, any()),
          operand(LogicalProject.class,
              operandJ(LogicalAggregate.class, null, Aggregate::isSimple,
                  operand(LogicalProject.class,
                      operand(RelNode.class, any()))))),
      relBuilderFactory, null);
}
 
Example #19
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
AdjustProjectForCountAggregateRule(boolean flavor,
    RelBuilderFactory relBuilderFactory) {
  super(
      flavor
          ? operand(LogicalCorrelate.class,
              operand(RelNode.class, any()),
                  operand(LogicalProject.class,
                      operand(LogicalAggregate.class, any())))
          : operand(LogicalCorrelate.class,
              operand(RelNode.class, any()),
                  operand(LogicalAggregate.class, any())),
      relBuilderFactory, null);
  this.flavor = flavor;
}
 
Example #20
Source File: FlinkAggregateExpandDistinctAggregatesRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FlinkAggregateExpandDistinctAggregatesRule(
		Class<? extends LogicalAggregate> clazz,
		boolean useGroupingSets,
		RelFactories.JoinFactory joinFactory) {
	this(clazz, useGroupingSets, RelBuilder.proto(Contexts.of(joinFactory)));
}
 
Example #21
Source File: StreamRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Aggregate aggregate = call.rel(1);
  final LogicalDelta newDelta =
      LogicalDelta.create(aggregate.getInput());
  final LogicalAggregate newAggregate =
      LogicalAggregate.create(newDelta, aggregate.getGroupSet(),
          aggregate.groupSets, aggregate.getAggCallList());
  call.transformTo(newAggregate);
}
 
Example #22
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 #23
Source File: FlinkAggregateExpandDistinctAggregatesRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FlinkAggregateExpandDistinctAggregatesRule(
		Class<? extends LogicalAggregate> clazz,
		boolean useGroupingSets,
		RelFactories.JoinFactory joinFactory) {
	this(clazz, useGroupingSets, RelBuilder.proto(Contexts.of(joinFactory)));
}
 
Example #24
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
	final SingleRel relNode = call.rel(0);
	return relNode instanceof LogicalWindowAggregate ||
		relNode instanceof LogicalAggregate ||
		relNode instanceof TableAggregate;
}
 
Example #25
Source File: CopyWithCluster.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalAggregate aggregate) {
  final RelNode input = aggregate.getInput().accept(this);
  return new LogicalAggregate(
    cluster,
    copyOf(aggregate.getTraitSet()),
    input,
    aggregate.indicator,
    aggregate.getGroupSet(),
    aggregate.getGroupSets(),
    copyOf(aggregate.getAggCallList())
  );
}
 
Example #26
Source File: AggregateExpandDistinctAggregatesRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateExpandDistinctAggregatesRule(
    Class<? extends LogicalAggregate> clazz,
    boolean useGroupingSets,
    RelFactories.JoinFactory joinFactory) {
  this(clazz, useGroupingSets, RelBuilder.proto(Contexts.of(joinFactory)));
}
 
Example #27
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 #28
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 #29
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** @deprecated Use {@link RelBuilder#distinct()}. */
@Deprecated // to be removed before 2.0
public static RelNode createDistinctRel(RelNode rel) {
  return LogicalAggregate.create(rel,
      ImmutableList.of(),
      ImmutableBitSet.range(rel.getRowType().getFieldCount()), null,
      ImmutableList.of());
}
 
Example #30
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonWriter} on
 * a simple tree of relational expressions, consisting of a table and a
 * project including window expressions.
 */
@Test void testWriter() {
  String s =
      Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> {
        rootSchema.add("hr",
            new ReflectiveSchema(new JdbcTest.HrSchema()));
        LogicalTableScan scan =
            LogicalTableScan.create(cluster,
                relOptSchema.getTableForMember(
                    Arrays.asList("hr", "emps")),
                ImmutableList.of());
        final RexBuilder rexBuilder = cluster.getRexBuilder();
        LogicalFilter filter =
            LogicalFilter.create(scan,
                rexBuilder.makeCall(
                    SqlStdOperatorTable.EQUALS,
                    rexBuilder.makeFieldAccess(
                        rexBuilder.makeRangeReference(scan),
                        "deptno", true),
                    rexBuilder.makeExactLiteral(BigDecimal.TEN)));
        final RelJsonWriter writer = new RelJsonWriter();
        final RelDataType bigIntType =
            cluster.getTypeFactory().createSqlType(SqlTypeName.BIGINT);
        LogicalAggregate aggregate =
            LogicalAggregate.create(filter,
                ImmutableList.of(),
                ImmutableBitSet.of(0),
                null,
                ImmutableList.of(
                    AggregateCall.create(SqlStdOperatorTable.COUNT,
                        true, false, false, ImmutableList.of(1), -1,
                        RelCollations.EMPTY, bigIntType, "c"),
                    AggregateCall.create(SqlStdOperatorTable.COUNT,
                        false, false, false, ImmutableList.of(), -1,
                        RelCollations.EMPTY, bigIntType, "d")));
        aggregate.explain(writer);
        return writer.asString();
      });
  assertThat(s, is(XX));
}