Skip to content

Commit

Permalink
Improve Trino to Iceberg conversion code
Browse files Browse the repository at this point in the history
- suppress intentional "redundant casts"
- exact division
- cast to primitive directly (looks nicer, same meaning)
- remove redundant `instanceof`
- put decimals together with other numbers
  • Loading branch information
findepi committed Sep 29, 2022
1 parent ca113f5 commit 8c63bb9
Showing 1 changed file with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 8c63bb9

Please sign in to comment.