Search in sources :

Example 1 with Direction

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

the class DruidQuery method deriveQuerySpec.

protected QuerySpec deriveQuerySpec() {
    final RelDataType rowType = table.getRowType();
    int i = 1;
    Filter filterRel = null;
    if (i < rels.size() && rels.get(i) instanceof Filter) {
        filterRel = (Filter) rels.get(i++);
    }
    Project project = null;
    if (i < rels.size() && rels.get(i) instanceof Project) {
        project = (Project) rels.get(i++);
    }
    ImmutableBitSet groupSet = null;
    List<AggregateCall> aggCalls = null;
    List<String> aggNames = null;
    if (i < rels.size() && rels.get(i) instanceof Aggregate) {
        final Aggregate aggregate = (Aggregate) rels.get(i++);
        groupSet = aggregate.getGroupSet();
        aggCalls = aggregate.getAggCallList();
        aggNames = Util.skip(aggregate.getRowType().getFieldNames(), groupSet.cardinality());
    }
    Filter havingFilter = null;
    if (i < rels.size() && rels.get(i) instanceof Filter) {
        havingFilter = (Filter) rels.get(i++);
    }
    Project postProject = null;
    if (i < rels.size() && rels.get(i) instanceof Project) {
        postProject = (Project) rels.get(i++);
    }
    List<Integer> collationIndexes = null;
    List<Direction> collationDirections = null;
    ImmutableBitSet.Builder numericCollationBitSetBuilder = ImmutableBitSet.builder();
    Integer fetch = null;
    if (i < rels.size() && rels.get(i) instanceof Sort) {
        final Sort sort = (Sort) rels.get(i++);
        collationIndexes = new ArrayList<>();
        collationDirections = new ArrayList<>();
        for (RelFieldCollation fCol : sort.collation.getFieldCollations()) {
            collationIndexes.add(fCol.getFieldIndex());
            collationDirections.add(fCol.getDirection());
            if (sort.getRowType().getFieldList().get(fCol.getFieldIndex()).getType().getFamily() == SqlTypeFamily.NUMERIC) {
                numericCollationBitSetBuilder.set(fCol.getFieldIndex());
            }
        }
        fetch = sort.fetch != null ? RexLiteral.intValue(sort.fetch) : null;
    }
    if (i != rels.size()) {
        throw new AssertionError("could not implement all rels");
    }
    return getQuery(rowType, filterRel, project, groupSet, aggCalls, aggNames, collationIndexes, collationDirections, numericCollationBitSetBuilder.build(), fetch, postProject, havingFilter);
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataType(org.apache.calcite.rel.type.RelDataType) Direction(org.apache.calcite.rel.RelFieldCollation.Direction) AggregateCall(org.apache.calcite.rel.core.AggregateCall) Project(org.apache.calcite.rel.core.Project) 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 2 with Direction

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

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

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

the class RelOptHiveTable method getCollationList.

@Override
public List<RelCollation> getCollationList() {
    ImmutableList.Builder<RelFieldCollation> collationList = new ImmutableList.Builder<RelFieldCollation>();
    for (Order sortColumn : this.hiveTblMetadata.getSortCols()) {
        for (int i = 0; i < this.hiveTblMetadata.getSd().getCols().size(); i++) {
            FieldSchema field = this.hiveTblMetadata.getSd().getCols().get(i);
            if (field.getName().equals(sortColumn.getCol())) {
                Direction direction = DirectionUtils.codeToDirection(sortColumn.getOrder());
                NullDirection nullDirection = sortColumn.getOrder() == DirectionUtils.ASCENDING_CODE ? NullDirection.FIRST : NullDirection.LAST;
                collationList.add(new RelFieldCollation(i, direction, nullDirection));
                break;
            }
        }
    }
    return new ImmutableList.Builder<RelCollation>().add(RelCollationTraitDef.INSTANCE.canonize(new HiveRelCollation(collationList.build()))).build();
}
Also used : Order(org.apache.hadoop.hive.metastore.api.Order) ImmutableList(com.google.common.collect.ImmutableList) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Direction(org.apache.calcite.rel.RelFieldCollation.Direction) NullDirection(org.apache.calcite.rel.RelFieldCollation.NullDirection) NullDirection(org.apache.calcite.rel.RelFieldCollation.NullDirection) UniqueConstraint(org.apache.hadoop.hive.ql.metadata.UniqueConstraint) RelReferentialConstraint(org.apache.calcite.rel.RelReferentialConstraint)

Aggregations

RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)3 Direction (org.apache.calcite.rel.RelFieldCollation.Direction)3 RelDataType (org.apache.calcite.rel.type.RelDataType)2 BoundType (com.google.common.collect.BoundType)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 Range (com.google.common.collect.Range)1 RangeSet (com.google.common.collect.RangeSet)1 IndexType (com.hazelcast.config.IndexType)1 HASH (com.hazelcast.config.IndexType.HASH)1 SORTED (com.hazelcast.config.IndexType.SORTED)1 BiTuple (com.hazelcast.internal.util.BiTuple)1 Util.toList (com.hazelcast.jet.impl.util.Util.toList)1 OptUtils (com.hazelcast.jet.sql.impl.opt.OptUtils)1 OptUtils.createRelTable (com.hazelcast.jet.sql.impl.opt.OptUtils.createRelTable)1 OptUtils.getCluster (com.hazelcast.jet.sql.impl.opt.OptUtils.getCluster)1 FullScanLogicalRel (com.hazelcast.jet.sql.impl.opt.logical.FullScanLogicalRel)1 IndexScanMapPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel)1 RexToExpression (com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression)1 RexToExpressionVisitor (com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpressionVisitor)1