Skip to content

fix(plan): preserve typed prepared JSON aggregate values - #29350

Merged
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue-29325-json-agg
Sep 25, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue-29325-json-agg

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG

What this PR does / why we need it

Prepared JSON_ARRAYAGG(?) and JSON_OBJECTAGG(key, ?) can keep the PREPARE-time TEXT marker/cast when EXECUTE supplies a DECIMAL or JSON value, silently changing the JSON atom to STRING.

Admit JSON aggregate value parameters to the existing runtime specialization path. Reconstruct each value argument from its SQL user-variable source type or COM_STMT runtime type, preserving the DECIMAL/JSON atom. The object key remains VARCHAR-normalized and explicit casts remain authoritative. Reuse the existing parameter-provenance restoration for runtime-plan caching.

Adds planner tests for SQL EXECUTE/COM_STMT metadata, DECIMAL/JSON/TEXT/NULL, explicit casts, object keys, and restored parameter references, plus a distributed SQL regression for prepared reuse across type changes.

The SCA failure was govet lostcancel in pkg/proxy/heartbeat_test.go, inherited from main. The heartbeat test now cancels its context when any assertion exits early.

Validation after rebasing to main 36c3fbce2c: focused planner JSON aggregate tests, pkg/proxy package tests, go vet and incremental golangci-lint for pkg/proxy and pkg/sql/plan, gofmt, and git diff --check all pass locally. The earlier head passed Ubuntu UT, coverage, and active multi-CN BVT jobs; its arm64 SCA failure is addressed here. CI for the new head is the full arm64 SCA and distributed regression evidence.

Which issue(s) this PR fixes

Fixes #29325

@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 →

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Sep 24, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Sep 24, 2026

@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.

Deep review of 2810aaa4f8dd1aa3ab862da0832a15fcca529daa against base/merge-base 36c3fbce2c99410d83bd084d1eb9f09044284012 (all 6 changed files).

Conclusion: no blocking findings; recommend merging. This account is also the PR author, so this is a COMMENT review, not a GitHub approval.

Correctness and scope

The change addresses #29325 at the binding boundary: bare JSON aggregate value markers enter existing runtime specialization and reconstruct the SQL source/protocol domain before overload rebinding. DECIMAL/JSON values therefore avoid the provisional TEXT domain. Explicit CAST remains authoritative; JSON_OBJECTAGG keys retain their existing VARCHAR normalization. Missing metadata follows the existing fallback. No new serialization, protocol, catalog, configuration, authorization, or tenant boundary is introduced. This is an ordinary focused bug fix; no new architecture/design artifact is warranted.

The 27 added production lines reuse source reconstruction and parameter provenance restoration. The separate one-line heartbeat-test cancellation fix closes assertion-failure cleanup. Neither change justifies a new framework or broad refactor.

Reuse, unhappy paths, and ownership

Closure Evidence / outcome
Q1: copied plan / cached compile ownership Rebinding works on an isolated template copy; errors return before candidate installation. Existing PrepareStmt ownership handles replacement, deferred retirement, and Close. No new cleanup owner.
Q2: waits / cancellation Planner additions introduce no blocking operations, goroutines, retries, or locks. Heartbeat test now defers cancellation on early failure; normal completion already joins the goroutine.
Q3: retained state Existing runtime cache retains one category per prepared statement, with replacement/close cleanup; no per-value cache growth is added.
Generation/value correctness Additional expression-execution probes restore ParamRefs, reset executors between executions, and verify different same-domain values, value → NULL → value, malformed JSON → valid JSON, and unchanged PREPARE templates.
Invalid inputs BLOB/BINARY/VARBINARY value domains are rejected, with subsequent valid specialization still working. Existing PR tests cover explicit casts, object-key boundaries, absent metadata, and typed NULL binding.

A diagnostic experiment forced reuse of a DECIMAL-NULL-seeded array plan and exposed aggregate NULL-type normalization. It is not a reachable cache regression here: SQL NULL without RuntimeType is excluded by preparedRuntimeCacheSupports; preparedParamValues does not attach DECIMAL RuntimeType to binary NULL packets, and conversion enrichment skips NULL. I discarded that candidate rather than treating a bypassed frontend contract as a product defect.

Performance and engineering quality

Stable eligible categories reuse the existing runtime plan (and eligible TP compile); category changes pay rebinding and replace one entry. AP compiles remain statement-owned. No aggregate row-processing loop or new I/O/synchronization is added. I found no structural performance regression; this review does not claim a measured throughput improvement. Existing focused UT/BVT fixtures are appropriate and the change is not overengineered.

Non-blocking test improvement: retain a small regression that evaluates restored parameters across same-domain value changes and failure/recovery. The committed tests check restored-reference presence and the BVT changes domains; those assertions alone would not catch a cached stale value. The additional local probes passed, so this is durable coverage advice, not a demonstrated bug or merge blocker.

Validation

  • Reused completed exact-head CI run 36078614960: Ubuntu UT, coverage, ARM64 SCA, and both active multi-CN BVT jobs passed. No waiting for CI.
  • Independently ran the three committed TestPreparedJSONAggregate* tests plus TestReview29350RestoredValueReuse (8 combinations: both aggregates × SQL/protocol metadata × DECIMAL/JSON) and TestReview29350InvalidDomainRecovery, using provenance-verified native artifacts in an isolated worktree. Terminal result: PASS, planner package execution 0.072s.
  • Command: .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=180s -run 'TestReview29350|TestPreparedJSONAggregate' ./pkg/sql/plan.
  • Extra probes validate planner/expression contracts, not a new end-to-end server run. Public distributed SQL evidence comes from the exact-head CI above. No production edits were made; probe source and logs were retained outside the review checkout.

@XuPeng-SH
XuPeng-SH merged commit 7db5bee into matrixorigin:main Sep 25, 2026
16 of 19 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/issue-29325-json-agg branch September 25, 2026 02:10
XuPeng-SH added a commit that referenced this pull request Sep 25, 2026
## What type of PR is this?

- [x] BUG
- [x] Test and CI

## Which issue(s) this PR fixes:

Fixes #29375

## What this PR does / why we need it:

`TestCDCNoFullPublicTakeoverLifecycle` must compare each replacement
reader with the final durable checkpoint of the previous owner. Cross-CN
catalog reads can briefly lag, and a guarded watermark write can finish
after the previous owner's cancellation returns. Either race could make
a valid takeover fail the test's strict first `CollectChanges`
assertion.

For A→B and B→C, the test now confirms old callbacks and readers
stopped, drains previously admitted updater writes with `ForceFlush`,
reads the final owner generation and checkpoint through CN A (the
updater's internal executor), and holds the new reader before owner
claim until its CN sees that exact tuple. The original strict
first-collection, generation, delayed-cleanup, and sink-delivery
assertions remain. A deterministic updater test holds a guarded write
after its owner check and verifies a later `ForceFlush` cannot complete
until that write is durable.

The C admission hook no longer has an unreleasable extra wait. Failure
cleanup releases C and joins its explicitly started detector scan with a
bound. The scenario owns a fresh embedded fixture generation and resets
the process-local updater only after the cluster closes; C starts only
one new CN. Production behavior is unchanged except for the inert test
hook argument and exported test reset entry point.

A later deep review reproduced a second false failure: B can
legitimately advance its durable checkpoint while A's delayed cleanup is
held, so a post-cleanup equality comparison to an earlier B sample is
invalid. The test now waits for B to advance beyond that sample, then
requires the same owner generation and no regression below the advanced
checkpoint after A cleanup and at B→C handoff. B's and C's first
`CollectChanges` boundaries remain strict equality checks.

## Validation

- Root-cause evidence: [#29350
UT](https://github.com/matrixorigin/matrixone/actions/runs/36032543828/job/107744901680),
[#29345
UT](https://github.com/matrixorigin/matrixone/actions/runs/36034909185/job/107752531985).
- Base/merge-base `426cf10b000b`; prior head
`14264bccf5389f5737739b1e2d3b19ca7babca82`. Go 1.26.4, Linux/amd64, CPU
`libmo` built in task worktree with `make cgo`; all Go tests use
repository CGo wrapper and isolated `TMPDIR`.
- `TestCDCNoFullPublicTakeoverLifecycle -count=2`: PASS, 197.187s total,
both iterations passed in one process (121.31s, 75.14s).
- Focused takeover `-race -count=1`: PASS, 134.852s.
- `pkg/cdc -count=1`: PASS, 11.845s; `pkg/cdc -race -count=1`: PASS,
13.364s.
- Guarded-progress regression `-race -count=100`: PASS, 3.442s.
- `pkg/tests/sqlintegration -count=1`: PASS, 253.862s. Full package
`-race -count=1`: PASS, 234.887s.
- `pkg/frontend -count=1`: PASS, 97.941s.
- Incremental golangci-lint v2.6.2 with repository CGo flags: 0 issues
(includes govet); `gofmt` and `git diff --check`: clean.
Follow-up validation for current head `5a54ee63d7` on the same base (Go
1.26.4, Linux/amd64, fresh CPU `make cgo`):

- `TestCDCNoFullPublicTakeoverLifecycle -count=1`: PASS, 137.88s; it
observes B advance past the earlier durable sample before releasing A.
- Exact test `-race -count=1`: PASS, 121.12s test execution, 122.798s
package.
- Entire `pkg/tests/sqlintegration -race -count=1`: PASS, 313.845s.
- Incremental `go vet` on `pkg/cdc`, `pkg/frontend`,
`pkg/tests/sqlintegration`: PASS.
- Incremental golangci-lint v2.6.2 (Go 1.26.4), `--new-from-rev
426cf10`, same three packages: PASS, 0 issues; `gofmt` and `git
diff --check`: PASS.
- Independent whole-change and final evidence review: PASS, no
unresolved blocker.

The previously reported equality failure was reproduced on the prior
head with a controlled live-B-progress schedule, and the same schedule
passed with monotonic comparison. CI for the new head is pending.

## Risk and rollback

The added synchronization runs only in tests. Updater drains and
visibility waits have 30-second bounds, and failure cleanup releases all
admission barriers before fixture teardown. Revert the CDC test-fix
commits to restore the previous test behavior.

This branch was previously deployed

1 inactive deployment
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.

[Bug]: prepared JSON aggregates convert typed runtime values to JSON strings

2 participants