use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project calcite by apache.
the class SqlToRelConverter method convertSelectImpl.
/**
* Implementation of {@link #convertSelect(SqlSelect, boolean)};
* derived class may override.
*/
protected void convertSelectImpl(final Blackboard bb, SqlSelect select) {
convertFrom(bb, select.getFrom());
convertWhere(bb, select.getWhere());
final List<SqlNode> orderExprList = new ArrayList<>();
final List<RelFieldCollation> collationList = new ArrayList<>();
gatherOrderExprs(bb, select, select.getOrderList(), orderExprList, collationList);
final RelCollation collation = cluster.traitSet().canonize(RelCollations.of(collationList));
if (validator.isAggregate(select)) {
convertAgg(bb, select, orderExprList);
} else {
convertSelectList(bb, select, orderExprList);
}
if (select.isDistinct()) {
distinctify(bb, true);
}
convertOrder(select, bb, collation, orderExprList, select.getOffset(), select.getFetch());
bb.setRoot(bb.root, true);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project calcite by apache.
the class SqlToRelConverter method convertQuery.
/**
* Converts an unvalidated query's parse tree into a relational expression.
*
* @param query Query to convert
* @param needsValidation Whether to validate the query before converting;
* <code>false</code> if the query has already been
* validated.
* @param top Whether the query is top-level, say if its result
* will become a JDBC result set; <code>false</code> if
* the query will be part of a view.
*/
public RelRoot convertQuery(SqlNode query, final boolean needsValidation, final boolean top) {
if (needsValidation) {
query = validator.validate(query);
}
RelMetadataQuery.THREAD_PROVIDERS.set(JaninoRelMetadataProvider.of(cluster.getMetadataProvider()));
RelNode result = convertQueryRecursive(query, top, null).rel;
if (top) {
if (isStream(query)) {
result = new LogicalDelta(cluster, result.getTraitSet(), result);
}
}
RelCollation collation = RelCollations.EMPTY;
if (!query.isA(SqlKind.DML)) {
if (isOrdered(query)) {
collation = requiredCollation(result);
}
}
checkConvertedType(query, result);
if (SQL2REL_LOGGER.isDebugEnabled()) {
SQL2REL_LOGGER.debug(RelOptUtil.dumpPlan("Plan after converting SqlNode to RelNode", result, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES));
}
final RelDataType validatedRowType = validator.getValidatedNodeType(query);
return RelRoot.of(result, validatedRowType, query.getKind()).withCollation(collation);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project calcite by apache.
the class RelStructuredTypeFlattener method rewriteRel.
public void rewriteRel(Sort rel) {
RelCollation oldCollation = rel.getCollation();
final RelNode oldChild = rel.getInput();
final RelNode newChild = getNewForOldRel(oldChild);
final Mappings.TargetMapping mapping = getNewForOldInputMapping(oldChild);
// validate
for (RelFieldCollation field : oldCollation.getFieldCollations()) {
int oldInput = field.getFieldIndex();
RelDataType sortFieldType = oldChild.getRowType().getFieldList().get(oldInput).getType();
if (sortFieldType.isStruct()) {
// TODO jvs 10-Feb-2005
throw Util.needToImplement("sorting on structured types");
}
}
RelCollation newCollation = RexUtil.apply(mapping, oldCollation);
Sort newRel = LogicalSort.create(newChild, newCollation, rel.offset, rel.fetch);
setNewForOldRel(rel, newRel);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project calcite by apache.
the class RelDecorrelator method decorrelateRel.
/**
* Rewrite Sort.
*
* @param rel Sort to be rewritten
*/
public Frame decorrelateRel(Sort rel) {
// Sort itself should not reference corVars.
assert !cm.mapRefRelToCorRef.containsKey(rel);
// Sort only references field positions in collations field.
// The collations field in the newRel now need to refer to the
// new output positions in its input.
// Its output does not change the input ordering, so there's no
// need to call propagateExpr.
final RelNode oldInput = rel.getInput();
final Frame frame = getInvoke(oldInput, rel);
if (frame == null) {
// If input has not been rewritten, do not rewrite this rel.
return null;
}
final RelNode newInput = frame.r;
Mappings.TargetMapping mapping = Mappings.target(frame.oldToNewOutputs, oldInput.getRowType().getFieldCount(), newInput.getRowType().getFieldCount());
RelCollation oldCollation = rel.getCollation();
RelCollation newCollation = RexUtil.apply(mapping, oldCollation);
final Sort newSort = LogicalSort.create(newInput, newCollation, rel.offset, rel.fetch);
// Sort does not change input ordering
return register(rel, newSort, frame.oldToNewOutputs, frame.corDefOutputs);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project drill by apache.
the class MapRDBPushProjectIntoScan method doPushProjectIntoGroupScan.
protected void doPushProjectIntoGroupScan(RelOptRuleCall call, ProjectPrel project, ScanPrel scan, JsonTableGroupScan groupScan) {
try {
DrillRelOptUtil.ProjectPushInfo columnInfo = DrillRelOptUtil.getFieldsInformation(scan.getRowType(), project.getProjects());
if (columnInfo == null || Utilities.isStarQuery(columnInfo.getFields()) || !groupScan.canPushdownProjects(columnInfo.getFields())) {
return;
}
RelTraitSet newTraits = call.getPlanner().emptyTraitSet();
// Clear out collation trait
for (RelTrait trait : scan.getTraitSet()) {
if (!(trait instanceof RelCollation)) {
newTraits.plus(trait);
}
}
final ScanPrel newScan = new ScanPrel(scan.getCluster(), newTraits.plus(Prel.DRILL_PHYSICAL), groupScan.clone(columnInfo.getFields()), columnInfo.createNewRowType(project.getInput().getCluster().getTypeFactory()), scan.getTable());
List<RexNode> newProjects = Lists.newArrayList();
for (RexNode n : project.getChildExps()) {
newProjects.add(n.accept(columnInfo.getInputReWriter()));
}
final ProjectPrel newProj = new ProjectPrel(project.getCluster(), project.getTraitSet().plus(Prel.DRILL_PHYSICAL), newScan, newProjects, project.getRowType());
if (ProjectRemoveRule.isTrivial(newProj) && // the old project did not involve any column renaming
sameRowTypeProjectionsFields(project.getRowType(), newScan.getRowType())) {
call.transformTo(newScan);
} else {
call.transformTo(newProj);
}
} catch (Exception e) {
throw new DrillRuntimeException(e);
}
}
Aggregations