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();
}
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;
}
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);
}
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();
}
}
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);
}
Aggregations