Search in sources :

Example 86 with RelFieldCollation

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);
}
Also used : SelectScope(org.apache.calcite.sql.validate.SelectScope) AggregatingSelectScope(org.apache.calcite.sql.validate.AggregatingSelectScope) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) SqlNode(org.apache.calcite.sql.SqlNode)

Example 87 with RelFieldCollation

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);
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) SqlNode(org.apache.calcite.sql.SqlNode)

Example 88 with RelFieldCollation

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);
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) Mappings(org.apache.calcite.util.mapping.Mappings) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 89 with RelFieldCollation

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;
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelCollation(org.apache.calcite.rel.RelCollation) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Map(java.util.Map)

Example 90 with RelFieldCollation

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);
            }
        }
    }
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SchemaPath(org.apache.drill.common.expression.SchemaPath) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) DrillParseContext(org.apache.drill.exec.planner.logical.DrillParseContext) DrillProjectRelBase(org.apache.drill.exec.planner.common.DrillProjectRelBase)

Aggregations

RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)101 ArrayList (java.util.ArrayList)36 RexNode (org.apache.calcite.rex.RexNode)36 RelCollation (org.apache.calcite.rel.RelCollation)33 RelNode (org.apache.calcite.rel.RelNode)28 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)17 RexInputRef (org.apache.calcite.rex.RexInputRef)17 RelDataType (org.apache.calcite.rel.type.RelDataType)14 ImmutableList (com.google.common.collect.ImmutableList)13 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)13 Sort (org.apache.calcite.rel.core.Sort)12 RelTraitSet (org.apache.calcite.plan.RelTraitSet)11 RexCall (org.apache.calcite.rex.RexCall)11 RexLiteral (org.apache.calcite.rex.RexLiteral)11 Project (org.apache.calcite.rel.core.Project)9 FieldReference (org.apache.drill.common.expression.FieldReference)9 HashMap (java.util.HashMap)8 RelOptCluster (org.apache.calcite.plan.RelOptCluster)8 Map (java.util.Map)7 AggregateCall (org.apache.calcite.rel.core.AggregateCall)7