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();
}
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;
}
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;
}
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())));
}
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);
}
Aggregations