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: support string literal -> tuple[] parsing #70030

Closed
nvanbenschoten opened this issue Sep 10, 2021 · 3 comments · Fixed by #71916
Closed

sql: support string literal -> tuple[] parsing #70030

nvanbenschoten opened this issue Sep 10, 2021 · 3 comments · Fixed by #71916
Assignees
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL A-sql-typing SQLtype inference, typing rules, type compatibility. A-tools-postgrest 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)

Comments

@nvanbenschoten
Copy link
Member

nvanbenschoten commented Sep 10, 2021

Needed for #69010.

In Postgres:

> SELECT COALESCE(ARRAY[ROW(1, 2)], '{}');
 coalesce
-----------
 {"(1,2)"}

In CockroachDB:

> SELECT COALESCE(ARRAY[ROW(1, 2)], '{}');
ERROR: incompatible COALESCE expressions: expected '{}' to be of type tuple{int, int}[], found type string

Prototype:

diff --git a/pkg/sql/sem/tree/constant.go b/pkg/sql/sem/tree/constant.go
index ea3efa19e5..a982c27887 100644
--- a/pkg/sql/sem/tree/constant.go
+++ b/pkg/sql/sem/tree/constant.go
@@ -493,6 +493,7 @@ var (
                types.AnyEnum,
                types.INetArray,
                types.VarBitArray,
+               types.AnyTupleArray,
        }
        // StrValAvailBytes is the set of types convertible to byte array.
        StrValAvailBytes = []*types.T{types.Bytes, types.Uuid, types.String, types.AnyEnum}

Epic CRDB-10300

@nvanbenschoten nvanbenschoten added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-sql-pgcompat Semantic compatibility with PostgreSQL A-sql-typing SQLtype inference, typing rules, type compatibility. labels Sep 10, 2021
@blathers-crl blathers-crl bot added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label Sep 10, 2021
@rafiss
Copy link
Collaborator

rafiss commented Sep 16, 2021

related: #63995

(i just rescoped that issue to be about improving the error message, but maybe we actually should support it after all)

@rafiss
Copy link
Collaborator

rafiss commented Sep 16, 2021

interestingly, it seems like Postgres only allows this if the string literal is an empty array {}

Postgres:

rafiss@127:postgres> SELECT COALESCE(ARRAY[ROW(1, 2)], '{"(3,4)"}');
input of anonymous composite types is not implemented
LINE 1: SELECT COALESCE(ARRAY[ROW(1, 2)], '{"(3,4)"}')

@nvanbenschoten
Copy link
Member Author

I think it does support parsing string literals as named composite types:

nathan=# CREATE TYPE complex AS (real INT, imag INT);
CREATE TYPE

nathan=# SELECT pg_typeof(COALESCE(ARRAY[ROW(1, 2)], '{"(3,4)"}'));
ERROR:  input of anonymous composite types is not implemented
LINE 1: SELECT pg_typeof(COALESCE(ARRAY[ROW(1, 2)], '{"(3,4)"}'));
                                                    ^

nathan=# SELECT COALESCE(ARRAY[ROW(1, 2)::complex], '{"(3,4)"}');
 coalesce
-----------
 {"(1,2)"}
(1 row)

nathan=# SELECT pg_typeof(COALESCE(ARRAY[ROW(1, 2)::complex], '{"(3,4)"}'));
 pg_typeof
-----------
 complex[]
(1 row)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL A-sql-typing SQLtype inference, typing rules, type compatibility. A-tools-postgrest 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)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants