Skip to content

[4.2-dev] Fix correlated scalar subquery with LIMIT 0 - #32870

Merged
XuPeng-SH merged 10 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:codex/issue-32869-42
Nov 23, 2026
Merged

XuPeng-SH merged 10 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:codex/issue-32869-42

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • Improvement
  • Test and CI

Which issue(s) this PR fixes:

Fixes #32869. The result metadata and control authorization follow the corresponding changes in #32871.

What this PR does / why we need it:

For the supported ordinary-table SELECT shapes on 4.2, a correlated scalar subquery with a semantic-root LIMIT 0 returns a typed NULL for each outer row. The planner retains source dependencies through expression folding for authorization and catalog validation. The execution path excludes detached authorization metadata from scheduling, DOP, and statistics.

Saved-result Tables lists the executable table and external scans. Saved-result authorization still uses the persisted plan and statement. An affected wrapper can keep a control function only when the optimized plan retains the projection that frontend authorization checks. A wrapper whose control call is folded away fails closed.

Validation

Rebased onto 4.2-dev at 6c05e42. All eight preceding patches are equivalent in git range-diff.

  • The rebased baseline listed outer_rows, inner_rows, restricted_wrapper in META_SCAN for a saved query whose folded sources were detached. The fixed build listed outer_rows and returned the same typed NULL results.
  • The administrator's retained control wrapper returns NULL. A restricted role with access to both tables is denied by frontend authorization. The planner rejects wrappers that lose their control source.
  • The isolated SQL BVT passed all 65 statements using the committed results. All pre-existing result assertions were retained.
  • Full short race suites for pkg/sql/plan and pkg/frontend passed. Formatting, diff checks, err-check, vet and incremental golangci-lint passed. Molint reported the same three checkrecover diagnostics on the candidate and the clean 4.2-dev base.

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner November 17, 2026 18:37
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: b0086322580d3d78be0322c6c1826e5abfd816ce

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: b0086322580d3d78be0322c6c1826e5abfd816ce

Assessment: request_changes

The rewrite reuses the existing scalar join and LIMIT 0 compiler path. The planner and public SQL checks passed, but the SQL UDF guard can miss table authorization dependencies in nested query blocks.

Model: gpt-6.2-sol / xhigh

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

  • P2 pkg/sql/plan/base_binder.go:2907: A bound SQL UDF argument contains a rewritten correlated scalar LIMIT 0 in a child query block whose context owns the rewrite marker.
    Impact: If UDF expansion discards the argument, optimization can prune its table scan before the frontend checks table privileges. This can omit a required authorization check for the referenced table.
    Evidence: The guard reads only the context at the subquery root. The rewrite marks the source and immediate consumer contexts in correlated_pagination.go:474–480. Set-operation operands use separate contexts in query_builder.go:3497–3531, and their parent node at line 3689 does not inherit the marker. Preserve rewrite provenance across the full bound argument or prevent the rewrite in arguments that expansion can discard. Extend the existing direct-block regression test to cover a separate child query block.

Validation:

  • Verified the frozen source, patch, discussion coverage, and 173 native build inputs. Reconciliation passed under receipt checks/1794941885911082969-2.json.

  • Reused the same-head short planner suite. All four added test roots passed. Receipt and log are retained under reused-attempt-1/1794941646388412736-2.json and reused-attempt-1/planner-normal.jsonl.

  • Reused the same-head MySQL probe on an embedded cluster. Typed NULLs, duplicate and NULL outer rows, filtered and unfiltered LIMIT 0, OFFSET, positive LIMIT, and the nested empty-input aggregate passed. Receipt and log are retained under reused-attempt-1/1794941781819439665-2.json and reused-attempt-1/public-sql.jsonl.

Limitations:

  • The authorization finding comes from source analysis. The public SQL probe covered results and metadata. Authorization denial was unverified at runtime.

  • The retained checks cover the short planner suite and focused MySQL probe. The supplied planner-relevant CI runs were pending.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I updated this PR at 48d6e47d1149cb2c035e454c2c9613b81c59d634, based on the latest 4.2-dev head 72c43e9305d8355c940075e414f30eaf2e4112a6.

  • The admission now carries rewrite provenance across query-block and set-operation binding contexts. SQL UDF calls that receive an affected pre-flattened argument fail closed, so an ignored argument cannot remove a protected source before authorization.
  • Added a UNION child regression alongside the direct ignored-argument control. BVT also retains the existing direct secret-table denial.
  • Re-ran the exact issue BVT: 32/32 passed. Full pkg/sql/plan tests passed at 76.9% statement coverage; planner race and go vet passed. gofmt and git diff --check are clean.

The new GitHub CI checks for this head are running. The existing PREPARE boundary remains covered by the branch's current guard.

@XuPeng-SH

XuPeng-SH commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: 48d6e47d1149cb2c035e454c2c9613b81c59d634

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 48d6e47d1149cb2c035e454c2c9613b81c59d634

Assessment: request_changes

The rewrite preserves the bound ordinary-table scan under a typed empty input and reuses the existing SINGLE join and compiler shortcut. The generation guard closes the earlier SQL-UDF UNION finding. Source retention still depends on the caller's expression simplification, and the separate AST and bound-expression checks disagree for the explicitly admitted IFNULL projection.

Model: gpt-6.2-sol / xhigh

Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0

  • P2 pkg/sql/plan/base_binder.go:2829: A built-in binder simplification discards an already-bound argument containing a nested scalar that took the new LIMIT 0 rewrite.
    Impact: The rewritten table dependency may disappear from the optimized plan before frontend authorization. The affected query shapes previously stopped in correlated pagination when the nested projection and predicate referenced the outer row.
    Evidence: The rewrite attaches the scan within the argument's query subtree and increments the generation at correlated_pagination.go:475. base_binder.go:2824–2829 returns a successful built-in result before the generation guard at line 2863. A null-propagating arithmetic simplification can discard an operand entirely at lines 3620–3628. The optimizer compacts the plan from Steps at optimize.go:92 before frontend authorization reads the surviving scans at mysql_cmd_executor.go:3078 and authenticate.go:7089. Preserve the dependency across argument simplification or fail closed at the dependency loss boundary. This is a source finding.

  • P2 pkg/sql/plan/correlated_pagination.go:390: An ordinary-table correlated scalar with root LIMIT 0 has IFNULL around the outer column in its projection and an outer reference in its WHERE predicate.
    Impact: The query still returns a correlated-pagination NYI in place of a typed NULL, although the new source eligibility check and its test explicitly admit IFNULL.
    Evidence: The AST proof admits IFNULL at line 211. base_binder.go:2629–2638 lowers it to CASE with ISNULL, while the bound result proof accepts IFNULL and rejects CASE and ISNULL. The focused probe in projection-probe.log (receipt checks/1794952782325848867-2.json) passed for the direct projection and failed for IFNULL with the old pagination error. Match admission to the bound representation and exercise the complete planner path with a regression case.

Validation:

  • Private /tmp and loopback preflight passed. See preflight.log and checks/1794952573293842000-4.json.

  • Built matching CPU native prerequisites from the frozen inputs. See native-inputs.txt, native-build.log, and checks/1794952613162290348-2.json.

  • Full pkg/sql/plan on the frozen head passed with 5,687 passing test and subtest events and three skips. The rewritten scan-retention and set-operation SQL-UDF guard tests passed. See planner.log and checks/1794952747886471274-2.json.

  • Focused planner overlay ran a direct projection control and an IFNULL variant. The control passed and the variant failed during planning. See projection_overlay source and mapping in projection_probe_test.go and projection-overlay.json, projection-probe.log, and checks/1794952782325848867-2.json.

Limitations:

  • The source-retention finding follows the production binding, compaction, and authorization path by inspection. It was not exercised with a restricted user.

  • The frozen author discussion reports standalone SQL/BVT, race, coverage, and vet runs. The raw logs were not supplied for independent verification, and relevant CI was still running in the frozen snapshot.

@matrix-meow matrix-meow added size/XXL Denotes a PR that changes 2000+ lines and removed size/XL Denotes a PR that changes [1000, 1999] lines labels Nov 18, 2026
@XuPeng-SH

XuPeng-SH commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: c0c7fc57ecaa00aa36995426bcfc8126863a0f10

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: c0c7fc57ecaa00aa36995426bcfc8126863a0f10

Assessment: approve

For the supported ordinary-table shape, a correlated scalar subquery with a root LIMIT 0 returns a typed NULL for each outer row. The rewrite reuses the existing SINGLE join and zero-limit compiler path to skip the inner projection. When folding discards a source, the plan retains its authorization and catalog metadata while keeping that metadata out of scheduling and execution statistics. The earlier UDF, expression binding, and scheduling findings are resolved on this head.

Model: gpt-6.2-sol / xhigh

Session: 01a1eecd-3287-70c0-bb93-ab923faf5f3f · slot 0 · generation 2

Validation:

  • The full short Go race suites passed for pkg/sql/plan and pkg/frontend, with 1,876 and 1,699 top-level tests passing respectively. The logs and matching exit-zero receipts are recorded in attempt-2/validation-reconciliation.json.

  • Matched the retained native build to the frozen source inputs and confirmed private /tmp and loopback preflight passed in both attempts. Verified the frozen patch and accounted for all 17 discussion entries.

Limitations:

  • The author reported three 59/59 isolated BVT runs on this head. The independent review did not rerun the distributed SQL fixture. Relevant CI jobs were still in progress in the frozen snapshot.

@XuPeng-SH

XuPeng-SH commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: de3f1fdc111242ae8d5b9606c6a107913abfd881

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: de3f1fdc111242ae8d5b9606c6a107913abfd881

Assessment: approve

The fix reuses the existing SINGLE join and zero-limit compiler path to return a typed NULL for each outer row in the supported SELECT shapes. Source metadata survives expression folding and remains available to authorization and catalog checks. Placement, DOP, and statement statistics exclude detached metadata nodes. The earlier UDF, expression-binding, and scheduling findings are resolved.

Model: gpt-6.2-sol / xhigh

Session: 01a1ee7f-fdcb-7b83-ad57-3b17a0e9d494 · slot 1 · generation 1

Validation:

  • Verified the frozen head, merge base, supplied patch, whitespace, native build inputs, and runtime isolation preflight.

  • Focused planner and frontend suites passed, covering source retention, CTE scope, SQL UDF guards, scheduling, and statistics.

  • Seven MySQL-protocol cases passed on the embedded three-CN fixture, covering typed NULLs, duplicate and NULL outer rows, OFFSET, IFNULL, arithmetic predicates, positive LIMIT, nested COUNT, and max_dop=1.

  • Reconciled five check receipts and all 19 discussion entries in the diagnostic directory.

Limitations:

  • Local checks used Go 1.26.6. The restricted-role BVT results were author-reported on the earlier revision. The fixture is byte-identical at this head, and current-head authorization review used source tracing and focused tests.

  • The ordinary SQL UDF path still walks contexts for positive-LIMIT arguments. The isolated guard benchmark measured about 88 microseconds and zero allocations at 256 modeled calls.

@XuPeng-SH

XuPeng-SH commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: c63d7e6fa0f8f781f533bf7eb7b5b9c1c0a2e6c2

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: c63d7e6fa0f8f781f533bf7eb7b5b9c1c0a2e6c2

Assessment: approve

Both queries reported in #32869 now return one typed NULL per outer row. The fix reuses the existing scalar join, LIMIT 0 compiler shortcut, and frontend control-call detector. Dependencies removed by folding remain available for authorization and catalog validation. Detached authorization nodes are excluded from scheduling, statistics, and the saved-result table listing.

Model: gpt-6.2-sol / xhigh

Session: 01a1feb0-0427-7772-9d89-786820ec84a5 · slot 1 · generation 2

Validation:

  • Both short race suites passed for pkg/sql/plan and pkg/frontend. See full-short-race.jsonl and checks/1795330651975161279-2.json.

  • A single-CN MySQL protocol probe passed for typed NULLs, duplicate and nullable outer rows, positive LIMIT and nested COUNT controls, saved-result metadata at max_dop=1, and privilege denial for a restricted role reading the saved result or invoking the retained control wrapper. See protocol-persisted-v2.jsonl and checks/1795330713296995204-2.json.

  • Reviewed all 21 discussion entries and traced the saved-result Plan and Ast fields against v4.2.8. The pinned base-to-head diff passed git diff --check. See discussion-assessment.json and checks/1795330153993780614-2.json.

Limitations:

  • An exploratory planner probe still rejected a correlated LIMIT 0 variant with a LIKE predicate. The verified fix covers the reported shapes and the tested extensions.

  • Distributed BVT execution is author-reported. Shutdown-upgrade compatibility was assessed by source inspection of the saved-result format and authorization reader.

This branch was previously deployed

1 inactive deployment
ci — c63d7e6f Deployed Nov 22, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/enhancement kind/test-ci size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants