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

sql: correctly type CASE expressions that result in tuples #78331

Open
Tracked by #75101
mgartner opened this issue Mar 23, 2022 · 0 comments
Open
Tracked by #75101

sql: correctly type CASE expressions that result in tuples #78331

mgartner opened this issue Mar 23, 2022 · 0 comments
Labels
A-sql-typing SQLtype inference, typing rules, type compatibility. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) T-sql-queries SQL Queries Team

Comments

@mgartner
Copy link
Collaborator

mgartner commented Mar 23, 2022

Consider the query:

SELECT CASE WHEN true THEN ('a', 2) ELSE NULL::RECORD END

As of 22.1, the query results in an error:

ERROR: CASE ELSE type tuple cannot be matched to WHEN type tuple{string, int}
SQLSTATE: 42804

The CASE is typed as a tuple{string, int} and the NULL::RECORD is typed as types.AnyTuple (what the RECORD type resolves to). The error occurs because there is no valid case from types.AnyTuple to any specific tuple type. The fix is to type the CASE expression as types.AnyTuple, following the logic described in Postgres's type conversion documentation:

  1. Select the first non-unknown input type as the candidate type, then consider each other non-unknown input type, left to right. [12] If the candidate type can be implicitly converted to the other type, but not vice-versa, select the other type as the new candidate type. Then continue considering the remaining inputs. If, at any stage of this process, a preferred type is selected, stop considering additional inputs.

Here's another example that errors in CRDB but succeeds in Postgres:

defaultdb> SELECT CASE WHEN false THEN ('a', 2) ELSE (1, 3) END;
ERROR: incompatible value type: tuples [('a', 2) (1, 3)] are not the same type: could not parse "a" as type int: strconv.ParseInt: parsing "a": invalid syntax
SQLSTATE: 22P02

The error suggests that some changes to the parser might be required in order to get this working correctly.

Epic CRDB-2474
Jira issue: CRDB-14072

@mgartner mgartner added the C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) label Mar 23, 2022
@blathers-crl blathers-crl bot added T-sql-queries SQL Queries Team T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) labels Mar 23, 2022
@exalate-issue-sync exalate-issue-sync bot removed the T-sql-queries SQL Queries Team label Aug 25, 2022
@yuzefovich yuzefovich added the T-sql-queries SQL Queries Team label May 2, 2024
@exalate-issue-sync exalate-issue-sync bot removed the T-sql-queries SQL Queries Team label May 2, 2024
@yuzefovich yuzefovich added the T-sql-queries SQL Queries Team label May 2, 2024
@michae2 michae2 added the A-sql-typing SQLtype inference, typing rules, type compatibility. label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-typing SQLtype inference, typing rules, type compatibility. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) T-sql-queries SQL Queries Team
Projects
Status: Backlog
Development

No branches or pull requests

3 participants