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
Conversation
…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
DerZc
force-pushed
the
fix-mdev-40565
branch
from
September 21, 2026 05:55
efd8e51 to
9bac006
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Regression coverage
The regression evaluates SELECT (SELECT 1 LIMIT 0) and requires a NULL result.
mysql-test/main/scalar_subquery_limit_zero.testmysql-test/main/scalar_subquery_limit_zero.resultValidation
On
11.4atd10e5d726799b1cd57cc866f40aad68c720803da:main.scalar_subquery_limit_zerofailed on the unchanged target branch and passed with this fix.main.select,main.subselect.