Skip to content

[Feature Request]: Support folded result_scan sources in correlated scalar LIMIT 0 wrappers #33057

Todo

Description

@XuPeng-SH

Is there an existing issue for the same feature request?

  • I have checked the existing issues.

Searches for the exact special source in correlated scalar LIMIT 0 wrapper error and for result_scan with correlated scalar subqueries did not find a matching issue. Existing result_scan issues cover different behavior.

Is your feature request related to a problem?

On the PR #32871 head, a direct saved-result source works in the correlated scalar LIMIT 0 shape, but adding an arithmetic wrapper makes planning reject the special source:

ERROR 20102 (HY000): special source in correlated scalar LIMIT 0 wrapper is not yet implemented

The rejection is fail-closed; no incorrect result or authorization bypass was observed. However, the two expression shapes have the same empty-scalar semantics, so the wrapper may be encountered through expression rewrites or SQL UDF folding.

Describe the feature you'd like

Support a saved result_scan source in this wrapper and return SQL NULL, while retaining the saved-result authorization and live-source checks.

If this combination is intentionally unsupported, please document that contract explicitly and close this request as a deliberate limitation rather than leaving the support boundary implicit.

Describe implementation you've considered

PR #32871 adds source-admission logic for semantic-root LIMIT 0 correlated scalars. Its safe direct-table path does not admit special sources such as result_scan; the observed failure is therefore controlled rather than a panic.

An implementation would need to preserve the frontend saved-result authorization and any live saved-result requirement while folding the wrapper. It should not weaken the new fail-closed boundary for other special sources.

Documentation, Adoption, Use Case, Migration Strategy

A consistent expression boundary would let planner rewrites and SQL UDF argument folding preserve behavior when they introduce arithmetic wrappers around an otherwise supported scalar.

Regression coverage should include:

  • Direct and arithmetic-wrapped result_scan forms.
  • Administrator and ordinary-role authorization.
  • Empty scalar results returning NULL.
  • Rejection of unsafe saved-result sources in named windows.
  • A control showing that unsupported special sources still fail closed.

Additional information

Tested revision: fcdb62f61954d40228d4c3716fca71382944dd07 (PR #32871 head).

Environment: macOS arm64, standalone 1 CN / 1 TN / 1 LogService, MySQL text protocol, fresh source build and new SQL probe.

Reproduction:

DROP DATABASE IF EXISTS saved_limit0_fold;
CREATE DATABASE saved_limit0_fold;
USE saved_limit0_fold;

CREATE TABLE outer_rows(a INT);
CREATE TABLE inner_rows(d INT);
INSERT INTO outer_rows VALUES (2), (20);
INSERT INTO inner_rows VALUES (4);

SET save_query_result = ON;
/* save_result */ SELECT a FROM outer_rows;
SET @saved = last_query_id();
SET save_query_result = OFF;

-- Succeeds on the PR head and returns NULL.
SELECT (
  SELECT (
    SELECT q.a FROM inner_rows i
    WHERE i.d > q.a LIMIT 0
  )
  FROM result_scan(@saved) q
  LIMIT 1
);

-- Rejected with "special source in correlated scalar LIMIT 0 wrapper".
SELECT NULL + (
  SELECT (
    SELECT q.a FROM inner_rows i
    WHERE i.d > q.a LIMIT 0
  )
  FROM result_scan(@saved) q
  LIMIT 1
);

Related work: #32869 and PR #32871. This request is intentionally scoped to the remaining special-source support boundary, not the original direct-table fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions