Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test and fix cast from date to varchar #10678

Merged
merged 5 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 10 additions & 2 deletions core/trino-main/src/main/java/io/trino/type/DateOperators.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.airlift.slice.Slice;
import io.trino.spi.TrinoException;
import io.trino.spi.function.LiteralParameter;
import io.trino.spi.function.LiteralParameters;
import io.trino.spi.function.ScalarFunction;
import io.trino.spi.function.ScalarOperator;
Expand All @@ -27,6 +28,7 @@
import static io.trino.spi.function.OperatorType.CAST;
import static io.trino.util.DateTimeUtils.parseDate;
import static io.trino.util.DateTimeUtils.printDate;
import static java.lang.String.format;

public final class DateOperators
{
Expand All @@ -35,9 +37,15 @@ private DateOperators() {}
@ScalarOperator(CAST)
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice castToSlice(@SqlType(StandardTypes.DATE) long value)
public static Slice castToSlice(@LiteralParameter("x") long x, @SqlType(StandardTypes.DATE) long value)
{
return utf8Slice(printDate((int) value));
String stringValue = printDate((int) 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)", stringValue, x));
}

@ScalarFunction("date")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ private DateTimeUtils() {}

public static int parseDate(String value)
{
// in order to follow the standard, we should validate the value:
// - the required format is 'YYYY-MM-DD'
// - all components should be unsigned numbers
// https://github.com/trinodb/trino/issues/10677
return toIntExact(TimeUnit.MILLISECONDS.toDays(DATE_FORMATTER.parseMillis(value)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,22 @@ public void testCastRealToBoundedVarchar()
.hasMessage("Value 1200000.0 cannot be represented as varchar(5)");
}

@Test
public void testCastDateToBoundedVarchar()
{
assertEvaluatedEquals("CAST(DATE '2013-02-02' AS varchar(10))", "'2013-02-02'");
// according to the SQL standard, this literal is incorrect. Year should be unsigned. https://github.com/trinodb/trino/issues/10677
assertEvaluatedEquals("CAST(DATE '-2013-02-02' AS varchar(50))", "'-2013-02-02'");

// the result value does not fit in the type
assertTrinoExceptionThrownBy(() -> evaluate("CAST(DATE '2013-02-02' AS varchar(9))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value 2013-02-02 cannot be represented as varchar(9)");
assertTrinoExceptionThrownBy(() -> evaluate("CAST(DATE '-2013-02-02' AS varchar(9))"))
.hasErrorCode(INVALID_CAST_ARGUMENT)
.hasMessage("Value -2013-02-02 cannot be represented as varchar(9)");
}

@Test
public void testCastToBoolean()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,30 @@ public void testCastRealToBoundedVarchar()
assertSimplifies("CAST(REAL '12e2' AS varchar(3)) = '1200.0'", "CAST(REAL '12e2' AS varchar(3)) = '1200.0'");
}

@Test
public void testCastDateToBoundedVarchar()
{
// the varchar type length is enough to contain the date's representation
assertSimplifies("CAST(DATE '2013-02-02' AS varchar(10))", "'2013-02-02'");
assertSimplifies("CAST(DATE '2013-02-02' AS varchar(50))", "CAST('2013-02-02' AS varchar(50))");

// cast from date to varchar fails, so the expression is not modified
assertSimplifies("CAST(DATE '2013-02-02' AS varchar(3))", "CAST(DATE '2013-02-02' AS varchar(3))");
assertSimplifies("CAST(DATE '2013-02-02' AS varchar(3)) = '2013-02-02'", "CAST(DATE '2013-02-02' AS varchar(3)) = '2013-02-02'");
}

private static void assertSimplifies(String expression, String expected)
{
ParsingOptions parsingOptions = new ParsingOptions();
Expression actualExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expression, parsingOptions));
Expression expectedExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expected, parsingOptions));
Expression rewritten = rewrite(actualExpression, TEST_SESSION, new SymbolAllocator(booleanSymbolTypeMapFor(actualExpression)), PLANNER_CONTEXT, createTestingTypeAnalyzer(PLANNER_CONTEXT));
Expression expectedExpression = normalize(rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expected, new ParsingOptions())));
assertEquals(
normalize(rewritten),
normalize(expectedExpression));
simplify(expression),
expectedExpression);
}

private static Expression simplify(String expression)
Copy link
Member

Choose a reason for hiding this comment

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

consider @Language("SQL")

{
Expression actualExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expression, new ParsingOptions()));
return normalize(rewrite(actualExpression, TEST_SESSION, new SymbolAllocator(booleanSymbolTypeMapFor(actualExpression)), PLANNER_CONTEXT, createTestingTypeAnalyzer(PLANNER_CONTEXT)));
}

private static Map<Symbol, Type> booleanSymbolTypeMapFor(Expression expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static io.trino.spi.type.TimestampType.createTimestampType;
import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static io.trino.spi.type.VarcharType.createVarcharType;
import static io.trino.testing.DateTimeTestingUtils.sqlTimestampOf;
import static io.trino.testing.TestingSession.testSessionBuilder;
import static org.joda.time.DateTimeZone.UTC;
Expand Down Expand Up @@ -264,6 +265,17 @@ public void testDateCastFromVarchar()
assertInvalidFunction("DATE '392251590-07-12'", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 392251590-07-12");
}

@Test
public void testDateCastToVarchar()
{
assertFunction("cast(DATE '2013-02-02' AS varchar)", VARCHAR, "2013-02-02");
// according to the SQL standard, this literal is incorrect. The required format is 'YYYY-MM-DD'. https://github.com/trinodb/trino/issues/10677
assertFunction("cast(DATE '13-2-2' AS varchar)", VARCHAR, "0013-02-02");
assertFunction("cast(DATE '2013-02-02' AS varchar(50))", createVarcharType(50), "2013-02-02");
assertFunction("cast(DATE '2013-02-02' AS varchar(10))", createVarcharType(10), "2013-02-02");
assertInvalidCast("cast(DATE '2013-02-02' AS varchar(9))", "Value 2013-02-02 cannot be represented as varchar(9)");
}

private static SqlDate toDate(DateTime dateTime)
{
return new SqlDate((int) TimeUnit.MILLISECONDS.toDays(dateTime.getMillis()));
Expand Down