-
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 set-returning functions within other render expressions #14369
Conversation
7d5b25c
to
4213012
Compare
Thanks for taking on this issue! At first glance I'm not sure what happened with the tests, but I gave this a pass of style comments until you figure out what's going on. Review status: 0 of 5 files reviewed at latest revision, 4 unresolved discussions, some commit checks failed. pkg/sql/render.go, line 343 at r1 (raw file):
nit: i think you can do without this extra pkg/sql/render.go, line 372 at r1 (raw file):
I would rephrase this comment, since it's not clear at what point if ever we'll be working on lateral correlated subqueries. Maybe something like "This visitor is intentionally limited to extracting a single SRF, because we don't support lateral correlated subqueries." pkg/sql/parser/generator_builtins.go, line 100 at r1 (raw file):
s/description/slice of Builtins/ pkg/sql/parser/indexed_vars.go, line 147 at r1 (raw file):
I think it might make more sense for reader comprehension and anti-rot to just use Comments from Reviewable |
4213012
to
e31c426
Compare
Thanks for the review! Turns out you can't call Review status: 0 of 10 files reviewed at latest revision, 4 unresolved discussions, some commit checks pending. pkg/sql/render.go, line 343 at r1 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
The linter complains about pkg/sql/render.go, line 372 at r1 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. pkg/sql/parser/generator_builtins.go, line 100 at r1 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. pkg/sql/parser/indexed_vars.go, line 147 at r1 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. Comments from Reviewable |
d8c38a1
to
5e461e7
Compare
I would recommend here and in future PRs that make a significant contribution to our SQL semantics that you outline in the commit message how this works. Reviewed 2 of 5 files at r1, 8 of 8 files at r2. pkg/sql/render.go, line 456 at r2 (raw file):
I saw that you removed the (previously incorrect) explicit reset of the ivarHelper. I still would like an explanatory comment here: "although the source was modified and now supports more columns, we do not change the ivarHelper here because any subsequent column references should be checked against the original column list, not the column list extended with the new SRF." That said, I think your patch still contains an error as-is. Right now the source name of a SRF will be the name of the function. Consider the following statement:
This should fail with Can you check this (we can also discuss the issues around that.) Comments from Reviewable |
d8d378d
to
43dae97
Compare
Done. Review status: 2 of 11 files reviewed at latest revision, 5 unresolved discussions, some commit checks pending. pkg/sql/render.go, line 456 at r2 (raw file): Previously, knz (kena) wrote…
I'm a bit confused, @knz, since I added a mutating Comments from Reviewable |
Reviewed 9 of 9 files at r3. pkg/sql/render.go, line 456 at r2 (raw file): Previously, benesch (Nikhil Benesch) wrote…
Thanks for pointing it out; I had overlooked what AppendSlot really does. Yes, this works. Comments from Reviewable |
43dae97
to
113dcad
Compare
Oops, no, I didn't mean to impact column names anywhere else. Can you sanity check the last update? I've reproduced the diff against the rev you last reviewed below, since Reviewable seems confused. diff --git a/pkg/sql/render.go b/pkg/sql/render.go
index e08e9cea4..17d55e5a5 100644
--- a/pkg/sql/render.go
+++ b/pkg/sql/render.go
@@ -505,6 +505,14 @@ func getRenderColName(searchPath parser.SearchPath, target parser.SelectExpr) (s
if err := target.NormalizeTopLevelVarName(); err != nil {
return "", err
}
+
+ // If target.Expr is a funcExpr, resolving the function within will normalize
+ // target.Expr's string representation. We want the output column name to be
+ // unnormalized, so we compute target.Expr's string representation now, even
+ // though we may choose to return something other than exprStr in the switch
+ // below.
+ exprStr := target.Expr.String()
+
switch t := target.Expr.(type) {
case *parser.ColumnItem:
// We only shorten the name of the result column to become the
@@ -525,7 +533,8 @@ func getRenderColName(searchPath parser.SearchPath, target parser.SelectExpr) (s
return fd.Name, nil
}
}
- return target.Expr.String(), nil
+
+ return exprStr, nil
}
// appendRenderColumn adds a new render expression at the end of the current list. |
In 0903a36 @jordanlewis added support for set-returning functions (SRFs) within a render expression, like: SELECT generate_series(1, 3); This commit adds support for detecting SRFs in more complicated render expressions, like: SELECT 1 + generate_series(1, 3); Expressions that involve multiple SRFs, like SELECT generate_series(generate_series(1, 3), 3); are still unsupported. (Yes, that really is a query which PostgreSQL supports.) Such queries require lateral correlated subqueries, which are not yet supported, but it should be easy to adapt the infrastructure in this commit when lateral correlated subqueries land. The approach is as follows: for each SRF that appears in a target expression, hoist the SRF into a CROSS JOIN and replace the SRF with a reference to the cross-joined table. For example, the query SELECT 1 + generate_series(1, 3) FROM generate_series(1, 2); is rewritten to SELECT 1 + t1.c1 FROM generate_series(1, 2) CROSS JOIN generate_series(1, 3) AS t1 (c1); except the rewritten table does not need to be explicitly named `t1.c1`, since we can refer to it internally using an IndexedVar with the appropriate index. Fixes cockroachdb#13146.
113dcad
to
f179160
Compare
In 0903a36 @jordanlewis added support for set-returning functions (SRFs) within a render expression, like:
This commit adds support for detecting SRFs in more complicated render expressions, like:
Expressions that involve multiple SRFs, like
are still unsupported. (Yes, that really is a query which PostgreSQL supports.) Such queries require lateral correlated subqueries, which are not yet supported, but it should be easy to adapt the infrastructure in this commit when lateral correlated subqueries land.
Fixes #13146.
This change is![Reviewable](https://camo.githubusercontent.com/1541c4039185914e83657d3683ec25920c672c6c5c7ab4240ee7bff601adec0b/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)