Skip to content

[4.2-dev] fix(colexec): align scalar expression vector cardinality - #33453

Merged
XuPeng-SH merged 3 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:fix/4.2-prepared-scalar-cardinality
Nov 29, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:fix/4.2-prepared-scalar-cardinality

Conversation

@XuPeng-SH

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

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

#33452
Related to #27415. Backport of #27436 to 4.2-dev.

What this PR does / why we need it:

A prepared scalar in the projection of a multi-row ordered insert has logical length one on 4.2.9. The sorter copies the projection into a flat batch that claims more rows than the copied scalar column, then returns SQL error 20101 when it permutes the rows. This blocks a tenant event backfill during upgrade.

Use the input batch row count as the logical length of constant parameter and variable results, including cached values, NULL, and fully masked input. Scalar storage stays constant-size. The 4.2 variable executor clears the previous logical length before overwriting its cached scalar so allocation is based on a single stored value, even when the previous batch was large.

Validation

  • Reproduced the upgrade event statement over MySQL TCP on 4.2.9 and on the 4.2-dev base. The fixed statement writes both expected events, and executing the same binary prepared statement again writes zero rows.
  • The maintained prepared projection BVT passed 35/35 statements twice on the same instance, with database cleanup verified after each run. The unmodified base fails the new ordered insert and its row checks.
  • Race tests passed in colexec, order, projection, filter, hashbuild, apply, frontend, and compile. The variable-capacity test verifies reuse across 8,192 and 2 logical rows.
  • Scoped go vet and incremental golangci-lint passed. Legacy molint reports the same seven diagnostics as the exact base.

APPLY regression follow-up on 85f0688d

  • metadata_scan now accepts nonempty scalar constants broadcast over an APPLY input batch while retaining rejection of flat multirow arguments. NULL and non-string inputs return an error before reading vector storage.
  • Added unit coverage for constant/mixed cardinality, empty input, NULL and fixed-width types.
  • Real embedded MySQL TCP probe failed before the fix and passes after it: singleton variables, two-row variable/literal APPLY with actual persisted metadata, and the flat-multirow rejection control.
  • go test -race -count=1 ./pkg/sql/colexec/table_function, scoped go vet, and incremental golangci-lint pass (0 new issues). Existing MOI prepared-insert TCP/BVT evidence remains valid.
  • Full-diff review of 85f0688d approved with no actionable findings.

Backport the scalar row-count fix from matrixorigin#27436. Adapt coverage to the 4.2 expression and prepared projection fixtures.

(cherry picked from commit 087092c)
@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 29, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Commit: 628946aca6ff3f3a3219046c17b41eab47d88d90

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: 628946aca6ff3f3a3219046c17b41eab47d88d90

Assessment: request_changes

The change fixes the reproduced ordered-insert failure by reusing the existing scalar executors, but their new logical cardinality breaks an existing metadata_scan APPLY query. The consumer must be updated to handle constant vectors with batch-sized logical lengths.

Model: gpt-6.1-terra / max

Session: 01a227fa-9be9-7372-8896-a8b81f9696c9 · slot 0 · generation 43

  • P2 pkg/sql/colexec/evalExpression.go:578: A CROSS APPLY evaluates metadata_scan(@src, @col) against a left input batch containing more than one row.
    Impact: The variable vectors now have the left batch's logical length, but metadata_scan rejects arguments whose length is not exactly one. A query that previously succeeded therefore returns Error 20101: internal error: wrong input len.
    Evidence: ApplyArgsEval evaluates arguments against the whole input batch at pkg/sql/colexec/table_function/table_function.go:255, and handleDataSource enforces length one at pkg/sql/colexec/table_function/metadata_scan.go:162. An embedded SQL probe reproduced the failure for a two-row left input; the same probe passed with the base evalExpression.go overlaid. Preserve the batch-sized cardinality needed by sorting while making metadata_scan accept constant vectors with multiple logical rows, and add an APPLY regression test.

Validation:

  • Paths below are relative to /mnt/nvme/mo-bot-55-live/state/jobs/3621da51ec94d3f0ef94c545.

  • Isolation preflight passed: model-switch-1795970579502307811/preflight.log and checks/1795970622276942627-2.json in that directory.

  • Focused colexec and hashbuild tests passed: model-switch-1795970579502307811/focused-tests.log and checks/1795970720050536361-2.json.

  • The ordered-insert SQL probe returned the expected two rows after each of two executions on the candidate; the base-executor overlay reproduced the shuffle index error. See model-switch-1795970579502307811/original-candidate.log and original-baseline.log, with receipts checks/1795970817587124429-2.json and checks/1795970936782447508-2.json.

  • The metadata_scan probe failed on the candidate for the two-row APPLY and passed with the base executor overlaid. See attempt-1/candidate-apply-retry.log and checks/1795970562603124623-2.json, and model-switch-1795970579502307811/baseline-apply.log and checks/1795970679733910487-2.json.

Limitations:

  • The SQL probes used an embedded cluster. The metadata_scan target had no flushed metadata objects, so that probe establishes success versus error rather than returned metadata contents.

  • The author's reported BVT and race results were not accompanied by local validation logs; the frozen CI snapshot had UT and BVT jobs pending.

  • The updated review plan and discussion assessment are retained in model-switch-1795970579502307811; attempt-1 was read-only in this session.

@XuPeng-SH

XuPeng-SH commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Commit: 85f0688d17dfcefd468b3305eb33dab1c88bc80d

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: 85f0688d17dfcefd468b3305eb33dab1c88bc80d

Assessment: approve

The scalar executors now give constant parameter and variable vectors the input batch's logical cardinality while retaining one physical value. This reuses the existing vector and expression owners, and the variable overwrite avoids allocating for the previous batch size. The prior review's metadata_scan APPLY regression is resolved: the consumer accepts broadcast constants and retains its rejection of flat multirow arguments. The reproduced ordered insert and the affected APPLY paths both passed through the MySQL frontend; no actionable defect remains.

Model: gpt-6.1-terra / max

Session: 01a226fb-40e1-7c82-8448-1a1fd4af5968 · slot 1 · generation 36

Validation:

  • Isolation preflight and git diff --check passed (preflight.log, diff-check.log; checks/1795973541945615583-3.json and checks/1795973874275558202-3.json). Native prerequisites were copied from a repository Makefile build after verifying identical Makefile, cgo, and thirdparties tree IDs; native-provenance.txt records the comparison.

  • Focused parameter, variable, hash-build, and metadata_scan tests passed under the race detector with nonempty named selections (focused-race.log; checks/1795973821019382052-3.json). The colexec, hashbuild, and table_function packages also passed with -short (packages.log; checks/1795973897764239182-3.json).

  • An embedded-cluster MySQL frontend probe ran the ordered prepared insert twice and observed rows 1/created and 2/created after each execution. It also verified two-row metadata_scan APPLY with variables and mixed literal arguments against flushed metadata, and flat multirow rejection (sql-probe.log; checks/1795973919278449305-3.json).

  • A separate embedded MySQL probe exercised binary prepared metadata_scan APPLY twice, NULL rejection, and a successful execution after NULL (sql-prepared-probe.log; checks/1795974017845420782-3.json).

  • The previous review's base-expression overlay produced the reported index-out-of-range failure for the ordered insert, while its prior candidate passed. Its baseline expression blob matches this assignment's base; the changes since that reviewed head affect only metadata_scan. The earlier receipt and logs are identified in evidence.json:discussion_index.

Limitations:

  • The exact mo-tester case and result-file comparison were not independently run: the runner was not installed and the attempt to fetch it timed out (mo-tester-fetch.log; checks/1795974166655166506-3.json). The PR reports 35/35 statements passing twice, but its underlying run logs were not supplied. The newly added ordered-insert behavior was independently checked through the embedded MySQL frontend.

  • There is no maintained SQL case specifically for metadata_scan APPLY; its producer and consumer have focused unit coverage, and the independent embedded SQL probes exercised the integrated path.

@XuPeng-SH
XuPeng-SH merged commit df65fa5 into matrixorigin:4.2-dev Nov 29, 2026
16 of 21 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/4.2-prepared-scalar-cardinality branch November 29, 2026 18:46

This branch was successfully deployed

No deployments
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 size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants