Search in sources :

Example 26 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project drill by apache.

the class FunctionalIndexHelper method convertRowTypeForIndexScan.

/**
 * For IndexScan in non-covering case, rowType to return contains only row_key('_id') of primary table.
 * so the rowType for IndexScan should be converted from [Primary_table.row_key, primary_table.indexed_col]
 * to [indexTable.row_key(primary_table.indexed_col), indexTable.<primary_key.row_key> (Primary_table.row_key)]
 * This will impact the columns of scan, the rowType of ScanRel
 *
 * @param origScan
 * @param idxMarker  the IndexableExprMarker that has analyzed original index condition on top of index scan
 * @param idxScan
 * @return
 */
public static RelDataType convertRowTypeForIndexScan(DrillScanRelBase origScan, IndexableExprMarker idxMarker, IndexGroupScan idxScan, FunctionalIndexInfo functionInfo) {
    RelDataTypeFactory typeFactory = origScan.getCluster().getTypeFactory();
    List<RelDataTypeField> fields = new ArrayList<>();
    Set<SchemaPath> rowPaths = new LinkedHashSet<>();
    // row_key in the rowType of scan on primary table
    RelDataTypeField rowkey_primary;
    RelRecordType newRowType = null;
    DbGroupScan scan = (DbGroupScan) IndexPlanUtils.getGroupScan(origScan);
    // first add row_key of primary table,
    rowkey_primary = new RelDataTypeFieldImpl(scan.getRowKeyName(), fields.size(), typeFactory.createSqlType(SqlTypeName.ANY));
    fields.add(rowkey_primary);
    Map<RexNode, LogicalExpression> idxExprMap = idxMarker.getIndexableExpression();
    for (LogicalExpression indexedExpr : idxExprMap.values()) {
        if (indexedExpr instanceof SchemaPath) {
            rowPaths.add((SchemaPath) indexedExpr);
        } else if (indexedExpr instanceof CastExpression) {
            SchemaPath newPath = functionInfo.getNewPathFromExpr(indexedExpr);
            if (newPath != null) {
                rowPaths.add(newPath);
            }
        }
    }
    for (SchemaPath newPath : rowPaths) {
        fields.add(new RelDataTypeFieldImpl(newPath.getRootSegmentPath(), fields.size(), typeFactory.createSqlType(SqlTypeName.ANY)));
    }
    // update columns of groupscan accordingly
    Set<RelDataTypeField> rowfields = Sets.newLinkedHashSet();
    final List<SchemaPath> columns = Lists.newArrayList();
    for (RelDataTypeField f : fields) {
        SchemaPath path = SchemaPath.parseFromString(f.getName());
        rowfields.add(new RelDataTypeFieldImpl(path.getRootSegmentPath(), rowfields.size(), typeFactory.createSqlType(SqlTypeName.ANY)));
        columns.add(path);
    }
    idxScan.setColumns(columns);
    // rowtype does not take the whole path, but only the rootSegment of the SchemaPath
    newRowType = new RelRecordType(Lists.newArrayList(rowfields));
    return newRowType;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) RelRecordType(org.apache.calcite.rel.type.RelRecordType) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SchemaPath(org.apache.drill.common.expression.SchemaPath) DbGroupScan(org.apache.drill.exec.physical.base.DbGroupScan) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) CastExpression(org.apache.drill.common.expression.CastExpression) RexNode(org.apache.calcite.rex.RexNode)

Example 27 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project drill by apache.

the class DrillProjectRel method convert.

public static DrillProjectRel convert(Project project, ConversionContext context) throws InvalidRelException {
    RelNode input = context.toRel(project.getInput());
    List<RelDataTypeField> fields = Lists.newArrayList();
    List<RexNode> exps = Lists.newArrayList();
    for (NamedExpression expr : project.getSelections()) {
        fields.add(new RelDataTypeFieldImpl(expr.getRef().getRootSegment().getPath(), fields.size(), context.getTypeFactory().createSqlType(SqlTypeName.ANY)));
        exps.add(context.toRex(expr.getExpr()));
    }
    return new DrillProjectRel(context.getCluster(), context.getLogicalTraits(), input, exps, new RelRecordType(fields));
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RexNode(org.apache.calcite.rex.RexNode)

Example 28 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project drill by apache.

the class SplitUpComplexExpressions method visitProject.

@Override
public Prel visitProject(final ProjectPrel project, Object unused) throws RelConversionException {
    final Prel oldInput = (Prel) project.getInput(0);
    RelNode newInput = oldInput.accept(this, unused);
    ProjectPrel newProject = (ProjectPrel) project.copy(project.getTraitSet(), Lists.newArrayList(newInput));
    final int lastColumnReferenced = PrelUtil.getLastUsedColumnReference(newProject.getProjects());
    if (lastColumnReferenced == -1) {
        return newProject;
    }
    List<RelDataTypeField> projectFields = newProject.getRowType().getFieldList();
    List<RelDataTypeField> origRelDataTypes = new ArrayList<>();
    List<RexNode> exprList = new ArrayList<>();
    final int lastRexInput = lastColumnReferenced + 1;
    RexVisitorComplexExprSplitter exprSplitter = new RexVisitorComplexExprSplitter(funcReg, rexBuilder, lastRexInput);
    int i = 0;
    for (RexNode rex : newProject.getChildExps()) {
        RelDataTypeField originField = projectFields.get(i++);
        RexNode splitRex = rex.accept(exprSplitter);
        origRelDataTypes.add(originField);
        exprList.add(splitRex);
    }
    final List<RexNode> complexExprs = exprSplitter.getComplexExprs();
    if (complexExprs.size() == 1 && findTopComplexFunc(newProject.getChildExps()).size() == 1) {
        return newProject;
    }
    // if the projection expressions contained complex outputs, split them into their own individual projects
    if (complexExprs.size() > 0) {
        List<RexNode> allExprs = new ArrayList<>();
        int exprIndex = 0;
        List<String> fieldNames = newInput.getRowType().getFieldNames();
        List<RelDataTypeField> relDataTypes = new ArrayList<>();
        for (int index = 0; index < lastRexInput; index++) {
            allExprs.add(rexBuilder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
            if (fieldNames.get(index).contains(SchemaPath.DYNAMIC_STAR)) {
                relDataTypes.add(new RelDataTypeFieldImpl(fieldNames.get(index), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
            } else {
                relDataTypes.add(new RelDataTypeFieldImpl(getExprName(exprIndex), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
                exprIndex++;
            }
        }
        RexNode currRexNode;
        int index = lastRexInput - 1;
        while (complexExprs.size() > 0) {
            if (index >= lastRexInput) {
                RexInputRef newLastRex = rexBuilder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index);
                // replace last rex with new one
                allExprs.set(allExprs.size() - 1, newLastRex);
            }
            index++;
            exprIndex++;
            currRexNode = complexExprs.remove(0);
            allExprs.add(currRexNode);
            relDataTypes.add(new RelDataTypeFieldImpl(getExprName(exprIndex), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
            RelRecordType childProjectType = new RelRecordType(relDataTypes);
            ProjectPrel childProject = new ProjectPrel(newProject.getCluster(), newProject.getTraitSet(), newInput, ImmutableList.copyOf(allExprs), childProjectType);
            newInput = childProject;
        }
        allExprs.set(allExprs.size() - 1, rexBuilder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
        relDataTypes.add(new RelDataTypeFieldImpl(getExprName(exprIndex), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
    }
    return (Prel) project.copy(project.getTraitSet(), newInput, 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) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) RelDataTypeHolder(org.apache.drill.exec.planner.types.RelDataTypeHolder) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelRecordType (org.apache.calcite.rel.type.RelRecordType)26 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)20 ArrayList (java.util.ArrayList)13 RexNode (org.apache.calcite.rex.RexNode)13 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)12 RelDataType (org.apache.calcite.rel.type.RelDataType)10 RelNode (org.apache.calcite.rel.RelNode)8 RexBuilder (org.apache.calcite.rex.RexBuilder)4 RexInputRef (org.apache.calcite.rex.RexInputRef)4 Prel (org.apache.drill.exec.planner.physical.Prel)4 ProjectPrel (org.apache.drill.exec.planner.physical.ProjectPrel)4 RelDataTypeDrillImpl (org.apache.drill.exec.planner.types.RelDataTypeDrillImpl)4 RelDataTypeHolder (org.apache.drill.exec.planner.types.RelDataTypeHolder)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 Type (java.lang.reflect.Type)2 LinkedHashSet (java.util.LinkedHashSet)2 RelDataTypeField (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField)2 RelRecordType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelRecordType)2 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)2 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)2