Search in sources :

Example 26 with RelDataType

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

the class DrillRelOptUtil method areRowTypesCompatible.

// Similar to RelOptUtil.areRowTypesEqual() with the additional check for allowSubstring
public static boolean areRowTypesCompatible(RelDataType rowType1, RelDataType rowType2, boolean compareNames, boolean allowSubstring) {
    if (rowType1 == rowType2) {
        return true;
    }
    if (compareNames) {
        // the types must be different
        return false;
    }
    if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
        return false;
    }
    final List<RelDataTypeField> f1 = rowType1.getFieldList();
    final List<RelDataTypeField> f2 = rowType2.getFieldList();
    for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
        final RelDataType type1 = pair.left.getType();
        final RelDataType type2 = pair.right.getType();
        // If one of the types is ANY comparison should succeed
        if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) {
            continue;
        }
        if (type1.getSqlTypeName() != type2.getSqlTypeName()) {
            if (allowSubstring && (type1.getSqlTypeName() == SqlTypeName.CHAR && type2.getSqlTypeName() == SqlTypeName.CHAR) && (type1.getPrecision() <= type2.getPrecision())) {
                return true;
            }
            // Check if Drill implicit casting can resolve the incompatibility
            List<TypeProtos.MinorType> types = Lists.newArrayListWithCapacity(2);
            types.add(Types.getMinorTypeFromName(type1.getSqlTypeName().getName()));
            types.add(Types.getMinorTypeFromName(type2.getSqlTypeName().getName()));
            if (TypeCastRules.getLeastRestrictiveType(types) != null) {
                return true;
            }
            return false;
        }
    }
    return true;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 27 with RelDataType

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

the class AvroDrillTable method getRowType.

@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    List<RelDataType> typeList = Lists.newArrayList();
    List<String> fieldNameList = Lists.newArrayList();
    Schema schema = reader.getSchema();
    for (Field field : schema.getFields()) {
        fieldNameList.add(field.name());
        typeList.add(getNullableRelDataTypeFromAvroType(typeFactory, field.schema()));
    }
    return typeFactory.createStructType(typeList, fieldNameList);
}
Also used : Field(org.apache.avro.Schema.Field) Schema(org.apache.avro.Schema) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 28 with RelDataType

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

the class AvroDrillTable method getNullableRelDataTypeFromAvroType.

private RelDataType getNullableRelDataTypeFromAvroType(RelDataTypeFactory typeFactory, Schema fieldSchema) {
    RelDataType relDataType = null;
    switch(fieldSchema.getType()) {
        case ARRAY:
            RelDataType eleType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getElementType());
            relDataType = typeFactory.createArrayType(eleType, -1);
            break;
        case BOOLEAN:
            relDataType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
            break;
        case BYTES:
            relDataType = typeFactory.createSqlType(SqlTypeName.BINARY);
            break;
        case DOUBLE:
            relDataType = typeFactory.createSqlType(SqlTypeName.DOUBLE);
            break;
        case FIXED:
            logger.error("{} type not supported", fieldSchema.getType());
            throw UserException.unsupportedError().message("FIXED type not supported yet").build(logger);
        case FLOAT:
            relDataType = typeFactory.createSqlType(SqlTypeName.FLOAT);
            break;
        case INT:
            relDataType = typeFactory.createSqlType(SqlTypeName.INTEGER);
            break;
        case LONG:
            relDataType = typeFactory.createSqlType(SqlTypeName.BIGINT);
            break;
        case MAP:
            RelDataType valueType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getValueType());
            RelDataType keyType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
            relDataType = typeFactory.createMapType(keyType, valueType);
            break;
        case NULL:
            relDataType = typeFactory.createSqlType(SqlTypeName.NULL);
            break;
        case RECORD:
            //      List<String> fieldNameList = Lists.newArrayList();
            //      List<RelDataType> fieldRelDataTypeList = Lists.newArrayList();
            //      for(Field field : fieldSchema.getFields()) {
            //        fieldNameList.add(field.name());
            //        fieldRelDataTypeList.add(getNullableRelDataTypeFromAvroType(typeFactory, field.schema()));
            //      }
            //      relDataType = typeFactory.createStructType(fieldRelDataTypeList, fieldNameList);
            //TODO This has to be mapped to struct type but because of calcite issue,
            //for now mapping it to map type.
            keyType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
            valueType = typeFactory.createSqlType(SqlTypeName.ANY);
            relDataType = typeFactory.createMapType(keyType, valueType);
            break;
        case ENUM:
        case STRING:
            relDataType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
            break;
        case UNION:
            RelDataType optinalType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getTypes().get(1));
            relDataType = typeFactory.createTypeWithNullability(optinalType, true);
            break;
    }
    return relDataType;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 29 with RelDataType

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

the class ConvertHiveParquetScanToDrillParquetScan method createColumnFormatConversion.

/**
   * Apply any data format conversion expressions.
   */
private RexNode createColumnFormatConversion(final DrillScanRel hiveScanRel, final DrillScanRel nativeScanRel, final String colName, final RexBuilder rb) {
    final RelDataType outputType = hiveScanRel.getRowType().getField(colName, false, false).getType();
    final RelDataTypeField inputField = nativeScanRel.getRowType().getField(colName, false, false);
    final RexInputRef inputRef = rb.makeInputRef(inputField.getType(), inputField.getIndex());
    if (outputType.getSqlTypeName() == SqlTypeName.TIMESTAMP) {
        // TODO: Remove this conversion once "store.parquet.reader.int96_as_timestamp" will be true by default
        return rb.makeCall(INT96_TO_TIMESTAMP, inputRef);
    }
    return inputRef;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 30 with RelDataType

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

the class ConvertHiveParquetScanToDrillParquetScan method createPartitionColumnCast.

/**
   * Create a cast for partition column. Partition column is output as "VARCHAR" in native parquet reader. Cast it
   * appropriate type according the partition type in HiveScan.
   */
private RexNode createPartitionColumnCast(final DrillScanRel hiveScanRel, final DrillScanRel nativeScanRel, final String outputColName, final String dirColName, final RexBuilder rb) {
    final RelDataType outputType = hiveScanRel.getRowType().getField(outputColName, false, false).getType();
    final RelDataTypeField inputField = nativeScanRel.getRowType().getField(dirColName, false, false);
    final RexInputRef inputRef = rb.makeInputRef(rb.getTypeFactory().createSqlType(SqlTypeName.VARCHAR), inputField.getIndex());
    if (outputType.getSqlTypeName() == SqlTypeName.CHAR) {
        return rb.makeCall(RTRIM, inputRef);
    }
    return rb.makeCast(outputType, inputRef);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)88 RexNode (org.apache.calcite.rex.RexNode)48 RexBuilder (org.apache.calcite.rex.RexBuilder)28 RelNode (org.apache.calcite.rel.RelNode)27 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)25 ArrayList (java.util.ArrayList)21 RexInputRef (org.apache.calcite.rex.RexInputRef)16 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)14 AggregateCall (org.apache.calcite.rel.core.AggregateCall)13 ImmutableList (com.google.common.collect.ImmutableList)9 BigDecimal (java.math.BigDecimal)8 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)8 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)7 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)7 RelOptCluster (org.apache.calcite.plan.RelOptCluster)6 RelBuilder (org.apache.calcite.tools.RelBuilder)6 Prel (org.apache.drill.exec.planner.physical.Prel)6 ProjectPrel (org.apache.drill.exec.planner.physical.ProjectPrel)6 Builder (com.google.common.collect.ImmutableList.Builder)5 LinkedList (java.util.LinkedList)5