Search in sources :

Example 81 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.

the class ProjectWindowTransposeRule method findReference.

private ImmutableBitSet findReference(final LogicalProject project, final LogicalWindow window) {
    final int windowInputColumn = window.getInput().getRowType().getFieldCount();
    final ImmutableBitSet.Builder beReferred = ImmutableBitSet.builder();
    final RexShuttle referenceFinder = new RexShuttle() {

        @Override
        public RexNode visitInputRef(RexInputRef inputRef) {
            final int index = inputRef.getIndex();
            if (index < windowInputColumn) {
                beReferred.set(index);
            }
            return inputRef;
        }
    };
    // Reference in LogicalProject
    for (RexNode rexNode : project.getChildExps()) {
        rexNode.accept(referenceFinder);
    }
    // Reference in LogicalWindow
    for (Window.Group group : window.groups) {
        // Reference in Partition-By
        for (int index : group.keys) {
            if (index < windowInputColumn) {
                beReferred.set(index);
            }
        }
        // Reference in Order-By
        for (RelFieldCollation relFieldCollation : group.orderKeys.getFieldCollations()) {
            if (relFieldCollation.getFieldIndex() < windowInputColumn) {
                beReferred.set(relFieldCollation.getFieldIndex());
            }
        }
        // Reference in Window Functions
        for (Window.RexWinAggCall rexWinAggCall : group.aggCalls) {
            rexWinAggCall.accept(referenceFinder);
        }
    }
    return beReferred.build();
}
Also used : Window(org.apache.calcite.rel.core.Window) LogicalWindow(org.apache.calcite.rel.logical.LogicalWindow) RexShuttle(org.apache.calcite.rex.RexShuttle) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 82 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.

the class SortJoinTransposeRule method matches.

// ~ Methods ----------------------------------------------------------------
@Override
public boolean matches(RelOptRuleCall call) {
    final Sort sort = call.rel(0);
    final Join join = call.rel(1);
    final RelMetadataQuery mq = call.getMetadataQuery();
    final JoinInfo joinInfo = JoinInfo.of(join.getLeft(), join.getRight(), join.getCondition());
    // condition, we bail out
    if (join.getJoinType() == JoinRelType.LEFT) {
        if (sort.getCollation() != RelCollations.EMPTY) {
            for (RelFieldCollation relFieldCollation : sort.getCollation().getFieldCollations()) {
                if (relFieldCollation.getFieldIndex() >= join.getLeft().getRowType().getFieldCount()) {
                    return false;
                }
            }
        }
        if (sort.offset != null && !RelMdUtil.areColumnsDefinitelyUnique(mq, join.getRight(), joinInfo.rightSet())) {
            return false;
        }
    } else if (join.getJoinType() == JoinRelType.RIGHT) {
        if (sort.getCollation() != RelCollations.EMPTY) {
            for (RelFieldCollation relFieldCollation : sort.getCollation().getFieldCollations()) {
                if (relFieldCollation.getFieldIndex() < join.getLeft().getRowType().getFieldCount()) {
                    return false;
                }
            }
        }
        if (sort.offset != null && !RelMdUtil.areColumnsDefinitelyUnique(mq, join.getLeft(), joinInfo.leftSet())) {
            return false;
        }
    } else {
        return false;
    }
    return true;
}
Also used : JoinInfo(org.apache.calcite.rel.core.JoinInfo) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) Join(org.apache.calcite.rel.core.Join) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin)

Example 83 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation 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 84 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.

the class RexUtil method apply.

/**
 * Applies a mapping to a collation list.
 *
 * @param mapping       Mapping
 * @param collationList Collation list
 * @return collation list with mapping applied to each field
 */
public static List<RelCollation> apply(Mappings.TargetMapping mapping, List<RelCollation> collationList) {
    final List<RelCollation> newCollationList = new ArrayList<>();
    for (RelCollation collation : collationList) {
        final List<RelFieldCollation> newFieldCollationList = new ArrayList<>();
        for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
            final RelFieldCollation newFieldCollation = apply(mapping, fieldCollation);
            if (newFieldCollation == null) {
                // if it's empty).
                break;
            }
            newFieldCollationList.add(newFieldCollation);
        }
        // and duplicate collations. Ignore these.
        if (!newFieldCollationList.isEmpty()) {
            final RelCollation newCollation = RelCollations.of(newFieldCollationList);
            if (!newCollationList.contains(newCollation)) {
                newCollationList.add(newCollation);
            }
        }
    }
    return newCollationList;
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 85 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.

the class GeodeSort method implement.

@Override
public void implement(GeodeImplementContext geodeImplementContext) {
    ((GeodeRel) getInput()).implement(geodeImplementContext);
    List<RelFieldCollation> sortCollations = collation.getFieldCollations();
    if (!sortCollations.isEmpty()) {
        List<String> orderByFields = new ArrayList<String>();
        for (RelFieldCollation fieldCollation : sortCollations) {
            final String name = fieldName(fieldCollation.getFieldIndex());
            orderByFields.add(name + " " + direction(fieldCollation.getDirection()));
        }
        geodeImplementContext.addOrderByFields(orderByFields);
    }
    if (fetch != null) {
        geodeImplementContext.setLimit(((RexLiteral) fetch).getValue().toString());
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) ArrayList(java.util.ArrayList)

Aggregations

RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)101 ArrayList (java.util.ArrayList)36 RexNode (org.apache.calcite.rex.RexNode)36 RelCollation (org.apache.calcite.rel.RelCollation)33 RelNode (org.apache.calcite.rel.RelNode)28 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)17 RexInputRef (org.apache.calcite.rex.RexInputRef)17 RelDataType (org.apache.calcite.rel.type.RelDataType)14 ImmutableList (com.google.common.collect.ImmutableList)13 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)13 Sort (org.apache.calcite.rel.core.Sort)12 RelTraitSet (org.apache.calcite.plan.RelTraitSet)11 RexCall (org.apache.calcite.rex.RexCall)11 RexLiteral (org.apache.calcite.rex.RexLiteral)11 Project (org.apache.calcite.rel.core.Project)9 FieldReference (org.apache.drill.common.expression.FieldReference)9 HashMap (java.util.HashMap)8 RelOptCluster (org.apache.calcite.plan.RelOptCluster)8 Map (java.util.Map)7 AggregateCall (org.apache.calcite.rel.core.AggregateCall)7