Search in sources :

Example 1 with DrillProjectRelBase

use of org.apache.drill.exec.planner.common.DrillProjectRelBase 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)

Example 2 with DrillProjectRelBase

use of org.apache.drill.exec.planner.common.DrillProjectRelBase in project drill by apache.

the class FindLimit0Visitor method addLimitOnTopOfLeafNodes.

public static DrillRel addLimitOnTopOfLeafNodes(final DrillRel rel) {
    final Pointer<Boolean> isUnsupported = new Pointer<>(false);
    // to visit unsupported functions
    final RexShuttle unsupportedFunctionsVisitor = new RexShuttle() {

        @Override
        public RexNode visitCall(RexCall call) {
            final SqlOperator operator = call.getOperator();
            if (isUnsupportedScalarFunction(operator)) {
                isUnsupported.value = true;
                return call;
            }
            return super.visitCall(call);
        }
    };
    // to visit unsupported operators
    final RelShuttle unsupportedOperationsVisitor = new RelShuttleImpl() {

        @Override
        public RelNode visit(RelNode other) {
            if (other instanceof DrillUnionRelBase) {
                isUnsupported.value = true;
                return other;
            } else if (other instanceof DrillProjectRelBase) {
                if (!isUnsupported.value) {
                    other.accept(unsupportedFunctionsVisitor);
                }
                if (isUnsupported.value) {
                    return other;
                }
            }
            return super.visit(other);
        }
    };
    rel.accept(unsupportedOperationsVisitor);
    if (isUnsupported.value) {
        return rel;
    }
    // to add LIMIT (0) on top of leaf nodes
    final RelShuttle addLimitOnScanVisitor = new RelShuttleImpl() {

        private RelNode addLimitAsParent(RelNode node) {
            final RexBuilder builder = node.getCluster().getRexBuilder();
            final RexLiteral offset = builder.makeExactLiteral(BigDecimal.ZERO);
            final RexLiteral fetch = builder.makeExactLiteral(BigDecimal.ZERO);
            return new DrillLimitRel(node.getCluster(), node.getTraitSet(), node, offset, fetch);
        }

        @Override
        public RelNode visit(LogicalValues values) {
            return addLimitAsParent(values);
        }

        @Override
        public RelNode visit(TableScan scan) {
            return addLimitAsParent(scan);
        }

        @Override
        public RelNode visit(RelNode other) {
            if (other.getInputs().isEmpty()) {
                // leaf operator
                return addLimitAsParent(other);
            }
            return super.visit(other);
        }
    };
    return (DrillRel) rel.accept(addLimitOnScanVisitor);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) TableScan(org.apache.calcite.rel.core.TableScan) RexShuttle(org.apache.calcite.rex.RexShuttle) DrillSqlOperator(org.apache.drill.exec.planner.sql.DrillSqlOperator) SqlOperator(org.apache.calcite.sql.SqlOperator) RelShuttle(org.apache.calcite.rel.RelShuttle) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) Pointer(org.apache.drill.exec.util.Pointer) DrillProjectRelBase(org.apache.drill.exec.planner.common.DrillProjectRelBase) DrillLimitRel(org.apache.drill.exec.planner.logical.DrillLimitRel) LogicalValues(org.apache.calcite.rel.logical.LogicalValues) RexCall(org.apache.calcite.rex.RexCall) RelNode(org.apache.calcite.rel.RelNode) DrillUnionRelBase(org.apache.drill.exec.planner.common.DrillUnionRelBase) RexBuilder(org.apache.calcite.rex.RexBuilder) DrillRel(org.apache.drill.exec.planner.logical.DrillRel)

Aggregations

DrillProjectRelBase (org.apache.drill.exec.planner.common.DrillProjectRelBase)2 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)1 RelNode (org.apache.calcite.rel.RelNode)1 RelShuttle (org.apache.calcite.rel.RelShuttle)1 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)1 TableScan (org.apache.calcite.rel.core.TableScan)1 LogicalValues (org.apache.calcite.rel.logical.LogicalValues)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexCall (org.apache.calcite.rex.RexCall)1 RexLiteral (org.apache.calcite.rex.RexLiteral)1 RexShuttle (org.apache.calcite.rex.RexShuttle)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)1 SchemaPath (org.apache.drill.common.expression.SchemaPath)1 DrillUnionRelBase (org.apache.drill.exec.planner.common.DrillUnionRelBase)1 DrillLimitRel (org.apache.drill.exec.planner.logical.DrillLimitRel)1 DrillParseContext (org.apache.drill.exec.planner.logical.DrillParseContext)1 DrillRel (org.apache.drill.exec.planner.logical.DrillRel)1 DrillSqlOperator (org.apache.drill.exec.planner.sql.DrillSqlOperator)1