use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.
the class SqlToRelConverter method convertOrderItem.
protected RelFieldCollation convertOrderItem(SqlSelect select, SqlNode orderItem, List<SqlNode> extraExprs, RelFieldCollation.Direction direction, RelFieldCollation.NullDirection nullDirection) {
assert select != null;
// Handle DESC keyword, e.g. 'select a, b from t order by a desc'.
switch(orderItem.getKind()) {
case DESCENDING:
return convertOrderItem(select, ((SqlCall) orderItem).operand(0), extraExprs, RelFieldCollation.Direction.DESCENDING, nullDirection);
case NULLS_FIRST:
return convertOrderItem(select, ((SqlCall) orderItem).operand(0), extraExprs, direction, RelFieldCollation.NullDirection.FIRST);
case NULLS_LAST:
return convertOrderItem(select, ((SqlCall) orderItem).operand(0), extraExprs, direction, RelFieldCollation.NullDirection.LAST);
}
SqlNode converted = validator.expandOrderExpr(select, orderItem);
switch(nullDirection) {
case UNSPECIFIED:
nullDirection = validator.getDefaultNullCollation().last(desc(direction)) ? RelFieldCollation.NullDirection.LAST : RelFieldCollation.NullDirection.FIRST;
}
// Scan the select list and order exprs for an identical expression.
final SelectScope selectScope = validator.getRawSelectScope(select);
int ordinal = -1;
for (SqlNode selectItem : selectScope.getExpandedSelectList()) {
++ordinal;
if (converted.equalsDeep(stripAs(selectItem), Litmus.IGNORE)) {
return new RelFieldCollation(ordinal, direction, nullDirection);
}
}
for (SqlNode extraExpr : extraExprs) {
++ordinal;
if (converted.equalsDeep(extraExpr, Litmus.IGNORE)) {
return new RelFieldCollation(ordinal, direction, nullDirection);
}
}
// TODO: handle collation sequence
// TODO: flag expressions as non-standard
extraExprs.add(converted);
return new RelFieldCollation(ordinal + 1, direction, nullDirection);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.
the class SqlToRelConverter method convertSelectImpl.
/**
* Implementation of {@link #convertSelect(SqlSelect, boolean)};
* derived class may override.
*/
protected void convertSelectImpl(final Blackboard bb, SqlSelect select) {
convertFrom(bb, select.getFrom());
convertWhere(bb, select.getWhere());
final List<SqlNode> orderExprList = new ArrayList<>();
final List<RelFieldCollation> collationList = new ArrayList<>();
gatherOrderExprs(bb, select, select.getOrderList(), orderExprList, collationList);
final RelCollation collation = cluster.traitSet().canonize(RelCollations.of(collationList));
if (validator.isAggregate(select)) {
convertAgg(bb, select, orderExprList);
} else {
convertSelectList(bb, select, orderExprList);
}
if (select.isDistinct()) {
distinctify(bb, true);
}
convertOrder(select, bb, collation, orderExprList, select.getOffset(), select.getFetch());
bb.setRoot(bb.root, true);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project calcite by apache.
the class RelStructuredTypeFlattener method rewriteRel.
public void rewriteRel(Sort rel) {
RelCollation oldCollation = rel.getCollation();
final RelNode oldChild = rel.getInput();
final RelNode newChild = getNewForOldRel(oldChild);
final Mappings.TargetMapping mapping = getNewForOldInputMapping(oldChild);
// validate
for (RelFieldCollation field : oldCollation.getFieldCollations()) {
int oldInput = field.getFieldIndex();
RelDataType sortFieldType = oldChild.getRowType().getFieldList().get(oldInput).getType();
if (sortFieldType.isStruct()) {
// TODO jvs 10-Feb-2005
throw Util.needToImplement("sorting on structured types");
}
}
RelCollation newCollation = RexUtil.apply(mapping, oldCollation);
Sort newRel = LogicalSort.create(newChild, newCollation, rel.offset, rel.fetch);
setNewForOldRel(rel, newRel);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project drill by apache.
the class IndexPlanUtils method buildCollationNonCoveringIndexScan.
public static RelCollation buildCollationNonCoveringIndexScan(IndexDescriptor indexDesc, RelDataType indexScanRowType, RelDataType restrictedScanRowType, IndexCallContext context) {
if (context.getSortExprs() == null) {
return RelCollations.of(RelCollations.EMPTY.getFieldCollations());
}
final List<RelDataTypeField> indexFields = indexScanRowType.getFieldList();
final List<RelDataTypeField> rsFields = restrictedScanRowType.getFieldList();
final Map<LogicalExpression, RelFieldCollation> collationMap = indexDesc.getCollationMap();
assert collationMap != null : "Invalid collation map for index";
List<RelFieldCollation> fieldCollations = Lists.newArrayList();
Map<Integer, RelFieldCollation> rsScanCollationMap = Maps.newTreeMap();
// restricted scan's row type.
for (int i = 0; i < indexScanRowType.getFieldCount(); i++) {
RelDataTypeField f1 = indexFields.get(i);
for (int j = 0; j < rsFields.size(); j++) {
RelDataTypeField f2 = rsFields.get(j);
if (f1.getName().equals(f2.getName())) {
FieldReference ref = FieldReference.getWithQuotedRef(f1.getName());
RelFieldCollation origCollation = collationMap.get(ref);
if (origCollation != null) {
RelFieldCollation fc = new RelFieldCollation(j, origCollation.direction, origCollation.nullDirection);
rsScanCollationMap.put(origCollation.getFieldIndex(), fc);
}
}
}
}
// should sort by the order of these fields in indexDesc
for (Map.Entry<Integer, RelFieldCollation> entry : rsScanCollationMap.entrySet()) {
RelFieldCollation fc = entry.getValue();
if (fc != null) {
fieldCollations.add(fc);
}
}
final RelCollation collation = RelCollations.of(fieldCollations);
return collation;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project drill by apache.
the class IndexPlanUtils method updateSortExpression.
/**
* generate logical expressions for sort rexNodes in SortRel, the result is store to IndexPlanCallContext
* @param indexContext the index call context
* @param coll list of field collations
*/
public static void updateSortExpression(IndexCallContext indexContext, List<RelFieldCollation> coll) {
if (coll == null) {
return;
}
DrillParseContext parserContext = new DrillParseContext(PrelUtil.getPlannerSettings(indexContext.getCall().rel(0).getCluster()));
indexContext.createSortExprs();
for (RelFieldCollation collation : coll) {
int idx = collation.getFieldIndex();
DrillProjectRelBase oneProject;
if (indexContext.getUpperProject() != null && indexContext.getLowerProject() != null) {
LogicalExpression expr = RexToExpression.toDrill(parserContext, indexContext.getLowerProject(), indexContext.getScan(), indexContext.getUpperProject().getProjects().get(idx));
indexContext.getSortExprs().add(expr);
} else {
// one project is null now
oneProject = (indexContext.getUpperProject() != null) ? indexContext.getUpperProject() : indexContext.getLowerProject();
if (oneProject != null) {
LogicalExpression expr = RexToExpression.toDrill(parserContext, null, indexContext.getScan(), getProjects(oneProject).get(idx));
indexContext.getSortExprs().add(expr);
} else {
// two projects are null
SchemaPath path;
RelDataTypeField f = indexContext.getScan().getRowType().getFieldList().get(idx);
String pathSeg = f.getName().replaceAll("`", "");
final String[] segs = pathSeg.split("\\.");
path = SchemaPath.getCompoundPath(segs);
indexContext.getSortExprs().add(path);
}
}
}
}
Aggregations