Search in sources :

Example 6 with ProjectPrel

use of org.apache.drill.exec.planner.physical.ProjectPrel in project drill by apache.

the class SplitUpComplexExpressions method visitProject.

@Override
public Prel visitProject(ProjectPrel project, Object unused) throws RelConversionException {
    // Apply the rule to the child
    RelNode originalInput = ((Prel) project.getInput(0)).accept(this, null);
    project = (ProjectPrel) project.copy(project.getTraitSet(), Lists.newArrayList(originalInput));
    List<RexNode> exprList = new ArrayList<>();
    List<RelDataTypeField> relDataTypes = new ArrayList<>();
    List<RelDataTypeField> origRelDataTypes = new ArrayList<>();
    int i = 0;
    final int lastColumnReferenced = PrelUtil.getLastUsedColumnReference(project.getProjects());
    if (lastColumnReferenced == -1) {
        return project;
    }
    final int lastRexInput = lastColumnReferenced + 1;
    RexVisitorComplexExprSplitter exprSplitter = new RexVisitorComplexExprSplitter(factory, funcReg, lastRexInput);
    for (RexNode rex : project.getChildExps()) {
        origRelDataTypes.add(project.getRowType().getFieldList().get(i));
        i++;
        exprList.add(rex.accept(exprSplitter));
    }
    List<RexNode> complexExprs = exprSplitter.getComplexExprs();
    if (complexExprs.size() == 1 && findTopComplexFunc(project.getChildExps()).size() == 1) {
        return project;
    }
    ProjectPrel childProject;
    List<RexNode> allExprs = new ArrayList<>();
    int exprIndex = 0;
    List<String> fieldNames = originalInput.getRowType().getFieldNames();
    for (int index = 0; index < lastRexInput; index++) {
        RexBuilder builder = new RexBuilder(factory);
        allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
        if (fieldNames.get(index).contains(StarColumnHelper.STAR_COLUMN)) {
            relDataTypes.add(new RelDataTypeFieldImpl(fieldNames.get(index), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
        } else {
            relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + exprIndex, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
            exprIndex++;
        }
    }
    RexNode currRexNode;
    int index = lastRexInput - 1;
    // if the projection expressions contained complex outputs, split them into their own individual projects
    if (complexExprs.size() > 0) {
        while (complexExprs.size() > 0) {
            if (index >= lastRexInput) {
                allExprs.remove(allExprs.size() - 1);
                RexBuilder builder = new RexBuilder(factory);
                allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
            }
            index++;
            exprIndex++;
            currRexNode = complexExprs.remove(0);
            allExprs.add(currRexNode);
            relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + exprIndex, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
            childProject = new ProjectPrel(project.getCluster(), project.getTraitSet(), originalInput, ImmutableList.copyOf(allExprs), new RelRecordType(relDataTypes));
            originalInput = childProject;
        }
        // copied from above, find a better way to do this
        allExprs.remove(allExprs.size() - 1);
        RexBuilder builder = new RexBuilder(factory);
        allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
        relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + index, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
    }
    return (Prel) project.copy(project.getTraitSet(), originalInput, exprList, new RelRecordType(origRelDataTypes));
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) ArrayList(java.util.ArrayList) RelDataTypeDrillImpl(org.apache.drill.exec.planner.types.RelDataTypeDrillImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType) Prel(org.apache.drill.exec.planner.physical.Prel) ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) RelDataTypeHolder(org.apache.drill.exec.planner.types.RelDataTypeHolder) RexNode(org.apache.calcite.rex.RexNode)

Example 7 with ProjectPrel

use of org.apache.drill.exec.planner.physical.ProjectPrel in project drill by apache.

the class StarColumnConverter method visitScan.

@Override
public Prel visitScan(ScanPrel scanPrel, Void value) throws RuntimeException {
    if (StarColumnHelper.containsStarColumn(scanPrel.getRowType()) && prefixedForStar) {
        List<RexNode> exprs = Lists.newArrayList();
        for (RelDataTypeField field : scanPrel.getRowType().getFieldList()) {
            RexNode expr = scanPrel.getCluster().getRexBuilder().makeInputRef(field.getType(), field.getIndex());
            exprs.add(expr);
        }
        List<String> fieldNames = Lists.newArrayList();
        long tableId = tableNumber.getAndIncrement();
        for (String name : scanPrel.getRowType().getFieldNames()) {
            if (StarColumnHelper.isNonPrefixedStarColumn(name)) {
                // Add prefix to * column.
                fieldNames.add("T" + tableId + StarColumnHelper.PREFIX_DELIMITER + name);
            } else {
                // Keep regular column as it is.
                fieldNames.add(name);
            }
        }
        RelDataType rowType = RexUtil.createStructType(scanPrel.getCluster().getTypeFactory(), exprs, fieldNames);
        // insert a PAS.
        ProjectPrel proj = new ProjectPrel(scanPrel.getCluster(), scanPrel.getTraitSet(), scanPrel, exprs, rowType);
        return proj;
    } else {
        return visitPrel(scanPrel, value);
    }
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with ProjectPrel

use of org.apache.drill.exec.planner.physical.ProjectPrel in project drill by apache.

the class StarColumnConverter method insertProjUnderScreenOrWriter.

// insert PUS or PUW: Project Under Screen/Writer, when necessary.
private Prel insertProjUnderScreenOrWriter(Prel prel, RelDataType origRowType, Prel child) {
    ProjectPrel proj = null;
    List<RelNode> children = Lists.newArrayList();
    List<RexNode> exprs = Lists.newArrayList();
    for (int i = 0; i < origRowType.getFieldCount(); i++) {
        RexNode expr = child.getCluster().getRexBuilder().makeInputRef(origRowType.getFieldList().get(i).getType(), i);
        exprs.add(expr);
    }
    RelDataType newRowType = RexUtil.createStructType(child.getCluster().getTypeFactory(), exprs, origRowType.getFieldNames());
    int fieldCount = prel.getRowType().isStruct() ? prel.getRowType().getFieldCount() : 1;
    // Insert PUS/PUW : remove the prefix and keep the original field name.
    if (fieldCount > 1) {
        // // no point in allowing duplicates if we only have one column
        proj = new ProjectAllowDupPrel(child.getCluster(), child.getTraitSet(), child, exprs, newRowType);
    } else {
        proj = new ProjectPrel(child.getCluster(), child.getTraitSet(), child, exprs, newRowType);
    }
    children.add(proj);
    return (Prel) prel.copy(prel.getTraitSet(), children);
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) RelNode(org.apache.calcite.rel.RelNode) ProjectAllowDupPrel(org.apache.drill.exec.planner.physical.ProjectAllowDupPrel) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode) ScanPrel(org.apache.drill.exec.planner.physical.ScanPrel) Prel(org.apache.drill.exec.planner.physical.Prel) ProjectAllowDupPrel(org.apache.drill.exec.planner.physical.ProjectAllowDupPrel) ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) ScreenPrel(org.apache.drill.exec.planner.physical.ScreenPrel) WriterPrel(org.apache.drill.exec.planner.physical.WriterPrel)

Example 9 with ProjectPrel

use of org.apache.drill.exec.planner.physical.ProjectPrel in project drill by apache.

the class RewriteProjectToFlatten method visitProject.

@Override
public Prel visitProject(ProjectPrel node, Object unused) throws RelConversionException {
    ProjectPrel project = node;
    List<RexNode> exprList = new ArrayList<>();
    boolean rewrite = false;
    List<RelDataTypeField> relDataTypes = new ArrayList<>();
    int i = 0;
    RexNode flatttenExpr = null;
    for (RexNode rex : project.getChildExps()) {
        RexNode newExpr = rex;
        if (rex instanceof RexCall) {
            RexCall function = (RexCall) rex;
            String functionName = function.getOperator().getName();
            if (functionName.equalsIgnoreCase("flatten")) {
                rewrite = true;
                if (function.getOperands().size() != 1) {
                    throw new RelConversionException("Flatten expression expects a single input.");
                }
                newExpr = function.getOperands().get(0);
                RexBuilder builder = new RexBuilder(factory);
                flatttenExpr = builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), i);
            }
        }
        relDataTypes.add(project.getRowType().getFieldList().get(i));
        i++;
        exprList.add(newExpr);
    }
    if (rewrite == true) {
        // TODO - figure out what is the right setting for the traits
        Prel newChild = ((Prel) project.getInput(0)).accept(this, null);
        ProjectPrel newProject = new ProjectPrel(node.getCluster(), project.getTraitSet(), newChild, exprList, new RelRecordType(relDataTypes));
        FlattenPrel flatten = new FlattenPrel(project.getCluster(), project.getTraitSet(), newProject, flatttenExpr);
        return flatten;
    }
    Prel child = ((Prel) project.getInput()).accept(this, null);
    return (Prel) project.copy(project.getTraitSet(), child, exprList, new RelRecordType(relDataTypes));
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) ArrayList(java.util.ArrayList) RelDataTypeDrillImpl(org.apache.drill.exec.planner.types.RelDataTypeDrillImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RelConversionException(org.apache.calcite.tools.RelConversionException) Prel(org.apache.drill.exec.planner.physical.Prel) ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) FlattenPrel(org.apache.drill.exec.planner.physical.FlattenPrel) RexCall(org.apache.calcite.rex.RexCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) FlattenPrel(org.apache.drill.exec.planner.physical.FlattenPrel) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataTypeHolder(org.apache.drill.exec.planner.types.RelDataTypeHolder) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

ProjectPrel (org.apache.drill.exec.planner.physical.ProjectPrel)9 RexNode (org.apache.calcite.rex.RexNode)7 Prel (org.apache.drill.exec.planner.physical.Prel)6 RelNode (org.apache.calcite.rel.RelNode)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 RexBuilder (org.apache.calcite.rex.RexBuilder)4 ScanPrel (org.apache.drill.exec.planner.physical.ScanPrel)3 ScreenPrel (org.apache.drill.exec.planner.physical.ScreenPrel)3 WriterPrel (org.apache.drill.exec.planner.physical.WriterPrel)3 ArrayList (java.util.ArrayList)2 RelRecordType (org.apache.calcite.rel.type.RelRecordType)2 ProjectAllowDupPrel (org.apache.drill.exec.planner.physical.ProjectAllowDupPrel)2 RelDataTypeDrillImpl (org.apache.drill.exec.planner.types.RelDataTypeDrillImpl)2 RelDataTypeHolder (org.apache.drill.exec.planner.types.RelDataTypeHolder)2 RelOptRuleCall (org.apache.calcite.plan.RelOptRuleCall)1 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)1 RexCall (org.apache.calcite.rex.RexCall)1 RexInputRef (org.apache.calcite.rex.RexInputRef)1 RelConversionException (org.apache.calcite.tools.RelConversionException)1