diff --git a/core/trino-main/src/main/java/io/trino/operator/scalar/FormatFunction.java b/core/trino-main/src/main/java/io/trino/operator/scalar/FormatFunction.java index f83bef1aea42..b61120f3b15a 100644 --- a/core/trino-main/src/main/java/io/trino/operator/scalar/FormatFunction.java +++ b/core/trino-main/src/main/java/io/trino/operator/scalar/FormatFunction.java @@ -56,8 +56,6 @@ import static io.trino.spi.type.BooleanType.BOOLEAN; import static io.trino.spi.type.Chars.padSpaces; import static io.trino.spi.type.DateType.DATE; -import static io.trino.spi.type.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.DoubleType.DOUBLE; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.RealType.REAL; @@ -122,8 +120,7 @@ private static void addDependencies(FunctionDependencyDeclarationBuilder builder type instanceof TimestampWithTimeZoneType || type instanceof TimestampType || type instanceof TimeType || - isShortDecimal(type) || - isLongDecimal(type) || + type instanceof DecimalType || type instanceof VarcharType || type instanceof CharType) { return; @@ -215,12 +212,11 @@ private static BiFunction valueConverter(Functi MethodHandle handle = functionDependencies.getScalarFunctionImplementation(QualifiedFunctionName.of("json_format"), ImmutableList.of(JSON), simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle(); return (session, block) -> convertToString(handle, type.getSlice(block, position)); } - if (isShortDecimal(type)) { - int scale = ((DecimalType) type).getScale(); - return (session, block) -> BigDecimal.valueOf(type.getLong(block, position), scale); - } - if (isLongDecimal(type)) { - int scale = ((DecimalType) type).getScale(); + if (type instanceof DecimalType decimalType) { + int scale = decimalType.getScale(); + if (decimalType.isShort()) { + return (session, block) -> BigDecimal.valueOf(type.getLong(block, position), scale); + } return (session, block) -> new BigDecimal(((Int128) type.getObject(block, position)).toBigInteger(), scale); } if (type instanceof VarcharType) { diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/LiteralEncoder.java b/core/trino-main/src/main/java/io/trino/sql/planner/LiteralEncoder.java index 2a1766df450d..34935655f6ad 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/LiteralEncoder.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/LiteralEncoder.java @@ -62,7 +62,6 @@ 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.Decimals.isShortDecimal; import static io.trino.spi.type.DoubleType.DOUBLE; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.RealType.REAL; @@ -184,13 +183,13 @@ public Expression toExpression(Session session, @Nullable Object object, Type ty return new GenericLiteral("REAL", value.toString()); } - if (type instanceof DecimalType) { + if (type instanceof DecimalType decimalType) { String string; - if (isShortDecimal(type)) { - string = Decimals.toString((long) object, ((DecimalType) type).getScale()); + if (decimalType.isShort()) { + string = Decimals.toString((long) object, decimalType.getScale()); } else { - string = Decimals.toString((Int128) object, ((DecimalType) type).getScale()); + string = Decimals.toString((Int128) object, decimalType.getScale()); } return new Cast(new DecimalLiteral(string), toSqlType(type)); } diff --git a/core/trino-main/src/main/java/io/trino/type/DecimalCasts.java b/core/trino-main/src/main/java/io/trino/type/DecimalCasts.java index 023a247242ea..681385bee464 100644 --- a/core/trino-main/src/main/java/io/trino/type/DecimalCasts.java +++ b/core/trino-main/src/main/java/io/trino/type/DecimalCasts.java @@ -46,7 +46,6 @@ import static io.trino.spi.function.OperatorType.CAST; import static io.trino.spi.type.BigintType.BIGINT; import static io.trino.spi.type.BooleanType.BOOLEAN; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.Decimals.longTenToNth; import static io.trino.spi.type.Decimals.overflows; import static io.trino.spi.type.DoubleType.DOUBLE; @@ -109,7 +108,7 @@ private static SqlScalarFunction castFunctionFromDecimalTo(TypeSignature to, Str long precision = context.getLiteral("precision"); long scale = context.getLiteral("scale"); Object tenToScale; - if (isShortDecimal(context.getParameterTypes().get(0))) { + if (((DecimalType) context.getParameterTypes().get(0)).isShort()) { tenToScale = longTenToNth(DecimalConversions.intScale(scale)); } else { @@ -143,7 +142,7 @@ private static SqlScalarFunction castFunctionToDecimalFromBuilder(TypeSignature .withExtraParameters(context -> { DecimalType resultType = (DecimalType) context.getReturnType(); Object tenToScale; - if (isShortDecimal(resultType)) { + if (resultType.isShort()) { tenToScale = longTenToNth(resultType.getScale()); } else { diff --git a/core/trino-main/src/main/java/io/trino/util/JsonUtil.java b/core/trino-main/src/main/java/io/trino/util/JsonUtil.java index 1b14f5de8caf..19339f243e14 100644 --- a/core/trino-main/src/main/java/io/trino/util/JsonUtil.java +++ b/core/trino-main/src/main/java/io/trino/util/JsonUtil.java @@ -82,7 +82,6 @@ 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.Decimals.isShortDecimal; import static io.trino.spi.type.DoubleType.DOUBLE; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.RealType.REAL; @@ -229,9 +228,8 @@ static ObjectKeyProvider createObjectKeyProvider(Type type) if (type instanceof DoubleType) { return (block, position) -> String.valueOf(type.getDouble(block, position)); } - if (type instanceof DecimalType) { - DecimalType decimalType = (DecimalType) type; - if (isShortDecimal(decimalType)) { + if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { return (block, position) -> Decimals.toString(decimalType.getLong(block, position), decimalType.getScale()); } return (block, position) -> Decimals.toString( @@ -270,11 +268,11 @@ static JsonGeneratorWriter createJsonGeneratorWriter(Type type, boolean legacyRo if (type instanceof DoubleType) { return new DoubleJsonGeneratorWriter(); } - if (type instanceof DecimalType) { - if (isShortDecimal(type)) { - return new ShortDecimalJsonGeneratorWriter((DecimalType) type); + if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { + return new ShortDecimalJsonGeneratorWriter(decimalType); } - return new LongDecimalJsonGeneratorWriter((DecimalType) type); + return new LongDecimalJsonGeneratorWriter(decimalType); } if (type instanceof VarcharType) { return new VarcharJsonGeneratorWriter(type); @@ -925,12 +923,12 @@ static BlockBuilderAppender createBlockBuilderAppender(Type type) if (type instanceof DoubleType) { return new DoubleBlockBuilderAppender(); } - if (type instanceof DecimalType) { - if (isShortDecimal(type)) { - return new ShortDecimalBlockBuilderAppender((DecimalType) type); + if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { + return new ShortDecimalBlockBuilderAppender(decimalType); } - return new LongDecimalBlockBuilderAppender((DecimalType) type); + return new LongDecimalBlockBuilderAppender(decimalType); } if (type instanceof VarcharType) { return new VarcharBlockBuilderAppender(type); diff --git a/core/trino-spi/src/main/java/io/trino/spi/connector/InMemoryRecordSet.java b/core/trino-spi/src/main/java/io/trino/spi/connector/InMemoryRecordSet.java index d49ccbbce493..3fb017724f8f 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/connector/InMemoryRecordSet.java +++ b/core/trino-spi/src/main/java/io/trino/spi/connector/InMemoryRecordSet.java @@ -17,6 +17,7 @@ import io.airlift.slice.Slices; import io.trino.spi.block.Block; import io.trino.spi.type.ArrayType; +import io.trino.spi.type.DecimalType; import io.trino.spi.type.Int128; import io.trino.spi.type.LongTimeWithTimeZone; import io.trino.spi.type.LongTimestamp; @@ -34,8 +35,6 @@ 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.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.DoubleType.DOUBLE; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS; @@ -248,11 +247,11 @@ else if (type instanceof RowType) { checkArgument(value instanceof Block, "Expected value %d to be an instance of Block, but is a %s", i, value.getClass().getSimpleName()); } - else if (isShortDecimal(type)) { + else if (type instanceof DecimalType decimalType && decimalType.isShort()) { checkArgument(value instanceof Long, "Expected value %d to be an instance of Long, but is a %s", i, value.getClass().getSimpleName()); } - else if (isLongDecimal(type)) { + else if (type instanceof DecimalType decimalType && !decimalType.isShort()) { checkArgument(value instanceof Int128, "Expected value %d to be an instance of LongDecimal, but is a %s", i, value.getClass().getSimpleName()); } diff --git a/core/trino-spi/src/main/java/io/trino/spi/type/Decimals.java b/core/trino-spi/src/main/java/io/trino/spi/type/Decimals.java index 65bccd09a536..763e1236235b 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/type/Decimals.java +++ b/core/trino-spi/src/main/java/io/trino/spi/type/Decimals.java @@ -259,11 +259,19 @@ public static BigInteger rescale(BigInteger value, int fromScale, int toScale) return value.multiply(bigIntegerTenToNth(toScale - fromScale)); } + /** + * @deprecated Use {@link DecimalType#isShort()} + */ + @Deprecated public static boolean isShortDecimal(Type type) { return type instanceof ShortDecimalType; } + /** + * @deprecated Use {@link DecimalType#isShort()} + */ + @Deprecated public static boolean isLongDecimal(Type type) { return type instanceof LongDecimalType; diff --git a/lib/trino-orc/src/test/java/io/trino/orc/OrcTester.java b/lib/trino-orc/src/test/java/io/trino/orc/OrcTester.java index f0393b82a350..e10a44d3d9b6 100644 --- a/lib/trino-orc/src/test/java/io/trino/orc/OrcTester.java +++ b/lib/trino-orc/src/test/java/io/trino/orc/OrcTester.java @@ -32,7 +32,6 @@ import io.trino.spi.type.ArrayType; import io.trino.spi.type.CharType; import io.trino.spi.type.DecimalType; -import io.trino.spi.type.Decimals; import io.trino.spi.type.Int128; import io.trino.spi.type.LongTimestamp; import io.trino.spi.type.LongTimestampWithTimeZone; @@ -695,11 +694,13 @@ private static void writeValue(Type type, BlockBuilder blockBuilder, Object valu else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) { type.writeLong(blockBuilder, ((Number) value).longValue()); } - else if (Decimals.isShortDecimal(type)) { - type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue()); - } - else if (Decimals.isLongDecimal(type)) { - type.writeObject(blockBuilder, Int128.valueOf(((SqlDecimal) value).toBigDecimal().unscaledValue())); + else if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { + type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue()); + } + else { + type.writeObject(blockBuilder, Int128.valueOf(((SqlDecimal) value).toBigDecimal().unscaledValue())); + } } else if (DOUBLE.equals(type)) { type.writeDouble(blockBuilder, ((Number) value).doubleValue()); diff --git a/lib/trino-rcfile/src/main/java/io/trino/rcfile/binary/DecimalEncoding.java b/lib/trino-rcfile/src/main/java/io/trino/rcfile/binary/DecimalEncoding.java index 2d46c34095b3..4df1218e111a 100644 --- a/lib/trino-rcfile/src/main/java/io/trino/rcfile/binary/DecimalEncoding.java +++ b/lib/trino-rcfile/src/main/java/io/trino/rcfile/binary/DecimalEncoding.java @@ -31,7 +31,6 @@ import static io.trino.rcfile.RcFileDecoderUtils.decodeVIntSize; import static io.trino.rcfile.RcFileDecoderUtils.readVInt; import static io.trino.rcfile.RcFileDecoderUtils.writeVInt; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.Decimals.rescale; import static java.lang.Math.toIntExact; @@ -64,7 +63,7 @@ public void encodeColumn(Block block, SliceOutput output, EncodeOutput encodeOut @Override public void encodeValueInto(Block block, int position, SliceOutput output) { - if (isShortDecimal(type)) { + if (type.isShort()) { writeLong(output, type.getLong(block, position)); } else { @@ -85,7 +84,7 @@ public Block decodeColumn(ColumnData columnData) if (length == 0) { builder.appendNull(); } - else if (isShortDecimal(type)) { + else if (type.isShort()) { type.writeLong(builder, parseLong(slice, offset)); } else { @@ -117,7 +116,7 @@ public int getValueLength(Slice slice, int offset) @Override public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length) { - if (isShortDecimal(type)) { + if (type.isShort()) { type.writeLong(builder, parseLong(slice, offset)); } else { diff --git a/lib/trino-rcfile/src/main/java/io/trino/rcfile/text/DecimalEncoding.java b/lib/trino-rcfile/src/main/java/io/trino/rcfile/text/DecimalEncoding.java index 2d98dc0ba3f7..4fff1d62ea1b 100644 --- a/lib/trino-rcfile/src/main/java/io/trino/rcfile/text/DecimalEncoding.java +++ b/lib/trino-rcfile/src/main/java/io/trino/rcfile/text/DecimalEncoding.java @@ -29,7 +29,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static io.airlift.slice.Slices.utf8Slice; -import static io.trino.spi.type.Decimals.isShortDecimal; import static java.math.RoundingMode.HALF_UP; public class DecimalEncoding @@ -67,7 +66,7 @@ public void encodeValueInto(int depth, Block block, int position, SliceOutput ou private void encodeValue(Block block, int position, SliceOutput output) { - if (isShortDecimal(type)) { + if (type.isShort()) { output.writeBytes(utf8Slice(Decimals.toString(type.getLong(block, position), type.getScale()))); } else { @@ -88,7 +87,7 @@ public Block decodeColumn(ColumnData columnData) if (length == 0 || nullSequence.equals(0, nullSequence.length(), slice, offset, length)) { builder.appendNull(); } - else if (isShortDecimal(type)) { + else if (type.isShort()) { type.writeLong(builder, parseLong(slice, offset, length)); } else { @@ -101,7 +100,7 @@ else if (isShortDecimal(type)) { @Override public void decodeValueInto(int depth, BlockBuilder builder, Slice slice, int offset, int length) { - if (isShortDecimal(type)) { + if (type.isShort()) { type.writeLong(builder, parseLong(slice, offset, length)); } else { diff --git a/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryStoragePageSource.java b/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryStoragePageSource.java index 3bc420383932..88f37d3836a5 100644 --- a/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryStoragePageSource.java +++ b/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryStoragePageSource.java @@ -62,7 +62,6 @@ import static io.trino.spi.type.DateType.DATE; import static io.trino.spi.type.Decimals.encodeShortScaledValue; import static io.trino.spi.type.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.LongTimestampWithTimeZone.fromEpochMillisAndFraction; import static io.trino.spi.type.TimeType.TIME_MICROS; @@ -170,9 +169,8 @@ else if (javaType == long.class) { else if (type.equals(INTEGER)) { type.writeLong(output, ((Number) value).intValue()); } - else if (type instanceof DecimalType) { - verify(isShortDecimal(type), "The type should be short decimal"); - DecimalType decimalType = (DecimalType) type; + else if (type instanceof DecimalType decimalType) { + verify(decimalType.isShort(), "The type should be short decimal"); BigDecimal decimal = DECIMAL_CONVERTER.convert(decimalType.getPrecision(), decimalType.getScale(), value); type.writeLong(output, encodeShortScaledValue(decimal, decimalType.getScale())); } diff --git a/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryType.java b/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryType.java index 1838da94c5b2..81ab23f1cc55 100644 --- a/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryType.java +++ b/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryType.java @@ -60,7 +60,6 @@ import static io.trino.plugin.bigquery.BigQueryMetadata.DEFAULT_NUMERIC_TYPE_SCALE; import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; import static io.trino.spi.type.DecimalType.createDecimalType; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.TimeWithTimeZoneType.DEFAULT_PRECISION; import static io.trino.spi.type.TimeWithTimeZoneType.createTimeWithTimeZoneType; import static io.trino.spi.type.TimeZoneKey.getTimeZoneKey; @@ -321,10 +320,10 @@ public Optional convertToString(Type type, Object value) if (type instanceof ArrayType) { return Optional.empty(); } - if (type instanceof DecimalType) { + if (type instanceof DecimalType decimalType) { String bigqueryTypeName = this.toString(); verify(bigqueryTypeName.equals("NUMERIC") || bigqueryTypeName.equals("BIGNUMERIC"), "Expected NUMERIC or BIGNUMERIC: %s", bigqueryTypeName); - if (isShortDecimal(type)) { + if (decimalType.isShort()) { return Optional.of(format("%s '%s'", bigqueryTypeName, Decimals.toString((long) value, ((DecimalType) type).getScale()))); } return Optional.of(format("%s '%s'", bigqueryTypeName, Decimals.toString((Int128) value, ((DecimalType) type).getScale()))); diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveRecordCursor.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveRecordCursor.java index b726e3127fdc..172f8f480849 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveRecordCursor.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveRecordCursor.java @@ -20,6 +20,7 @@ import io.trino.spi.TrinoException; import io.trino.spi.connector.RecordCursor; import io.trino.spi.type.CharType; +import io.trino.spi.type.DecimalType; import io.trino.spi.type.Type; import io.trino.spi.type.VarcharType; @@ -32,8 +33,6 @@ 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.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.DoubleType.DOUBLE; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.RealType.REAL; @@ -128,10 +127,10 @@ else if (TIMESTAMP_MILLIS.equals(type)) { else if (TIMESTAMP_TZ_MILLIS.equals(type)) { longs[columnIndex] = (long) prefilledValue; } - else if (isShortDecimal(type)) { + else if (type instanceof DecimalType decimalType && decimalType.isShort()) { longs[columnIndex] = (long) prefilledValue; } - else if (isLongDecimal(type)) { + else if (type instanceof DecimalType decimalType && !decimalType.isShort()) { objects[columnIndex] = prefilledValue; } else { diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/MetastoreUtil.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/MetastoreUtil.java index 219044f2d716..a24c6c0fe6ce 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/MetastoreUtil.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/MetastoreUtil.java @@ -390,11 +390,11 @@ public static String sqlScalarToString(Type type, Object value, String nullStrin Slice slice = (Slice) value; return slice.toStringUtf8(); } - if (type instanceof DecimalType && !((DecimalType) type).isShort()) { - return Decimals.toString((Int128) value, ((DecimalType) type).getScale()); + if (type instanceof DecimalType decimalType && !decimalType.isShort()) { + return Decimals.toString((Int128) value, decimalType.getScale()); } - if (type instanceof DecimalType && ((DecimalType) type).isShort()) { - return Decimals.toString((long) value, ((DecimalType) type).getScale()); + if (type instanceof DecimalType decimalType && decimalType.isShort()) { + return Decimals.toString((long) value, decimalType.getScale()); } if (type instanceof DateType) { DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.date().withZoneUTC(); diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/util/HiveUtil.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/util/HiveUtil.java index 38f11ad8f515..7d195d022475 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/util/HiveUtil.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/util/HiveUtil.java @@ -144,8 +144,6 @@ import static io.trino.spi.type.DateTimeEncoding.packDateTimeWithZone; import static io.trino.spi.type.DateType.DATE; import static io.trino.spi.type.DecimalType.createDecimalType; -import static io.trino.spi.type.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.DoubleType.DOUBLE; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.RealType.REAL; @@ -1021,11 +1019,11 @@ else if (isPartitionColumnHandle(columnHandle)) { // used for $file_modified_time return NullableValue.of(type, packDateTimeWithZone(floorDiv(timestampPartitionKey(columnValue, name), MICROSECONDS_PER_MILLISECOND), DateTimeZone.getDefault().getID())); } - if (isShortDecimal(type)) { - return NullableValue.of(type, shortDecimalPartitionKey(columnValue, (DecimalType) type, name)); - } - if (isLongDecimal(type)) { - return NullableValue.of(type, longDecimalPartitionKey(columnValue, (DecimalType) type, name)); + if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { + return NullableValue.of(type, shortDecimalPartitionKey(columnValue, decimalType, name)); + } + return NullableValue.of(type, longDecimalPartitionKey(columnValue, decimalType, name)); } if (type.equals(VarbinaryType.VARBINARY)) { return NullableValue.of(type, utf8Slice(columnValue)); diff --git a/plugin/trino-hive/src/test/java/io/trino/plugin/hive/parquet/ParquetTester.java b/plugin/trino-hive/src/test/java/io/trino/plugin/hive/parquet/ParquetTester.java index 566fc1d08f6a..34c9543453fc 100644 --- a/plugin/trino-hive/src/test/java/io/trino/plugin/hive/parquet/ParquetTester.java +++ b/plugin/trino-hive/src/test/java/io/trino/plugin/hive/parquet/ParquetTester.java @@ -803,11 +803,11 @@ private static void writeValue(Type type, BlockBuilder blockBuilder, Object valu else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) { type.writeLong(blockBuilder, ((Number) value).longValue()); } - else if (Decimals.isShortDecimal(type)) { - type.writeLong(blockBuilder, ((SqlDecimal) value).getUnscaledValue().longValue()); - } - else if (Decimals.isLongDecimal(type)) { - if (Decimals.overflows(((SqlDecimal) value).getUnscaledValue(), MAX_PRECISION_INT64)) { + else if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { + type.writeLong(blockBuilder, ((SqlDecimal) value).getUnscaledValue().longValue()); + } + else if (Decimals.overflows(((SqlDecimal) value).getUnscaledValue(), MAX_PRECISION_INT64)) { type.writeObject(blockBuilder, Int128.valueOf(((SqlDecimal) value).toBigDecimal().unscaledValue())); } else { diff --git a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/ExpressionConverter.java b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/ExpressionConverter.java index 57428864f4f1..205ce2183afa 100644 --- a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/ExpressionConverter.java +++ b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/ExpressionConverter.java @@ -23,7 +23,6 @@ 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; @@ -222,9 +221,8 @@ private static Object getIcebergLiteralValue(Type type, Object trinoNativeValue) return trinoUuidToJavaUuid(((Slice) trinoNativeValue)); } - if (type instanceof DecimalType) { - DecimalType decimalType = (DecimalType) type; - if (Decimals.isShortDecimal(decimalType)) { + if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { return BigDecimal.valueOf((long) trinoNativeValue).movePointLeft(decimalType.getScale()); } return new BigDecimal(((Int128) trinoNativeValue).toBigInteger(), decimalType.getScale()); 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 56d4c27ca253..81bea4d0a70c 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 @@ -27,7 +27,6 @@ import static io.airlift.slice.Slices.utf8Slice; import static io.trino.plugin.iceberg.util.Timestamps.timestampTzFromMicros; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_MICROSECOND; public final class IcebergTypes @@ -60,10 +59,9 @@ public static Object convertIcebergValueToTrino(Type icebergType, Object value) //noinspection RedundantCast return (Double) value; } - if (icebergType instanceof Types.DecimalType) { - Types.DecimalType icebergDecimalType = (Types.DecimalType) icebergType; + if (icebergType instanceof Types.DecimalType icebergDecimalType) { DecimalType trinoDecimalType = DecimalType.createDecimalType(icebergDecimalType.precision(), icebergDecimalType.scale()); - if (isShortDecimal(trinoDecimalType)) { + if (trinoDecimalType.isShort()) { return Decimals.encodeShortScaledValue((BigDecimal) value, trinoDecimalType.getScale()); } return Decimals.encodeScaledValue((BigDecimal) value, trinoDecimalType.getScale()); @@ -84,9 +82,9 @@ public static Object convertIcebergValueToTrino(Type icebergType, Object value) if (icebergType instanceof Types.TimeType) { return Math.multiplyExact((Long) value, PICOSECONDS_PER_MICROSECOND); } - if (icebergType instanceof Types.TimestampType) { + if (icebergType instanceof Types.TimestampType icebergTimestampType) { long epochMicros = (long) value; - if (((Types.TimestampType) icebergType).shouldAdjustToUTC()) { + if (icebergTimestampType.shouldAdjustToUTC()) { return timestampTzFromMicros(epochMicros); } return epochMicros; diff --git a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergUtil.java b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergUtil.java index 512c5ef2331c..475f29c815a0 100644 --- a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergUtil.java +++ b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergUtil.java @@ -116,8 +116,6 @@ 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.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.DoubleType.DOUBLE; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.RealType.REAL; @@ -473,15 +471,14 @@ public static Object deserializePartitionValue(Type type, String valueString, St if (type.equals(UuidType.UUID)) { return javaUuidToTrinoUuid(UUID.fromString(valueString)); } - if (isShortDecimal(type) || isLongDecimal(type)) { - DecimalType decimalType = (DecimalType) type; + if (type instanceof DecimalType decimalType) { BigDecimal decimal = new BigDecimal(valueString); decimal = decimal.setScale(decimalType.getScale(), BigDecimal.ROUND_UNNECESSARY); if (decimal.precision() > decimalType.getPrecision()) { throw new IllegalArgumentException(); } BigInteger unscaledValue = decimal.unscaledValue(); - return isShortDecimal(type) ? unscaledValue.longValue() : Int128.valueOf(unscaledValue); + return decimalType.isShort() ? unscaledValue.longValue() : Int128.valueOf(unscaledValue); } } catch (IllegalArgumentException e) { diff --git a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/PartitionTransforms.java b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/PartitionTransforms.java index 57c2b75be741..5a607403719a 100644 --- a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/PartitionTransforms.java +++ b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/PartitionTransforms.java @@ -48,8 +48,6 @@ import static io.trino.spi.type.DateType.DATE; import static io.trino.spi.type.Decimals.encodeScaledValue; import static io.trino.spi.type.Decimals.encodeShortScaledValue; -import static io.trino.spi.type.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.Decimals.readBigDecimal; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.TimeType.TIME_MICROS; @@ -145,13 +143,11 @@ public static ColumnTransform getColumnTransform(PartitionField field, Type sour if (sourceType.equals(BIGINT)) { return truncateBigint(width); } - if (isShortDecimal(sourceType)) { - DecimalType decimal = (DecimalType) sourceType; - return truncateShortDecimal(sourceType, width, decimal); - } - if (isLongDecimal(sourceType)) { - DecimalType decimal = (DecimalType) sourceType; - return truncateLongDecimal(sourceType, width, decimal); + if (sourceType instanceof DecimalType decimalType) { + if (decimalType.isShort()) { + return truncateShortDecimal(sourceType, width, decimalType); + } + return truncateLongDecimal(sourceType, width, decimalType); } if (sourceType instanceof VarcharType) { return truncateVarchar(width); @@ -197,11 +193,11 @@ private static Hasher getBucketingHash(Type type) if (type.equals(BIGINT)) { return PartitionTransforms::hashBigint; } - if (isShortDecimal(type)) { - return hashShortDecimal((DecimalType) type); - } - if (isLongDecimal(type)) { - return hashLongDecimal((DecimalType) type); + if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { + return hashShortDecimal(decimalType); + } + return hashLongDecimal(decimalType); } if (type.equals(DATE)) { return PartitionTransforms::hashDate; diff --git a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/util/Timestamps.java b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/util/Timestamps.java index 6c5703d725c8..502386ba13c7 100644 --- a/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/util/Timestamps.java +++ b/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/util/Timestamps.java @@ -13,6 +13,7 @@ */ package io.trino.plugin.iceberg.util; +import com.google.common.math.LongMath; import io.trino.spi.block.Block; import io.trino.spi.type.LongTimestampWithTimeZone; @@ -20,9 +21,9 @@ import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS; import static io.trino.spi.type.Timestamps.MICROSECONDS_PER_MILLISECOND; import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_MICROSECOND; -import static io.trino.spi.type.Timestamps.roundDiv; import static java.lang.Math.floorDiv; import static java.lang.Math.floorMod; +import static java.math.RoundingMode.UNNECESSARY; public final class Timestamps { @@ -31,7 +32,7 @@ private Timestamps() {} public static long timestampTzToMicros(LongTimestampWithTimeZone timestamp) { return (timestamp.getEpochMillis() * MICROSECONDS_PER_MILLISECOND) + - roundDiv(timestamp.getPicosOfMilli(), PICOSECONDS_PER_MICROSECOND); + LongMath.divide(timestamp.getPicosOfMilli(), PICOSECONDS_PER_MICROSECOND, UNNECESSARY); } public static LongTimestampWithTimeZone timestampTzFromMicros(long epochMicros) diff --git a/plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/IcebergQueryRunner.java b/plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/IcebergQueryRunner.java index 0257df4516b2..3e06b8d33e6e 100644 --- a/plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/IcebergQueryRunner.java +++ b/plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/IcebergQueryRunner.java @@ -134,8 +134,10 @@ public DistributedQueryRunner build() } } - public static class IcebergGlueQueryRunnerMain + public static final class IcebergGlueQueryRunnerMain { + private IcebergGlueQueryRunnerMain() {} + public static void main(String[] args) throws Exception { diff --git a/plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/TestIcebergBucketing.java b/plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/TestIcebergBucketing.java index 3096d7cc5eaa..5e67a5456100 100644 --- a/plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/TestIcebergBucketing.java +++ b/plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/TestIcebergBucketing.java @@ -49,8 +49,6 @@ import static io.trino.plugin.iceberg.TypeConverter.toTrinoType; import static io.trino.spi.type.BigintType.BIGINT; import static io.trino.spi.type.DateType.DATE; -import static io.trino.spi.type.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.IntegerType.INTEGER; import static io.trino.spi.type.TimeType.TIME_MICROS; import static io.trino.spi.type.TimeZoneKey.UTC_KEY; @@ -315,12 +313,11 @@ private static Object toTrinoValue(Type icebergType, Object icebergValue) return (long) icebergValue; } - if (isShortDecimal(trinoType)) { - return Decimals.encodeShortScaledValue((BigDecimal) icebergValue, ((io.trino.spi.type.DecimalType) trinoType).getScale()); - } - - if (isLongDecimal(trinoType)) { - return Decimals.encodeScaledValue((BigDecimal) icebergValue, ((io.trino.spi.type.DecimalType) trinoType).getScale()); + if (trinoType instanceof io.trino.spi.type.DecimalType trinoDecimalType) { + if (trinoDecimalType.isShort()) { + return Decimals.encodeShortScaledValue((BigDecimal) icebergValue, trinoDecimalType.getScale()); + } + return Decimals.encodeScaledValue((BigDecimal) icebergValue, trinoDecimalType.getScale()); } if (trinoType == VARCHAR) { diff --git a/plugin/trino-tpcds/src/main/java/io/trino/plugin/tpcds/statistics/TpcdsTableStatisticsFactory.java b/plugin/trino-tpcds/src/main/java/io/trino/plugin/tpcds/statistics/TpcdsTableStatisticsFactory.java index 595691a8b73c..ac960313e602 100644 --- a/plugin/trino-tpcds/src/main/java/io/trino/plugin/tpcds/statistics/TpcdsTableStatisticsFactory.java +++ b/plugin/trino-tpcds/src/main/java/io/trino/plugin/tpcds/statistics/TpcdsTableStatisticsFactory.java @@ -35,8 +35,6 @@ import static io.trino.spi.type.BigintType.BIGINT; import static io.trino.spi.type.DateType.DATE; -import static io.trino.spi.type.Decimals.isLongDecimal; -import static io.trino.spi.type.Decimals.isShortDecimal; import static io.trino.spi.type.DoubleType.DOUBLE; import static io.trino.spi.type.IntegerType.INTEGER; import static java.lang.Double.parseDouble; @@ -99,15 +97,11 @@ private static double toDouble(Object value, Type type) if (type.equals(BIGINT) || type.equals(INTEGER) || type.equals(DATE)) { return ((Number) value).doubleValue(); } - if (type instanceof DecimalType) { - DecimalType decimalType = (DecimalType) type; - if (isShortDecimal(decimalType)) { + if (type instanceof DecimalType decimalType) { + if (decimalType.isShort()) { return parseDouble(Decimals.toString(((Number) value).longValue(), decimalType.getScale())); } - if (isLongDecimal(decimalType)) { - return parseDouble(Decimals.toString((Int128) value, decimalType.getScale())); - } - throw new IllegalArgumentException("Unexpected decimal type: " + decimalType); + return parseDouble(Decimals.toString((Int128) value, decimalType.getScale())); } if (type.equals(DOUBLE)) { return ((Number) value).doubleValue();