From 8c63bb91b6b261010e62dec9fc75a5bb9551a265 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Thu, 29 Sep 2022 13:58:22 +0200 Subject: [PATCH] Improve Trino to Iceberg conversion code - suppress intentional "redundant casts" - exact division - cast to primitive directly (looks nicer, same meaning) - remove redundant `instanceof` - put decimals together with other numbers --- .../io/trino/plugin/iceberg/IcebergTypes.java | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergTypes.java b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergTypes.java index e5edf8015d50..7b8ef16b6a86 100644 --- a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergTypes.java +++ b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergTypes.java @@ -13,18 +13,13 @@ */ package io.trino.plugin.iceberg; +import com.google.common.math.LongMath; import io.airlift.slice.Slice; import io.airlift.slice.Slices; -import io.trino.spi.type.BigintType; -import io.trino.spi.type.BooleanType; -import io.trino.spi.type.DateType; import io.trino.spi.type.DecimalType; import io.trino.spi.type.Decimals; -import io.trino.spi.type.DoubleType; import io.trino.spi.type.Int128; -import io.trino.spi.type.IntegerType; import io.trino.spi.type.LongTimestampWithTimeZone; -import io.trino.spi.type.RealType; import io.trino.spi.type.UuidType; import io.trino.spi.type.VarbinaryType; import io.trino.spi.type.VarcharType; @@ -39,6 +34,12 @@ import static io.airlift.slice.Slices.utf8Slice; import static io.trino.plugin.iceberg.util.Timestamps.timestampTzFromMicros; import static io.trino.plugin.iceberg.util.Timestamps.timestampTzToMicros; +import static io.trino.spi.type.BigintType.BIGINT; +import static io.trino.spi.type.BooleanType.BOOLEAN; +import static io.trino.spi.type.DateType.DATE; +import static io.trino.spi.type.DoubleType.DOUBLE; +import static io.trino.spi.type.IntegerType.INTEGER; +import static io.trino.spi.type.RealType.REAL; import static io.trino.spi.type.TimeType.TIME_MICROS; import static io.trino.spi.type.TimestampType.TIMESTAMP_MICROS; import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS; @@ -49,6 +50,7 @@ import static java.lang.Float.intBitsToFloat; import static java.lang.Math.multiplyExact; import static java.lang.Math.toIntExact; +import static java.math.RoundingMode.UNNECESSARY; import static java.util.Objects.requireNonNull; public final class IcebergTypes @@ -64,35 +66,46 @@ public static Object convertTrinoValueToIceberg(io.trino.spi.type.Type type, Obj { requireNonNull(trinoNativeValue, "trinoNativeValue is null"); - if (type instanceof BooleanType) { + if (type == BOOLEAN) { + //noinspection RedundantCast return (boolean) trinoNativeValue; } - if (type instanceof IntegerType) { + if (type == INTEGER) { return toIntExact((long) trinoNativeValue); } - if (type instanceof BigintType) { + if (type == BIGINT) { + //noinspection RedundantCast return (long) trinoNativeValue; } - if (type instanceof RealType) { + if (type == REAL) { return intBitsToFloat(toIntExact((long) trinoNativeValue)); } - if (type instanceof DoubleType) { + if (type == DOUBLE) { + //noinspection RedundantCast return (double) trinoNativeValue; } - if (type instanceof DateType) { - return toIntExact(((Long) trinoNativeValue)); + if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { + return BigDecimal.valueOf((long) trinoNativeValue).movePointLeft(decimalType.getScale()); + } + return new BigDecimal(((Int128) trinoNativeValue).toBigInteger(), decimalType.getScale()); + } + + if (type == DATE) { + return toIntExact((long) trinoNativeValue); } if (type.equals(TIME_MICROS)) { - return ((long) trinoNativeValue) / PICOSECONDS_PER_MICROSECOND; + return LongMath.divide((long) trinoNativeValue, PICOSECONDS_PER_MICROSECOND, UNNECESSARY); } if (type.equals(TIMESTAMP_MICROS)) { + //noinspection RedundantCast return (long) trinoNativeValue; } @@ -108,17 +121,10 @@ public static Object convertTrinoValueToIceberg(io.trino.spi.type.Type type, Obj return ByteBuffer.wrap(((Slice) trinoNativeValue).getBytes()); } - if (type instanceof UuidType) { + if (type == UuidType.UUID) { return trinoUuidToJavaUuid(((Slice) trinoNativeValue)); } - if (type instanceof DecimalType decimalType) { - if (decimalType.isShort()) { - return BigDecimal.valueOf((long) trinoNativeValue).movePointLeft(decimalType.getScale()); - } - return new BigDecimal(((Int128) trinoNativeValue).toBigInteger(), decimalType.getScale()); - } - throw new UnsupportedOperationException("Unsupported type: " + type); }