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

TRY should handle invalid value error in cast VACHAR as TIMESTAMP #11259

Merged
merged 1 commit into from
Mar 3, 2022

Conversation

takezoe
Copy link
Member

@takezoe takezoe commented Mar 1, 2022

Description

Previously (at least in 317), TRY(CAST(xxxx AS TIMESTAMP) returned NULL for a timestamp that contains an invalid value like 0000-00-00 but in the latest Trino, it fails with the following error:

Invalid value for MonthOfYear (valid values 1 - 12): 0

Trino's document says:

The following errors are handled by TRY:

  • Division by zero
  • Invalid cast or function argument
  • Numeric value out of range

Therefore, I think TRY should cover this case. This pull request will resurrect the previous behavior which returns NULL for invalid values in timestamp.

Related issues, pull requests, and links

Documentation

(x) No documentation is needed.
( ) Sufficient documentation is included in this PR.
( ) Documentation PR is available with #prnumber.
( ) Documentation issue #issuenumber is filed, and can be handled later.

Release notes

( ) No release notes entries required.
(x) Release notes entries required with the following suggested text:

# General
* Handle `varchar` to `timestamp` conversion errors in {func}`try`. ({issue}`11259`)

@cla-bot cla-bot bot added the cla-signed label Mar 1, 2022
@takezoe takezoe changed the title TRY should handle invalid value error in VACHAR to TIMESTAMP cast TRY should handle invalid value error in cast VACHAR as TIMESTAMP Mar 1, 2022
@takezoe takezoe force-pushed the try_cast_invalid_timestamp branch from a7934c8 to 4b5d21b Compare March 1, 2022 17:06
@hashhar hashhar requested a review from martint March 1, 2022 17:16
.toEpochSecond();
}
catch (DateTimeException e) {
throw new IllegalArgumentException("Invalid timestamp: " + value, e);
Copy link
Member

Choose a reason for hiding this comment

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

TrinoException

Copy link
Member Author

@takezoe takezoe Mar 2, 2022

Choose a reason for hiding this comment

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

Fixed. Is it better to replace another IllegalArgumentException in this method too?

if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid timestamp: " + value);
}

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please

@takezoe takezoe force-pushed the try_cast_invalid_timestamp branch from 4b5d21b to 15a4511 Compare March 2, 2022 13:08
Comment on lines 167 to 170
catch (ZoneRulesException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value, e);
}
try {
Copy link
Member

Choose a reason for hiding this comment

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

merge try blocks together

Copy link
Member Author

Choose a reason for hiding this comment

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

Merged into a single try block.

@@ -70,6 +71,7 @@ public void testExceptions()
assertFunction(createTryExpression("1/0"), INTEGER, null);
assertFunction(createTryExpression("JSON_PARSE('INVALID')"), JSON, null);
assertFunction(createTryExpression("CAST(NULL AS INTEGER)"), INTEGER, null);
assertFunction(createTryExpression("CAST('0000-00-00' AS TIMESTAMP)"), TIMESTAMP_MILLIS, null);
Copy link
Member

Choose a reason for hiding this comment

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

add test with timestamp with time zone

  • zero day of month
  • invalid tz specifier

Copy link
Member Author

Choose a reason for hiding this comment

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

Added cases.

@takezoe takezoe force-pushed the try_cast_invalid_timestamp branch from 15a4511 to 6d5d136 Compare March 2, 2022 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants