Skip to content

[Bug]: correlated scalar projection with outer reference fails on LIMIT 0 #32869

Done

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

The closest match is #26816, but it describes LIMIT being applied globally across outer groups and returning incorrect rows. This report is a different, still-reproducible shape: a root LIMIT 0 on a correlated scalar subquery whose projection references an outer column is rejected instead of producing an empty scalar result. No open issue with the current error or this exact behavior was found.

Branch Name

main

Also reproduced on 4.2-dev and v4.2.8.

Commit ID

1064837ae2648aa20e5e1abb7edd0564493e6b4d (local main used for this report)

Additional verified revisions:

Other Environment Information

  • Hardware parameters: macOS arm64
  • OS type: macOS
  • Others: standalone local mo-service, 1 CN / 1 TN / 1 Log service, MySQL text protocol; no unit tests involved

Actual Behavior

A correlated scalar subquery with root LIMIT 0 fails when its result expression references an outer column.

On the reported main revision, both reproduction queries fail with:

ERROR 20102 (HY000): wrapped correlated scalar projection cannot be safely decorrelated is not yet implemented

The error text differs by revision:

  • 4.2-dev@7400cae: the filtered form reports correlated pagination across nested boundaries or correlated expressions is not yet implemented; the unfiltered form reports an unsupported correlated expression executor error.
  • v4.2.8@10d204b: both forms report an unsupported correlated expression executor error.

The same failure is present before and after #32841, so this is not being reported as a regression introduced by that PR.

Expected Behavior

Both queries should return one row per outer row, with NULL for the scalar result:

a    x
1    NULL
11   NULL

LIMIT 0 makes the inner query empty. An empty scalar subquery should evaluate to NULL; the outer projection must not be evaluated for a nonexistent inner row.

Steps to Reproduce

DROP DATABASE IF EXISTS correlated_limit_zero;
CREATE DATABASE correlated_limit_zero;
USE correlated_limit_zero;

CREATE TABLE t1(a INT);
CREATE TABLE t2(d INT);
INSERT INTO t1 VALUES (1), (11);
INSERT INTO t2 VALUES (4);

-- Fails on main, 4.2-dev, and v4.2.8.
SELECT a,
       (SELECT a FROM t2 WHERE d > a LIMIT 0) AS x
FROM t1
ORDER BY a;

-- Also fails on all three revisions.
SELECT a,
       (SELECT a FROM t2 LIMIT 0) AS x
FROM t1
ORDER BY a;

Controls that work and isolate the failure to an outer-reference scalar projection:

-- LIMIT 1 works: returns (1, 1), (11, NULL).
SELECT a,
       (SELECT a FROM t2 WHERE d > a LIMIT 1) AS x
FROM t1 ORDER BY a;

-- A constant correlated projection works and returns NULL for both rows.
SELECT a,
       (SELECT 1 FROM t2 WHERE d > a LIMIT 0) AS x
FROM t1 ORDER BY a;

-- A truly uncorrelated scalar works and returns NULL for both rows.
SELECT a,
       (SELECT d FROM t2 LIMIT 0) AS x
FROM t1 ORDER BY a;

-- Correlated EXISTS and IN with LIMIT 0 also work and return false.
SELECT a, EXISTS(SELECT 1 FROM t2 WHERE d > a LIMIT 0) AS x
FROM t1 ORDER BY a;

SELECT a, a IN(SELECT d FROM t2 WHERE d > a LIMIT 0) AS x
FROM t1 ORDER BY a;

Additional information

On 4.2-dev, pkg/sql/plan/correlated_pagination.go explicitly treats a semantic-root LIMIT 0 as making the whole subquery empty. The observed failure is therefore inconsistent with the intended empty-subquery handling.

Related closed issue: #26816. Its verification covered correlated LIMIT 1 global-group semantics and some LIMIT 0 cases, but the outer-reference projection shapes above remain broken on the revisions listed here.

Activity

  1. added
    kind/bug
    on Nov 17, 2026
  2. added 2 commits that reference this issue on Nov 22, 2026
    4607944
    5245da0
  3. XuPeng-SH commented on Nov 23, 2026

    @XuPeng-SH
    ContributorAuthor

    转测:本单的两个普通表相关标量子查询 LIMIT 0 原始场景已有两个分支修复:#32871 合入 main(46079446),#32870 合入 4.2-dev(5245da02)。4.2-dev 合入树与 c63d7e6f 相同,该版本 Required 和双路多 CN BVT 通过;另有复核的单 CN MySQL 协议及作者定向 BVT 记录。main 合入树同 6f199d83,有作者 SQL/BVT 记录,但最终 head 的 Required 未通过,不当作全套 CI 已通过。

    请在两个合入版本分别复跑原有带/不带 WHERE 的查询,核每个外层行返回有类型的 SQL NULL,正向 LIMIT 1 及 EXISTS/IN 对照不回归;同时核 max_dop=1、保存结果表元数据和受限角色按原规则拒绝。#33056 的旧有 untyped-NULL 排序问题和 #33057 的特殊 result_scan 支持另行跟踪,不在本次验收范围。未验证已发布 4.2.8 或生产部署;本次是交接,不是正式验收。

  4. slyunnn commented on Nov 23, 2026

    @slyunnn

    Regression and extended verification for #32869 (2026-11-23)

    Fixed for the reported ordinary-table SELECT shapes on main and 4.2-dev. Branch-specific PREPARE/CTAS boundaries are retained and stated below.

    Verification Environment

    Both latest remote refs were fetched before validation and checked in separate clean source directories:

    • main@f86cc46cfc5d5e4a0e2e52af6e4cbd58f9f9c449 — refactor(vector): remove the grouping bitmap (gsp) from Vector (#33020) (#33075), built with Go 1.27.1 / GOEXPERIMENT=simd.
    • 4.2-dev@f811a8673b44f592e5f60db10843de119d662e89 — fix(tae): bound catalog replay input memory (#33078), built with Go 1.26.5.

    Each branch independently ran a native 2 CN / 1 TN / 1 Log service / Proxy topology, sequentially on Proxy 6001, CN1 16001, CN2 16002. All three endpoints actually executed the SQL and protocol regressions, returned the expected version prefix (f86cc46cfc / f811a8673b), and passed post-error/post-test health checks. These are regression executions, not just connectivity checks. Clients used MySQL text SQL plus explicit Go MySQL-driver binary prepared operations.

    The main binary embeds full VCS revision and vcs.modified=false; SHA-256 is 97d9d4e683f65642f02462a2b595451f391ede9795e425a078faeec07ef4a71b. The 4.2 binary SHA-256 is d04bf3f98db223e68fbf9b59ce96e6b67a59f82d5954e01e3acba585b64ec990. Go 1.26.5 did not emit optional VCS stamps for that build: identity was corroborated by the clean full-SHA checkout, retained build command with the embedded commit prefix, new binary hash and runtime version checks at all three endpoints. No service source, existing binaries, Docker configuration or old data was modified. The local 4.2 ONNX download and macOS library-search-path setup issues were repaired before acceptance; their failed setup attempts are not product failures.

    What Was Covered

    • OK — both exact original queries, with and without inner WHERE d > a, return (1,NULL) and (11,NULL); the scalar's wire type is INT, not an untyped NULL placeholder.
    • OK — duplicate and NULL outer rows, empty inner input, LIMIT 0 OFFSET 1, arithmetic correlation, a discarded invalid-value projection, LIMIT 1, constant/uncorrelated scalar, EXISTS, IN, nested empty-input COUNT, and max_dop=1 controls.
    • OK — main binary prepare/execute of both original shapes, parameter changes including NULL and rebind after NULL, typed CTAS materialization and row counts. The protocol script performs 20 checks per endpoint.
    • OK — 4.2-dev's intentional correlated-LIMIT-0 PREPARE refusal (20102) and affected CTAS/function-wrapper refusal (20102), with positive binary prepared LIMIT 1 rebinding. This is its existing planner contract, not a claim of main/4.2 feature parity. The protocol script performs 22 checks per endpoint.
    • OK — persisted typed results are readable; META_SCAN excludes the detached wrapper from executable table metadata. The persisted authorization closure still requires that wrapper's table privilege: ordinary table/root-zero and saved-result queries are denied with 20101 before necessary grants and succeed afterward. The original scalar result retains its type.
    • OK — 4.2's retained control-function wrapper returns NULL for the administrator and is denied with 20101 for the restricted role after both ordinary tables are granted. Main also retains its tested control-call denial. No broad equivalence claim is made for other uncorrelated/dead-control-call forms.
    • OK — main: each of Proxy/CN1/CN2 passed 2/2 cases, 39/39 runner statements. Independently on 4.2-dev, each endpoint passed 2/2 cases, 38/38 statements. All final recording/replay groups had 0 failures and 0 skips; internal protocol counts are separate from the runner's one script statement.
    • OK — 9/9 branch-targeting checks on the independent 4.2 branch and the integrated daily branch; integrated four-case replay with existing [Bug]: 4.2-dev Sysbench random_points TPS 下降 84%,二分指向 PR #32878(待隔离复验) #32965 passed 4/4 cases and 62/62 statements.
    • Not covered — successful correlated-LIMIT-0 PREPARE or affected CTAS/view expansion on 4.2-dev, full upstream Required/BVT, released v4.2.8 or production deployments. The separately tracked untyped-NULL sorting ([Bug]: ORDER BY a view with untyped correlated LIMIT 0 NULL panics in vector.Shuffle #33056) and special wrapped result_scan sources ([Feature Request]: Support folded result_scan sources in correlated scalar LIMIT 0 wrappers #33057) are outside this acceptance; direct typed saved-result reads and authorization are covered.

    Findings

    main repair #32871, Fix correlated scalar subquery with LIMIT 0, merged as 46079446d00d40e1cef91e653378ff49c52a5895, final head 6f199d837fd838348ff67c0d16e2ec22e5de5946. 4.2-dev repair #32870, [4.2-dev] Fix correlated scalar subquery with LIMIT 0, merged as 5245da02028eeea2174c7d5b09b20584176834ab, final head c63d7e6fa0f8f781f533bf7eb7b5b9c1c0a2e6c2. Separate ancestor checks confirm inclusion in each tested branch.

    The reported root LIMIT 0 SELECTs now produce an empty scalar result without evaluating the discarded outer-reference projection. Catalog/authorization dependencies survive folding; detached metadata does not interfere with the tested max_dop=1 execution and saved-result table listing.

    The branch contracts differ. Main supports the tested correlated-zero prepared queries and typed CTAS control; 4.2 intentionally fails closed for those prepared/wrapped shapes. These were first observed as extension mismatches and then checked against the 4.2 source and existing PREPARE guard test; the results are not described as equivalent support. Plain direct CTAS is also a recorded unsupported boundary on main. This conclusion covers the reported ordinary-table SELECT bug, not every correlated-subquery form.

    Large-data regression is not required for this planner/empty-scalar defect. The original two-row fixture plus duplicate/NULL/empty and type/privilege controls directly cover the relevant semantic boundary. Both same-version CN endpoints were exercised; no engine-parallelism claim is inferred from MOTR scheduling.

    MOTR / BVT Coverage

    • Main base 1ea137a2cc09508fd1d6fe29b670143ed46e346c; branch test/issue-32869-scalar-limit-zero-main; single source commit aeb3533ddccfc13d90deba23132c87fa03e71eaa, daily cherry-pick 0733ee6881f1b1953ed774dd1edeadb238cfc31c in main daily PR #382.
    • 4.2-dev base 7ee76c16f9bac6fdb65939d56c997344474791bb; branch test/issue-32869-scalar-limit-zero-4.2-dev; single source commit e37179ca79f4adf3927db334c33523a7ad5f7fff, daily cherry-pick 6454a7ad45e00ccc9ab4dad4d85b0b0b7fe4d702 in 4.2-dev daily PR #381.

    Each issue branch has one test: add MOTR regression for #32869 commit, independently recorded branch-appropriate SQL/protocol goldens and environment-configurable Go/sh protocol coverage. The independent 4.2 discovery count is 562; the daily count is 564 after preserving #32965's two cases. PRs are authored by slyunnn. No per-issue MOTR PR was created. Fixtures and endpoint runs were serial (-j 1) because role and database lifecycle ownership is shared within the group.

    Final Conclusion

    Fixed for the two reported ordinary-table correlated scalar LIMIT 0 SELECT shapes on both tested maintenance branches. The stated 4.2 PREPARE/wrapper restrictions remain intentional unsupported boundaries. Keep #32869 open and add the user-requested close label; no issue-state transition is requested.

  5. added
    close
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

closekind/bugneeds-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions