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);
}
}
}
}
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;
}
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);
}
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);
}
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;
}
Aggregations