Search in sources :

Example 36 with RelFieldCollation

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

the class LimitOffsetScanToOrderByLimitConverter method orderByItemToFieldCollation.

private static RelFieldCollation orderByItemToFieldCollation(ResolvedOrderByItem item, long inputOffset) {
    Direction sortDirection = item.getIsDescending() ? DESCENDING : ASCENDING;
    final long fieldIndex = item.getColumnRef().getColumn().getId() - inputOffset;
    return new RelFieldCollation((int) fieldIndex, sortDirection);
}
Also used : RelFieldCollation(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation) Direction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation.Direction)

Example 37 with RelFieldCollation

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

the class DruidQuery method computeSorting.

@Nonnull
private static Sorting computeSorting(final PartialDruidQuery partialQuery, final PlannerContext plannerContext, final RowSignature rowSignature, @Nullable final VirtualColumnRegistry virtualColumnRegistry) {
    final Sort sort = Preconditions.checkNotNull(partialQuery.getSort(), "sort");
    final Project sortProject = partialQuery.getSortProject();
    // Extract limit and offset.
    final OffsetLimit offsetLimit = OffsetLimit.fromSort(sort);
    // Extract orderBy column specs.
    final List<OrderByColumnSpec> orderBys = new ArrayList<>(sort.getChildExps().size());
    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("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 = rowSignature.getColumnName(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.
            throw new CannotBuildQueryException(sort, sortExpression);
        }
    }
    // Extract any post-sort Projection.
    final Projection projection;
    if (sortProject == null) {
        projection = null;
    } else if (partialQuery.getAggregate() == null) {
        if (virtualColumnRegistry == null) {
            throw new ISE("Must provide 'virtualColumnRegistry' for pre-aggregation Projection!");
        }
        projection = Projection.preAggregation(sortProject, plannerContext, rowSignature, virtualColumnRegistry);
    } else {
        projection = Projection.postAggregation(sortProject, plannerContext, rowSignature, "s");
    }
    return Sorting.create(orderBys, offsetLimit, projection);
}
Also used : OffsetLimit(org.apache.druid.sql.calcite.planner.OffsetLimit) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) ArrayList(java.util.ArrayList) StringComparator(org.apache.druid.query.ordering.StringComparator) OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) Project(org.apache.calcite.rel.core.Project) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Sort(org.apache.calcite.rel.core.Sort) RexInputRef(org.apache.calcite.rex.RexInputRef) ISE(org.apache.druid.java.util.common.ISE) RexNode(org.apache.calcite.rex.RexNode) Nonnull(javax.annotation.Nonnull)

Example 38 with RelFieldCollation

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

the class SortPhysicalRule method requiresSort.

private static boolean requiresSort(RelCollation sortCollation, RelCollation inputCollation) {
    if (sortCollation.getFieldCollations().isEmpty()) {
        // No need for sorting
        return false;
    }
    List<RelFieldCollation> sortFields = sortCollation.getFieldCollations();
    List<RelFieldCollation> inputFields = inputCollation.getFieldCollations();
    if (sortFields.size() <= inputFields.size()) {
        for (int i = 0; i < sortFields.size(); i++) {
            RelFieldCollation sortField = sortFields.get(i);
            RelFieldCollation inputField = inputFields.get(i);
            // Different collation, local sorting is needed.
            if (!sortField.equals(inputField)) {
                return true;
            }
        }
        // Prefix is confirmed, no local sorting is needed.
        return false;
    } else {
        // Input has less collated fields than sort. Definitely not a prefix => local sorting is needed.
        return true;
    }
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 39 with RelFieldCollation

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

the class IndexResolver method buildCollationTrait.

/**
 * Builds a collation with collation fields re-mapped according to the table projections.
 *
 * @param scan  the logical map scan
 * @param index the index
 * @param ascs  the collation of index fields
 * @return the new collation trait
 */
private static RelCollation buildCollationTrait(FullScanLogicalRel scan, MapTableIndex index, List<Boolean> ascs) {
    if (index.getType() != SORTED) {
        return RelCollations.of(Collections.emptyList());
    }
    List<RelFieldCollation> fields = new ArrayList<>(index.getFieldOrdinals().size());
    HazelcastTable table = OptUtils.extractHazelcastTable(scan);
    // Extract those projections that are direct input field references. Only those can be used
    // for index access
    List<Integer> fieldProjects = table.getProjects().stream().filter(expr -> expr instanceof RexInputRef).map(inputRef -> ((RexInputRef) inputRef).getIndex()).collect(Collectors.toList());
    for (int i = 0; i < index.getFieldOrdinals().size(); ++i) {
        Integer indexFieldOrdinal = index.getFieldOrdinals().get(i);
        int remappedIndexFieldOrdinal = fieldProjects.indexOf(indexFieldOrdinal);
        if (remappedIndexFieldOrdinal == -1) {
            // The field is not used in the query
            break;
        }
        Direction direction = ascs.get(i) ? ASCENDING : DESCENDING;
        RelFieldCollation fieldCollation = new RelFieldCollation(remappedIndexFieldOrdinal, direction);
        fields.add(fieldCollation);
    }
    return RelCollations.of(fields);
}
Also used : RangeSet(com.google.common.collect.RangeSet) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) OptUtils.getCluster(com.hazelcast.jet.sql.impl.opt.OptUtils.getCluster) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) Collections.singletonList(java.util.Collections.singletonList) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) TypeConverters(com.hazelcast.query.impl.TypeConverters) RexUtil(org.apache.calcite.rex.RexUtil) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) QueryDataTypeUtils(com.hazelcast.sql.impl.type.QueryDataTypeUtils) HASH(com.hazelcast.config.IndexType.HASH) IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) RexToExpressionVisitor(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpressionVisitor) PlanNodeFieldTypeProvider(com.hazelcast.sql.impl.plan.node.PlanNodeFieldTypeProvider) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SqlKind(org.apache.calcite.sql.SqlKind) HazelcastTypeUtils(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils) RexLiteral(org.apache.calcite.rex.RexLiteral) Collection(java.util.Collection) Range(com.google.common.collect.Range) Set(java.util.Set) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Collectors(java.util.stream.Collectors) IndexInFilter(com.hazelcast.sql.impl.exec.scan.index.IndexInFilter) RexInputRef(org.apache.calcite.rex.RexInputRef) List(java.util.List) FullScanLogicalRel(com.hazelcast.jet.sql.impl.opt.logical.FullScanLogicalRel) BoundType(com.google.common.collect.BoundType) RelCollation(org.apache.calcite.rel.RelCollation) MapTableIndex(com.hazelcast.sql.impl.schema.map.MapTableIndex) TRUE(java.lang.Boolean.TRUE) RexCall(org.apache.calcite.rex.RexCall) OptUtils.createRelTable(com.hazelcast.jet.sql.impl.opt.OptUtils.createRelTable) Iterables(com.google.common.collect.Iterables) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) IndexFilter(com.hazelcast.sql.impl.exec.scan.index.IndexFilter) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) HashMap(java.util.HashMap) POSITIVE_INFINITY(com.hazelcast.query.impl.CompositeValue.POSITIVE_INFINITY) RelOptUtil(org.apache.calcite.plan.RelOptUtil) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IndexType(com.hazelcast.config.IndexType) BiTuple(com.hazelcast.internal.util.BiTuple) Expression(com.hazelcast.sql.impl.expression.Expression) Nonnull(javax.annotation.Nonnull) ComparableIdentifiedDataSerializable(com.hazelcast.query.impl.ComparableIdentifiedDataSerializable) RelCollations(org.apache.calcite.rel.RelCollations) RexToExpression(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) FALSE(java.lang.Boolean.FALSE) HazelcastRelOptTable(com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable) RelCollationTraitDef(org.apache.calcite.rel.RelCollationTraitDef) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SORTED(com.hazelcast.config.IndexType.SORTED) Util.toList(com.hazelcast.jet.impl.util.Util.toList) RexBuilder(org.apache.calcite.rex.RexBuilder) OptUtils(com.hazelcast.jet.sql.impl.opt.OptUtils) IndexScanMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel) RelNode(org.apache.calcite.rel.RelNode) Direction(org.apache.calcite.rel.RelFieldCollation.Direction) ASCENDING(org.apache.calcite.rel.RelFieldCollation.Direction.ASCENDING) TreeMap(java.util.TreeMap) NEGATIVE_INFINITY(com.hazelcast.query.impl.CompositeValue.NEGATIVE_INFINITY) ConstantExpression(com.hazelcast.sql.impl.expression.ConstantExpression) DESCENDING(org.apache.calcite.rel.RelFieldCollation.Direction.DESCENDING) Collections(java.util.Collections) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) ArrayList(java.util.ArrayList) RexInputRef(org.apache.calcite.rex.RexInputRef) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) Direction(org.apache.calcite.rel.RelFieldCollation.Direction)

Example 40 with RelFieldCollation

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

the class RelCollationComparator method compare.

@Override
public int compare(RelCollation coll1, RelCollation coll2) {
    // Compare the collations field by field
    int coll1Size = coll1.getFieldCollations().size();
    int coll2Size = coll2.getFieldCollations().size();
    for (int i = 0; i < coll1Size; ++i) {
        if (i >= coll2Size) {
            // The coll1 has more fields and the prefixes are equal
            return 1;
        }
        RelFieldCollation fieldColl1 = coll1.getFieldCollations().get(i);
        RelFieldCollation fieldColl2 = coll2.getFieldCollations().get(i);
        // First, compare directions
        int cmp = fieldColl1.getDirection().compareTo(fieldColl2.getDirection());
        if (cmp == 0) {
            // Directions are the same
            if (fieldColl1.getFieldIndex() == fieldColl2.getFieldIndex()) {
                // And fieldIndex is the same, try the next field
                continue;
            } else {
                return Integer.compare(fieldColl1.getFieldIndex(), fieldColl2.getFieldIndex());
            }
        }
        return cmp;
    }
    // All the fields from coll1 are equal to the fields from coll2, compare the size
    return Integer.compare(coll1Size, coll2Size);
}
Also used : 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