Search in sources :

Example 1 with RelFieldCollation

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

the class HiveSortLimitPullUpConstantsRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final RelNode parent = call.rel(0);
    final Sort sort = call.rel(1);
    final int count = sort.getInput().getRowType().getFieldCount();
    if (count == 1) {
        // Project operator.
        return;
    }
    final RexBuilder rexBuilder = sort.getCluster().getRexBuilder();
    final RelMetadataQuery mq = call.getMetadataQuery();
    final RelOptPredicateList predicates = mq.getPulledUpPredicates(sort.getInput());
    if (predicates == null) {
        return;
    }
    Map<RexNode, RexNode> conditionsExtracted = HiveReduceExpressionsRule.predicateConstants(RexNode.class, rexBuilder, predicates);
    Map<RexNode, RexNode> constants = new HashMap<>();
    for (int i = 0; i < count; i++) {
        RexNode expr = rexBuilder.makeInputRef(sort.getInput(), i);
        if (conditionsExtracted.containsKey(expr)) {
            constants.put(expr, conditionsExtracted.get(expr));
        }
    }
    // None of the expressions are constant. Nothing to do.
    if (constants.isEmpty()) {
        return;
    }
    if (count == constants.size()) {
        // At least a single item in project is required.
        constants.remove(constants.keySet().iterator().next());
    }
    // Create expressions for Project operators before and after the Sort
    List<RelDataTypeField> fields = sort.getInput().getRowType().getFieldList();
    List<Pair<RexNode, String>> newChildExprs = new ArrayList<>();
    List<RexNode> topChildExprs = new ArrayList<>();
    List<String> topChildExprsFields = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        RexNode expr = rexBuilder.makeInputRef(sort.getInput(), i);
        RelDataTypeField field = fields.get(i);
        if (constants.containsKey(expr)) {
            topChildExprs.add(constants.get(expr));
            topChildExprsFields.add(field.getName());
        } else {
            newChildExprs.add(Pair.<RexNode, String>of(expr, field.getName()));
            topChildExprs.add(expr);
            topChildExprsFields.add(field.getName());
        }
    }
    // Update field collations
    final Mappings.TargetMapping mapping = RelOptUtil.permutation(Pair.left(newChildExprs), sort.getInput().getRowType()).inverse();
    List<RelFieldCollation> fieldCollations = new ArrayList<>();
    for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) {
        final int target = mapping.getTargetOpt(fc.getFieldIndex());
        if (target < 0) {
            // It is a constant, we can ignore it
            continue;
        }
        fieldCollations.add(fc.copy(target));
    }
    // Update top Project positions
    topChildExprs = ImmutableList.copyOf(RexUtil.apply(mapping, topChildExprs));
    // Create new Project-Sort-Project sequence
    final RelBuilder relBuilder = call.builder();
    relBuilder.push(sort.getInput());
    relBuilder.project(Pair.left(newChildExprs), Pair.right(newChildExprs));
    final ImmutableList<RexNode> sortFields = relBuilder.fields(RelCollations.of(fieldCollations));
    relBuilder.sortLimit(sort.offset == null ? -1 : RexLiteral.intValue(sort.offset), sort.fetch == null ? -1 : RexLiteral.intValue(sort.fetch), sortFields);
    // Create top Project fixing nullability of fields
    relBuilder.project(topChildExprs, topChildExprsFields);
    relBuilder.convert(sort.getRowType(), false);
    List<RelNode> inputs = new ArrayList<>();
    for (RelNode child : parent.getInputs()) {
        if (!((HepRelVertex) child).getCurrentRel().equals(sort)) {
            inputs.add(child);
        } else {
            inputs.add(relBuilder.build());
        }
    }
    call.transformTo(parent.copy(parent.getTraitSet(), inputs));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) Sort(org.apache.calcite.rel.core.Sort) RexBuilder(org.apache.calcite.rex.RexBuilder) Pair(org.apache.calcite.util.Pair) RelBuilder(org.apache.calcite.tools.RelBuilder) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) Mappings(org.apache.calcite.util.mapping.Mappings) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with RelFieldCollation

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

the class CassandraSchema method getClusteringOrder.

/**
 * Get the collation of all clustering key columns.
 *
 * @return A RelCollations representing the collation of all clustering keys
 */
public List<RelFieldCollation> getClusteringOrder(String columnFamily, boolean view) {
    AbstractTableMetadata table;
    if (view) {
        table = getKeyspace().getMaterializedView(columnFamily);
    } else {
        table = getKeyspace().getTable(columnFamily);
    }
    List<ClusteringOrder> clusteringOrder = table.getClusteringOrder();
    List<RelFieldCollation> keyCollations = new ArrayList<RelFieldCollation>();
    int i = 0;
    for (ClusteringOrder order : clusteringOrder) {
        RelFieldCollation.Direction direction;
        switch(order) {
            case DESC:
                direction = RelFieldCollation.Direction.DESCENDING;
                break;
            case ASC:
            default:
                direction = RelFieldCollation.Direction.ASCENDING;
                break;
        }
        keyCollations.add(new RelFieldCollation(i, direction));
        i++;
    }
    return keyCollations;
}
Also used : AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) ArrayList(java.util.ArrayList) ClusteringOrder(com.datastax.driver.core.ClusteringOrder)

Example 3 with RelFieldCollation

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

the class ElasticsearchSort method implement.

@Override
public void implement(Implementor implementor) {
    implementor.visitChild(0, getInput());
    if (!collation.getFieldCollations().isEmpty()) {
        final List<String> keys = new ArrayList<>();
        if (input instanceof Project) {
            final List<RexNode> projects = ((Project) input).getProjects();
            for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
                RexNode project = projects.get(fieldCollation.getFieldIndex());
                String name = project.accept(MapProjectionFieldVisitor.INSTANCE);
                keys.add(ElasticsearchRules.quote(name) + ": " + direction(fieldCollation));
            }
        } else {
            final List<RelDataTypeField> fields = getRowType().getFieldList();
            for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
                final String name = fields.get(fieldCollation.getFieldIndex()).getName();
                keys.add(ElasticsearchRules.quote(name) + ": " + direction(fieldCollation));
            }
        }
        implementor.add("\"sort\": [ " + Util.toString(keys, "{", "}, {", "}") + "]");
    }
    if (offset != null) {
        implementor.add("\"from\": " + ((RexLiteral) offset).getValue());
    }
    if (fetch != null) {
        implementor.add("\"size\": " + ((RexLiteral) fetch).getValue());
    }
}
Also used : Project(org.apache.calcite.rel.core.Project) RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with RelFieldCollation

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

the class RelMdCollation method values.

/**
 * Helper method to determine a
 * {@link org.apache.calcite.rel.core.Values}'s collation.
 *
 * <p>We actually under-report the collations. A Values with 0 or 1 rows - an
 * edge case, but legitimate and very common - is ordered by every permutation
 * of every subset of the columns.
 *
 * <p>So, our algorithm aims to:<ul>
 *   <li>produce at most N collations (where N is the number of columns);
 *   <li>make each collation as long as possible;
 *   <li>do not repeat combinations already emitted -
 *       if we've emitted {@code (a, b)} do not later emit {@code (b, a)};
 *   <li>probe the actual values and make sure that each collation is
 *      consistent with the data
 * </ul>
 *
 * <p>So, for an empty Values with 4 columns, we would emit
 * {@code (a, b, c, d), (b, c, d), (c, d), (d)}.
 */
public static List<RelCollation> values(RelMetadataQuery mq, RelDataType rowType, ImmutableList<ImmutableList<RexLiteral>> tuples) {
    // for future use
    Util.discard(mq);
    final List<RelCollation> list = Lists.newArrayList();
    final int n = rowType.getFieldCount();
    final List<Pair<RelFieldCollation, Ordering<List<RexLiteral>>>> pairs = Lists.newArrayList();
    outer: for (int i = 0; i < n; i++) {
        pairs.clear();
        for (int j = i; j < n; j++) {
            final RelFieldCollation fieldCollation = new RelFieldCollation(j);
            Ordering<List<RexLiteral>> comparator = comparator(fieldCollation);
            Ordering<List<RexLiteral>> ordering;
            if (pairs.isEmpty()) {
                ordering = comparator;
            } else {
                ordering = Util.last(pairs).right.compound(comparator);
            }
            pairs.add(Pair.of(fieldCollation, ordering));
            if (!ordering.isOrdered(tuples)) {
                if (j == i) {
                    continue outer;
                }
                pairs.remove(pairs.size() - 1);
            }
        }
        if (!pairs.isEmpty()) {
            list.add(RelCollations.of(Pair.left(pairs)));
        }
    }
    return list;
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelCollation(org.apache.calcite.rel.RelCollation) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Ordering(com.google.common.collect.Ordering) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) List(java.util.List) Pair(org.apache.calcite.util.Pair)

Example 5 with RelFieldCollation

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

the class RelJson method toJson.

public Object toJson(RelCollationImpl node) {
    final List<Object> list = new ArrayList<Object>();
    for (RelFieldCollation fieldCollation : node.getFieldCollations()) {
        final Map<String, Object> map = jsonBuilder.map();
        map.put("field", fieldCollation.getFieldIndex());
        map.put("direction", fieldCollation.getDirection().name());
        map.put("nulls", fieldCollation.nullDirection.name());
        list.add(map);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

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