use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation 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);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project druid by druid-io.
the class DruidQuery method computeSorting.
@Nonnull
private static Sorting computeSorting(final PartialDruidQuery partialQuery, final PlannerContext plannerContext, final RowSignature rowSignature, @Nullable final VirtualColumnRegistry virtualColumnRegistry) {
final Sort sort = Preconditions.checkNotNull(partialQuery.getSort(), "sort");
final Project sortProject = partialQuery.getSortProject();
// Extract limit and offset.
final OffsetLimit offsetLimit = OffsetLimit.fromSort(sort);
// Extract orderBy column specs.
final List<OrderByColumnSpec> orderBys = new ArrayList<>(sort.getChildExps().size());
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("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 = rowSignature.getColumnName(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.
throw new CannotBuildQueryException(sort, sortExpression);
}
}
// Extract any post-sort Projection.
final Projection projection;
if (sortProject == null) {
projection = null;
} else if (partialQuery.getAggregate() == null) {
if (virtualColumnRegistry == null) {
throw new ISE("Must provide 'virtualColumnRegistry' for pre-aggregation Projection!");
}
projection = Projection.preAggregation(sortProject, plannerContext, rowSignature, virtualColumnRegistry);
} else {
projection = Projection.postAggregation(sortProject, plannerContext, rowSignature, "s");
}
return Sorting.create(orderBys, offsetLimit, projection);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project hazelcast by hazelcast.
the class SortPhysicalRule method requiresSort.
private static boolean requiresSort(RelCollation sortCollation, RelCollation inputCollation) {
if (sortCollation.getFieldCollations().isEmpty()) {
// No need for sorting
return false;
}
List<RelFieldCollation> sortFields = sortCollation.getFieldCollations();
List<RelFieldCollation> inputFields = inputCollation.getFieldCollations();
if (sortFields.size() <= inputFields.size()) {
for (int i = 0; i < sortFields.size(); i++) {
RelFieldCollation sortField = sortFields.get(i);
RelFieldCollation inputField = inputFields.get(i);
// Different collation, local sorting is needed.
if (!sortField.equals(inputField)) {
return true;
}
}
// Prefix is confirmed, no local sorting is needed.
return false;
} else {
// Input has less collated fields than sort. Definitely not a prefix => local sorting is needed.
return true;
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation 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);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project hazelcast by hazelcast.
the class RelCollationComparator method compare.
@Override
public int compare(RelCollation coll1, RelCollation coll2) {
// Compare the collations field by field
int coll1Size = coll1.getFieldCollations().size();
int coll2Size = coll2.getFieldCollations().size();
for (int i = 0; i < coll1Size; ++i) {
if (i >= coll2Size) {
// The coll1 has more fields and the prefixes are equal
return 1;
}
RelFieldCollation fieldColl1 = coll1.getFieldCollations().get(i);
RelFieldCollation fieldColl2 = coll2.getFieldCollations().get(i);
// First, compare directions
int cmp = fieldColl1.getDirection().compareTo(fieldColl2.getDirection());
if (cmp == 0) {
// Directions are the same
if (fieldColl1.getFieldIndex() == fieldColl2.getFieldIndex()) {
// And fieldIndex is the same, try the next field
continue;
} else {
return Integer.compare(fieldColl1.getFieldIndex(), fieldColl2.getFieldIndex());
}
}
return cmp;
}
// All the fields from coll1 are equal to the fields from coll2, compare the size
return Integer.compare(coll1Size, coll2Size);
}
Aggregations