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: unwanted correlation hides semantic errors in delegated SHOW statements #100996

Open
knz opened this issue Apr 8, 2023 · 1 comment
Open
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team

Comments

@knz
Copy link
Contributor

knz commented Apr 8, 2023

Describe the problem

I typed the following INVALID query while investigating #99200:

SHOW JOBS SELECT job_id FROM system.jobs WHERE job_type='CHANGEFEED'

(The query is invalid because system.jobs does not have a job_id column.)

Observe: The query runs to completion (and returns bogus results).

The reason for this behavior is that under the hood SHOW JOBS XXX expands into
SELECT ... FROM crdb_internal.jobs WHERE job_id IN (XXX)

So in the example above we get:

SELECT ... FROM crdb_internal.jobs WHERE job_id IN (
  SELECT job_id FROM system.jobs ...
)

And then the query correlation rules apply: because job_id in the sub-query doesn't exist in system.jobs, it is picked from the outer scope (from crdb_internal.jobs). So we get effectively a cross-join between the two tables.

Expected behavior

Every time one of the "delegate" functions creates SQL syntax using a parameter that is also a "select clause", we need to be careful to disable the query correlation rules.

For example, in the above this can be achieved via

WITH ids AS (SELECT job_id FROM system.jobs)
SELECT ... FROM crdb_internal.jobs WHERE job_id IN (TABLE ids)

which, in this case, properly errors out.

We would also need to audit the other delegate functions accordingly.

Jira issue: CRDB-26737

@knz knz added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team labels Apr 8, 2023
@dt
Copy link
Member

dt commented Apr 10, 2023

Not that this behavior is identical on 22.1, 22.2 -- this isn't new or related to more recent jobs changes.

@knz knz changed the title sql: unwanted correlation hides semantic errors in SHOW JOBS and perhaps other delegated SHOW statements sql: unwanted correlation hides semantic errors in delegated SHOW statements Apr 11, 2023
@mgartner mgartner moved this to New Backlog in SQL Queries Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team
Projects
Status: Backlog
Development

No branches or pull requests

2 participants