-
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: add the ability to access a column from the results of an SRF #24832
Conversation
@knz Here is my WIP I was talking about. Take a look at the final test case the new logic tests. The one that is commented out. This is what I still need to address. |
This is good work and a clear step forward. Also this PR has helped us (at least me) understand/uncover the additional semantics described in #24866, which suggests there is more work needed before fully solving #16971. I recommend not going there for the time being, and focusing on one step at a time (this one). Reviewed 10 of 10 files at r1. pkg/sql/render.go, line 554 at r1 (raw file):
I fear that this logic is incomplete. I have experimented and found an extension of this code that works better. Let's chat about it. pkg/sql/logictest/testdata/logic_test/generators, line 492 at r1 (raw file):
This test is exercising a totally unrelated feature, unfortunately. Please add a TODO and refer to this: #24866 Comments from Reviewable |
b33daac
to
2412bcb
Compare
Reviewed 6 of 7 files at r2, 2 of 2 files at r3. pkg/sql/logictest/testdata/logic_test/generators, line 109 at r3 (raw file):
maybe add a TODO here with a reference to #20511. pkg/sql/logictest/testdata/logic_test/generators, line 235 at r3 (raw file):
we may or may not want to keep the change in the column label. I haven't yet figured out how to restore the previous behavior. Comments from Reviewable |
pkg/sql/logictest/testdata/logic_test/generators, line 235 at r3 (raw file): Previously, knz (kena) wrote…
I like this new behaviour. Comments from Reviewable |
The commit message (and the release note) need to be extended to showcase some examples. Review status: all files reviewed at latest revision, 4 unresolved discussions. Comments from Reviewable |
d34b745
to
2b4493e
Compare
Ok, this is ready for a proper review. There one leftover failing logic test that I'm looking into. @jordanlewis you've touched this code in the past, can you take a quick look? I think it would be a good idea to get another set of eyes on it. @rytaft there is a sql language change here, so I wanted someone on the optimizer team to take note. |
I also updated the PR message with the updated commit message. |
Found and fixed that failing logic test. This PR is ready for a final review. Review status: 6 of 12 files reviewed at latest revision, 4 unresolved discussions. pkg/sql/logictest/testdata/logic_test/generators, line 492 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/logictest/testdata/logic_test/generators, line 109 at r3 (raw file): Previously, knz (kena) wrote…
Done. Comments from Reviewable |
👍 thanks! I don't feel like I have enough background on this to give you a good quality review, but I've made a note of this on our spreadsheet of TODOs for the optimizer. Review status: 6 of 12 files reviewed at latest revision, 4 unresolved discussions. Comments from Reviewable |
@jordanlewis Soft ping to please take a look when you have a few mins. |
Reviewed 2 of 10 files at r1, 4 of 7 files at r2, 4 of 5 files at r4, 2 of 2 files at r5. pkg/sql/sem/tree/expr.go, line 1446 at r5 (raw file):
^^^ needs attention Comments from Reviewable |
To be compatible with postgres, specifically to be able to handle cockroachdb#16971 correctly. There is a need to be able to access the results of a Set Returning Function (SRF) using a `.` accessor. This commit does 3 things. Firstly, it adds the appropriate grammer. Secondly, it rewrites the rewriteSRF function to handle named column accessors correctly. Before this change, the only way to decompose an SRF into columns was to set it it as the data source in the FROM clause, like so: ```sql SELECT * FROM information_schema._pg_expandarray(ARRAY['c', 'b', 'a']); +---+---+ | x | n | +---+---+ | c | 1 | | b | 2 | | a | 3 | +---+---+ SELECT x FROM information_schema._pg_expandarray(ARRAY['c', 'b', 'a']); +---+ | x | +---+ | c | | b | | a | +---+ SELECT n FROM information_schema._pg_expandarray(ARRAY['c', 'b', 'a']); +---+ | n | +---+ | 1 | | 2 | | 3 | +---+ ``` With this commit, the following are now valid syntax: ```sql SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).*; +---+---+ | x | n | +---+---+ | c | 1 | | b | 2 | | a | 3 | +---+---+ SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).x; +---+ | x | +---+ | c | | b | | a | +---+ SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).n; +---+ | n | +---+ | 1 | | 2 | | 3 | +---+ ``` Thirdly, this changes the behaviour of the resulting columns from SRFs. If there is more than one named element in the resulting tuple, the column for the tuple will now be named for all elements: ```sql SELECT information_schema._pg_expandarray(ARRAY[3, 2, 1]) ``` Will produce a tuple column like named `("information_schema._pg_expandarray".x, "information_schema._pg_expandarray".n)`. The bahaviour does deviate from postgres, which simply names the column after the name of the function, in this case `_pg_expandarray`. Here's another example of this difference: ```sql SELECT 'a', pg_get_keywords(), 'c' LIMIT 1; In Postgres: ?column? | pg_get_keywords | ?column? ----------+----------------------+---------- a | (abort,U,unreserved) | c In Cockroach: +-----+--------------------------------+-----+ | 'a' | (pg_get_keywords.word, | 'c' | | | pg_get_keywords.catcode, | | | | pg_get_keywords.catdesc) | | +-----+--------------------------------+-----+ | a | ('abort','U','unreserved') | c | +-----+--------------------------------+-----+ ``` Release note (sql change): Set Returning Functions (SRF) can now be accessed using `(SRF).x` where `x` is the name of a column returned form the SRF or a `*`. For example, `SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).x` and `SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).*` are both now valid syntax. Also, the naming of the resulting columns from SRFs has been updated to provide more information about the resulting tuple. Co-authored-by: Raphael 'kena' Poss <[email protected]>
Review status: 4 of 12 files reviewed at latest revision, 5 unresolved discussions. pkg/sql/sem/tree/expr.go, line 1446 at r5 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. Comments from Reviewable |
bors +r |
Did you mean "r+"? |
bors r+ |
24832: sql: add the ability to access a column from the results of an SRF r=BramGruneir a=BramGruneir sql: add the ability to access a column from the results of an SRF To be compatible with postgres, specifically to be able to handle #16971 correctly. There is a need to be able to access the results of a Set Returning Function (SRF) using a `.` accessor. This commit does 3 things. Firstly, it adds the appropriate grammer. Secondly, it rewrites the rewriteSRF function to handle named column accessors correctly. Before this change, the only way to decompose an SRF into columns was to set it it as the data source in the FROM clause, like so: ```sql SELECT * FROM information_schema._pg_expandarray(ARRAY['c', 'b', 'a']); +---+---+ | x | n | +---+---+ | c | 1 | | b | 2 | | a | 3 | +---+---+ SELECT x FROM information_schema._pg_expandarray(ARRAY['c', 'b', 'a']); +---+ | x | +---+ | c | | b | | a | +---+ SELECT n FROM information_schema._pg_expandarray(ARRAY['c', 'b', 'a']); +---+ | n | +---+ | 1 | | 2 | | 3 | +---+ ``` With this commit, the following are now valid syntax: ```sql SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).*; +---+---+ | x | n | +---+---+ | c | 1 | | b | 2 | | a | 3 | +---+---+ SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).x; +---+ | x | +---+ | c | | b | | a | +---+ SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).n; +---+ | n | +---+ | 1 | | 2 | | 3 | +---+ ``` Thirdly, this changes the behaviour of the resulting columns from SRFs. If there is more than one named element in the resulting tuple, the column for the tuple will now be named for all elements: ```sql SELECT information_schema._pg_expandarray(ARRAY[3, 2, 1]) ``` Will produce a tuple column like named `("information_schema._pg_expandarray".x, "information_schema._pg_expandarray".n)`. The bahaviour does deviate from postgres, which simply names the column after the name of the function, in this case `_pg_expandarray`. Here's another example of this difference: ```sql SELECT 'a', pg_get_keywords(), 'c' LIMIT 1; In Postgres: ?column? | pg_get_keywords | ?column? ----------+----------------------+---------- a | (abort,U,unreserved) | c In Cockroach: +-----+--------------------------------+-----+ | 'a' | (pg_get_keywords.word, | 'c' | | | pg_get_keywords.catcode, | | | | pg_get_keywords.catdesc) | | +-----+--------------------------------+-----+ | a | ('abort','U','unreserved') | c | +-----+--------------------------------+-----+ ``` Release note (sql change): Set Returning Functions (SRF) can now be accessed using `(SRF).x` where `x` is the name of a column returned form the SRF or a `*`. For example, `SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).x` and `SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).*` are both now valid syntax. Also, the naming of the resulting columns from SRFs has been updated to provide more information about the resulting tuple. Co-authored-by: Raphael 'kena' Poss <[email protected]> Co-authored-by: Bram Gruneir <[email protected]>
Build succeeded |
sql: add the ability to access a column from the results of an SRF
To be compatible with postgres, specifically to be able to handle #16971
correctly. There is a need to be able to access the results of a Set Returning
Function (SRF) using a
.
accessor.This commit does 3 things.
Firstly, it adds the appropriate grammer.
Secondly, it rewrites the rewriteSRF function to handle named column accessors
correctly.
Before this change, the only way to decompose an SRF into columns was to set it
it as the data source in the FROM clause, like so:
With this commit, the following are now valid syntax:
Thirdly, this changes the behaviour of the resulting columns from SRFs. If
there is more than one named element in the resulting tuple, the column for the
tuple will now be named for all elements:
Will produce a tuple column like named
("information_schema._pg_expandarray".x, "information_schema._pg_expandarray".n)
.The bahaviour does deviate from postgres, which simply names the column after
the name of the function, in this case
_pg_expandarray
.Here's another example of this difference:
Release note (sql change): Set Returning Functions (SRF) can now be accessed
using
(SRF).x
wherex
is the name of a column returned form the SRF or a*
. For example,SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).x
andSELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).*
are bothnow valid syntax.
Also, the naming of the resulting columns from SRFs has been updated to provide
more information about the resulting tuple.
Co-authored-by: Raphael 'kena' Poss [email protected]