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

sql: properly reject nested generators in ROWS FROM #27390

Merged
merged 1 commit into from
Jul 11, 2018

Conversation

knz
Copy link
Contributor

@knz knz commented Jul 11, 2018

Fixes #27389.

Prior to this patch, invalid uses of SRFs as arguments to other
functions in ROWS FROM were not properly rejected, and were only
caught at evaluation time (i.e. much too late).

This patch fixes it by rejecting these uses early.

Release note (bug fix): invalid uses of set-generating functions in
FROM clauses are now reported with the same error code as PostgreSQL.

@knz knz requested review from rytaft and a team July 11, 2018 09:15
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

Thanks for the quick fix!

Reviewed 4 of 4 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained


pkg/sql/sem/tree/type_check.go, line 57 at r1 (raw file):

	// inFuncExpr is temporarily set to true while type checking the
	// parameters of a function. Used to detect nested generators.
	inFuncExpr bool

Do you really need this flag? Seems like you should be able to get away without it...


pkg/sql/sem/tree/type_check.go, line 711 at r1 (raw file):

	if ctx != nil {
		// We'll need to remember we are in a function application to
		// generate suitable errors in checkFunctionUsage().

I'm confused - why did you add this block of code after the call to checkFunctionUsage?

@knz knz force-pushed the 20180711-rows-from-reject branch from 4596f9d to 108ab92 Compare July 11, 2018 16:28
@knz knz requested a review from a team July 11, 2018 16:28
Copy link
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained


pkg/sql/sem/tree/type_check.go, line 57 at r1 (raw file):

Previously, rytaft wrote…

Do you really need this flag? Seems like you should be able to get away without it...

Hah! Mind explaining me how?


pkg/sql/sem/tree/type_check.go, line 711 at r1 (raw file):

Previously, rytaft wrote…

I'm confused - why did you add this block of code after the call to checkFunctionUsage?

Added clarification in comment.

See also the (new) comment above RejectNestedGenerators.

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 2 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale)


pkg/sql/sem/tree/type_check.go, line 57 at r1 (raw file):

Previously, knz (kena) wrote…

Hah! Mind explaining me how?

Ah never mind - I was confused.


pkg/sql/sem/tree/type_check.go, line 711 at r1 (raw file):

Previously, knz (kena) wrote…

Added clarification in comment.

See also the (new) comment above RejectNestedGenerators.

Thanks!


pkg/sql/sem/tree/type_check.go, line 734 at r2 (raw file):

		// to checkFunctionUsage() above) because the top-level FuncExpr
		// must be acceptable even if it is a SRF and
		// RehectNestedGenerators is set.

Rehect -> Reject

Prior to this patch, invalid uses of SRFs as arguments to other
functions in ROWS FROM were not properly rejected, and were only
caught at evaluation time (i.e. much too late).

This patch fixes it by rejecting these uses early.

Release note (bug fix): invalid uses of set-generating functions in
FROM clauses are now reported with the same error code as PostgreSQL.
@knz knz force-pushed the 20180711-rows-from-reject branch from 108ab92 to e870a82 Compare July 11, 2018 19:06
Copy link
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

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

Thank you!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale)


pkg/sql/sem/tree/type_check.go, line 734 at r2 (raw file):

Previously, rytaft wrote…

Rehect -> Reject

Done.

@knz
Copy link
Contributor Author

knz commented Jul 11, 2018

bors r+

craig bot pushed a commit that referenced this pull request Jul 11, 2018
27390: sql: properly reject nested generators in ROWS FROM r=knz a=knz

Fixes #27389.

Prior to this patch, invalid uses of SRFs as arguments to other
functions in ROWS FROM were not properly rejected, and were only
caught at evaluation time (i.e. much too late).

This patch fixes it by rejecting these uses early.

Release note (bug fix): invalid uses of set-generating functions in
FROM clauses are now reported with the same error code as PostgreSQL.

27396: sql: help the user understand unsupported correlation r=knz a=knz

Fixes #24684.

Prior to this patch, a client trying to use an unsupported correlated
query would encounter an obscure error like "column v does not exist"
or "no data source matches prefix".

Given that the new optimizer code can determine whether a query is
correlated, we can use this information to enhance the error message.

Before:

```
> select * from pg_class a where exists (select * from pg_class b where a.oid = b.oid);
pq: no data source matches prefix: a

> select * from pg_class a where exists (select * from kv where v::oid = oid);
pq: column "oid" does not exist
```

After:

```
> select * from pg_class a where exists (select * from pg_class b where a.oid = b.oid);
pq: no data source matches prefix: a
HINT: some correlated subqueries are not supported yet - see
      #3288

> select * from pg_class a where exists (select * from kv where v::oid = oid);
pq: column "oid" does not exist
HINT: some correlated subqueries are not supported yet - see
      #3288
```

Note: some correlated queries do not benefit from this improvement,
specifically those for which the optimizer code aborts early before it
has detected correlation (e.g. because of some other unrelated
feature).

Release note (sql change): CockroachDB will now report a hint in the
error message if it encounters a correlated query that it does not
support yet.


Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented Jul 11, 2018

Build succeeded

@craig craig bot merged commit e870a82 into cockroachdb:master Jul 11, 2018
@knz knz deleted the 20180711-rows-from-reject branch February 14, 2019 12:50
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: nested SRFs not properly rejected in ROWS FROM
3 participants