Skip to content

fix(sql): reimplement hash MARK join for IN subqueries - #25822

Merged
XuPeng-SH merged 7 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/systematic-in-optimization
Jul 17, 2026
Merged

XuPeng-SH merged 7 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/systematic-in-optimization

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Jul 16, 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:

Fixes #25821

Related: #12212

What this PR does / why we need it:

Reimplements the safe subset of hash MARK joins for IN / NOT IN subqueries and removes linear-depth expression construction for large IN and row-comparison expansions.

Correctness

  • Uses hash MARK only for a single equality key, or for composite equality keys whose arguments are statically NOT NULL.
  • Preserves SQL three-valued semantics: match → TRUE; no match with a build NULL or a NULL probe → UNKNOWN; empty build → FALSE, including for a NULL probe.
  • Retains LoopJoin for non-equality predicates, same-side equality, mixed-side correlated expressions, and nullable composite keys, where hashing cannot preserve the full row-aware predicate.
  • Carries the build-side NULL-key fact through HashBuild, JoinMap, and distributed pipeline serialization.
  • Ensures MARK joins emit every probe row even when the build side is empty, and clears null-key state on operator reuse.
  • Rejects any residual condition on a hash MARK operator during Prepare, preventing unsupported plans from silently dropping predicates.

Scalability

  • Replaces left-deep AND/OR construction for tuple/mixed-type IN and tuple subquery comparisons with order-preserving balanced trees.
  • The number of predicate nodes remains linear, while expression depth becomes O(log N), avoiding recursive stack amplification for large valid inputs.
  • This PR does not claim a percentage benchmark. The hash MARK path removes nested-loop O(P × B) work for the safe MARK-join subset; ordinary filter-only IN already uses SEMI join planning.

Validation

  • Hash MARK truth table: matching value with build NULL, non-match with/without build NULL, all-NULL build, NULL probe, and empty build.
  • Batch boundary and operator Reset/reuse state; tests assert zero remaining mpool bytes.
  • Planner selection/fallback matrix, including a hashable IN key plus mixed-side correlated equality and ordinary residual predicates, and distributed pipeline TrackNullKeys round trip.
  • Tuple/mixed-type IN and row-comparison trees at up to TableColumnCountLimit (4096); depth is asserted at most 14.
  • Race test for hash MARK join.

Commands run after the final semantic edit:

.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=300s \
  ./pkg/pb/pipeline ./pkg/vm/message ./pkg/sql/colexec/hashbuild \
  ./pkg/sql/colexec/hashjoin ./pkg/sql/plan ./pkg/sql/compile

.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=300s \
  ./pkg/sql/colexec/hashjoin -run 'TestHashMarkJoin'

Both commands passed. go build and go vet for the modified plan/compile packages also passed, and git diff --check is clean.

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner July 16, 2026 17:23
@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 added kind/bug Something isn't working kind/enhancement kind/test-ci labels Jul 16, 2026
@XuPeng-SH
XuPeng-SH force-pushed the codex/systematic-in-optimization branch from 27ee09d to cbbd4ab Compare July 16, 2026 17:24
@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Jul 16, 2026
@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Review: request changes

I still see one correctness blocker before this can be approved.

canUseHashMarkJoin admits MARK joins by checking exprRelPos on each equality term. That accepts predicates where one equality argument mixes probe/build columns, for example t2.z + t1.c = t1.d. constructHashJoin later rejects the same predicate as a hash key via HasColExpr and moves it into NonEqCond, but the new probeMark fast path in pkg/sql/colexec/hashjoin/join.go never evaluates NonEqCond once MARK uses the hash path.

That means a query like ... where t1.a in (select t2.x from t2 where t2.y = t1.b and t2.z + t1.c = t1.d) can now return TRUE based only on t2.x = t1.a, silently ignoring the residual correlated equality.

Please derive hash-MARK eligibility from the same criterion used by extraJoinConditions/HasColExpr (or explicitly require NonEqCond == nil) and add a regression test for a hashable IN key plus a mixed-side correlated equality.

@XuPeng-SH
XuPeng-SH force-pushed the codex/systematic-in-optimization branch from 47f4741 to 1f2c74c Compare July 16, 2026 23:45
@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Fixed in 1f2c74c.

The reported correctness case now falls back to LoopJoin:

  • canUseHashMarkJoin first requires the same condition split used by constructHashJoin to produce no residual condition.
  • It additionally walks every equality operand and requires all referenced columns to belong to exactly one relation. This extra check is necessary because the current recursive HasColExpr can return a representative relation for nested mixed-side expressions instead of rejecting them.
  • HashJoin.Prepare now fails fast if a MARK operator contains NonEqCond, so an unsupported operator cannot silently skip a residual predicate.

Regression coverage includes one hashable IN key plus t2.z + t1.c = t1.d, one hashable key plus an ordinary non-equality residual, and the runtime fail-fast invariant.

Post-rebase validation passed:

mo-cgo-test ./pkg/pb/pipeline ./pkg/vm/message ./pkg/sql/colexec/hashbuild ./pkg/sql/colexec/hashjoin ./pkg/sql/plan ./pkg/sql/compile
mo-cgo-test -race ./pkg/sql/colexec/hashjoin -run 'TestHashMarkJoin'
go build ./pkg/sql/colexec/hashjoin ./pkg/sql/compile
go vet ./pkg/sql/colexec/hashjoin ./pkg/sql/compile

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

Reviewed the current head (cd7bdfb9) across planner selection, MARK three-valued semantics, normal/spill execution, operator duplication, remote serialization, and Reset/reuse lifecycle.

No merge-blocking findings. The affected package suite and focused hash MARK race tests pass locally; required CI checks are green.

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed the latest head (ada8a80). The MARK join planner eligibility, three-valued semantics, spill/recursive-spill paths, state transfer, and reset handling are consistent. Targeted package tests, race test, build, and vet passed locally.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Self-review conclusion: APPROVE (code) — exact head ada8a8083895983820667679b1fd6439df0350da.

GitHub does not allow the PR author to submit a formal approval review, so I am recording the exact-head decision here. Independent approval for this head is already present.

I re-reviewed the complete diff from first principles across planner admission/fallback, SQL three-valued MARK semantics, normal build, forced spill and recursive re-spill, reset/reuse, duplicate and remote state transfer, FD ownership, mixed-version CN scheduling, and expression-depth/complexity bounds. I found no merge-blocking issue.

Local validation on this head passed:

  • affected packages: pipeline, message, hashbuild, hashjoin, plan, compile
  • focused TestHashMarkJoin* race run, 3 repetitions
  • planner/compile admission, fallback, duplication, and balanced-tree tests, 20 repetitions
  • go vet, make err-check, and git diff --check

The first local run exposed only a temporary-worktree CGo rpath issue; rerunning with deterministic absolute rpaths passed. Required CI jobs are still running and remain the delivery gate, separate from the code-review conclusion.

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 kind/enhancement kind/test-ci size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: IN/NOT IN MARK joins fall back to nested loops and large expressions are left-deep

4 participants