use of org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveSubQRemoveRelBuilder method fields.
/** Returns references to fields for a given collation. */
public ImmutableList<RexNode> fields(RelCollation collation) {
final ImmutableList.Builder<RexNode> nodes = ImmutableList.builder();
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
RexNode node = field(fieldCollation.getFieldIndex());
switch(fieldCollation.direction) {
case DESCENDING:
node = desc(node);
}
switch(fieldCollation.nullDirection) {
case FIRST:
node = nullsFirst(node);
break;
case LAST:
node = nullsLast(node);
break;
}
nodes.add(node);
}
return nodes.build();
}
use of org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveSubQRemoveRelBuilder method sortLimit.
/** Creates a {@link Sort} by a list of expressions, with limit and offset.
*
* @param offset Number of rows to skip; non-positive means don't skip any
* @param fetch Maximum number of rows to fetch; negative means no limit
* @param nodes Sort expressions
*/
public HiveSubQRemoveRelBuilder sortLimit(int offset, int fetch, Iterable<? extends RexNode> nodes) {
final List<RelFieldCollation> fieldCollations = new ArrayList<>();
final RelDataType inputRowType = peek().getRowType();
final List<RexNode> extraNodes = projects(inputRowType);
final List<RexNode> originalExtraNodes = ImmutableList.copyOf(extraNodes);
for (RexNode node : nodes) {
fieldCollations.add(collation(node, RelFieldCollation.Direction.ASCENDING, null, extraNodes));
}
final RexNode offsetNode = offset <= 0 ? null : literal(offset);
final RexNode fetchNode = fetch < 0 ? null : literal(fetch);
if (offsetNode == null && fetch == 0) {
return empty();
}
if (offsetNode == null && fetchNode == null && fieldCollations.isEmpty()) {
// sort is trivial
return this;
}
final boolean addedFields = extraNodes.size() > originalExtraNodes.size();
if (fieldCollations.isEmpty()) {
assert !addedFields;
RelNode top = peek();
if (top instanceof Sort) {
final Sort sort2 = (Sort) top;
if (sort2.offset == null && sort2.fetch == null) {
stack.pop();
push(sort2.getInput());
final RelNode sort = sortFactory.createSort(build(), sort2.collation, offsetNode, fetchNode);
push(sort);
return this;
}
}
if (top instanceof Project) {
final Project project = (Project) top;
if (project.getInput() instanceof Sort) {
final Sort sort2 = (Sort) project.getInput();
if (sort2.offset == null && sort2.fetch == null) {
stack.pop();
push(sort2.getInput());
final RelNode sort = sortFactory.createSort(build(), sort2.collation, offsetNode, fetchNode);
push(sort);
project(project.getProjects());
return this;
}
}
}
}
if (addedFields) {
project(extraNodes);
}
final RelNode sort = sortFactory.createSort(build(), RelCollations.of(fieldCollations), offsetNode, fetchNode);
push(sort);
if (addedFields) {
project(originalExtraNodes);
}
return this;
}
use of org.apache.calcite.rel.RelFieldCollation in project druid by druid-io.
the class GroupByRules method toLimitSpec.
public static DefaultLimitSpec toLimitSpec(final List<String> rowOrder, final Sort sort) {
final Integer limit = sort.fetch != null ? RexLiteral.intValue(sort.fetch) : null;
final List<OrderByColumnSpec> orderBys = Lists.newArrayListWithCapacity(sort.getChildExps().size());
if (sort.offset != null) {
// LimitSpecs don't accept offsets.
return null;
}
// Extract orderBy column specs.
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("WTF?! 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 = rowOrder.get(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.
return null;
}
}
return new DefaultLimitSpec(orderBys, limit);
}
use of org.apache.calcite.rel.RelFieldCollation in project druid by druid-io.
the class DruidQueryBuilder method getRelTraits.
public RelTrait[] getRelTraits() {
final List<RelFieldCollation> collations = Lists.newArrayList();
if (limitSpec != null) {
for (OrderByColumnSpec orderBy : limitSpec.getColumns()) {
final int i = outputRowSignature.getRowOrder().indexOf(orderBy.getDimension());
final RelFieldCollation.Direction direction = orderBy.getDirection() == OrderByColumnSpec.Direction.ASCENDING ? RelFieldCollation.Direction.ASCENDING : RelFieldCollation.Direction.DESCENDING;
collations.add(new RelFieldCollation(i, direction));
}
}
if (!collations.isEmpty()) {
return new RelTrait[] { RelCollations.of(collations) };
} else {
return new RelTrait[] {};
}
}
use of org.apache.calcite.rel.RelFieldCollation in project drill by apache.
the class WindowPrule method getDistributionFieldsFromCollation.
private List<DistributionField> getDistributionFieldsFromCollation(Window.Group window) {
List<DistributionField> distFields = Lists.newArrayList();
for (RelFieldCollation relField : window.collation().getFieldCollations()) {
DistributionField field = new DistributionField(relField.getFieldIndex());
distFields.add(field);
}
return distFields;
}
Aggregations