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

builtins: use IdentityReturnType when possible #70469

Merged
merged 2 commits into from
Sep 23, 2021

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Sep 21, 2021

fixes #70475

see individual commits

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss force-pushed the use-identity-return-type branch from 027e4b2 to 7857b72 Compare September 21, 2021 04:59
@rafiss rafiss requested review from jordanlewis, otan and a team September 21, 2021 14:50
@rafiss rafiss marked this pull request as ready for review September 21, 2021 14:50
}
return args[0].ResolvedType()
},
Types: tree.ArgTypes{{"base", types.AnyTuple}, {"from_json", types.Jsonb}},
Copy link
Member

Choose a reason for hiding this comment

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

The reason I set this to be types.Any is to match the argument type in Postgres itself, which is any, and not tuple. I'm not sure if this matters, but that's why I did it:

postgres=# select proargtypes[0]::regtype from pg_proc where proname = 'jsonb_populate_record';
 proargtypes
-------------
 anyelement
(1 row)

Copy link
Collaborator Author

@rafiss rafiss Sep 21, 2021

Choose a reason for hiding this comment

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

hm, so looks like PG must do some other validation on the input args

rafiss@127:postgres> select * from json_populate_record(null::int, '{"a":1,"b":2}')
first argument of json_populate_record must be a row type

if you try this in crdb:

root@:26257/defaultdb> select * from json_populate_record(null::int, '{"a":1,"b":2}');
ERROR: anonymous records cannot be used with json_populate_record

which seems like a misleading message

Copy link
Contributor

Choose a reason for hiding this comment

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

interesting, postgres caches records, wonder if we should do something similar:
https://github.com/postgres/postgres/blob/4548c76738b368a11a5dad052f9653a349eeb52c/src/backend/utils/adt/jsonfuncs.c#L3815-L3825

anyway yeah it's checked internally i think? the NULL is checked here
https://github.com/postgres/postgres/blob/4548c76738b368a11a5dad052f9653a349eeb52c/src/backend/utils/adt/jsonfuncs.c#L3416-L3422

though i can't find where it returns the signature error if it is a not-null not-tuple. maybe we should just change the tree.MustBeDTuple check here:

defaultElems = tree.MustBeDTuple(evalled[0]).D

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It looks like PG checks the not-null not-tuple case in the same place as above. there's also this fun message that PG has, which probably doesn't apply to CRDB's type system:

rafiss@127:postgres> select * from json_populate_recordset(null, '{"a":1,"b":2}')
could not determine polymorphic type because input has type unknown

@rafiss rafiss force-pushed the use-identity-return-type branch from 7857b72 to e820ea2 Compare September 23, 2021 03:20
@rafiss
Copy link
Collaborator Author

rafiss commented Sep 23, 2021

this is ready for another review

The function accepts types.Any, but at execution time it actually
requires a tuple type. This is now checked. The behavior of checking at
execution time matches PostgreSQL.

Release note: None
@rafiss rafiss force-pushed the use-identity-return-type branch from e820ea2 to 23f4ee3 Compare September 23, 2021 14:57
@rafiss
Copy link
Collaborator Author

rafiss commented Sep 23, 2021

bors r=otan

@jordanlewis
Copy link
Member

@rafiss thanks for fixing this issue!

@craig
Copy link
Contributor

craig bot commented Sep 23, 2021

Build succeeded:

@craig craig bot merged commit 55ef033 into cockroachdb:master Sep 23, 2021
@rafiss rafiss deleted the use-identity-return-type branch September 24, 2021 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql/tests: TestRandomSyntaxFunctions failed
4 participants