Search in sources :

Example 91 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(IndexPhysicalPlanCallContext indexContext, List<RelFieldCollation> coll) {
    if (coll == null) {
        return;
    }
    DrillParseContext parserContext = new DrillParseContext(PrelUtil.getPlannerSettings(indexContext.call.rel(0).getCluster()));
    indexContext.sortExprs = Lists.newArrayList();
    for (RelFieldCollation collation : coll) {
        int idx = collation.getFieldIndex();
        ProjectPrel oneProject;
        if (indexContext.upperProject != null && indexContext.lowerProject != null) {
            LogicalExpression expr = RexToExpression.toDrill(parserContext, indexContext.lowerProject, indexContext.scan, indexContext.upperProject.getProjects().get(idx));
            indexContext.sortExprs.add(expr);
        } else {
            // one project is null now
            oneProject = (indexContext.upperProject != null) ? indexContext.upperProject : indexContext.lowerProject;
            if (oneProject != null) {
                LogicalExpression expr = RexToExpression.toDrill(parserContext, null, indexContext.scan, oneProject.getProjects().get(idx));
                indexContext.sortExprs.add(expr);
            } else {
                // two projects are null
                SchemaPath path;
                RelDataTypeField f = indexContext.scan.getRowType().getFieldList().get(idx);
                String pathSeg = f.getName().replaceAll("`", "");
                final String[] segs = pathSeg.split("\\.");
                path = SchemaPath.getCompoundPath(segs);
                indexContext.sortExprs.add(path);
            }
        }
    }
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) 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)

Example 92 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 getDistributionField.

public static List<DrillDistributionTrait.DistributionField> getDistributionField(Sort rel) {
    List<DrillDistributionTrait.DistributionField> distFields = Lists.newArrayList();
    for (RelFieldCollation relField : getCollation(rel).getFieldCollations()) {
        DrillDistributionTrait.DistributionField field = new DrillDistributionTrait.DistributionField(relField.getFieldIndex());
        distFields.add(field);
    }
    return distFields;
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) DrillDistributionTrait(org.apache.drill.exec.planner.physical.DrillDistributionTrait)

Example 93 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 buildCollationForExpressions.

/**
 * Given index, compute the collations for a list of projected expressions(from Scan's rowType or Project's )
 * in the context
 * @param projectExprs the output expression list of a RelNode
 * @param indexDesc  the index for which we are building index plan
 * @param context  the context of this index planning process
 * @return the collation provided by index that will be exposed by the expression list
 */
public static RelCollation buildCollationForExpressions(Map<LogicalExpression, Integer> projectExprs, IndexDescriptor indexDesc, IndexCallContext context) {
    assert projectExprs != null;
    final List<LogicalExpression> sortExpressions = context.getSortExprs();
    // if leading fields of index are here, add them to RelCollation
    List<RelFieldCollation> newFields = Lists.newArrayList();
    if (indexDesc.getCollation() == null) {
        return RelCollations.of(newFields);
    }
    // go through indexed fields to build collation
    // break out of the loop when found first indexed field [not projected && not _only_ in equality condition of filter]
    // or the leading field is not projected
    List<LogicalExpression> indexedCols = indexDesc.getIndexColumns();
    for (int idxFieldCount = 0; idxFieldCount < indexedCols.size(); ++idxFieldCount) {
        LogicalExpression expr = indexedCols.get(idxFieldCount);
        if (!projectExprs.containsKey(expr)) {
            // but it is only-in-equality field, -- we continue to next indexed field, but we don't generate collation for this field
            if (exprOnlyInEquality(expr, context)) {
                continue;
            }
            // else no more collation is needed to be generated, since we now have one leading field which is not in equality condition
            break;
        }
        // and we are okay to continue: generate collation for next indexed field.
        if (sortExpressions != null && !sortExpressions.contains(expr) && exprOnlyInEquality(expr, context)) {
            continue;
        }
        RelCollation idxCollation = indexDesc.getCollation();
        RelFieldCollation.NullDirection nullsDir = idxCollation == null ? RelFieldCollation.NullDirection.UNSPECIFIED : idxCollation.getFieldCollations().get(idxFieldCount).nullDirection;
        RelFieldCollation.Direction dir = (idxCollation == null) ? null : idxCollation.getFieldCollations().get(idxFieldCount).direction;
        if (dir == null) {
            break;
        }
        newFields.add(new RelFieldCollation(projectExprs.get(expr), dir, nullsDir));
    }
    return RelCollations.of(newFields);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) RelCollation(org.apache.calcite.rel.RelCollation) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 94 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 buildCollationUpperProject.

/**
 * Build collation property for the 'upper' project, the one above the filter
 * @param projectRexs list of row expressions
 * @param inputCollation the input collation
 * @param indexInfo collects functional index information
 * @param collationFilterMap map for collation filter
 * @return the output RelCollation
 */
public static RelCollation buildCollationUpperProject(List<RexNode> projectRexs, RelCollation inputCollation, FunctionalIndexInfo indexInfo, Map<Integer, List<RexNode>> collationFilterMap) {
    List<RelFieldCollation> outputFieldCollations = Lists.newArrayList();
    if (inputCollation != null) {
        List<RelFieldCollation> inputFieldCollations = inputCollation.getFieldCollations();
        if (!indexInfo.hasFunctional()) {
            for (int projectExprIdx = 0; projectExprIdx < projectRexs.size(); projectExprIdx++) {
                RexNode n = projectRexs.get(projectExprIdx);
                if (n instanceof RexInputRef) {
                    RexInputRef ref = (RexInputRef) n;
                    boolean eligibleForCollation = true;
                    int maxIndex = getIndexFromCollation(ref.getIndex(), inputFieldCollations);
                    if (maxIndex < 0) {
                        eligibleForCollation = false;
                        continue;
                    }
                    // check if the prefix has equality conditions
                    for (int i = 0; i < maxIndex; i++) {
                        int fieldIdx = inputFieldCollations.get(i).getFieldIndex();
                        List<RexNode> conditions = collationFilterMap != null ? collationFilterMap.get(fieldIdx) : null;
                        if ((conditions == null || conditions.size() == 0) && i < maxIndex - 1) {
                            // if an intermediate column has no filter condition, it would select all values
                            // of that column, so a subsequent column cannot be eligible for collation
                            eligibleForCollation = false;
                            break;
                        } else {
                            for (RexNode r : conditions) {
                                if (!(r.getKind() == SqlKind.EQUALS)) {
                                    eligibleForCollation = false;
                                    break;
                                }
                            }
                        }
                    }
                    // corresponding field collation from the input
                    if (eligibleForCollation) {
                        for (RelFieldCollation c : inputFieldCollations) {
                            if (ref.getIndex() == c.getFieldIndex()) {
                                RelFieldCollation outFieldCollation = new RelFieldCollation(projectExprIdx, c.getDirection(), c.nullDirection);
                                outputFieldCollations.add(outFieldCollation);
                            }
                        }
                    }
                }
            }
        } else {
        // TODO: handle functional index
        }
    }
    return RelCollations.of(outputFieldCollations);
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 95 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project drill by apache.

the class WindowPrel method getPhysicalOperator.

@Override
public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException {
    Prel child = (Prel) this.getInput();
    PhysicalOperator childPOP = child.getPhysicalOperator(creator);
    final List<String> childFields = getInput().getRowType().getFieldNames();
    // We don't support distinct partitions
    checkState(groups.size() == 1, "Only one window is expected in WindowPrel");
    Group window = groups.get(0);
    List<NamedExpression> withins = Lists.newArrayList();
    List<NamedExpression> aggs = Lists.newArrayList();
    List<Order.Ordering> orderings = Lists.newArrayList();
    for (int group : BitSets.toIter(window.keys)) {
        FieldReference fr = new FieldReference(childFields.get(group), ExpressionPosition.UNKNOWN);
        withins.add(new NamedExpression(fr, fr));
    }
    for (AggregateCall aggCall : window.getAggregateCalls(this)) {
        FieldReference ref = new FieldReference(aggCall.getName());
        LogicalExpression expr = toDrill(aggCall, childFields);
        aggs.add(new NamedExpression(expr, ref));
    }
    for (RelFieldCollation fieldCollation : window.orderKeys.getFieldCollations()) {
        orderings.add(new Order.Ordering(fieldCollation.getDirection(), new FieldReference(childFields.get(fieldCollation.getFieldIndex())), fieldCollation.nullDirection));
    }
    WindowPOP windowPOP = new WindowPOP(childPOP, withins, aggs, orderings, window.isRows, WindowPOP.newBound(window.lowerBound), WindowPOP.newBound(window.upperBound));
    creator.addMetadata(this, windowPOP);
    return windowPOP;
}
Also used : Order(org.apache.drill.common.logical.data.Order) FieldReference(org.apache.drill.common.expression.FieldReference) WindowPOP(org.apache.drill.exec.physical.config.WindowPOP) AggregateCall(org.apache.calcite.rel.core.AggregateCall) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

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