Search in sources :

Example 11 with RelDataTypeFieldImpl

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

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 12 with RelDataTypeFieldImpl

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

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(SchemaPath.DYNAMIC_STAR)) {
            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 13 with RelDataTypeFieldImpl

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

the class DrillFixedRelDataTypeImpl method addField.

private void addField(String columnName) {
    RelDataTypeField newField = new RelDataTypeFieldImpl(columnName, fields.size(), typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true));
    fields.add(newField);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl)

Example 14 with RelDataTypeFieldImpl

use of org.apache.calcite.rel.type.RelDataTypeFieldImpl in project hazelcast by hazelcast.

the class HazelcastTable method computeRowType.

private RelDataType computeRowType(List<RexNode> projects) {
    List<RelDataTypeField> typeFields = new ArrayList<>(projects.size());
    for (int i = 0; i < projects.size(); i++) {
        RexNode project = projects.get(i);
        RelDataTypeField fieldType;
        if (project instanceof RexInputRef) {
            TableField field = target.getField(((RexInputRef) project).getIndex());
            fieldType = new RelDataTypeFieldImpl(field.getName(), i, project.getType());
            if (field.isHidden()) {
                hiddenFieldNames.add(field.getName());
            }
        } else {
            fieldType = new RelDataTypeFieldImpl("EXPR$" + i, i, project.getType());
        }
        typeFields.add(fieldType);
    }
    return new RelRecordType(StructKind.PEEK_FIELDS, typeFields, false);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) TableField(com.hazelcast.sql.impl.schema.TableField) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RelReferentialConstraint(org.apache.calcite.rel.RelReferentialConstraint) RexNode(org.apache.calcite.rex.RexNode)

Example 15 with RelDataTypeFieldImpl

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

the class JavaTypeFactoryImpl method createStructType.

public RelDataType createStructType(Class type) {
    final List<RelDataTypeField> list = new ArrayList<>();
    for (Field field : type.getFields()) {
        if (!Modifier.isStatic(field.getModifiers())) {
            // FIXME: watch out for recursion
            final Type fieldType = fieldType(field);
            list.add(new RelDataTypeFieldImpl(field.getName(), list.size(), createType(fieldType)));
        }
    }
    return canonize(new JavaRecordType(list, type));
}
Also used : Field(java.lang.reflect.Field) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) 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) BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) IntervalSqlType(org.apache.calcite.sql.type.IntervalSqlType) ArrayList(java.util.ArrayList) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl)

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