Java Code Examples for org.apache.orc.TypeDescription#createDouble()

The following examples show how to use org.apache.orc.TypeDescription#createDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ORCSchemaUtil.java    From iceberg with Apache License 2.0 6 votes vote down vote up
private static Optional<TypeDescription> getPromotedType(Type icebergType,
                                                         TypeDescription originalOrcType) {
  TypeDescription promotedOrcType = null;
  if (Type.TypeID.LONG.equals(icebergType.typeId()) &&
      TypeDescription.Category.INT.equals(originalOrcType.getCategory())) {
    // Promote: int to long
    promotedOrcType = TypeDescription.createLong();
  } else if (Type.TypeID.DOUBLE.equals(icebergType.typeId()) &&
      TypeDescription.Category.FLOAT.equals(originalOrcType.getCategory())) {
    // Promote: float to double
    promotedOrcType = TypeDescription.createDouble();
  } else if (Type.TypeID.DECIMAL.equals(icebergType.typeId()) &&
      TypeDescription.Category.DECIMAL.equals(originalOrcType.getCategory())) {
    // Promote: decimal(P, S) to decimal(P', S) if P' > P
    Types.DecimalType newDecimal = (Types.DecimalType) icebergType;
    if (newDecimal.scale() == originalOrcType.getScale() &&
        newDecimal.precision() > originalOrcType.getPrecision()) {
      promotedOrcType = TypeDescription.createDecimal()
          .withScale(newDecimal.scale())
          .withPrecision(newDecimal.precision());
    }
  }
  return Optional.ofNullable(promotedOrcType);
}
 
Example 2
Source File: OrcUtils.java    From tajo with Apache License 2.0 5 votes vote down vote up
public static TypeDescription convertTypeInfo(TypeDesc desc) {
  switch (desc.getDataType().getType()) {
    case BOOLEAN:
      return TypeDescription.createBoolean();
    case BIT:
      return TypeDescription.createByte();
    case INT2:
      return TypeDescription.createShort();
    case INT4:
      return TypeDescription.createInt();
    case INT8:
      return TypeDescription.createLong();
    case FLOAT4:
      return TypeDescription.createFloat();
    case FLOAT8:
      return TypeDescription.createDouble();
    case TEXT:
      return TypeDescription.createString();
    case DATE:
      return TypeDescription.createDate();
    case TIMESTAMP:
      return TypeDescription.createTimestamp();
    case BLOB:
      return TypeDescription.createBinary();
    case CHAR:
      return TypeDescription.createChar()
          .withMaxLength(desc.getDataType().getLength());
    case RECORD: {
      TypeDescription result = TypeDescription.createStruct();
      for (Column eachColumn : desc.getNestedSchema().getRootColumns()) {
        result.addField(eachColumn.getQualifiedName(),
            convertTypeInfo(eachColumn.getTypeDesc()));
      }
      return result;
    }
    default:
      throw new TajoRuntimeException(new UnsupportedDataTypeException(desc.getDataType().getType().name()));
  }
}
 
Example 3
Source File: OrcSchemaConverter.java    From pentaho-hadoop-shims with Apache License 2.0 5 votes vote down vote up
private TypeDescription determineOrcType( OrcSpec.DataType dataType ) {
  switch ( dataType ) {
    case BOOLEAN:
      return TypeDescription.createBoolean();
    case TINYINT:
      return TypeDescription.createByte();
    case SMALLINT:
      return TypeDescription.createShort();
    case INTEGER:
      return TypeDescription.createInt();
    case BIGINT:
      return TypeDescription.createLong();
    case DATE:
      return TypeDescription.createDate();
    case BINARY:
      return TypeDescription.createBinary();
    case CHAR:
      return TypeDescription.createChar();
    case VARCHAR:
      return TypeDescription.createVarchar();
    case STRING:
      return TypeDescription.createString();
    case FLOAT:
      return TypeDescription.createFloat();
    case DOUBLE:
      return TypeDescription.createDouble();
    case DECIMAL:
      return TypeDescription.createDecimal();
    case TIMESTAMP:
      return TypeDescription.createTimestamp();
    default:
      throw new RuntimeException( "Attempted to write an unsupported Orc type: " + dataType.getName() );
  }
}
 
Example 4
Source File: ORCSchemaUtil.java    From iceberg with Apache License 2.0 4 votes vote down vote up
private static TypeDescription convert(Integer fieldId, Type type, boolean isRequired) {
  final TypeDescription orcType;

  switch (type.typeId()) {
    case BOOLEAN:
      orcType = TypeDescription.createBoolean();
      break;
    case INTEGER:
      orcType = TypeDescription.createInt();
      break;
    case TIME:
      orcType = TypeDescription.createLong();
      orcType.setAttribute(ICEBERG_LONG_TYPE_ATTRIBUTE, LongType.TIME.toString());
      break;
    case LONG:
      orcType = TypeDescription.createLong();
      orcType.setAttribute(ICEBERG_LONG_TYPE_ATTRIBUTE, LongType.LONG.toString());
      break;
    case FLOAT:
      orcType = TypeDescription.createFloat();
      break;
    case DOUBLE:
      orcType = TypeDescription.createDouble();
      break;
    case DATE:
      orcType = TypeDescription.createDate();
      break;
    case TIMESTAMP:
      Types.TimestampType tsType = (Types.TimestampType) type;
      if (tsType.shouldAdjustToUTC()) {
        orcType = TypeDescription.createTimestampInstant();
      } else {
        orcType = TypeDescription.createTimestamp();
      }
      break;
    case STRING:
      orcType = TypeDescription.createString();
      break;
    case UUID:
      orcType = TypeDescription.createBinary();
      orcType.setAttribute(ICEBERG_BINARY_TYPE_ATTRIBUTE, BinaryType.UUID.toString());
      break;
    case FIXED:
      orcType = TypeDescription.createBinary();
      orcType.setAttribute(ICEBERG_BINARY_TYPE_ATTRIBUTE, BinaryType.FIXED.toString());
      orcType.setAttribute(ICEBERG_FIELD_LENGTH, Integer.toString(((Types.FixedType) type).length()));
      break;
    case BINARY:
      orcType = TypeDescription.createBinary();
      orcType.setAttribute(ICEBERG_BINARY_TYPE_ATTRIBUTE, BinaryType.BINARY.toString());
      break;
    case DECIMAL: {
      Types.DecimalType decimal = (Types.DecimalType) type;
      orcType = TypeDescription.createDecimal()
          .withScale(decimal.scale())
          .withPrecision(decimal.precision());
      break;
    }
    case STRUCT: {
      orcType = TypeDescription.createStruct();
      for (Types.NestedField field : type.asStructType().fields()) {
        TypeDescription childType = convert(field.fieldId(), field.type(), field.isRequired());
        orcType.addField(field.name(), childType);
      }
      break;
    }
    case LIST: {
      Types.ListType list = (Types.ListType) type;
      TypeDescription elementType = convert(list.elementId(), list.elementType(),
          list.isElementRequired());
      orcType = TypeDescription.createList(elementType);
      break;
    }
    case MAP: {
      Types.MapType map = (Types.MapType) type;
      TypeDescription keyType = convert(map.keyId(), map.keyType(), true);
      TypeDescription valueType = convert(map.valueId(), map.valueType(), map.isValueRequired());
      orcType = TypeDescription.createMap(keyType, valueType);
      break;
    }
    default:
      throw new IllegalArgumentException("Unhandled type " + type.typeId());
  }

  // Set Iceberg column attributes for mapping
  orcType.setAttribute(ICEBERG_ID_ATTRIBUTE, String.valueOf(fieldId));
  orcType.setAttribute(ICEBERG_REQUIRED_ATTRIBUTE, String.valueOf(isRequired));
  return orcType;
}
 
Example 5
Source File: TypeConversion.java    From iceberg with Apache License 2.0 4 votes vote down vote up
static TypeDescription toOrc(Integer fieldId,
                             Type type,
                             ColumnIdMap columnIds) {
  TypeDescription result;
  switch (type.typeId()) {
    case BOOLEAN:
      result = TypeDescription.createBoolean();
      break;
    case INTEGER:
      result = TypeDescription.createInt();
      break;
    case LONG:
      result = TypeDescription.createLong();
      break;
    case FLOAT:
      result = TypeDescription.createFloat();
      break;
    case DOUBLE:
      result = TypeDescription.createDouble();
      break;
    case DATE:
      result = TypeDescription.createDate();
      break;
    case TIME:
      result = TypeDescription.createInt();
      break;
    case TIMESTAMP:
      result = TypeDescription.createTimestamp();
      break;
    case STRING:
      result = TypeDescription.createString();
      break;
    case UUID:
      result = TypeDescription.createBinary();
      break;
    case FIXED:
      result = TypeDescription.createBinary();
      break;
    case BINARY:
      result = TypeDescription.createBinary();
      break;
    case DECIMAL: {
      Types.DecimalType decimal = (Types.DecimalType) type;
      result = TypeDescription.createDecimal()
          .withScale(decimal.scale())
          .withPrecision(decimal.precision());
      break;
    }
    case STRUCT: {
      result = TypeDescription.createStruct();
      for(Types.NestedField field: type.asStructType().fields()) {
        result.addField(field.name(), toOrc(field.fieldId(), field.type(), columnIds));
      }
      break;
    }
    case LIST: {
      Types.ListType list = (Types.ListType) type;
      result = TypeDescription.createList(toOrc(list.elementId(), list.elementType(),
          columnIds));
      break;
    }
    case MAP: {
      Types.MapType map = (Types.MapType) type;
      TypeDescription key = toOrc(map.keyId(),map.keyType(), columnIds);
      result = TypeDescription.createMap(key,
          toOrc(map.valueId(), map.valueType(), columnIds));
      break;
    }
    default:
      throw new IllegalArgumentException("Unhandled type " + type.typeId());
  }
  if (fieldId != null) {
    columnIds.put(result, fieldId);
  }
  return result;
}
 
Example 6
Source File: OrcSplitReaderUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * See {@code org.apache.flink.table.catalog.hive.util.HiveTypeUtil}.
 */
public static TypeDescription logicalTypeToOrcType(LogicalType type) {
	type = type.copy(true);
	switch (type.getTypeRoot()) {
		case CHAR:
			return TypeDescription.createChar().withMaxLength(((CharType) type).getLength());
		case VARCHAR:
			int len = ((VarCharType) type).getLength();
			if (len == VarCharType.MAX_LENGTH) {
				return TypeDescription.createString();
			} else {
				return TypeDescription.createVarchar().withMaxLength(len);
			}
		case BOOLEAN:
			return TypeDescription.createBoolean();
		case VARBINARY:
			if (type.equals(DataTypes.BYTES().getLogicalType())) {
				return TypeDescription.createBinary();
			} else {
				throw new UnsupportedOperationException(
						"Not support other binary type: " + type);
			}
		case DECIMAL:
			DecimalType decimalType = (DecimalType) type;
			return TypeDescription.createDecimal()
					.withScale(decimalType.getScale())
					.withPrecision(decimalType.getPrecision());
		case TINYINT:
			return TypeDescription.createByte();
		case SMALLINT:
			return TypeDescription.createShort();
		case INTEGER:
			return TypeDescription.createInt();
		case BIGINT:
			return TypeDescription.createLong();
		case FLOAT:
			return TypeDescription.createFloat();
		case DOUBLE:
			return TypeDescription.createDouble();
		case DATE:
			return TypeDescription.createDate();
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			return TypeDescription.createTimestamp();
		case ARRAY:
			ArrayType arrayType = (ArrayType) type;
			return TypeDescription.createList(logicalTypeToOrcType(arrayType.getElementType()));
		case MAP:
			MapType mapType = (MapType) type;
			return TypeDescription.createMap(
					logicalTypeToOrcType(mapType.getKeyType()),
					logicalTypeToOrcType(mapType.getValueType()));
		case ROW:
			RowType rowType = (RowType) type;
			TypeDescription struct = TypeDescription.createStruct();
			for (int i = 0; i < rowType.getFieldCount(); i++) {
				struct.addField(
						rowType.getFieldNames().get(i),
						logicalTypeToOrcType(rowType.getChildren().get(i)));
			}
			return struct;
		default:
			throw new UnsupportedOperationException("Unsupported type: " + type);
	}
}