sql: add user-defined composite types#90491
Conversation
|
super cool! two idle comments before a full review:
|
Definitely.
There are a couple of differences, but they're pretty minor. It's telling that in Postgres, a UDT appears in I'm curious what @ajwerner thinks as the majority of the code here is in schema world but my instinct is that we should model it as a new type kind just like enum. |
That matches my expectation. I can imagine a future migration to add such types for tables too. |
a19be20 to
acf0bc6
Compare
|
I'm feeling relatively good about contents of this PR, if someone has a chance to review I'd appreciate it. It was nice to learn a little bit about the new schema changer - once you get the hang of it, it seems to do an impressive job of abstracting away the underlying complexity. Or, perhaps I just had blinders on :) I'm not sure whether I did the dependency tracking right, and I'm sure it's missing a bunch of other stuff as well. |
|
Also the PR is fairly large, say the word and I'll try to chunk it up a bit. |
|
Actually, I don't think I have done anything about backup and restore in this patch, so it is probably not complete :) |
|
Nice work. Maybe it's just me but I found myself had to do quite a bit of context switching while going through the PR briefly. I'd appreciate if you could at least split the PR into several commits : 1. front-end change (parser), 2. legacy schema changer support, 3. fix hydration logic, 4. declarative schema changer support (this worth a separate PR, I think). |
|
Thanks @chengxiong-ruan I will split it up. Question though, how do I avoid seeing failing tests on the intermediate commits? In particular maybe I need to find a way to tell the declarative schema changer to skip handling DROP TYPE in the case of composite types? I usually like each commit to pass all the tests but maybe that's not a realistic hope at this point. |
Ah, yeah, making CI passed on each commit is hard heh. I think in this case for the declarative schema changer, you could raise an unimplemented error like this so that it can fall back to the legacy schema changer. |
|
@chengxiong-ruan I have removed the declarative schema changer implementation from this PR and split into 2 commits, one for the parser and one for the rest. I wasn't sure what you meant by fixing the hydration so please let me know if this is helpful, or if you have additional split requests. |
7385453 to
68fa7b4
Compare
|
@postamar PTAL, I have:
|
rafiss
left a comment
There was a problem hiding this comment.
does this support arrays of composite types?
Release note: None
Yes. |
rafiss
left a comment
There was a problem hiding this comment.
can there be tests for arrays of composite types?
There was a problem hiding this comment.
Do we want this to be a table ID instead? Zero meaning it's not an implicit record type, and vice-versa.
There was a problem hiding this comment.
I don't see a use case for recording that table ID, so I guess not? :) We can easily add it if need be.
There was a problem hiding this comment.
Is the schema changer support PR ready for review? I'm just being mindful of accruing technical debt so to be honest I'd rather this change were part of this PR, but won't insist on it. Hopefully removing this scerrors.NotImplementedErrorf error should be quite easy at this point.
There was a problem hiding this comment.
Yes, it's ready for review. I moved it out of the PR at @chengxiong-ruan suggestion to keep things small. At this point I do not want to re-add it - can we get this one merged and then I'll work on the other one immediately?
There was a problem hiding this comment.
That's fine by me, thanks! This need to change a zillion things everytime one touches anything in schema-land is sadly a fairly typical feature of our development experience.
There was a problem hiding this comment.
nit: consider using catalog.DescriptorIDSet here.
There was a problem hiding this comment.
Done, but for my edification, why? Doesn't IDClosure already dedupe the IDs here? I don't see what it's adding.
There was a problem hiding this comment.
Sorry, I overlooked the fact that .GetIDClosure() returned a map and not a slice (or even better, a catalog.DescriptorIDSet). My bad!
There was a problem hiding this comment.
Should we wrap the error in something which references the label? Seems like things could get confusing otherwise.
There was a problem hiding this comment.
We don't seem to ever wrap the result error from ResolveType in other spots, even in places like CREATE TABLE, so I guess I don't see why this one is special - I'll leave it alone.
There was a problem hiding this comment.
I didn't realize that. Fair enough. Let's stay consistent then.
There was a problem hiding this comment.
Just curious: is this hydration logic exercised at all now that we can't reference user-defined types within composite types?
There was a problem hiding this comment.
Yeah, it'll no-op. But, there's no harm in leaving it, right? We should always run hydration on types that are pulled from disk, everywhere - better to be consistent about that than have to remember to re-add it later when we resolve #91779.
jordanlewis
left a comment
There was a problem hiding this comment.
Thanks for the review! PTAL.
There was a problem hiding this comment.
Yeah, it'll no-op. But, there's no harm in leaving it, right? We should always run hydration on types that are pulled from disk, everywhere - better to be consistent about that than have to remember to re-add it later when we resolve #91779.
There was a problem hiding this comment.
We don't seem to ever wrap the result error from ResolveType in other spots, even in places like CREATE TABLE, so I guess I don't see why this one is special - I'll leave it alone.
There was a problem hiding this comment.
It's not a correctness bug the way we define it - errors where there shouldn't be errors are not correctness bugs.
The reason this is broken is a type system issue that's unrelated to composite types. I filed #93349 for it but I don't agree that it's a showstopper - it's a classic example of CockroachDB's type system differences from Postgres, and is correctable by adding casts. I've improved the comments here.
Here's an example of the existing broken-ness:
demo@127.0.0.1:26257/defaultdb> values((1, 2)), ((1, null));
ERROR: VALUES types tuple{int, unknown} and tuple{int, int} cannot be matched
SQLSTATE: 42804
There was a problem hiding this comment.
Yes, it's ready for review. I moved it out of the PR at @chengxiong-ruan suggestion to keep things small. At this point I do not want to re-add it - can we get this one merged and then I'll work on the other one immediately?
There was a problem hiding this comment.
I don't see a use case for recording that table ID, so I guess not? :) We can easily add it if need be.
This commit adds user-defined composite types, which are created with the `CREATE TYPE t AS (field1 type1, field2 type2, ...)` syntax. See documentation here: https://www.postgresql.org/docs/current/rowtypes.html It permits this kind of thing: ``` CREATE TYPE t AS (a INT, b INT); CREATE TABLE a (a t); INSERT INTO a VALUES((1, 2)) SELECT (a).b FROM a ``` Release note (sql change): add user-defined composite column types
|
TFTR! I added the test suite at @rafiss suggestion 😅 |
|
Woohoo! Thanks for the reviews 😁 bors r+ |
|
Build succeeded: |
|
Hello, just stumbled on thia. Psycopg 2 and 3's test suites have an amount of tests involving composite types. Did you try to un-skip them for the crdb version supporting this feature and give them a go? 🙂 |
|
Hey @dvarrazzo, I didn't yet - if I understand our test infrastructure properly, the nightly tests will automatically exercise this tonight and show us which ones are now passing that should be marked as failed. I'll make sure I have the right idea with @rafiss though :-) |
|
We previously worked with @dvarrazzo to test CRDB in the psycopg CI. Example: There's a helper in the test suite that skips certain tests that don't work on CRDB: https://github.com/psycopg/psycopg2/blob/e8d92b74fd4b219cb4725cb906c526f3f2dce31d/tests/testutils.py#L426 So we can try it out and make a commit to psycopg if more tests are passing (conditional on using CRDB >= 23.1) |

Closes #27792
This commit adds user-defined composite types, which are created with
the
CREATE TYPE t AS (field1 type1, field2 type2, ...)syntax.See documentation here:
https://www.postgresql.org/docs/current/rowtypes.html
It permits this kind of thing:
Release note (sql change): add user-defined composite column types