From a09436036ae3d10ea2dcb98ae973e8933f3e0646 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Tue, 28 Jun 2022 12:03:50 +0200 Subject: [PATCH] Add more varchar to date conversion tests Add test cases for non-canonically formatted input, whitespace, zero-padding, etc. --- .../io/trino/type/TestDateTimeOperators.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core/trino-main/src/test/java/io/trino/type/TestDateTimeOperators.java b/core/trino-main/src/test/java/io/trino/type/TestDateTimeOperators.java index 0dda5b7a1d7f..39cb96f35cb3 100644 --- a/core/trino-main/src/test/java/io/trino/type/TestDateTimeOperators.java +++ b/core/trino-main/src/test/java/io/trino/type/TestDateTimeOperators.java @@ -265,6 +265,24 @@ public void testIsDistinctFrom() public void testDateLiteral() { assertFunction("DATE '2013-02-02'", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + // one digit for month or day + assertFunction("DATE '2013-2-02'", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + assertFunction("DATE '2013-02-2'", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + // three digit for month or day + assertInvalidFunction("DATE '2013-02-002'", INVALID_LITERAL, "line 1:1: '2013-02-002' is not a valid date literal"); + assertInvalidFunction("DATE '2013-002-02'", INVALID_LITERAL, "line 1:1: '2013-002-02' is not a valid date literal"); + // zero-padded year + assertFunction("DATE '02013-02-02'", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + assertFunction("DATE '0013-02-02'", DATE, toDate(new DateTime(13, 2, 2, 0, 0, 0, 0, UTC))); + // invalid date + assertInvalidFunction("DATE '2013-02-29'", INVALID_LITERAL, "line 1:1: '2013-02-29' is not a valid date literal"); + // surrounding whitespace + assertFunction("DATE ' 2013-02-02 '", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + assertFunction("DATE ' \t\n\u000B\f\r\u001C\u001D\u001E\u001F 2013-02-02 \t\n\u000B\f\n\u001C\u001D\u001E\u001F '", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + // intra whitespace + assertInvalidFunction("DATE '2013 -02-02'", INVALID_LITERAL, "line 1:1: '2013 -02-02' is not a valid date literal"); + assertInvalidFunction("DATE '2013- 2-02'", INVALID_LITERAL, "line 1:1: '2013- 2-02' is not a valid date literal"); + // large year assertInvalidFunction("DATE '5881580-07-12'", INVALID_LITERAL, "line 1:1: '5881580-07-12' is not a valid date literal"); assertInvalidFunction("DATE '392251590-07-12'", INVALID_LITERAL, "line 1:1: '392251590-07-12' is not a valid date literal"); } @@ -276,6 +294,24 @@ public void testDateLiteral() public void testDateCastFromVarchar() { assertFunction("CAST('2013-02-02' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + // one digit for month or day + assertFunction("CAST('2013-2-02' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + assertFunction("CAST('2013-02-2' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + // three digit for month or day + assertInvalidFunction("CAST('2013-02-002' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013-02-002"); + assertInvalidFunction("CAST('2013-002-02' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013-002-02"); + // zero-padded year + assertFunction("CAST('02013-02-02' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + assertFunction("CAST('0013-02-02' AS date)", DATE, toDate(new DateTime(13, 2, 2, 0, 0, 0, 0, UTC))); + // invalid date + assertInvalidFunction("CAST('2013-02-29' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013-02-29"); + // surrounding whitespace + assertFunction("CAST(' 2013-02-02 ' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + assertFunction("CAST(' \t\n\u000B\f\r\u001C\u001D\u001E\u001F 2013-02-02 \t\n\u000B\f\n\u001C\u001D\u001E\u001F ' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC))); + // intra whitespace + assertInvalidFunction("CAST('2013 -02-02' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013 -02-02"); + assertInvalidFunction("CAST('2013- 2-02' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013- 2-02"); + // large year assertInvalidFunction("CAST('5881580-07-12' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 5881580-07-12"); assertInvalidFunction("CAST('392251590-07-12' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 392251590-07-12"); }