Search in sources :

Example 41 with RelFieldCollation

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

Example 42 with RelFieldCollation

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

the class HiveRelOptUtil method getNewRelFieldCollations.

/**
 * Map Sort and SortExchange keys to the specified Project columns.
 * @param project the Project
 * @param sortCollation current collation in Sort
 * @param cluster RelOptCluster
 * @return new collation should be used in the Sort
 */
public static List<RelFieldCollation> getNewRelFieldCollations(HiveProject project, RelCollation sortCollation, RelOptCluster cluster) {
    // Determine mapping between project input and output fields.
    // In Hive, Sort is always based on RexInputRef
    // HiveSort*PullUpConstantsRule should remove constants (RexLiteral)
    // We only need to check if project can contain all the positions that sortCollation needs.
    final Mappings.TargetMapping map = RelOptUtil.permutationIgnoreCast(project.getProjects(), project.getInput().getRowType()).inverse();
    Set<Integer> needed = new HashSet<>();
    for (RelFieldCollation fc : sortCollation.getFieldCollations()) {
        needed.add(fc.getFieldIndex());
        final RexNode node = project.getProjects().get(map.getTarget(fc.getFieldIndex()));
        if (node.isA(SqlKind.CAST)) {
            // Check whether it is a monotonic preserving cast, otherwise we cannot push
            final RexCall cast = (RexCall) node;
            final RexCallBinding binding = RexCallBinding.create(cluster.getTypeFactory(), cast, ImmutableList.of(RexUtil.apply(map, sortCollation)));
            if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) {
                return null;
            }
        }
    }
    Map<Integer, Integer> m = new HashMap<>();
    for (int projPos = 0; projPos < project.getProjects().size(); projPos++) {
        RexNode expr = project.getProjects().get(projPos);
        if (expr instanceof RexInputRef) {
            Set<Integer> positions = HiveCalciteUtil.getInputRefs(expr);
            if (positions.size() <= 1) {
                int parentPos = positions.iterator().next();
                if (needed.contains(parentPos)) {
                    m.put(parentPos, projPos);
                    needed.remove(parentPos);
                }
            }
        }
    }
    if (!needed.isEmpty()) {
        return null;
    }
    List<RelFieldCollation> fieldCollations = new ArrayList<>();
    for (RelFieldCollation fc : sortCollation.getFieldCollations()) {
        fieldCollations.add(new RelFieldCollation(m.get(fc.getFieldIndex()), fc.direction, fc.nullDirection));
    }
    return fieldCollations;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RelReferentialConstraint(org.apache.calcite.rel.RelReferentialConstraint) RexCall(org.apache.calcite.rex.RexCall) RexCallBinding(org.apache.calcite.rex.RexCallBinding) Mappings(org.apache.calcite.util.mapping.Mappings) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexInputRef(org.apache.calcite.rex.RexInputRef) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) RexNode(org.apache.calcite.rex.RexNode)

Example 43 with RelFieldCollation

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

the class ASTConverter method convertFieldCollationsToASTNode.

private void convertFieldCollationsToASTNode(RelNode node, Schema schema, List<RelFieldCollation> fieldCollations, Map<Integer, RexNode> obRefToCallMap, int astToken, String astText) {
    if (fieldCollations.isEmpty()) {
        return;
    }
    // 1 Add order/sort by token
    ASTNode orderAst = ASTBuilder.createAST(astToken, astText);
    RexNode obExpr;
    ASTNode astCol;
    for (RelFieldCollation c : fieldCollations) {
        // 2 Add Direction token
        ASTNode directionAST = c.getDirection() == RelFieldCollation.Direction.ASCENDING ? ASTBuilder.createAST(HiveParser.TOK_TABSORTCOLNAMEASC, "TOK_TABSORTCOLNAMEASC") : ASTBuilder.createAST(HiveParser.TOK_TABSORTCOLNAMEDESC, "TOK_TABSORTCOLNAMEDESC");
        ASTNode nullDirectionAST;
        // Null direction
        if (c.nullDirection == RelFieldCollation.NullDirection.FIRST) {
            nullDirectionAST = ASTBuilder.createAST(HiveParser.TOK_NULLS_FIRST, "TOK_NULLS_FIRST");
            directionAST.addChild(nullDirectionAST);
        } else if (c.nullDirection == RelFieldCollation.NullDirection.LAST) {
            nullDirectionAST = ASTBuilder.createAST(HiveParser.TOK_NULLS_LAST, "TOK_NULLS_LAST");
            directionAST.addChild(nullDirectionAST);
        } else {
            // Default
            if (c.getDirection() == RelFieldCollation.Direction.ASCENDING) {
                nullDirectionAST = ASTBuilder.createAST(HiveParser.TOK_NULLS_FIRST, "TOK_NULLS_FIRST");
                directionAST.addChild(nullDirectionAST);
            } else {
                nullDirectionAST = ASTBuilder.createAST(HiveParser.TOK_NULLS_LAST, "TOK_NULLS_LAST");
                directionAST.addChild(nullDirectionAST);
            }
        }
        // 3 Convert OB expr (OB Expr is usually an input ref except for top
        // level OB; top level OB will have RexCall kept in a map.)
        obExpr = null;
        if (obRefToCallMap != null) {
            obExpr = obRefToCallMap.get(c.getFieldIndex());
        }
        if (obExpr != null) {
            astCol = obExpr.accept(new RexVisitor(schema, false, node.getCluster().getRexBuilder()));
        } else {
            ColumnInfo cI = schema.get(c.getFieldIndex());
            /*
         * The RowResolver setup for Select drops Table associations. So
         * setup ASTNode on unqualified name.
         */
            astCol = ASTBuilder.unqualifiedName(cI.column);
        }
        // 4 buildup the ob expr AST
        nullDirectionAST.addChild(astCol);
        orderAst.addChild(directionAST);
    }
    hiveAST.order = orderAst;
}
Also used : ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexNode(org.apache.calcite.rex.RexNode)

Example 44 with RelFieldCollation

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

the class HiveRelMdCollation method collations.

public ImmutableList<RelCollation> collations(HiveAggregate aggregate, RelMetadataQuery mq) {
    // Compute collations
    ImmutableList.Builder<RelFieldCollation> collationListBuilder = new ImmutableList.Builder<RelFieldCollation>();
    for (int pos : aggregate.getGroupSet().asList()) {
        final RelFieldCollation fieldCollation = new RelFieldCollation(pos);
        collationListBuilder.add(fieldCollation);
    }
    // Return aggregate collations
    return ImmutableList.of(RelCollationTraitDef.INSTANCE.canonize(new HiveRelCollation(collationListBuilder.build())));
}
Also used : HiveRelCollation(org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelCollation) ImmutableList(com.google.common.collect.ImmutableList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 45 with RelFieldCollation

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

the class RelFieldTrimmer method trimChild.

/**
 * Trims the fields of an input relational expression.
 *
 * @param rel        Relational expression
 * @param input      Input relational expression, whose fields to trim
 * @param fieldsUsed Bitmap of fields needed by the consumer
 * @return New relational expression and its field mapping
 */
protected TrimResult trimChild(RelNode rel, RelNode input, final ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild();
    // Fields that define the collation cannot be discarded.
    final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    final ImmutableList<RelCollation> collations = mq.collations(input);
    if (collations != null) {
        for (RelCollation collation : collations) {
            for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
                fieldsUsedBuilder.set(fieldCollation.getFieldIndex());
            }
        }
    }
    // fields.
    for (final CorrelationId correlation : rel.getVariablesSet()) {
        rel.accept(new CorrelationReferenceFinder() {

            protected RexNode handle(RexFieldAccess fieldAccess) {
                final RexCorrelVariable v = (RexCorrelVariable) fieldAccess.getReferenceExpr();
                if (v.id.equals(correlation)) {
                    fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
                }
                return fieldAccess;
            }
        });
    }
    return dispatchTrimFields(input, fieldsUsedBuilder.build(), extraFields);
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) RelCollation(org.apache.calcite.rel.RelCollation) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) CorrelationReferenceFinder(org.apache.calcite.sql2rel.CorrelationReferenceFinder) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) RexNode(org.apache.calcite.rex.RexNode)

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