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_cast(parse_json(...)) incorrectly throws error in some cases #21877

Closed
ragnard opened this issue May 8, 2024 · 3 comments
Closed

try_cast(parse_json(...)) incorrectly throws error in some cases #21877

ragnard opened this issue May 8, 2024 · 3 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@ragnard
Copy link
Member

ragnard commented May 8, 2024

When using try_cast on the result of a parse_json call, the cast can still fail with an error.

Reproduction

with 
example as (
  select '42' as json
) 
select 
  try_cast(json_parse(json) as array(varchar))
from
  example;
Query 20240508_195937_00029_qrpb2 failed: Cannot cast to array(varchar). Expected a json array, but got 42

From what I can tell, this is due to the optimization "for CAST(JSON_PARSE(...) AS ARRAY/MAP/ROW)" here:

https://github.com/trinodb/trino/blob/master/core/trino-main/src/main/java/io/trino/sql/planner/IrExpressionInterpreter.java#L769

However, the "safeness" of the original Cast has seems to be lost in this process, as the JsonToXXXFunctions always error on failure.

@ragnard
Copy link
Member Author

ragnard commented May 8, 2024

I should add this behaviour was introduced in 446. It works as expected in 445 (returns NULL).

@ragnard ragnard added the bug Something isn't working label May 8, 2024
@martint
Copy link
Member

martint commented May 8, 2024

Thanks for the report. I'll look into it.

@martint martint self-assigned this May 8, 2024
@martint
Copy link
Member

martint commented May 8, 2024

This is fixed by #21800 and will be in the next release.

@martint martint closed this as completed May 8, 2024
@martint martint added this to the 447 milestone May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants