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
30 changes: 28 additions & 2 deletions core/trino-main/src/main/java/io/trino/type/DoubleOperators.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.trino.spi.function.SqlType;
import io.trino.spi.type.StandardTypes;

import java.text.DecimalFormat;

import static com.google.common.base.Preconditions.checkState;
import static io.airlift.slice.Slices.utf8Slice;
import static io.trino.spi.StandardErrorCode.INVALID_CAST_ARGUMENT;
Expand Down Expand Up @@ -53,6 +55,8 @@ public final class DoubleOperators
private static final double MIN_BYTE_AS_DOUBLE = -0x1p7;
private static final double MAX_BYTE_PLUS_ONE_AS_DOUBLE = 0x1p7;

private static final ThreadLocal<DecimalFormat> FORMAT = ThreadLocal.withInitial(() -> new DecimalFormat("0.0###################E0"));

private DoubleOperators()
{
}
Expand Down Expand Up @@ -175,13 +179,35 @@ public static long castToReal(@SqlType(StandardTypes.DOUBLE) double value)
@SqlType("varchar(x)")
public static Slice castToVarchar(@LiteralParameter("x") long x, @SqlType(StandardTypes.DOUBLE) double value)
{
String stringValue = String.valueOf(value);
String stringValue;

// handle positive and negative 0
if (value == 0e0) {
if (1e0 / value > 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, arguably more directly, Double.compare(0.0, value) == 0
the Double.compare sorts negative zero before positive zero

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's clearer the way it is. Double.compare would require a comment.

stringValue = "0E0";
}
else {
stringValue = "-0E0";
}
}
else if (Double.isInfinite(value)) {
if (value > 0) {
stringValue = "Infinity";
}
else {
stringValue = "-Infinity";
}
}
else {
stringValue = FORMAT.get().format(value);
}

// String is all-ASCII, so String.length() here returns actual code points count
if (stringValue.length() <= x) {
return utf8Slice(stringValue);
}

throw new TrinoException(INVALID_CAST_ARGUMENT, format("Value %s cannot be represented as varchar(%s)", value, x));
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Value %s (%s) cannot be represented as varchar(%s)", value, stringValue, x));
}

@ScalarOperator(SATURATED_FLOOR_CAST)
Expand Down
31 changes: 29 additions & 2 deletions core/trino-main/src/main/java/io/trino/type/RealOperators.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.trino.spi.function.SqlType;
import io.trino.spi.type.StandardTypes;

import java.text.DecimalFormat;

import static io.airlift.slice.Slices.utf8Slice;
import static io.trino.spi.StandardErrorCode.INVALID_CAST_ARGUMENT;
import static io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE;
Expand All @@ -49,6 +51,8 @@ public final class RealOperators
private static final float MIN_BYTE_AS_FLOAT = -0x1p7f;
private static final float MAX_BYTE_PLUS_ONE_AS_FLOAT = 0x1p7f;

private static final ThreadLocal<DecimalFormat> FORMAT = ThreadLocal.withInitial(() -> new DecimalFormat("0.0#####E0"));

private RealOperators()
{
}
Expand Down Expand Up @@ -100,13 +104,36 @@ public static long negate(@SqlType(StandardTypes.REAL) long value)
@SqlType("varchar(x)")
public static Slice castToVarchar(@LiteralParameter("x") long x, @SqlType(StandardTypes.REAL) long value)
{
String stringValue = String.valueOf(intBitsToFloat((int) value));
float floatValue = intBitsToFloat((int) value);
String stringValue;

// handle positive and negative 0
if (floatValue == 0.0f) {
if (1.0f / floatValue > 0) {
stringValue = "0E0";
}
else {
stringValue = "-0E0";
}
}
else if (Float.isInfinite(floatValue)) {
if (floatValue > 0) {
stringValue = "Infinity";
}
else {
stringValue = "-Infinity";
}
}
else {
stringValue = FORMAT.get().format(Double.parseDouble(Float.toString(floatValue)));
}

// String is all-ASCII, so String.length() here returns actual code points count
if (stringValue.length() <= x) {
return utf8Slice(stringValue);
}

throw new TrinoException(INVALID_CAST_ARGUMENT, format("Value %s cannot be represented as varchar(%s)", stringValue, x));
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Value %s (%s) cannot be represented as varchar(%s)", floatValue, stringValue, x));
}

@ScalarOperator(CAST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testTypeCombinations()
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> CAST(x AS BIGINT))", new ArrayType(BIGINT), ImmutableList.of(26L, 27L));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> x + 1.0E0)", new ArrayType(DOUBLE), ImmutableList.of(26.6, 28.3));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> x = 25.6E0)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> CAST(x AS VARCHAR))", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("25.6", "27.3"));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> CAST(x AS VARCHAR))", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("2.56E1", "2.73E1"));
assertFunction(
"transform(ARRAY [25.6E0, 27.3E0], x -> MAP(ARRAY[x + 1], ARRAY[true]))",
new ArrayType(mapType(DOUBLE, BOOLEAN)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void testTypeCombinations()
assertFunction("apply(25.6E0, x -> CAST(x AS BIGINT))", BIGINT, 26L);
assertFunction("apply(25.6E0, x -> x + 1.0E0)", DOUBLE, 26.6);
assertFunction("apply(25.6E0, x -> x = 25.6E0)", BOOLEAN, true);
assertFunction("apply(25.6E0, x -> CAST(x AS VARCHAR))", createUnboundedVarcharType(), "25.6");
assertFunction("apply(25.6E0, x -> CAST(x AS VARCHAR))", createUnboundedVarcharType(), "2.56E1");
assertFunction("apply(25.6E0, x -> MAP(ARRAY[x + 1], ARRAY[true]))", mapType(DOUBLE, BOOLEAN), ImmutableMap.of(26.6, true));

assertFunction("apply(true, x -> if(x, 25, 26))", INTEGER, 25);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ public void testTypeCombinations()
assertFunction(
"transform_keys(map(ARRAY [25.5E0, 26.5E0, 27.5E0], ARRAY ['abc', 'def', 'xyz']), (k, v) -> CAST(k AS VARCHAR) || substr(v, 1, 1))",
mapType(VARCHAR, createVarcharType(3)),
ImmutableMap.of("25.5a", "abc", "26.5d", "def", "27.5x", "xyz"));
ImmutableMap.of("2.55E1a", "abc", "2.65E1d", "def", "2.75E1x", "xyz"));
assertFunction(
"transform_keys(map(ARRAY [25.5E0, 26.5E0], ARRAY [ARRAY ['a'], ARRAY ['b']]), (k, v) -> ARRAY [CAST(k AS VARCHAR)] || v)",
mapType(new ArrayType(VARCHAR), new ArrayType(createVarcharType(1))),
ImmutableMap.of(ImmutableList.of("25.5", "a"), ImmutableList.of("a"), ImmutableList.of("26.5", "b"), ImmutableList.of("b")));
ImmutableMap.of(ImmutableList.of("2.55E1", "a"), ImmutableList.of("a"), ImmutableList.of("2.65E1", "b"), ImmutableList.of("b")));

assertFunction(
"transform_keys(map(ARRAY [true, false], ARRAY [25, 26]), (k, v) -> if(k, 2 * v, 3 * v))",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void testBasic()
assertFunction(
"transform_values(map(ARRAY[1, 2, 3], ARRAY [1.0E0, 1.4E0, 1.7E0]), (k, v) -> map(ARRAY[1, 2, 3], ARRAY['one', 'two', 'three'])[k] || '_' || CAST(v AS VARCHAR))",
mapType(INTEGER, VARCHAR),
ImmutableMap.of(1, "one_1.0", 2, "two_1.4", 3, "three_1.7"));
ImmutableMap.of(1, "one_1.0E0", 2, "two_1.4E0", 3, "three_1.7E0"));

assertFunction(
"transform_values(map(ARRAY[1, 2], ARRAY [TIMESTAMP '2020-05-10 12:34:56.123456789', TIMESTAMP '2010-05-10 12:34:56.123456789']), (k, v) -> date_add('year', 1, v))",
Expand Down Expand Up @@ -183,7 +183,7 @@ public void testTypeCombinations()
assertFunction(
"transform_values(map(ARRAY ['s0', 's1', 's2'], ARRAY [25.5E0, 26.5E0, 27.5E0]), (k, v) -> k || ':' || CAST(v as VARCHAR))",
mapType(createVarcharType(2), VARCHAR),
ImmutableMap.of("s0", "s0:25.5", "s1", "s1:26.5", "s2", "s2:27.5"));
ImmutableMap.of("s0", "s0:2.55E1", "s1", "s1:2.65E1", "s2", "s2:2.75E1"));
assertFunction(
"transform_values(map(ARRAY ['s0', 's2'], ARRAY [false, true]), (k, v) -> if(v, k, CAST(v AS VARCHAR)))",
mapType(createVarcharType(2), VARCHAR),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,10 @@ public void testCastToString()
assertOptimizedEquals("CAST(-12300000000 AS varchar)", "'-12300000000'");

// double
assertOptimizedEquals("CAST(123.0E0 AS varchar)", "'123.0'");
assertOptimizedEquals("CAST(-123.0E0 AS varchar)", "'-123.0'");
assertOptimizedEquals("CAST(123.456E0 AS varchar)", "'123.456'");
assertOptimizedEquals("CAST(-123.456E0 AS varchar)", "'-123.456'");
assertOptimizedEquals("CAST(123.0E0 AS varchar)", "'1.23E2'");
assertOptimizedEquals("CAST(-123.0E0 AS varchar)", "'-1.23E2'");
assertOptimizedEquals("CAST(123.456E0 AS varchar)", "'1.23456E2'");
assertOptimizedEquals("CAST(-123.456E0 AS varchar)", "'-1.23456E2'");

// boolean
assertOptimizedEquals("CAST(true AS varchar)", "'true'");
Expand Down Expand Up @@ -729,51 +729,48 @@ public void testCastDoubleToBoundedVarchar()
assertEvaluatedEquals("CAST(DOUBLE 'Infinity' AS varchar(8))", "'Infinity'");
assertEvaluatedEquals("CAST(DOUBLE 'Infinity' AS varchar(50))", "'Infinity'");

// incorrect behavior: the string representation is not compliant with the SQL standard
assertEvaluatedEquals("CAST(0e0 AS varchar(3))", "'0.0'");
assertEvaluatedEquals("CAST(DOUBLE '0' AS varchar(3))", "'0.0'");
assertEvaluatedEquals("CAST(DOUBLE '-0' AS varchar(4))", "'-0.0'");
assertEvaluatedEquals("CAST(DOUBLE '0' AS varchar(50))", "'0.0'");
assertEvaluatedEquals("CAST(0e0 AS varchar(3))", "'0E0'");
assertEvaluatedEquals("CAST(DOUBLE '0' AS varchar(3))", "'0E0'");
assertEvaluatedEquals("CAST(DOUBLE '-0' AS varchar(4))", "'-0E0'");
assertEvaluatedEquals("CAST(DOUBLE '0' AS varchar(50))", "'0E0'");

assertEvaluatedEquals("CAST(12e0 AS varchar(4))", "'12.0'");
assertEvaluatedEquals("CAST(12e2 AS varchar(6))", "'1200.0'");
assertEvaluatedEquals("CAST(12e-2 AS varchar(4))", "'0.12'");
assertEvaluatedEquals("CAST(12e0 AS varchar(5))", "'1.2E1'");
assertEvaluatedEquals("CAST(12e2 AS varchar(6))", "'1.2E3'");
assertEvaluatedEquals("CAST(12e-2 AS varchar(6))", "'1.2E-1'");

assertEvaluatedEquals("CAST(12e0 AS varchar(50))", "'12.0'");
assertEvaluatedEquals("CAST(12e2 AS varchar(50))", "'1200.0'");
assertEvaluatedEquals("CAST(12e-2 AS varchar(50))", "'0.12'");
assertEvaluatedEquals("CAST(12e0 AS varchar(50))", "'1.2E1'");
assertEvaluatedEquals("CAST(12e2 AS varchar(50))", "'1.2E3'");
assertEvaluatedEquals("CAST(12e-2 AS varchar(50))", "'1.2E-1'");

assertEvaluatedEquals("CAST(-12e0 AS varchar(5))", "'-12.0'");
assertEvaluatedEquals("CAST(-12e2 AS varchar(7))", "'-1200.0'");
assertEvaluatedEquals("CAST(-12e-2 AS varchar(5))", "'-0.12'");
assertEvaluatedEquals("CAST(-12e0 AS varchar(6))", "'-1.2E1'");
assertEvaluatedEquals("CAST(-12e2 AS varchar(6))", "'-1.2E3'");
assertEvaluatedEquals("CAST(-12e-2 AS varchar(7))", "'-1.2E-1'");

assertEvaluatedEquals("CAST(-12e0 AS varchar(50))", "'-12.0'");
assertEvaluatedEquals("CAST(-12e2 AS varchar(50))", "'-1200.0'");
assertEvaluatedEquals("CAST(-12e-2 AS varchar(50))", "'-0.12'");
assertEvaluatedEquals("CAST(-12e0 AS varchar(50))", "'-1.2E1'");
assertEvaluatedEquals("CAST(-12e2 AS varchar(50))", "'-1.2E3'");
assertEvaluatedEquals("CAST(-12e-2 AS varchar(50))", "'-1.2E-1'");

// the string representation is compliant with the SQL standard
assertEvaluatedEquals("CAST(12345678.9e0 AS varchar(12))", "'1.23456789E7'");
assertEvaluatedEquals("CAST(0.00001e0 AS varchar(6))", "'1.0E-5'");

// the result value does not fit in the type (also, it is not compliant with the SQL standard)
// the result value does not fit in the type
assertTrinoExceptionThrownBy(() -> evaluate("CAST(12e0 AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value 12.0 cannot be represented as varchar(1)");
.hasMessage("Value 12.0 (1.2E1) cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(-12e2 AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value -1200.0 cannot be represented as varchar(1)");
.hasMessage("Value -1200.0 (-1.2E3) cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(0e0 AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value 0.0 cannot be represented as varchar(1)");
.hasMessage("Value 0.0 (0E0) cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(0e0 / 0e0 AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value NaN cannot be represented as varchar(1)");
.hasMessage("Value NaN (NaN) cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(DOUBLE 'Infinity' AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value Infinity cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(1200000e0 AS varchar(5))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value 1200000.0 cannot be represented as varchar(5)");
.hasMessage("Value Infinity (Infinity) cannot be represented as varchar(1)");

assertEvaluatedEquals("CAST(1200000e0 AS varchar(5))", "'1.2E6'");
}

@Test
Expand All @@ -787,50 +784,47 @@ public void testCastRealToBoundedVarchar()
assertEvaluatedEquals("CAST(REAL 'Infinity' AS varchar(8))", "'Infinity'");
assertEvaluatedEquals("CAST(REAL 'Infinity' AS varchar(50))", "'Infinity'");

// incorrect behavior: the string representation is not compliant with the SQL standard
assertEvaluatedEquals("CAST(REAL '0' AS varchar(3))", "'0.0'");
assertEvaluatedEquals("CAST(REAL '-0' AS varchar(4))", "'-0.0'");
assertEvaluatedEquals("CAST(REAL '0' AS varchar(50))", "'0.0'");
assertEvaluatedEquals("CAST(REAL '0' AS varchar(3))", "'0E0'");
assertEvaluatedEquals("CAST(REAL '-0' AS varchar(4))", "'-0E0'");
assertEvaluatedEquals("CAST(REAL '0' AS varchar(50))", "'0E0'");

assertEvaluatedEquals("CAST(REAL '12' AS varchar(4))", "'12.0'");
assertEvaluatedEquals("CAST(REAL '12e2' AS varchar(6))", "'1200.0'");
assertEvaluatedEquals("CAST(REAL '12e-2' AS varchar(4))", "'0.12'");
assertEvaluatedEquals("CAST(REAL '12' AS varchar(5))", "'1.2E1'");
assertEvaluatedEquals("CAST(REAL '12e2' AS varchar(5))", "'1.2E3'");
assertEvaluatedEquals("CAST(REAL '12e-2' AS varchar(6))", "'1.2E-1'");

assertEvaluatedEquals("CAST(REAL '12' AS varchar(50))", "'12.0'");
assertEvaluatedEquals("CAST(REAL '12e2' AS varchar(50))", "'1200.0'");
assertEvaluatedEquals("CAST(REAL '12e-2' AS varchar(50))", "'0.12'");
assertEvaluatedEquals("CAST(REAL '12' AS varchar(50))", "'1.2E1'");
assertEvaluatedEquals("CAST(REAL '12e2' AS varchar(50))", "'1.2E3'");
assertEvaluatedEquals("CAST(REAL '12e-2' AS varchar(50))", "'1.2E-1'");

assertEvaluatedEquals("CAST(REAL '-12' AS varchar(5))", "'-12.0'");
assertEvaluatedEquals("CAST(REAL '-12e2' AS varchar(7))", "'-1200.0'");
assertEvaluatedEquals("CAST(REAL '-12e-2' AS varchar(5))", "'-0.12'");
assertEvaluatedEquals("CAST(REAL '-12' AS varchar(6))", "'-1.2E1'");
assertEvaluatedEquals("CAST(REAL '-12e2' AS varchar(6))", "'-1.2E3'");
assertEvaluatedEquals("CAST(REAL '-12e-2' AS varchar(7))", "'-1.2E-1'");

assertEvaluatedEquals("CAST(REAL '-12' AS varchar(50))", "'-12.0'");
assertEvaluatedEquals("CAST(REAL '-12e2' AS varchar(50))", "'-1200.0'");
assertEvaluatedEquals("CAST(REAL '-12e-2' AS varchar(50))", "'-0.12'");
assertEvaluatedEquals("CAST(REAL '-12' AS varchar(50))", "'-1.2E1'");
assertEvaluatedEquals("CAST(REAL '-12e2' AS varchar(50))", "'-1.2E3'");
assertEvaluatedEquals("CAST(REAL '-12e-2' AS varchar(50))", "'-1.2E-1'");

// the string representation is compliant with the SQL standard
assertEvaluatedEquals("CAST(REAL '12345678.9e0' AS varchar(12))", "'1.2345679E7'");
assertEvaluatedEquals("CAST(REAL '0.00001e0' AS varchar(6))", "'1.0E-5'");
assertEvaluatedEquals("CAST(REAL '12345678.9e0' AS varchar(12))", "'1.234568E7'");
assertEvaluatedEquals("CAST(REAL '0.00001e0' AS varchar(12))", "'1.0E-5'");

// the result value does not fit in the type (also, it is not compliant with the SQL standard)
// the result value does not fit in the type
assertTrinoExceptionThrownBy(() -> evaluate("CAST(REAL '12' AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value 12.0 cannot be represented as varchar(1)");
.hasMessage("Value 12.0 (1.2E1) cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(REAL '-12e2' AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value -1200.0 cannot be represented as varchar(1)");
.hasMessage("Value -1200.0 (-1.2E3) cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(REAL '0' AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value 0.0 cannot be represented as varchar(1)");
.hasMessage("Value 0.0 (0E0) cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(REAL '0e0' / REAL '0e0' AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value NaN cannot be represented as varchar(1)");
.hasMessage("Value NaN (NaN) cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(REAL 'Infinity' AS varchar(1))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value Infinity cannot be represented as varchar(1)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(REAL '1200000' AS varchar(5))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value 1200000.0 cannot be represented as varchar(5)");
.hasMessage("Value Infinity (Infinity) cannot be represented as varchar(1)");

assertEvaluatedEquals("CAST(REAL '1200000' AS varchar(5))", "'1.2E6'");
}

@Test
Expand Down
Loading