Search in sources :

Example 16 with RelFieldCollation

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

the class HiveSubQRemoveRelBuilder method fields.

/** Returns references to fields for a given collation. */
public ImmutableList<RexNode> fields(RelCollation collation) {
    final ImmutableList.Builder<RexNode> nodes = ImmutableList.builder();
    for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
        RexNode node = field(fieldCollation.getFieldIndex());
        switch(fieldCollation.direction) {
            case DESCENDING:
                node = desc(node);
        }
        switch(fieldCollation.nullDirection) {
            case FIRST:
                node = nullsFirst(node);
                break;
            case LAST:
                node = nullsLast(node);
                break;
        }
        nodes.add(node);
    }
    return nodes.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexNode(org.apache.calcite.rex.RexNode)

Example 17 with RelFieldCollation

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

the class HiveSubQRemoveRelBuilder method sortLimit.

/** Creates a {@link Sort} by a list of expressions, with limit and offset.
   *
   * @param offset Number of rows to skip; non-positive means don't skip any
   * @param fetch Maximum number of rows to fetch; negative means no limit
   * @param nodes Sort expressions
   */
public HiveSubQRemoveRelBuilder sortLimit(int offset, int fetch, Iterable<? extends RexNode> nodes) {
    final List<RelFieldCollation> fieldCollations = new ArrayList<>();
    final RelDataType inputRowType = peek().getRowType();
    final List<RexNode> extraNodes = projects(inputRowType);
    final List<RexNode> originalExtraNodes = ImmutableList.copyOf(extraNodes);
    for (RexNode node : nodes) {
        fieldCollations.add(collation(node, RelFieldCollation.Direction.ASCENDING, null, extraNodes));
    }
    final RexNode offsetNode = offset <= 0 ? null : literal(offset);
    final RexNode fetchNode = fetch < 0 ? null : literal(fetch);
    if (offsetNode == null && fetch == 0) {
        return empty();
    }
    if (offsetNode == null && fetchNode == null && fieldCollations.isEmpty()) {
        // sort is trivial
        return this;
    }
    final boolean addedFields = extraNodes.size() > originalExtraNodes.size();
    if (fieldCollations.isEmpty()) {
        assert !addedFields;
        RelNode top = peek();
        if (top instanceof Sort) {
            final Sort sort2 = (Sort) top;
            if (sort2.offset == null && sort2.fetch == null) {
                stack.pop();
                push(sort2.getInput());
                final RelNode sort = sortFactory.createSort(build(), sort2.collation, offsetNode, fetchNode);
                push(sort);
                return this;
            }
        }
        if (top instanceof Project) {
            final Project project = (Project) top;
            if (project.getInput() instanceof Sort) {
                final Sort sort2 = (Sort) project.getInput();
                if (sort2.offset == null && sort2.fetch == null) {
                    stack.pop();
                    push(sort2.getInput());
                    final RelNode sort = sortFactory.createSort(build(), sort2.collation, offsetNode, fetchNode);
                    push(sort);
                    project(project.getProjects());
                    return this;
                }
            }
        }
    }
    if (addedFields) {
        project(extraNodes);
    }
    final RelNode sort = sortFactory.createSort(build(), RelCollations.of(fieldCollations), offsetNode, fetchNode);
    push(sort);
    if (addedFields) {
        project(originalExtraNodes);
    }
    return this;
}
Also used : Project(org.apache.calcite.rel.core.Project) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Sort(org.apache.calcite.rel.core.Sort) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 18 with RelFieldCollation

use of org.apache.calcite.rel.RelFieldCollation in project druid by druid-io.

the class GroupByRules method toLimitSpec.

public static DefaultLimitSpec toLimitSpec(final List<String> rowOrder, final Sort sort) {
    final Integer limit = sort.fetch != null ? RexLiteral.intValue(sort.fetch) : null;
    final List<OrderByColumnSpec> orderBys = Lists.newArrayListWithCapacity(sort.getChildExps().size());
    if (sort.offset != null) {
        // LimitSpecs don't accept offsets.
        return null;
    }
    // Extract orderBy column specs.
    for (int sortKey = 0; sortKey < sort.getChildExps().size(); sortKey++) {
        final RexNode sortExpression = sort.getChildExps().get(sortKey);
        final RelFieldCollation collation = sort.getCollation().getFieldCollations().get(sortKey);
        final OrderByColumnSpec.Direction direction;
        final StringComparator comparator;
        if (collation.getDirection() == RelFieldCollation.Direction.ASCENDING) {
            direction = OrderByColumnSpec.Direction.ASCENDING;
        } else if (collation.getDirection() == RelFieldCollation.Direction.DESCENDING) {
            direction = OrderByColumnSpec.Direction.DESCENDING;
        } else {
            throw new ISE("WTF?! Don't know what to do with direction[%s]", collation.getDirection());
        }
        final SqlTypeName sortExpressionType = sortExpression.getType().getSqlTypeName();
        if (SqlTypeName.NUMERIC_TYPES.contains(sortExpressionType) || SqlTypeName.TIMESTAMP == sortExpressionType || SqlTypeName.DATE == sortExpressionType) {
            comparator = StringComparators.NUMERIC;
        } else {
            comparator = StringComparators.LEXICOGRAPHIC;
        }
        if (sortExpression.isA(SqlKind.INPUT_REF)) {
            final RexInputRef ref = (RexInputRef) sortExpression;
            final String fieldName = rowOrder.get(ref.getIndex());
            orderBys.add(new OrderByColumnSpec(fieldName, direction, comparator));
        } else {
            // We don't support sorting by anything other than refs which actually appear in the query result.
            return null;
        }
    }
    return new DefaultLimitSpec(orderBys, limit);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) DefaultLimitSpec(io.druid.query.groupby.orderby.DefaultLimitSpec) StringComparator(io.druid.query.ordering.StringComparator) OrderByColumnSpec(io.druid.query.groupby.orderby.OrderByColumnSpec) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexInputRef(org.apache.calcite.rex.RexInputRef) ISE(io.druid.java.util.common.ISE) RexNode(org.apache.calcite.rex.RexNode)

Example 19 with RelFieldCollation

use of org.apache.calcite.rel.RelFieldCollation in project druid by druid-io.

the class DruidQueryBuilder method getRelTraits.

public RelTrait[] getRelTraits() {
    final List<RelFieldCollation> collations = Lists.newArrayList();
    if (limitSpec != null) {
        for (OrderByColumnSpec orderBy : limitSpec.getColumns()) {
            final int i = outputRowSignature.getRowOrder().indexOf(orderBy.getDimension());
            final RelFieldCollation.Direction direction = orderBy.getDirection() == OrderByColumnSpec.Direction.ASCENDING ? RelFieldCollation.Direction.ASCENDING : RelFieldCollation.Direction.DESCENDING;
            collations.add(new RelFieldCollation(i, direction));
        }
    }
    if (!collations.isEmpty()) {
        return new RelTrait[] { RelCollations.of(collations) };
    } else {
        return new RelTrait[] {};
    }
}
Also used : OrderByColumnSpec(io.druid.query.groupby.orderby.OrderByColumnSpec) RelTrait(org.apache.calcite.plan.RelTrait) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 20 with RelFieldCollation

use of org.apache.calcite.rel.RelFieldCollation in project drill by apache.

the class WindowPrule method getDistributionFieldsFromCollation.

private List<DistributionField> getDistributionFieldsFromCollation(Window.Group window) {
    List<DistributionField> distFields = Lists.newArrayList();
    for (RelFieldCollation relField : window.collation().getFieldCollations()) {
        DistributionField field = new DistributionField(relField.getFieldIndex());
        distFields.add(field);
    }
    return distFields;
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) DistributionField(org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField)

Aggregations

RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)25 RelNode (org.apache.calcite.rel.RelNode)10 RexNode (org.apache.calcite.rex.RexNode)9 ImmutableList (com.google.common.collect.ImmutableList)8 ArrayList (java.util.ArrayList)5 FieldReference (org.apache.drill.common.expression.FieldReference)4 HiveSortLimit (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit)4 RelCollation (org.apache.calcite.rel.RelCollation)3 RexInputRef (org.apache.calcite.rex.RexInputRef)3 Mappings (org.apache.calcite.util.mapping.Mappings)3 Order (org.apache.drill.common.logical.data.Order)3 HiveRelCollation (org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelCollation)3 OrderByColumnSpec (io.druid.query.groupby.orderby.OrderByColumnSpec)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 AggregateCall (org.apache.calcite.rel.core.AggregateCall)2 Sort (org.apache.calcite.rel.core.Sort)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 RexLiteral (org.apache.calcite.rex.RexLiteral)2 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)2