Search in sources :

Example 21 with RelRecordType

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

the class JavaTypeFactoryExtImpl method createPdxType.

public RelDataType createPdxType(PdxInstance pdxInstance) {
    final List<RelDataTypeField> list = new ArrayList<>();
    for (String fieldName : pdxInstance.getFieldNames()) {
        Object field = pdxInstance.getField(fieldName);
        Type fieldType;
        if (field == null) {
            fieldType = String.class;
        } else if (field instanceof PdxInstance) {
            // Map Nested PDX structures as String. This relates with
            // GeodeUtils.convert case when clazz is Null.
            fieldType = Map.class;
        // RelDataType boza = createPdxType((PdxInstance) field);
        } else {
            fieldType = field.getClass();
        }
        list.add(new RelDataTypeFieldImpl(fieldName, list.size(), createType(fieldType)));
    }
    return canonize(new RelRecordType(list));
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType) RelRecordType(org.apache.calcite.rel.type.RelRecordType) Type(java.lang.reflect.Type) JavaRecordType(org.apache.calcite.jdbc.JavaRecordType) PdxInstance(org.apache.geode.pdx.PdxInstance) ArrayList(java.util.ArrayList) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) Map(java.util.Map) RelRecordType(org.apache.calcite.rel.type.RelRecordType)

Example 22 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType 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 23 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType 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 24 with RelRecordType

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

the class RewriteProjectToFlatten method visitProject.

@Override
public Prel visitProject(ProjectPrel project, Object unused) throws RelConversionException {
    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) {
        // TODO - figure out what is the right setting for the traits
        Prel newChild = ((Prel) project.getInput(0)).accept(this, null);
        ProjectPrel newProject = new ProjectPrel(project.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);
    if (child == project.getInput() && exprList.equals(project.getChildExps())) {
        return project;
    }
    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)

Example 25 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType 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)

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