Skip to content

fix(python-sdk): preserve query execution semantics - #30961

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
MatrixAdventurer:codex/python-query-semantics-20261023
Oct 24, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
MatrixAdventurer:codex/python-query-semantics-20261023

Conversation

@MatrixAdventurer

@MatrixAdventurer MatrixAdventurer commented Oct 23, 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:

Closes #30960

What this PR does / why we need it:

Python SDK query terminal operations currently return wrong counts and corrupt reusable query state. For four rows with IDs 10/20/30/40, selecting ID then calling count() returns 10, while limit(2).offset(1).count() returns 0. Calling first() on a three-row page permanently shrinks subsequent all() calls to one row; a failed async projection build also leaves a database prefix behind.

  • Count the complete derived query instead of textually rewriting SELECT *, retaining projection, grouping, DISTINCT, aggregate-result cardinality, pagination and bound parameters.
  • Execute first() on a private shallow copy whose only change is LIMIT, preserving the caller's query after success or executor failure.
  • Restore the async builder's temporary database prefix in finally, so the caller can fix an invalid projection and reuse the same query.

The two implementations share the existing ORM owner helpers. Legacy Query and SQL-builder clone behavior are outside this change. Scope: 3 files, +155/-20, including one behavioral regression file; no dependency, server, workflow or gate changes.

Validation (Linux, Python 3.12.14):

  • Original main e19f698de0fc52a60f60f2ff689d6d16a5100334 plus the new regressions: 17 failed, 2 zero-limit controls passed. The old-source export is independent of the working tree.
  • SQLAlchemy 2.0.55: full PYTHONPATH=. python -m pytest tests/offline -o addopts='' -q --tb=short --junitxml=... passes 1329 tests and 95 subtests.
  • SQLAlchemy 1.4.54: the same full offline suite passes 1333 tests and 95 subtests.
  • Final focused regressions pass 19/19 under both versions. An explicit ORDER BY was added to the parameter/reuse oracle after the full run; the final focused checks validate that change, and the unchanged full-suite evidence is reused for the remaining cases.
  • Repository CI fatal lint (python -m flake8 matrixone --select=E9,F63,F7,F82 --show-source --statistics), regression-file lint, compileall and git diff --check pass.
  • Regression tests use the public Client/AsyncClient query API and execute the generated SELECT subset against SQLite; they do not merely compare SQL text. Fixtures close their owned connection on all outcomes.
  • Exact head 56b58c2f169bf3a08f72a81b8bbc56517e678e68 passed live public Client and AsyncClient checks against MatrixOne v4.2.6-hotfix-20261021 (reported backend 8.0.30-MatrixOne-v4.2.6-hotfix-20261021): 9 assertions per client covering projection, grouped, DISTINCT, aggregate, paged and parameterized counts plus first()/all() query-state preservation.
  • The official Linux x86_64 release archive used for the live check had SHA-256 fce5904efdbac1b126b052dbaca1a5fb23977ac216d48203d94f3ff4012652d9.
  • Exact-head remote Iceberg Connector E2E completed successfully.

Remaining limits: Full SQL BVT and server-specific derived-query edge cases outside the reported public-client scenarios were not run.

@mergify mergify Bot added the kind/bug Something isn't working label Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Validation update for exact head 56b58c2f169bf3a08f72a81b8bbc56517e678e68:

  • Live MatrixOne v4.2.6-hotfix-20261021 public-client proof passed.
  • Sync Client: 9/9 assertions passed.
  • Async AsyncClient: 9/9 assertions passed.
  • Covered projection, grouped, DISTINCT, aggregate, paged and parameterized query cardinality, plus first() followed by reusable all().
  • Focused regression rerun: 19 passed.
  • Independent review: APPROVE, no actionable findings.
  • Exact-head remote Iceberg Connector E2E: success.

The live server reported 8.0.30-MatrixOne-v4.2.6-hotfix-20261021. Official Linux x86_64 archive SHA-256: fce5904efdbac1b126b052dbaca1a5fb23977ac216d48203d94f3ff4012652d9.

The MatrixOne-specific compatibility gap that kept this PR in Draft is now closed; full SQL BVT remains outside this PR's targeted Python SDK scope.

@MatrixAdventurer
MatrixAdventurer marked this pull request as ready for review October 24, 2026 00:25
@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 →

@XuPeng-SH

XuPeng-SH commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 56b58c2f169bf3a08f72a81b8bbc56517e678e68

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

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: 56b58c2f169bf3a08f72a81b8bbc56517e678e68

Assessment: approve

Reviewed the complete three-file delta and affected callers. The shared ORM helpers correctly preserve query cardinality and reusable state without duplicating execution paths or adding database round trips. No actionable findings remain.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1586-7a01-9000-d45e20995cf8 · slot 1 · generation 1

Validation:

  • All 19 PR regressions and 11 additional checks passed on Python 3.13.5 / SQLAlchemy 2.0.55, covering CTE/HAVING parameters, joins, transaction routing, cursor results, empty inputs and cancellation.

  • Test receipt checks/1792801906852526647-3.json matches the frozen head, exit 0, query-tests.log and query-tests.xml.

  • Final evidence audit and git diff --check passed; receipt checks/1792802014340501210-4.json confirms the frozen head and clean source.

  • Reviewed the supplied successful CI results for six Python/SQLAlchemy combinations and the exact-head author report of nine live MatrixOne assertions per client. Audit details are retained in /home/xupeng/.local/state/mo-bot/jobs/f71ff722739d60532a8a104d/attempt-1/review-evidence.json.

Limitations:

  • Local behavioral checks use SQLite. The author's live MatrixOne validation was not independently rerun, and raw live-run receipts were not supplied.

  • No performance benchmark was run. The changes add a shallow query copy and SQL wrapping; complex count-query costs remain workload-dependent.

@mergify mergify Bot added the queued label Oct 24, 2026
@mergify

mergify Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-24 00:36 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-10-24 00:39 UTC · at 2b94111625d746d048468ccc214c6b27fc23bd0d · squash

This pull request spent 3 minutes 3 seconds in the queue, including 2 minutes 1 second running CI.

Required conditions to merge

@mergify
mergify Bot merged commit 2b94111 into matrixorigin:main Oct 24, 2026
28 of 29 checks passed
@mergify mergify Bot removed the queued label Oct 24, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Python ORM count/first and async failed builds break query semantics

2 participants