Search in sources :

Example 21 with RelDataTypeFieldImpl

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

the class CountToDirectScanUtils method constructDataType.

/**
 * For each aggregate call creates field based on its name with bigint type.
 * Constructs record type for created fields.
 *
 * @param aggregateRel aggregate relation expression
 * @param fieldNames field names
 * @return record type
 */
public static RelDataType constructDataType(Aggregate aggregateRel, Collection<String> fieldNames) {
    List<RelDataTypeField> fields = new ArrayList<>();
    int fieldIndex = 0;
    for (String name : fieldNames) {
        RelDataTypeField field = new RelDataTypeFieldImpl(name, fieldIndex++, aggregateRel.getCluster().getTypeFactory().createSqlType(SqlTypeName.BIGINT));
        fields.add(field);
    }
    return new RelRecordType(fields);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType)

Example 22 with RelDataTypeFieldImpl

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

the class AbstractIndexPlanGenerator method convertRowType.

protected RelDataType convertRowType(RelDataType origRowType, RelDataTypeFactory typeFactory) {
    if (getRowKeyIndex(origRowType, origScan) >= 0) {
        // row key already present
        return origRowType;
    }
    List<RelDataTypeField> fields = new ArrayList<>();
    fields.addAll(origRowType.getFieldList());
    fields.add(new RelDataTypeFieldImpl(((DbGroupScan) IndexPlanUtils.getGroupScan(origScan)).getRowKeyName(), fields.size(), typeFactory.createSqlType(SqlTypeName.ANY)));
    return new RelRecordType(fields);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) DbGroupScan(org.apache.drill.exec.physical.base.DbGroupScan) ArrayList(java.util.ArrayList) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType)

Example 23 with RelDataTypeFieldImpl

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

the class FunctionalIndexHelper method rewriteFunctionalRowType.

/**
 * if a field in rowType serves only the to-be-replaced column(s), we should replace it with new name "$1",
 * otherwise we should keep this dataTypeField and add a new one for "$1"
 * @param origScan  the original scan whose rowtype is to be rewritten
 * @param indexContext the index plan context
 * @param functionInfo functional index information that may impact rewrite
 * @return
 */
public static RelDataType rewriteFunctionalRowType(RelNode origScan, IndexCallContext indexContext, FunctionalIndexInfo functionInfo, Collection<SchemaPath> addedPaths) {
    RelDataType origRowType = origScan.getRowType();
    if (!functionInfo.hasFunctional()) {
        return origRowType;
    }
    List<RelDataTypeField> fields = Lists.newArrayList();
    Set<String> leftOutFieldNames = Sets.newHashSet();
    if (indexContext.getLeftOutPathsInFunctions() != null) {
        for (LogicalExpression expr : indexContext.getLeftOutPathsInFunctions()) {
            leftOutFieldNames.add(((SchemaPath) expr).getRootSegmentPath());
        }
    }
    Set<String> fieldInFunctions = Sets.newHashSet();
    for (SchemaPath path : functionInfo.allPathsInFunction()) {
        fieldInFunctions.add(path.getRootSegmentPath());
    }
    RelDataTypeFactory typeFactory = origScan.getCluster().getTypeFactory();
    for (RelDataTypeField field : origRowType.getFieldList()) {
        final String fieldName = field.getName();
        if (fieldInFunctions.contains(fieldName)) {
            if (!leftOutFieldNames.contains(fieldName)) {
                continue;
            }
        }
        fields.add(new RelDataTypeFieldImpl(SchemaPath.parseFromString(fieldName).getRootSegmentPath(), fields.size(), typeFactory.createSqlType(SqlTypeName.ANY)));
    }
    final Collection<SchemaPath> toAddToRowType = (addedPaths == null) ? functionInfo.allNewSchemaPaths() : addedPaths;
    for (SchemaPath dollarPath : toAddToRowType) {
        fields.add(new RelDataTypeFieldImpl(dollarPath.getRootSegmentPath(), fields.size(), origScan.getCluster().getTypeFactory().createSqlType(SqlTypeName.ANY)));
    }
    return new RelRecordType(fields);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SchemaPath(org.apache.drill.common.expression.SchemaPath) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType)

Example 24 with RelDataTypeFieldImpl

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFieldImpl 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 25 with RelDataTypeFieldImpl

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFieldImpl 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)

Aggregations

RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)27 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)26 ArrayList (java.util.ArrayList)15 RelRecordType (org.apache.calcite.rel.type.RelRecordType)15 RelDataType (org.apache.calcite.rel.type.RelDataType)9 RexNode (org.apache.calcite.rex.RexNode)8 RelNode (org.apache.calcite.rel.RelNode)6 Type (java.lang.reflect.Type)4 RexInputRef (org.apache.calcite.rex.RexInputRef)4 ImmutableList (com.google.common.collect.ImmutableList)3 JavaRecordType (org.apache.calcite.jdbc.JavaRecordType)3 Field (java.lang.reflect.Field)2 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)2 NlsString (org.apache.calcite.util.NlsString)2 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)2 SchemaPath (org.apache.drill.common.expression.SchemaPath)2 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)2 DbGroupScan (org.apache.drill.exec.physical.base.DbGroupScan)2 Prel (org.apache.drill.exec.planner.physical.Prel)2