Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -215,12 +212,11 @@ private static BiFunction<ConnectorSession, Block, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
5 changes: 2 additions & 3 deletions core/trino-main/src/main/java/io/trino/type/DecimalCasts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 10 additions & 12 deletions core/trino-main/src/main/java/io/trino/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
8 changes: 8 additions & 0 deletions core/trino-spi/src/main/java/io/trino/spi/type/Decimals.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions lib/trino-orc/src/test/java/io/trino/orc/OrcTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -321,10 +320,10 @@ public Optional<String> 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())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading