Search in sources :

Example 71 with RelCollation

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);
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) SqlNode(org.apache.calcite.sql.SqlNode)

Example 72 with RelCollation

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);
}
Also used : LogicalDelta(org.apache.calcite.rel.stream.LogicalDelta) RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 73 with RelCollation

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);
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) Mappings(org.apache.calcite.util.mapping.Mappings) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 74 with RelCollation

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);
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) Mappings(org.apache.calcite.util.mapping.Mappings) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort)

Example 75 with RelCollation

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);
    }
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) RelCollation(org.apache.calcite.rel.RelCollation) ScanPrel(org.apache.drill.exec.planner.physical.ScanPrel) RelTrait(org.apache.calcite.plan.RelTrait) DrillRelOptUtil(org.apache.drill.exec.planner.common.DrillRelOptUtil) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelCollation (org.apache.calcite.rel.RelCollation)83 RelNode (org.apache.calcite.rel.RelNode)40 RelTraitSet (org.apache.calcite.plan.RelTraitSet)37 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)33 RexNode (org.apache.calcite.rex.RexNode)24 ArrayList (java.util.ArrayList)19 RelDataType (org.apache.calcite.rel.type.RelDataType)17 RelOptCluster (org.apache.calcite.plan.RelOptCluster)15 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)13 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)12 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)12 List (java.util.List)11 Sort (org.apache.calcite.rel.core.Sort)11 ImmutableList (com.google.common.collect.ImmutableList)8 RelDistribution (org.apache.calcite.rel.RelDistribution)8 RexInputRef (org.apache.calcite.rex.RexInputRef)8 Map (java.util.Map)7 Mappings (org.apache.calcite.util.mapping.Mappings)7 Supplier (com.google.common.base.Supplier)6 HashMap (java.util.HashMap)6