diff --git a/core/trino-main/src/main/java/io/trino/sql/gen/DereferenceCodeGenerator.java b/core/trino-main/src/main/java/io/trino/sql/gen/DereferenceCodeGenerator.java index 6f5736356791..9c0f5da61a9e 100644 --- a/core/trino-main/src/main/java/io/trino/sql/gen/DereferenceCodeGenerator.java +++ b/core/trino-main/src/main/java/io/trino/sql/gen/DereferenceCodeGenerator.java @@ -44,7 +44,7 @@ public DereferenceCodeGenerator(SpecialForm specialForm) returnType = specialForm.getType(); checkArgument(specialForm.getArguments().size() == 2); base = specialForm.getArguments().get(0); - index = (int) ((ConstantExpression) specialForm.getArguments().get(1)).getValue(); + index = toIntValue(((ConstantExpression) specialForm.getArguments().get(1)).getValue()); } @Override @@ -93,4 +93,13 @@ public BytecodeNode generateExpression(BytecodeGeneratorContext generator) return block; } + + private static int toIntValue(Object value) + { + if (value instanceof Number number) { + return number.intValue(); + } + + return (int) value; + } } diff --git a/core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java b/core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java index 7e5cd97a39ed..acca8963a919 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java +++ b/core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java @@ -83,7 +83,7 @@ else if (type.getJavaType() == double.class) { type.writeDouble(blockBuilder, ((double) value)); } else if (type.getJavaType() == long.class) { - type.writeLong(blockBuilder, ((long) value)); + type.writeLong(blockBuilder, castToLong(value)); } else if (type.getJavaType() == Slice.class) { Slice slice; @@ -117,6 +117,15 @@ public static boolean isFloatingPointNaN(Type type, Object value) return false; } + private static long castToLong(Object value) + { + if (value instanceof Number number) { + return number.longValue(); + } + + return (long) value; + } + static void checkElementNotNull(boolean isNull, String errorMsg) { if (isNull) {