Java Code Examples for org.apache.iceberg.types.Types#DecimalType

The following examples show how to use org.apache.iceberg.types.Types#DecimalType . 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: TestTruncatesProjection.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimalInclusiveUpperBound() {
  Types.DecimalType type = Types.DecimalType.of(9, 2);
  BigDecimal value = (BigDecimal) Literal.of("99.99").to(type).value();
  Schema schema = new Schema(optional(1, "value", type));
  PartitionSpec spec = PartitionSpec.builderFor(schema).truncate("value", 10).build();

  assertProjectionInclusive(spec, lessThan("value", value), Expression.Operation.LT_EQ, "99.90");
  assertProjectionInclusive(spec, lessThanOrEqual("value", value), Expression.Operation.LT_EQ, "99.90");
  assertProjectionInclusive(spec, greaterThan("value", value), Expression.Operation.GT_EQ, "100.00");
  assertProjectionInclusive(spec, greaterThanOrEqual("value", value), Expression.Operation.GT_EQ, "99.90");
  assertProjectionInclusive(spec, equal("value", value), Expression.Operation.EQ, "99.90");
  assertProjectionInclusiveValue(spec, notEqual("value", value), Expression.Operation.TRUE);

  BigDecimal delta = new BigDecimal(1);
  assertProjectionInclusive(spec, in("value", value.add(delta), value, value.subtract(delta)),
      Expression.Operation.IN, "[98.90, 99.90, 100.90]");
  assertProjectionInclusiveValue(spec, notIn("value", value, value.subtract(delta)), Expression.Operation.TRUE);
}
 
Example 2
Source File: TestTruncatesProjection.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimalInclusiveLowerBound() {
  Types.DecimalType type = Types.DecimalType.of(9, 2);
  BigDecimal value = (BigDecimal) Literal.of("100.00").to(type).value();
  Schema schema = new Schema(optional(1, "value", type));
  PartitionSpec spec = PartitionSpec.builderFor(schema).truncate("value", 10).build();

  assertProjectionInclusive(spec, lessThan("value", value), Expression.Operation.LT_EQ, "99.90");
  assertProjectionInclusive(spec, lessThanOrEqual("value", value), Expression.Operation.LT_EQ, "100.00");
  assertProjectionInclusive(spec, greaterThan("value", value), Expression.Operation.GT_EQ, "100.00");
  assertProjectionInclusive(spec, greaterThanOrEqual("value", value), Expression.Operation.GT_EQ, "100.00");
  assertProjectionInclusive(spec, equal("value", value), Expression.Operation.EQ, "100.00");
  assertProjectionInclusiveValue(spec, notEqual("value", value), Expression.Operation.TRUE);

  BigDecimal delta = new BigDecimal(1);
  assertProjectionInclusive(spec, in("value", value.add(delta), value, value.subtract(delta)),
      Expression.Operation.IN, "[99.00, 100.00, 101.00]");
  assertProjectionInclusiveValue(spec, notIn("value", value, value.add(delta)), Expression.Operation.TRUE);
}
 
Example 3
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 4
Source File: TestTruncatesProjection.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimalStrictUpperBound() {
  Types.DecimalType type = Types.DecimalType.of(9, 2);
  BigDecimal value = (BigDecimal) Literal.of("99.99").to(type).value();
  Schema schema = new Schema(optional(1, "value", type));
  PartitionSpec spec = PartitionSpec.builderFor(schema).truncate("value", 10).build();

  assertProjectionStrict(spec, lessThan("value", value), Expression.Operation.LT, "99.90");
  assertProjectionStrict(spec, lessThanOrEqual("value", value), Expression.Operation.LT, "100.00");
  assertProjectionStrict(spec, greaterThan("value", value), Expression.Operation.GT, "99.90");
  assertProjectionStrict(spec, greaterThanOrEqual("value", value), Expression.Operation.GT, "99.90");
  assertProjectionStrict(spec, notEqual("value", value), Expression.Operation.NOT_EQ, "99.90");
  assertProjectionStrictValue(spec, equal("value", value), Expression.Operation.FALSE);

  BigDecimal delta = new BigDecimal(1);
  assertProjectionStrict(spec, notIn("value", value.add(delta), value, value.subtract(delta)),
      Expression.Operation.NOT_IN, "[98.90, 99.90, 100.90]");
  assertProjectionStrictValue(spec, in("value", value, value.subtract(delta)), Expression.Operation.FALSE);
}
 
Example 5
Source File: TestTruncatesProjection.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimalStrictLowerBound() {
  Types.DecimalType type = Types.DecimalType.of(9, 2);
  BigDecimal value = (BigDecimal) Literal.of("100.00").to(type).value();
  Schema schema = new Schema(optional(1, "value", type));
  PartitionSpec spec = PartitionSpec.builderFor(schema).truncate("value", 10).build();

  assertProjectionStrict(spec, lessThan("value", value), Expression.Operation.LT, "100.00");
  assertProjectionStrict(spec, lessThanOrEqual("value", value), Expression.Operation.LT, "100.00");
  assertProjectionStrict(spec, greaterThan("value", value), Expression.Operation.GT, "100.00");
  assertProjectionStrict(spec, greaterThanOrEqual("value", value), Expression.Operation.GT, "99.90");
  assertProjectionStrict(spec, notEqual("value", value), Expression.Operation.NOT_EQ, "100.00");
  assertProjectionStrictValue(spec, equal("value", value), Expression.Operation.FALSE);

  BigDecimal delta = new BigDecimal(1);
  assertProjectionStrict(spec, notIn("value", value.add(delta), value, value.subtract(delta)),
          Expression.Operation.NOT_IN, "[99.00, 100.00, 101.00]");
  assertProjectionStrictValue(spec, in("value", value, value.add(delta)), Expression.Operation.FALSE);
}
 
Example 6
Source File: TestBucketingProjection.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testBucketDecimalStrict() {
  Types.DecimalType type = Types.DecimalType.of(9, 2);
  BigDecimal value = (BigDecimal) Literal.of("100.00").to(type).value();
  Schema schema = new Schema(optional(1, "value", type));
  PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("value", 10).build();

  // the bucket number of the value (i.e. 100.00) is 2
  assertProjectionStrict(spec, notEqual("value", value), Expression.Operation.NOT_EQ, "2");
  assertProjectionStrictValue(spec, equal("value", value), Expression.Operation.FALSE);
  assertProjectionStrictValue(spec, lessThan("value", value), Expression.Operation.FALSE);
  assertProjectionStrictValue(spec, lessThanOrEqual("value", value), Expression.Operation.FALSE);
  assertProjectionStrictValue(spec, greaterThan("value", value), Expression.Operation.FALSE);
  assertProjectionStrictValue(spec, greaterThanOrEqual("value", value), Expression.Operation.FALSE);

  BigDecimal delta = new BigDecimal(1);
  assertProjectionStrict(spec, notIn("value", value.add(delta), value, value.subtract(delta)),
      Expression.Operation.NOT_IN, "[2, 2, 6]");
  assertProjectionStrictValue(spec, in("value", value, value.add(delta)), Expression.Operation.FALSE);
}
 
Example 7
Source File: TestBucketingProjection.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testBucketDecimalInclusive() {
  Types.DecimalType type = Types.DecimalType.of(9, 2);
  BigDecimal value = (BigDecimal) Literal.of("100.00").to(type).value();
  Schema schema = new Schema(optional(1, "value", type));
  PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("value", 10).build();

  // the bucket number of the value (i.e. 100.00) is 2
  assertProjectionInclusive(spec, equal("value", value), Expression.Operation.EQ, "2");
  assertProjectionInclusiveValue(spec, notEqual("value", value), Expression.Operation.TRUE);
  assertProjectionInclusiveValue(spec, lessThan("value", value), Expression.Operation.TRUE);
  assertProjectionInclusiveValue(spec, lessThanOrEqual("value", value), Expression.Operation.TRUE);
  assertProjectionInclusiveValue(spec, greaterThan("value", value), Expression.Operation.TRUE);
  assertProjectionInclusiveValue(spec, greaterThanOrEqual("value", value), Expression.Operation.TRUE);

  BigDecimal delta = new BigDecimal(1);
  assertProjectionInclusive(spec, in("value", value.add(delta), value, value.subtract(delta)),
      Expression.Operation.IN, "[2, 2, 6]");
  assertProjectionInclusiveValue(spec, notIn("value", value, value.add(delta)), Expression.Operation.TRUE);
}
 
Example 8
Source File: TypeConverter.java    From presto with Apache License 2.0 5 votes vote down vote up
private static List<OrcType> toOrcType(int nextFieldTypeIndex, org.apache.iceberg.types.Type type, Map<String, String> attributes)
{
    switch (type.typeId()) {
        case BOOLEAN:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.BOOLEAN, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case INTEGER:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.INT, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case LONG:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.LONG, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case FLOAT:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.FLOAT, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case DOUBLE:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.DOUBLE, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case DATE:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.DATE, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case TIME:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.INT, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case TIMESTAMP:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.TIMESTAMP, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case STRING:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.STRING, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case UUID:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.BINARY, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case FIXED:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.BINARY, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case BINARY:
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.BINARY, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), attributes));
        case DECIMAL:
            Types.DecimalType decimalType = (Types.DecimalType) type;
            return ImmutableList.of(new OrcType(OrcType.OrcTypeKind.DECIMAL, ImmutableList.of(), ImmutableList.of(), Optional.empty(), Optional.of(decimalType.precision()), Optional.of(decimalType.scale()), attributes));
        case STRUCT:
            return toOrcStructType(nextFieldTypeIndex, (Types.StructType) type, attributes);
        case LIST:
            return toOrcListType(nextFieldTypeIndex, (Types.ListType) type, attributes);
        case MAP:
            return toOrcMapType(nextFieldTypeIndex, (Types.MapType) type, attributes);
        default:
            throw new PrestoException(NOT_SUPPORTED, "Unsupported Iceberg type: " + type);
    }
}
 
Example 9
Source File: RandomUtil.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static Object generateDictionaryEncodablePrimitive(Type.PrimitiveType primitive, Random random) {
  int value = random.nextInt(3);
  switch (primitive.typeId()) {
    case BOOLEAN:
      return true; // doesn't really matter for booleans since they are not dictionary encoded
    case INTEGER:
    case DATE:
      return value;
    case FLOAT:
      return (float) value;
    case DOUBLE:
      return (double) value;
    case LONG:
    case TIME:
    case TIMESTAMP:
      return (long) value;
    case STRING:
      return String.valueOf(value);
    case FIXED:
      byte[] fixed = new byte[((Types.FixedType) primitive).length()];
      Arrays.fill(fixed, (byte) value);
      return fixed;
    case BINARY:
      byte[] binary = new byte[value + 1];
      Arrays.fill(binary, (byte) value);
      return binary;
    case DECIMAL:
      Types.DecimalType type = (Types.DecimalType) primitive;
      BigInteger unscaled = new BigInteger(String.valueOf(value + 1));
      return new BigDecimal(unscaled, type.scale());
    default:
      throw new IllegalArgumentException(
          "Cannot generate random value for unknown type: " + primitive);
  }
}
 
Example 10
Source File: TestIdentity.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Test
public void testBigDecimalToHumanString() {
  Types.DecimalType decimal = Types.DecimalType.of(9, 2);
  Transform<BigDecimal, BigDecimal> identity = Transforms.identity(decimal);

  String decimalString = "-1.50";
  BigDecimal bigDecimal = new BigDecimal(decimalString);
  Assert.assertEquals("Should not modify Strings", decimalString, identity.toHumanString(bigDecimal));
}
 
Example 11
Source File: VectorizedReadDictionaryEncodedFlatParquetDataBenchmark.java    From iceberg with Apache License 2.0 5 votes vote down vote up
private static Dataset<Row> withDecimalColumnDictEncoded(Dataset<Row> df) {
  Types.DecimalType type = Types.DecimalType.of(20, 5);
  return df.withColumn(
      "decimalCol",
      when(modColumn(9, 0), bigDecimal(type, 0))
          .when(modColumn(9, 1), bigDecimal(type, 1))
          .when(modColumn(9, 2), bigDecimal(type, 2))
          .when(modColumn(9, 3), bigDecimal(type, 3))
          .when(modColumn(9, 4), bigDecimal(type, 4))
          .when(modColumn(9, 5), bigDecimal(type, 5))
          .when(modColumn(9, 6), bigDecimal(type, 6))
          .when(modColumn(9, 7), bigDecimal(type, 7))
          .when(modColumn(9, 8), bigDecimal(type, 8)));
}
 
Example 12
Source File: VectorizedReadDictionaryEncodedFlatParquetDataBenchmark.java    From iceberg with Apache License 2.0 4 votes vote down vote up
private static BigDecimal bigDecimal(Types.DecimalType type, int value) {
  BigInteger unscaled = new BigInteger(String.valueOf(value + 1));
  return new BigDecimal(unscaled, type.scale());
}
 
Example 13
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 14
Source File: HiveTypeConverter.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public static String convert(Type type) {
  switch (type.typeId()) {
    case BOOLEAN:
      return "boolean";
    case INTEGER:
      return "int";
    case LONG:
      return "bigint";
    case FLOAT:
      return "float";
    case DOUBLE:
      return "double";
    case DATE:
      return "date";
    case TIME:
      return "string";
    case TIMESTAMP:
      return "timestamp";
    case STRING:
    case UUID:
      return "string";
    case FIXED:
      return "binary";
    case BINARY:
      return "binary";
    case DECIMAL:
      final Types.DecimalType decimalType = (Types.DecimalType) type;
      // TODO may be just decimal?
      return String.format("decimal(%s,%s)", decimalType.precision(), decimalType.scale());
    case STRUCT:
      final Types.StructType structType = type.asStructType();
      final String nameToType = structType.fields().stream()
          .map(f -> String.format("%s:%s", f.name(), convert(f.type())))
          .collect(Collectors.joining(","));
      return String.format("struct<%s>", nameToType);
    case LIST:
      final Types.ListType listType = type.asListType();
      return String.format("array<%s>", convert(listType.elementType()));
    case MAP:
      final Types.MapType mapType = type.asMapType();
      return String.format("map<%s,%s>", convert(mapType.keyType()), convert(mapType.valueType()));
    default:
      throw new UnsupportedOperationException(type + " is not supported");
  }
}
 
Example 15
Source File: TypeConverter.java    From presto with Apache License 2.0 4 votes vote down vote up
public static Type toPrestoType(org.apache.iceberg.types.Type type, TypeManager typeManager)
{
    switch (type.typeId()) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case BINARY:
        case FIXED:
            return VarbinaryType.VARBINARY;
        case DATE:
            return DateType.DATE;
        case DECIMAL:
            Types.DecimalType decimalType = (Types.DecimalType) type;
            return DecimalType.createDecimalType(decimalType.precision(), decimalType.scale());
        case DOUBLE:
            return DoubleType.DOUBLE;
        case LONG:
            return BigintType.BIGINT;
        case FLOAT:
            return RealType.REAL;
        case INTEGER:
            return IntegerType.INTEGER;
        case TIME:
            return TimeType.TIME;
        case TIMESTAMP:
            Types.TimestampType timestampType = (Types.TimestampType) type;
            if (timestampType.shouldAdjustToUTC()) {
                return TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE;
            }
            return TimestampType.TIMESTAMP;
        case UUID:
        case STRING:
            return VarcharType.createUnboundedVarcharType();
        case LIST:
            Types.ListType listType = (Types.ListType) type;
            return new ArrayType(toPrestoType(listType.elementType(), typeManager));
        case MAP:
            Types.MapType mapType = (Types.MapType) type;
            TypeSignature keyType = toPrestoType(mapType.keyType(), typeManager).getTypeSignature();
            TypeSignature valueType = toPrestoType(mapType.valueType(), typeManager).getTypeSignature();
            return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(keyType), TypeSignatureParameter.typeParameter(valueType)));
        case STRUCT:
            List<Types.NestedField> fields = ((Types.StructType) type).fields();
            return RowType.from(fields.stream()
                    .map(field -> new RowType.Field(Optional.of(field.name()), toPrestoType(field.type(), typeManager)))
                    .collect(toImmutableList()));
        default:
            throw new UnsupportedOperationException(format("Cannot convert from Iceberg type '%s' (%s) to Presto type", type, type.typeId()));
    }
}
 
Example 16
Source File: TestHelpers.java    From iceberg with Apache License 2.0 4 votes vote down vote up
private static Object getValue(SpecializedGetters container, int ord,
                               Type type) {
  if (container.isNullAt(ord)) {
    return null;
  }
  switch (type.typeId()) {
    case BOOLEAN:
      return container.getBoolean(ord);
    case INTEGER:
      return container.getInt(ord);
    case LONG:
      return container.getLong(ord);
    case FLOAT:
      return container.getFloat(ord);
    case DOUBLE:
      return container.getDouble(ord);
    case STRING:
      return container.getUTF8String(ord).toString();
    case BINARY:
    case FIXED:
    case UUID:
      return container.getBinary(ord);
    case DATE:
      return new DateWritable(container.getInt(ord)).get();
    case TIMESTAMP:
      return DateTimeUtils.toJavaTimestamp(container.getLong(ord));
    case DECIMAL: {
      Types.DecimalType dt = (Types.DecimalType) type;
      return container.getDecimal(ord, dt.precision(), dt.scale()).toJavaBigDecimal();
    }
    case STRUCT:
      Types.StructType struct = type.asStructType();
      InternalRow internalRow = container.getStruct(ord, struct.fields().size());
      Object[] data = new Object[struct.fields().size()];
      for (int i = 0; i < data.length; i += 1) {
        if (internalRow.isNullAt(i)) {
          data[i] = null;
        } else {
          data[i] = getValue(internalRow, i, struct.fields().get(i).type());
        }
      }
      return new GenericRow(data);
    default:
      throw new IllegalArgumentException("Unhandled type " + type);
  }
}
 
Example 17
Source File: HiveTypeConverter.java    From metacat with Apache License 2.0 4 votes vote down vote up
/**
 * convert iceberg to hive type.
 * @param type iceberg type.
 * @return hive type string.
 */
public static String fromIcebergToHiveType(final org.apache.iceberg.types.Type type) {
    switch (type.typeId()) {
        case BOOLEAN:
            return serdeConstants.BOOLEAN_TYPE_NAME;
        case INTEGER:
            return serdeConstants.INT_TYPE_NAME;
        case LONG:
            return serdeConstants.BIGINT_TYPE_NAME;
        case FLOAT:
            return serdeConstants.FLOAT_TYPE_NAME;
        case DOUBLE:
            return serdeConstants.DOUBLE_TYPE_NAME;
        case DATE:
            return serdeConstants.DATE_TYPE_NAME;
        case TIME:
            throw new UnsupportedOperationException("Hive does not support time fields");
        case TIMESTAMP:
            return serdeConstants.TIMESTAMP_TYPE_NAME;
        case STRING:
        case UUID:
            return serdeConstants.STRING_TYPE_NAME;
        case FIXED:
            return serdeConstants.BINARY_TYPE_NAME;
        case BINARY:
            return serdeConstants.BINARY_TYPE_NAME;
        case DECIMAL:
            final Types.DecimalType decimalType = (Types.DecimalType) type;
            return String.format("decimal(%s,%s)", decimalType.precision(), decimalType.scale());
        case STRUCT:
            final Types.StructType structType = type.asStructType();
            final String nameToType = (String) structType.fields().stream().map((f) -> {
                return String.format("%s:%s", f.name(), fromIcebergToHiveType(f.type()));
            }).collect(Collectors.joining(","));
            return String.format("struct<%s>", nameToType);
        case LIST:
            final Types.ListType listType = type.asListType();
            return String.format("array<%s>", fromIcebergToHiveType(listType.elementType()));
        case MAP:
            final Types.MapType mapType = type.asMapType();
            return String.format("map<%s,%s>", fromIcebergToHiveType(mapType.keyType()),
                fromIcebergToHiveType(mapType.valueType()));
        default:
            throw new UnsupportedOperationException(type + " is not supported");
    }
}