Skip to content

MDEV-40565 LIMIT / OFFSET is discarded when a table-less scalar subquery is flattened, so a zero-row subquery returns a value instead of NULL - #5707

Open
DerZc wants to merge 1 commit into
MariaDB:11.4from
DerZc:fix-mdev-40565
Open

DerZc wants to merge 1 commit into
MariaDB:11.4from
DerZc:fix-mdev-40565

Conversation

@DerZc

@DerZc DerZc commented Sep 21, 2026

Copy link
Copy Markdown

SELECT (SELECT 1 LIMIT 0) should return NULL, but flattening the table-less scalar subquery discards its row-limiting semantics and returns 1. A nonzero OFFSET can have the same effect.

Bug report: https://jira.mariadb.org/browse/MDEV-40565

Root cause

The scalar-subquery pull-out checks that there is one selected expression and no tables or filtering conditions, but does not establish that LIMIT/OFFSET preserves that row.

Changes

  • Add a guard for scalar-subquery LIMIT/OFFSET before pulling the selected item into the outer query.
  • Allow a missing limit or a positive basic constant limit, and a missing offset or a basic constant offset of zero.
  • Keep zero limits, nonzero offsets, and limits that cannot be established statically on the normal subquery path.

Regression coverage

The regression evaluates SELECT (SELECT 1 LIMIT 0) and requires a NULL result.

  • mysql-test/main/scalar_subquery_limit_zero.test
  • mysql-test/main/scalar_subquery_limit_zero.result

Validation

On 11.4 at d10e5d726799b1cd57cc866f40aad68c720803da:

  • The server build passed.
  • main.scalar_subquery_limit_zero failed on the unchanged target branch and passed with this fix.
  • Existing MTR tests passed: main.select, main.subselect.
  • MTR reported no test-state cleanup failures.
  • The full regression suite was not run.

@CLAassistant

CLAassistant commented Sep 21, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…ery is flattened, so a zero-row subquery returns a value instead of NULL

SELECT (SELECT 1 LIMIT 0) should return NULL, but flattening the table-
less scalar subquery discards its row-limiting semantics and returns 1.
A nonzero OFFSET can have the same effect.

The scalar-subquery pull-out checks that there is one selected
expression and no tables or filtering conditions, but does not establish
that LIMIT/OFFSET preserves that row.

Add a guard for scalar-subquery LIMIT/OFFSET before pulling the selected
item into the outer query. Allow a missing limit or a positive basic
constant limit, and a missing offset or a basic constant offset of zero.
Keep zero limits, nonzero offsets, and limits that cannot be established
statically on the normal subquery path.

The regression evaluates SELECT (SELECT 1 LIMIT 0) and requires a NULL
result.

Bug report: https://jira.mariadb.org/browse/MDEV-40565
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants