Search in sources :

Example 11 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project calcite by apache.

the class FilterAggregateTransposeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final Filter filterRel = call.rel(0);
    final Aggregate aggRel = call.rel(1);
    final List<RexNode> conditions = RelOptUtil.conjunctions(filterRel.getCondition());
    final RexBuilder rexBuilder = filterRel.getCluster().getRexBuilder();
    final List<RelDataTypeField> origFields = aggRel.getRowType().getFieldList();
    final int[] adjustments = new int[origFields.size()];
    int j = 0;
    for (int i : aggRel.getGroupSet()) {
        adjustments[j] = i - j;
        j++;
    }
    final List<RexNode> pushedConditions = Lists.newArrayList();
    final List<RexNode> remainingConditions = Lists.newArrayList();
    for (RexNode condition : conditions) {
        ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(condition);
        if (canPush(aggRel, rCols)) {
            pushedConditions.add(condition.accept(new RelOptUtil.RexInputConverter(rexBuilder, origFields, aggRel.getInput(0).getRowType().getFieldList(), adjustments)));
        } else {
            remainingConditions.add(condition);
        }
    }
    final RelBuilder builder = call.builder();
    RelNode rel = builder.push(aggRel.getInput()).filter(pushedConditions).build();
    if (rel == aggRel.getInput(0)) {
        return;
    }
    rel = aggRel.copy(aggRel.getTraitSet(), ImmutableList.of(rel));
    rel = builder.push(rel).filter(remainingConditions).build();
    call.transformTo(rel);
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) RexBuilder(org.apache.calcite.rex.RexBuilder) Aggregate(org.apache.calcite.rel.core.Aggregate) RexNode(org.apache.calcite.rex.RexNode)

Example 12 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project calcite by apache.

the class RelToSqlConverter method visit.

/**
 * @see #dispatch
 */
public Result visit(Filter e) {
    final RelNode input = e.getInput();
    Result x = visitChild(0, input);
    parseCorrelTable(e, x);
    if (input instanceof Aggregate) {
        final Builder builder;
        if (((Aggregate) input).getInput() instanceof Project) {
            builder = x.builder(e);
            builder.clauses.add(Clause.HAVING);
        } else {
            builder = x.builder(e, Clause.HAVING);
        }
        builder.setHaving(builder.context.toSql(null, e.getCondition()));
        return builder.result();
    } else {
        final Builder builder = x.builder(e, Clause.WHERE);
        builder.setWhere(builder.context.toSql(null, e.getCondition()));
        return builder.result();
    }
}
Also used : Project(org.apache.calcite.rel.core.Project) RelNode(org.apache.calcite.rel.RelNode) Aggregate(org.apache.calcite.rel.core.Aggregate)

Example 13 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project calcite by apache.

the class RelOptRulesTest method testAggregateExtractProjectRuleWithFilter.

@Test
public void testAggregateExtractProjectRuleWithFilter() {
    final String sql = "select sum(sal) filter (where empno = 40)\n" + "from emp";
    HepProgram pre = new HepProgramBuilder().addRuleInstance(AggregateProjectMergeRule.INSTANCE).build();
    // AggregateProjectMergeRule does not merges Project with Filter.
    // Force match Aggregate on top of Project once explicitly in unit test.
    final AggregateExtractProjectRule rule = new AggregateExtractProjectRule(operand(Aggregate.class, operand(Project.class, null, new PredicateImpl<Project>() {

        int matchCount = 0;

        public boolean test(@Nullable Project project) {
            return matchCount++ == 0;
        }
    }, none())), RelFactories.LOGICAL_BUILDER);
    sql(sql).withPre(pre).withRule(rule).checkUnchanged();
}
Also used : Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) HepProgram(org.apache.calcite.plan.hep.HepProgram) AggregateExtractProjectRule(org.apache.calcite.rel.rules.AggregateExtractProjectRule) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Aggregate(org.apache.calcite.rel.core.Aggregate) Test(org.junit.Test)

Example 14 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project calcite by apache.

the class SemiJoinRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final Join join = call.rel(0);
    final RelNode left = call.rel(1);
    final Aggregate aggregate = call.rel(2);
    perform(call, null, join, left, aggregate);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) Join(org.apache.calcite.rel.core.Join) Aggregate(org.apache.calcite.rel.core.Aggregate)

Example 15 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project calcite by apache.

the class DruidQuery method deriveQuerySpec.

protected QuerySpec deriveQuerySpec() {
    final RelDataType rowType = table.getRowType();
    int i = 1;
    Filter filterRel = null;
    if (i < rels.size() && rels.get(i) instanceof Filter) {
        filterRel = (Filter) rels.get(i++);
    }
    Project project = null;
    if (i < rels.size() && rels.get(i) instanceof Project) {
        project = (Project) rels.get(i++);
    }
    ImmutableBitSet groupSet = null;
    List<AggregateCall> aggCalls = null;
    List<String> aggNames = null;
    if (i < rels.size() && rels.get(i) instanceof Aggregate) {
        final Aggregate aggregate = (Aggregate) rels.get(i++);
        groupSet = aggregate.getGroupSet();
        aggCalls = aggregate.getAggCallList();
        aggNames = Util.skip(aggregate.getRowType().getFieldNames(), groupSet.cardinality());
    }
    Filter havingFilter = null;
    if (i < rels.size() && rels.get(i) instanceof Filter) {
        havingFilter = (Filter) rels.get(i++);
    }
    Project postProject = null;
    if (i < rels.size() && rels.get(i) instanceof Project) {
        postProject = (Project) rels.get(i++);
    }
    List<Integer> collationIndexes = null;
    List<Direction> collationDirections = null;
    ImmutableBitSet.Builder numericCollationBitSetBuilder = ImmutableBitSet.builder();
    Integer fetch = null;
    if (i < rels.size() && rels.get(i) instanceof Sort) {
        final Sort sort = (Sort) rels.get(i++);
        collationIndexes = new ArrayList<>();
        collationDirections = new ArrayList<>();
        for (RelFieldCollation fCol : sort.collation.getFieldCollations()) {
            collationIndexes.add(fCol.getFieldIndex());
            collationDirections.add(fCol.getDirection());
            if (sort.getRowType().getFieldList().get(fCol.getFieldIndex()).getType().getFamily() == SqlTypeFamily.NUMERIC) {
                numericCollationBitSetBuilder.set(fCol.getFieldIndex());
            }
        }
        fetch = sort.fetch != null ? RexLiteral.intValue(sort.fetch) : null;
    }
    if (i != rels.size()) {
        throw new AssertionError("could not implement all rels");
    }
    return getQuery(rowType, filterRel, project, groupSet, aggCalls, aggNames, collationIndexes, collationDirections, numericCollationBitSetBuilder.build(), fetch, postProject, havingFilter);
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataType(org.apache.calcite.rel.type.RelDataType) Direction(org.apache.calcite.rel.RelFieldCollation.Direction) AggregateCall(org.apache.calcite.rel.core.AggregateCall) Project(org.apache.calcite.rel.core.Project) Filter(org.apache.calcite.rel.core.Filter) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Sort(org.apache.calcite.rel.core.Sort) Aggregate(org.apache.calcite.rel.core.Aggregate)

Aggregations

Aggregate (org.apache.calcite.rel.core.Aggregate)75 RelNode (org.apache.calcite.rel.RelNode)44 ArrayList (java.util.ArrayList)39 AggregateCall (org.apache.calcite.rel.core.AggregateCall)37 RexNode (org.apache.calcite.rex.RexNode)32 RelBuilder (org.apache.calcite.tools.RelBuilder)27 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)27 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)23 RexBuilder (org.apache.calcite.rex.RexBuilder)22 Project (org.apache.calcite.rel.core.Project)21 HashMap (java.util.HashMap)17 RexInputRef (org.apache.calcite.rex.RexInputRef)15 Join (org.apache.calcite.rel.core.Join)14 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)14 RelDataType (org.apache.calcite.rel.type.RelDataType)14 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)14 Filter (org.apache.calcite.rel.core.Filter)13 HiveAggregate (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate)13 List (java.util.List)12 ImmutableList (com.google.common.collect.ImmutableList)11