-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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 Hibernate's use of _pg_expandarray #16971
Comments
I haven't investigated too deeply, but if we only support the result type then I suspect it's not too bad. My guess would be that the most difficult part would probably be handling the |
It's definitely a mid-size project, and somewhat larger than implementing arrays as result-value type. |
I just realized that this is more difficult than we thought. Not only do we need to support (or at least pretend to support) record types, we also need to support generator functions in render positions, which we currently don't support at all. See #13156 for more context. |
To be more clear, this is in all reality blocked on correlated subquery support as well! |
Looking at this briefly on @awoods187's request. Resolution here requires support for record types in in-memory values (datums or whatever will replace them), not db encoding for them (i.e. we don't need to store them). Analogous to the same distinction we used when array support was first implemented. |
@jordanlewis Is the query that hibernate issues identical to the use of |
They're not all identical. Sometimes they occur in the context of larger queries. Here's another example:
The only thing we really need to support for this "indexing into" the |
Bram and I had an offline discussion and Bram discovered this item has two aspects to it:
|
@awoods187 this does mean this item is much easier to implement than what we initially thought, it downgrades it from M-L to S. |
I'll update to a S and am excited to see the results. |
I just wanted to add a bit here, there are 3 parts that are required to support the statement above and
|
|
Hmm... This works: +---------+
| indkey |
+---------+
| 1 2 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 2 4 3 |
| 1 6 |
| 1 7 |
| 1 |
| 1 |
| 2 3 |
| 1 |
| 5 |
| 4 |
| 1 2 |
| 1 2 |
| 1 2 |
| 1 |
| 2 |
+---------+
(19 rows) But this doesn't (in my branch with _pg_expandarray working): SELECT information_schema._pg_expandarray(indkey) FROM pg_index;
pq: column name "indkey" not found And even this doesn't work: SELECT unnest(indkey) FROM pg_index;
pq: column name "indkey" not found |
Yeah, our name resolution can't handle this case at the moment. |
Ok, I'll update that checklist for this case. |
This adds the information_schema._pg_expandarray() function. It is needed for compatibility with a number of ORMs. See cockroachdb#16971. information_schema._pg_expandarray takes an array and returns a set of the value and an index into the array. This is a very old function and from what I can disern, was designed for internal use only, but was picked up by a few ORMs. There is no real supporting documentation for the feature. The code for it can be seen here: https://sourcegraph.com/github.com/postgres/postgres/-/blob/src/backend/catalog/information_schema.sql#L43:17 Furthermore, if the function is a Set Retruning Function, it returns tuples when evaluated in a scaler context: In Postgres: ```sql SELECT information_schema._pg_expandarray(array['i','i','o','t','b']); _pg_expandarray ----------------- (i,1) (i,2) (o,3) (t,4) (b,5) (5 rows) ``` With this patch Cockroach now supports this directly as well: ```sql SELECT information_schema._pg_expandarray(array['i','i','o','t','b']); +------------------------------------+ | information_schema._pg_expandarray | +------------------------------------+ | ('i',1) | | ('i',2) | | ('o',3) | | ('t',4) | | ('b',5) | +------------------------------------+ (5 rows) ``` However, when evaluating an SRF in the `FROM` context, it should return columns. Luckily, we already do exactly that. This is true for `unnest()` as well as the new `_pg_expandarray`. The difference is a little subtle and I couldn't find any good documentation on it, but this answer seems to cover it quite well: https://dba.stackexchange.com/questions/172463/understanding-set-returning-function-srf-in-the-select-list In Postgres: ```sql SELECT * FROM information_schema._pg_expandarray(array['i','i','o','t','b']); x | n ---+--- i | 1 i | 2 o | 3 t | 4 b | 5 (5 rows) ``` In Cockroach: ```sql SELECT * from information_schema._pg_expandarray(array['i','i','o','t','b']); +---+---+ | x | n | +---+---+ | i | 1 | | i | 2 | | o | 3 | | t | 4 | | b | 5 | +---+---+ (5 rows) ``` Note that after froming the resulting set to a table, this function is essentially the same as: ```sql SELECT * from unnest(array['i','1','3','r']) with ordinality as c(x,n); +---+---+ | x | n | +---+---+ | i | 1 | | 1 | 2 | | 3 | 3 | | r | 4 | +---+---+ (4 rows) ``` But to retain direct compatibility with Postgres, the original SET response needs to be maintained as well. Part of cockroachdb#16971. Release note (sql change): Added support for the information_schema._pg_expandarray() function.
Another test query from metabase/metabase#5050 SELECT NULL AS TABLE_CAT,
n.nspname AS TABLE_SCHEM,
ct.relname AS TABLE_NAME,
a.attname AS COLUMN_NAME,
(i.keys).n AS KEY_SEQ,
ci.relname AS PK_NAME
FROM pg_catalog.pg_class ct
JOIN pg_catalog.pg_attribute a ON (ct.oid = a.attrelid)
JOIN pg_catalog.pg_namespace n ON (ct.relnamespace = n.oid)
JOIN (SELECT i.indexrelid, i.indrelid, i.indisprimary, information_schema._pg_expandarray(i.indkey) AS keys FROM pg_catalog.pg_index i) i ON (a.attnum = (i.keys).x AND a.attrelid = i.indrelid)
JOIN pg_catalog.pg_class ci ON (ci.oid = i.indexrelid)
WHERE true AND ct.relname = 'sometablename' AND i.indisprimary
ORDER BY table_name, pk_name, key_seq |
This work is yet another step towards cockroachdb#16971. The labeled tuples introduced in cockroachdb#25283 can now be accessed using their labels. Star expansion is still not allowed for tuples with fewer or more than 1 columns. Release note (sql change): Labeled tuples can now be accessed using their labels (e.g. `SELECT (x).word FROM (SELECT pg_expand_keywords() AS x)`. Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
This work is yet another step towards cockroachdb#16971. The labeled tuples introduced in cockroachdb#25283 can now be accessed using their labels. Star expansion is still not allowed for tuples with fewer or more than 1 columns. Release note (sql change): Labeled tuples can now be accessed using their labels (e.g. `SELECT (x).word FROM (SELECT pg_expand_keywords() AS x)`. Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
This work is yet another step towards cockroachdb#16971. The labeled tuples introduced in cockroachdb#25283 can now be accessed using their labels or using a star, e.g. `(E).*`. Release note (sql change): Labeled tuples can now be accessed using their labels (e.g. `SELECT (x).word FROM (SELECT pg_expand_keywords() AS x)` or a star (e.g. `SELECT (x).* FROM (SELECT pg_expand_keywords() AS x)`). Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
This work is yet another step towards cockroachdb#16971. The labeled tuples introduced in cockroachdb#25283 can now be accessed using their labels or using a star, e.g. `(E).*`. Release note (sql change): Labeled tuples can now be accessed using their labels (e.g. `SELECT (x).word FROM (SELECT pg_expand_keywords() AS x)` or a star (e.g. `SELECT (x).* FROM (SELECT pg_expand_keywords() AS x)`). Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
This work is yet another step towards cockroachdb#16971. The labeled tuples introduced in cockroachdb#25283 can now be accessed using their labels or using a star, e.g. `(E).*`. Release note (sql change): Labeled tuples can now be accessed using their labels (e.g. `SELECT (x).word FROM (SELECT pg_expand_keywords() AS x)` or a star (e.g. `SELECT (x).* FROM (SELECT pg_expand_keywords() AS x)`). Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
This work is yet another step towards cockroachdb#16971. The labeled tuples introduced in cockroachdb#25283 can now be accessed using their labels or using a star, e.g. `(E).*`. Release note (sql change): Labeled tuples can now be accessed using their labels (e.g. `SELECT (x).word FROM (SELECT pg_expand_keywords() AS x)` or a star (e.g. `SELECT (x).* FROM (SELECT pg_expand_keywords() AS x)`). Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
26628: sql: Support tuple column access and tuple stars r=knz a=knz First commits from #26621. Completes the fix to #24866 by re-activating disabled tests. This work is yet another step towards #16971. It would actually fix #16971 if it were not for #26627, #26624 and #26629. This work is yet another step towards #16971. The labeled tuples introduced in #25283 can now be accessed using their labels or using a star, e.g. `(E).*`. Release note (sql change): Labeled tuples can now be accessed using their labels (e.g. `SELECT (x).word FROM (SELECT pg_expand_keywords() AS x)` or a star (e.g. `SELECT (x).* FROM (SELECT pg_expand_keywords() AS x)`). Fixes #26720. Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com> Co-authored-by: Bram Gruneir <bram@cockroachlabs.com>
Oops, no this is not done yet |
This is complete now. |
Adding an index to a table in Hibernate issues a query which makes use of this. Note that supporting this also means that we would need to support record types. The query below should work, if this is implemented:
This function takes an array
a
and returns a set of records that looks like the following:The work is to be decomposed as follows:
information_schema._pg_expandarray
sql: Add the builtin function _pg_expandarray() #24422srfs
logic test file work properlyThe text was updated successfully, but these errors were encountered: