Search in sources :

Example 51 with Aggregate

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

the class DruidQuery method signature.

/**
 * Returns a string describing the operations inside this query.
 *
 * <p>For example, "sfpahol" means {@link TableScan} (s)
 * followed by {@link Filter} (f)
 * followed by {@link Project} (p)
 * followed by {@link Aggregate} (a)
 * followed by {@link Filter} (h)
 * followed by {@link Project} (o)
 * followed by {@link Sort} (l).
 *
 * @see #isValidSignature(String)
 */
String signature() {
    final StringBuilder b = new StringBuilder();
    boolean flag = false;
    for (RelNode rel : rels) {
        b.append(rel instanceof TableScan ? 's' : (rel instanceof Project && flag) ? 'o' : (rel instanceof Filter && flag) ? 'h' : rel instanceof Aggregate ? 'a' : rel instanceof Filter ? 'f' : rel instanceof Sort ? 'l' : rel instanceof Project ? 'p' : '!');
        flag = flag || rel instanceof Aggregate;
    }
    return b.toString();
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) Project(org.apache.calcite.rel.core.Project) AbstractRelNode(org.apache.calcite.rel.AbstractRelNode) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) Sort(org.apache.calcite.rel.core.Sort) Aggregate(org.apache.calcite.rel.core.Aggregate)

Example 52 with Aggregate

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

the class DruidQuery method explainTerms.

@Override
public RelWriter explainTerms(RelWriter pw) {
    for (RelNode rel : rels) {
        if (rel instanceof TableScan) {
            TableScan tableScan = (TableScan) rel;
            pw.item("table", tableScan.getTable().getQualifiedName());
            pw.item("intervals", intervals);
        } else if (rel instanceof Filter) {
            pw.item("filter", ((Filter) rel).getCondition());
        } else if (rel instanceof Project) {
            if (((Project) rel).getInput() instanceof Aggregate) {
                pw.item("post_projects", ((Project) rel).getProjects());
            } else {
                pw.item("projects", ((Project) rel).getProjects());
            }
        } else if (rel instanceof Aggregate) {
            final Aggregate aggregate = (Aggregate) rel;
            pw.item("groups", aggregate.getGroupSet()).item("aggs", aggregate.getAggCallList());
        } else if (rel instanceof Sort) {
            final Sort sort = (Sort) rel;
            for (Ord<RelFieldCollation> ord : Ord.zip(sort.collation.getFieldCollations())) {
                pw.item("sort" + ord.i, ord.e.getFieldIndex());
            }
            for (Ord<RelFieldCollation> ord : Ord.zip(sort.collation.getFieldCollations())) {
                pw.item("dir" + ord.i, ord.e.shortString());
            }
            pw.itemIf("fetch", sort.fetch, sort.fetch != null);
        } else {
            throw new AssertionError("rel type not supported in Druid query " + rel);
        }
    }
    return pw;
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) Project(org.apache.calcite.rel.core.Project) AbstractRelNode(org.apache.calcite.rel.AbstractRelNode) RelNode(org.apache.calcite.rel.RelNode) 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)

Example 53 with Aggregate

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

the class PluginConverterRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    RelNode rel = call.rel(0);
    boolean canImplement = false;
    // cannot use visitor pattern here, since RelShuttle supports only logical rel implementations
    if (rel instanceof Aggregate) {
        canImplement = pluginImplementor.canImplement(((Aggregate) rel));
    } else if (rel instanceof Filter) {
        canImplement = pluginImplementor.canImplement(((Filter) rel));
    } else if (rel instanceof DrillLimitRelBase) {
        canImplement = pluginImplementor.canImplement(((DrillLimitRelBase) rel));
    } else if (rel instanceof Project) {
        canImplement = pluginImplementor.canImplement(((Project) rel));
    } else if (rel instanceof Sort) {
        canImplement = pluginImplementor.canImplement(((Sort) rel));
    } else if (rel instanceof Union) {
        canImplement = pluginImplementor.canImplement(((Union) rel));
    } else if (rel instanceof Join) {
        canImplement = pluginImplementor.canImplement(((Join) rel));
    }
    return canImplement && super.matches(call);
}
Also used : Project(org.apache.calcite.rel.core.Project) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) DrillLimitRelBase(org.apache.drill.exec.planner.common.DrillLimitRelBase) Sort(org.apache.calcite.rel.core.Sort) Join(org.apache.calcite.rel.core.Join) Aggregate(org.apache.calcite.rel.core.Aggregate) Union(org.apache.calcite.rel.core.Union)

Example 54 with Aggregate

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

the class PhoenixImplementor method visit.

@Override
public Result visit(Filter e) {
    final RelNode input = e.getInput();
    Result x = visitChild(0, input);
    parseCorrelTable(e, x);
    if (input instanceof Aggregate) {
        return super.visit(e);
    } else {
        final Builder builder = x.builder(e, Clause.WHERE);
        builder.setWhere(builder.context.toSql(null, e.getCondition()));
        final List<SqlNode> selectList = new ArrayList<>();
        e.getRowType().getFieldNames().forEach(fieldName -> {
            /*
         * phoenix does not support the wildcard in the subqueries,
         * expand the wildcard at this time.
         */
            addSelect(selectList, new SqlIdentifier(fieldName, POS), e.getRowType());
        });
        builder.setSelect(new SqlNodeList(selectList, POS));
        return builder.result();
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) Aggregate(org.apache.calcite.rel.core.Aggregate) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 55 with Aggregate

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

the class ConvertCountToDirectScanRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final Aggregate agg = call.rel(0);
    final TableScan scan = call.rel(call.rels.length - 1);
    final Project project = call.rels.length == 3 ? (Project) call.rel(1) : null;
    // 3) Additional checks are done further below ..
    if (agg.getGroupCount() > 0 || agg.containsDistinctCall()) {
        return;
    }
    DrillTable drillTable = DrillRelOptUtil.getDrillTable(scan);
    if (drillTable == null) {
        logger.debug("Rule does not apply since an eligible drill table instance was not found.");
        return;
    }
    Object selection = drillTable.getSelection();
    if (!(selection instanceof FormatSelection)) {
        logger.debug("Rule does not apply since only Parquet file format is eligible.");
        return;
    }
    PlannerSettings settings = call.getPlanner().getContext().unwrap(PlannerSettings.class);
    // Rule is applicable only if the statistics for row count and null count are available from the metadata,
    FormatSelection formatSelection = (FormatSelection) selection;
    // Rule cannot be applied if the selection had wildcard since the totalrowcount cannot be read from the parent directory
    if (formatSelection.getSelection().hadWildcard()) {
        logger.debug("Rule does not apply when there is a wild card since the COUNT could not be determined from metadata.");
        return;
    }
    Pair<Boolean, Metadata_V4.MetadataSummary> status = checkMetadataForScanStats(settings, drillTable, formatSelection);
    if (!status.getLeft()) {
        logger.debug("Rule does not apply since MetadataSummary metadata was not found.");
        return;
    }
    Metadata_V4.MetadataSummary metadataSummary = status.getRight();
    Map<String, Long> result = collectCounts(settings, metadataSummary, agg, scan, project);
    logger.trace("Calculated the following aggregate counts: {}", result);
    // if counts could not be determined, rule won't be applied
    if (result.isEmpty()) {
        logger.debug("Rule does not apply since one or more COUNTs could not be determined from metadata.");
        return;
    }
    Path summaryFileName = Metadata.getSummaryFileName(formatSelection.getSelection().getSelectionRoot());
    final RelDataType scanRowType = CountToDirectScanUtils.constructDataType(agg, result.keySet());
    final DynamicPojoRecordReader<Long> reader = new DynamicPojoRecordReader<>(CountToDirectScanUtils.buildSchema(scanRowType.getFieldNames()), Collections.singletonList(new ArrayList<>(result.values())));
    final ScanStats scanStats = new ScanStats(ScanStats.GroupScanProperty.EXACT_ROW_COUNT, 1, 1, scanRowType.getFieldCount());
    final MetadataDirectGroupScan directScan = new MetadataDirectGroupScan(reader, summaryFileName, 1, scanStats, true, false);
    final DrillDirectScanRel newScan = new DrillDirectScanRel(scan.getCluster(), scan.getTraitSet().plus(DrillRel.DRILL_LOGICAL), directScan, scanRowType);
    final DrillProjectRel newProject = new DrillProjectRel(agg.getCluster(), agg.getTraitSet().plus(DrillRel.DRILL_LOGICAL), newScan, CountToDirectScanUtils.prepareFieldExpressions(scanRowType), agg.getRowType());
    call.transformTo(newProject);
}
Also used : Path(org.apache.hadoop.fs.Path) SchemaPath(org.apache.drill.common.expression.SchemaPath) TableScan(org.apache.calcite.rel.core.TableScan) PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) DynamicPojoRecordReader(org.apache.drill.exec.store.pojo.DynamicPojoRecordReader) ArrayList(java.util.ArrayList) FormatSelection(org.apache.drill.exec.store.dfs.FormatSelection) RelDataType(org.apache.calcite.rel.type.RelDataType) Project(org.apache.calcite.rel.core.Project) MetadataDirectGroupScan(org.apache.drill.exec.store.direct.MetadataDirectGroupScan) Metadata_V4(org.apache.drill.exec.store.parquet.metadata.Metadata_V4) Aggregate(org.apache.calcite.rel.core.Aggregate) ScanStats(org.apache.drill.exec.physical.base.ScanStats)

Aggregations

Aggregate (org.apache.calcite.rel.core.Aggregate)63 RelNode (org.apache.calcite.rel.RelNode)34 AggregateCall (org.apache.calcite.rel.core.AggregateCall)30 ArrayList (java.util.ArrayList)29 RexNode (org.apache.calcite.rex.RexNode)25 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)23 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)20 RelBuilder (org.apache.calcite.tools.RelBuilder)19 RexBuilder (org.apache.calcite.rex.RexBuilder)15 HashMap (java.util.HashMap)14 Project (org.apache.calcite.rel.core.Project)14 RexInputRef (org.apache.calcite.rex.RexInputRef)13 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)12 Filter (org.apache.calcite.rel.core.Filter)11 RelDataType (org.apache.calcite.rel.type.RelDataType)11 Join (org.apache.calcite.rel.core.Join)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 HiveAggregate (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate)9 List (java.util.List)8 ImmutableList (com.google.common.collect.ImmutableList)7