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

Implicit typecast literals to correct type (postgres compatibility) #38965

Closed
stevecookform3 opened this issue Jul 18, 2019 · 8 comments
Closed
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3 Medium-low impact: incurs increased costs for some users (incl lower avail, recoverable bad data) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@stevecookform3
Copy link

stevecookform3 commented Jul 18, 2019

Looks like postgres will auto-type cast literals based on the context (more info here: https://www.postgresql.org/docs/10/typeconv.html). That isnt done by CockroachDB.

Thats causing some incompatibilities when we try to migrate postgres over to cockroach.

Here are some simple examples - the following work fine in postgres 9.5, but fail with cockroach 19.1.3 as cockroach doesnt cast them to the expected type.

-- auto cast jsonb & text[] types from strings
SELECT jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false);
Postgres -> [{"f1":[2,3,4],"f2":null},2,null,3]
Cockroach -> ERROR:  unknown signature: jsonb_set(string, string, string, bool)

-- auto cast numeric type from string
SELECT sign('180');
Postgres -> 1
Cockroach -> ERROR:  unknown signature: sign(string)

-- auto cast string from numeric
SELECT 123 || '456';
Postgres -> 123456
Cockroach -> ERROR:  unsupported binary operator: <int> || <string>

Looks like some specific examples have been mentioned already here:
#33341 and #23299

However the underlying issue I think is more generic than mentioned on other issues.

Epic CRDB-2474
Jira issue: CRDB-5598

@jordanlewis
Copy link
Member

@stevecookform3, we do coerce many literals already. Text arrays are coming soon: #38869. String <->numeric is tricky to get right, so we haven't done it yet. Json actually does get automatically coerced:

[email protected]:54720/defaultdb> select '{"a":3}'->'a';
  ?column?
+----------+
         3
(1 row)

@jordanlewis jordanlewis added the A-sql-pgcompat Semantic compatibility with PostgreSQL label Jul 18, 2019
@jordanlewis
Copy link
Member

But point taken that this is irritating. What ORM or framework do you use? Would you be willing to give us a sample of your application that runs into these issues?

Thanks!

@stevecookform3
Copy link
Author

(github seems to have lost my previous comment, so apologies if this is a duplicate)
@jordanlewis we use an inhouse framework, so mostly this is just raw SQL similar to the cases above.
We can potentially just add explicit casts where we hit issues, which should work in both postgres & cockroach.
I thought it worth creating this as a separate issue as I could only find some specific examples in other tickets.

@awoods187 awoods187 added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-2 Medium-high impact: many users impacted, risks of availability and difficult-to-fix data errors labels Jul 23, 2019
@jordanlewis jordanlewis added S-3 Medium-low impact: incurs increased costs for some users (incl lower avail, recoverable bad data) and removed S-2 Medium-high impact: many users impacted, risks of availability and difficult-to-fix data errors labels Jul 31, 2019
@rafiss rafiss added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label May 12, 2021
@mgartner
Copy link
Collaborator

-- auto cast jsonb & text[] types from strings
SELECT jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false);
Postgres -> [{"f1":[2,3,4],"f2":null},2,null,3]
Cockroach -> ERROR:  unknown signature: jsonb_set(string, string, string, bool)

This works in CockroachDB version 19.2 and later.

-- auto cast numeric type from string
SELECT sign('180');
Postgres -> 1
Cockroach -> ERROR:  unknown signature: sign(string)

-- auto cast string from numeric
SELECT 123 || '456';
Postgres -> 123456
Cockroach -> ERROR:  unsupported binary operator: <int> || <string>

I believe these examples will require type resolution of functions as described in the Postgres type conversion docs. This is related to implicit casts (see #75101).

@jordanlewis
Copy link
Member

These examples work now.

@mgartner
Copy link
Collaborator

mgartner commented Jan 4, 2023

@jordanlewis SELECT 123 || '456'; works but SELECT sign('180'); does not. I'm still getting an error:

defaultdb> SELECT sign('180');
ERROR: ambiguous call: sign(string), candidates are:
sign(float) -> float
sign(decimal) -> decimal
sign(int) -> int
SQLSTATE: 42725

@mgartner mgartner reopened this Jan 4, 2023
@jordanlewis
Copy link
Member

Isn't that a new kind of problem though? Should we open a new issue for that one?

@mgartner
Copy link
Collaborator

mgartner commented Jan 4, 2023

That example was given in the orginal PR description. I created #94718 to track part of what would be require to fix it. Fixing it would also require fixing our function overload resolution logic. The meta issue #75101 tracks some of this work.

@mgartner mgartner closed this as completed Jan 4, 2023
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 C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3 Medium-low impact: incurs increased costs for some users (incl lower avail, recoverable bad data) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
Development

No branches or pull requests

5 participants