use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveSortLimitPullUpConstantsRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
final RelNode parent = call.rel(0);
final Sort sort = call.rel(1);
final int count = sort.getInput().getRowType().getFieldCount();
if (count == 1) {
// Project operator.
return;
}
final RexBuilder rexBuilder = sort.getCluster().getRexBuilder();
final RelMetadataQuery mq = call.getMetadataQuery();
final RelOptPredicateList predicates = mq.getPulledUpPredicates(sort.getInput());
if (predicates == null) {
return;
}
Map<RexNode, RexNode> conditionsExtracted = HiveReduceExpressionsRule.predicateConstants(RexNode.class, rexBuilder, predicates);
Map<RexNode, RexNode> constants = new HashMap<>();
for (int i = 0; i < count; i++) {
RexNode expr = rexBuilder.makeInputRef(sort.getInput(), i);
if (conditionsExtracted.containsKey(expr)) {
constants.put(expr, conditionsExtracted.get(expr));
}
}
// None of the expressions are constant. Nothing to do.
if (constants.isEmpty()) {
return;
}
if (count == constants.size()) {
// At least a single item in project is required.
constants.remove(constants.keySet().iterator().next());
}
// Create expressions for Project operators before and after the Sort
List<RelDataTypeField> fields = sort.getInput().getRowType().getFieldList();
List<Pair<RexNode, String>> newChildExprs = new ArrayList<>();
List<RexNode> topChildExprs = new ArrayList<>();
List<String> topChildExprsFields = new ArrayList<>();
for (int i = 0; i < count; i++) {
RexNode expr = rexBuilder.makeInputRef(sort.getInput(), i);
RelDataTypeField field = fields.get(i);
if (constants.containsKey(expr)) {
topChildExprs.add(constants.get(expr));
topChildExprsFields.add(field.getName());
} else {
newChildExprs.add(Pair.<RexNode, String>of(expr, field.getName()));
topChildExprs.add(expr);
topChildExprsFields.add(field.getName());
}
}
// Update field collations
final Mappings.TargetMapping mapping = RelOptUtil.permutation(Pair.left(newChildExprs), sort.getInput().getRowType()).inverse();
List<RelFieldCollation> fieldCollations = new ArrayList<>();
for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) {
final int target = mapping.getTargetOpt(fc.getFieldIndex());
if (target < 0) {
// It is a constant, we can ignore it
continue;
}
fieldCollations.add(fc.copy(target));
}
// Update top Project positions
topChildExprs = ImmutableList.copyOf(RexUtil.apply(mapping, topChildExprs));
// Create new Project-Sort-Project sequence
final RelBuilder relBuilder = call.builder();
relBuilder.push(sort.getInput());
relBuilder.project(Pair.left(newChildExprs), Pair.right(newChildExprs));
final ImmutableList<RexNode> sortFields = relBuilder.fields(RelCollations.of(fieldCollations));
relBuilder.sortLimit(sort.offset == null ? -1 : RexLiteral.intValue(sort.offset), sort.fetch == null ? -1 : RexLiteral.intValue(sort.fetch), sortFields);
// Create top Project fixing nullability of fields
relBuilder.project(topChildExprs, topChildExprsFields);
relBuilder.convert(sort.getRowType(), false);
List<RelNode> inputs = new ArrayList<>();
for (RelNode child : parent.getInputs()) {
if (!((HepRelVertex) child).getCurrentRel().equals(sort)) {
inputs.add(child);
} else {
inputs.add(relBuilder.build());
}
}
call.transformTo(parent.copy(parent.getTraitSet(), inputs));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.
the class CassandraSchema method getClusteringOrder.
/**
* Get the collation of all clustering key columns.
*
* @return A RelCollations representing the collation of all clustering keys
*/
public List<RelFieldCollation> getClusteringOrder(String columnFamily, boolean view) {
AbstractTableMetadata table;
if (view) {
table = getKeyspace().getMaterializedView(columnFamily);
} else {
table = getKeyspace().getTable(columnFamily);
}
List<ClusteringOrder> clusteringOrder = table.getClusteringOrder();
List<RelFieldCollation> keyCollations = new ArrayList<RelFieldCollation>();
int i = 0;
for (ClusteringOrder order : clusteringOrder) {
RelFieldCollation.Direction direction;
switch(order) {
case DESC:
direction = RelFieldCollation.Direction.DESCENDING;
break;
case ASC:
default:
direction = RelFieldCollation.Direction.ASCENDING;
break;
}
keyCollations.add(new RelFieldCollation(i, direction));
i++;
}
return keyCollations;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.
the class ElasticsearchSort method implement.
@Override
public void implement(Implementor implementor) {
implementor.visitChild(0, getInput());
if (!collation.getFieldCollations().isEmpty()) {
final List<String> keys = new ArrayList<>();
if (input instanceof Project) {
final List<RexNode> projects = ((Project) input).getProjects();
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
RexNode project = projects.get(fieldCollation.getFieldIndex());
String name = project.accept(MapProjectionFieldVisitor.INSTANCE);
keys.add(ElasticsearchRules.quote(name) + ": " + direction(fieldCollation));
}
} else {
final List<RelDataTypeField> fields = getRowType().getFieldList();
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
final String name = fields.get(fieldCollation.getFieldIndex()).getName();
keys.add(ElasticsearchRules.quote(name) + ": " + direction(fieldCollation));
}
}
implementor.add("\"sort\": [ " + Util.toString(keys, "{", "}, {", "}") + "]");
}
if (offset != null) {
implementor.add("\"from\": " + ((RexLiteral) offset).getValue());
}
if (fetch != null) {
implementor.add("\"size\": " + ((RexLiteral) fetch).getValue());
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.
the class RelMdCollation method values.
/**
* Helper method to determine a
* {@link org.apache.calcite.rel.core.Values}'s collation.
*
* <p>We actually under-report the collations. A Values with 0 or 1 rows - an
* edge case, but legitimate and very common - is ordered by every permutation
* of every subset of the columns.
*
* <p>So, our algorithm aims to:<ul>
* <li>produce at most N collations (where N is the number of columns);
* <li>make each collation as long as possible;
* <li>do not repeat combinations already emitted -
* if we've emitted {@code (a, b)} do not later emit {@code (b, a)};
* <li>probe the actual values and make sure that each collation is
* consistent with the data
* </ul>
*
* <p>So, for an empty Values with 4 columns, we would emit
* {@code (a, b, c, d), (b, c, d), (c, d), (d)}.
*/
public static List<RelCollation> values(RelMetadataQuery mq, RelDataType rowType, ImmutableList<ImmutableList<RexLiteral>> tuples) {
// for future use
Util.discard(mq);
final List<RelCollation> list = Lists.newArrayList();
final int n = rowType.getFieldCount();
final List<Pair<RelFieldCollation, Ordering<List<RexLiteral>>>> pairs = Lists.newArrayList();
outer: for (int i = 0; i < n; i++) {
pairs.clear();
for (int j = i; j < n; j++) {
final RelFieldCollation fieldCollation = new RelFieldCollation(j);
Ordering<List<RexLiteral>> comparator = comparator(fieldCollation);
Ordering<List<RexLiteral>> ordering;
if (pairs.isEmpty()) {
ordering = comparator;
} else {
ordering = Util.last(pairs).right.compound(comparator);
}
pairs.add(Pair.of(fieldCollation, ordering));
if (!ordering.isOrdered(tuples)) {
if (j == i) {
continue outer;
}
pairs.remove(pairs.size() - 1);
}
}
if (!pairs.isEmpty()) {
list.add(RelCollations.of(Pair.left(pairs)));
}
}
return list;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.
the class RelJson method toJson.
public Object toJson(RelCollationImpl node) {
final List<Object> list = new ArrayList<Object>();
for (RelFieldCollation fieldCollation : node.getFieldCollations()) {
final Map<String, Object> map = jsonBuilder.map();
map.put("field", fieldCollation.getFieldIndex());
map.put("direction", fieldCollation.getDirection().name());
map.put("nulls", fieldCollation.nullDirection.name());
list.add(map);
}
return list;
}
Aggregations