Skip to content

fix(plan/fulltext): rewrite MATCH in scalar subqueries to the fulltext/fulltext2 index (#27962) - #28011

Merged
XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
cpegeric:bug_27962
Sep 2, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
cpegeric:bug_27962

Conversation

@cpegeric

@cpegeric cpegeric commented Sep 2, 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:

issue #27962

What this PR does / why we need it:

A MATCH() AGAINST() inside a scalar subquery -- e.g. select (select count(*) from docs where match(body) against('ready')) -- failed with error 20105 ("MATCH() AGAINST() cannot be replaced by FULLTEXT INDEX..."), even though the same predicate works at top level, in IN subqueries, and in derived tables.

Root cause: the fulltext MATCH->index rewrite was reachable from only two anchors -- a PROJECT (resolveFullTextIndexPath) and INNER/SEMI join children (applyFullTextFiltersForJoinChildren). A scalar subquery decorrelates into shapes neither anchor covers:

  • uncorrelated: JOIN(LEFT) -> AGG -> SCAN(match). The AGG sits under a 2-input JOIN, so the project-anchored single-input walk stops at the JOIN and never reaches the AGG.
  • correlated: AGG -> JOIN(LEFT) -> SCAN(match). The match scan is a LEFT-join
    child, which applyFullTextFiltersForJoinChildren skipped (INNER/SEMI only).
    The un-rewritten MATCH then reached execution and threw 20105.

Fix (two complementary anchors in the shared, algo-agnostic apply path):

  • apply_indices.go: add a standalone Node_AGG anchor. When applyIndices visits an AGG whose (single-input) descendant is a fulltext-indexed scan carrying MATCH filters, run the existing agg rewrite anchored on the AGG itself. When the AGG does sit under a PROJECT, this fires first during child recursion and consumes the scan's MATCH filters, so the later PROJECT pass no-ops (no double rewrite).
  • apply_indices_fulltext.go: accept LEFT/SINGLE joins (the scalar-subquery decorrelation types) in applyFullTextFiltersForJoinChildren. The per-child rewrite replaces WHERE match with a 1:1 INNER join to the fulltext-index result on the pk, which is row-equivalent to the filter it replaces and so does not change the enclosing join's row-preservation/NULL-extension.

Works for every aggregate (COUNT/MAX/MIN/SUM/AVG), wrapped MATCH (match>0), HAVING, and composes with fulltext2 INCLUDE columns (in-index prefilter and aggregating an include column) in both correlated and uncorrelated subqueries. No per-algo switch or SQL-layer changes; classic fulltext and fulltext2 both benefit. Top-level / IN / derived paths are unchanged.

Tests:

  • Unit: apply_indices_fulltext_subquery_test.go builds both flattened shapes and asserts the MATCH is served (countReachableFullTextMatches == 0, one fulltext scan). Each fails without its corresponding anchor.
  • BVT: test/distributed/cases/fulltext2/fulltext2_scalar_subquery.{sql,result} -- the [Bug]: Fulltext2 MATCH in scalar subquery is not rewritten and returns 20105 #27962 repro plus all agg funcs, wrapped, HAVING, INCLUDE prefilter/agg, and a correlated subquery; baselines (top-level/IN/derived) retained. Validated -m run -g -n: 100%.

…t index (matrixorigin#27962)

A MATCH() AGAINST() inside a scalar subquery -- e.g.
`select (select count(*) from docs where match(body) against('ready'))` -- failed
with error 20105 ("MATCH() AGAINST() cannot be replaced by FULLTEXT INDEX..."),
even though the same predicate works at top level, in IN subqueries, and in
derived tables.

Root cause: the fulltext MATCH->index rewrite was reachable from only two anchors
-- a PROJECT (resolveFullTextIndexPath) and INNER/SEMI join children
(applyFullTextFiltersForJoinChildren). A scalar subquery decorrelates into shapes
neither anchor covers:
- uncorrelated: JOIN(LEFT) -> AGG -> SCAN(match). The AGG sits under a 2-input
  JOIN, so the project-anchored single-input walk stops at the JOIN and never
  reaches the AGG.
- correlated:   AGG -> JOIN(LEFT) -> SCAN(match). The match scan is the right
  (non-preserved) child of a LEFT join, which applyFullTextFiltersForJoinChildren
  skipped (INNER/SEMI only).
The un-rewritten MATCH then reached execution and threw 20105.

Fix (two complementary anchors in the shared, algo-agnostic apply path):
- apply_indices.go: add a standalone Node_AGG anchor. When applyIndices visits an
  AGG whose single-input descendant is a fulltext-indexed scan carrying MATCH
  filters, run the existing agg rewrite anchored on the AGG itself. When the AGG
  does sit under a PROJECT, this fires first during child recursion and consumes
  the scan's MATCH filters, so the later PROJECT pass no-ops (no double rewrite).
- apply_indices_fulltext.go: accept LEFT/SINGLE joins in
  applyFullTextFiltersForJoinChildren, but only for the RIGHT (non-preserved /
  null-producing) child -- where a scalar subquery's match lands. The per-child
  rewrite replaces `WHERE match` with a 1:1 INNER join to the fulltext index on
  the pk, which is row-equivalent to the filter it replaces, so it changes no
  row's presence or NULL-extension in the enclosing join. The row-preserving
  (outer) child stays untouched (TestFullTextJoinRewriteSkipsOuterJoins).

Works for every aggregate (COUNT/MAX/MIN/SUM/AVG), wrapped MATCH (`match>0`),
HAVING, correlated and uncorrelated subqueries, and -- for fulltext2 -- composes
with INCLUDE columns (in-index prefilter and aggregating an include column). No
per-algo switch or SQL-layer changes; both classic fulltext and fulltext2 benefit.
Top-level / IN / derived paths are unchanged.

Tests:
- Unit: apply_indices_fulltext_subquery_test.go builds both flattened shapes and
  asserts the MATCH is served (countReachableFullTextMatches == 0, one fulltext
  scan). Each fails without its corresponding anchor.
- BVT (fulltext2): cases/fulltext2/fulltext2_scalar_subquery.{sql,result} -- the
  repro plus all agg funcs, wrapped, HAVING, INCLUDE prefilter/agg, and a
  correlated subquery; baselines retained.
- BVT (classic fulltext): cases/fulltext/fulltext_scalar_subquery.{sql,result} --
  the same matrix minus INCLUDE (a fulltext2-only feature).
Both BVTs validated -m run -g -n: 100%.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@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 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.

Blocking counterexample at exact head b79466c70559d1f6f6b7ee4eb67426ec1e90efed:

applyIndices runs after determineBuildAndProbeSide + swapJoinChildren (query_builder.go). For a correlated scalar-subquery join, the optimizer may set IsRightJoin=true when the preserved outer input is smaller than the MATCH input.

  • LEFT is then physically swapped and converted to RIGHT; the non-preserved MATCH relation is child 0, but this switch rejects RIGHT entirely.
  • SINGLE is physically swapped without changing JoinType; the non-preserved MATCH relation is child 0, but this code only visits child 1 (the preserved outer relation).

So the fix remains statistics/plan-choice dependent: the same #27962 query can still leave fulltext_match in the MATCH scan and fail with 20105 after stats choose the right-join implementation. For right-SINGLE it can also inspect the wrong relation.

Please determine the eligible child from the post-swap preservation contract (for example, handle LEFT/RIGHT/SINGLE using nodeNullExtendsChild(joinNode, i), rather than hard-coding child 1), and add synthetic tests for both right-swapped SINGLE and LEFT→RIGHT shapes. Those tests need to set the exact post-swapJoinChildren shape/IsRightJoin so the regression cannot depend on test-table statistics.

The AGG anchor, empty-input aggregate semantics, wrapped MATCH, fulltext2 INCLUDE paths, and 1:1 PK/doc_id rewrite otherwise look sound. Exact-head Linux UT/SCA/coverage/BVT are green; current main merge-tree is clean. Local focused execution was blocked only by the isolated worktree's missing exact-build cgo/libmo.dylib, not by a code/test failure.

… fixed index (matrixorigin#27962)

applyIndices runs AFTER determineBuildAndProbeSide + swapJoinChildren, which -- based
on input-size statistics -- can physically swap join children and convert LEFT->RIGHT
(IsRightJoin) or right-swap a SINGLE. The prior scalar-subquery relaxation hardcoded
"only child 1, reject RIGHT", so a swapped plan left the fulltext_match unrewritten and
failed with 20105:
- LEFT swapped to RIGHT: non-preserved MATCH relation becomes child 0, and RIGHT was
  rejected outright.
- right-swapped SINGLE: non-preserved MATCH relation becomes child 0, but only child 1
  was inspected (the preserved outer relation).

So the fix was stats/plan-choice dependent: the same correlated scalar-subquery query
could still throw 20105 once stats chose the right-join implementation.

Fix: derive the eligible child from the POST-swap preservation contract via
nodeNullExtendsChild(joinNode, i) instead of a fixed index. INNER/SEMI keep both inputs
eligible; every outer join (LEFT/RIGHT/SINGLE/OUTER, either swap state) rewrites only the
null-extending (non-preserved) child. The row-preserving child is never null-extending,
so it stays untouched (TestFullTextJoinRewriteSkipsOuterJoins). The per-child rewrite is
unchanged (1:1 index join, row-equivalent to the WHERE match it replaces).

Tests: two synthetic tests set the EXACT post-swapJoinChildren shape (JoinType=RIGHT with
MATCH at child 0; JoinType=SINGLE + IsRightJoin with MATCH at child 0) so the regression
cannot depend on test-table statistics. Both fail with the old hardcoded logic and pass
with the contract-based eligibility.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

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

Re-reviewed exact head 6ebb539 after my previous request changes.

The prior statistics-dependent blocker is closed. Join-child eligibility now follows the post-swap null-extension contract: LEFT-to-RIGHT selects child 0, and right-swapped SINGLE selects child 0 via IsRightJoin. The preserved side remains untouched. The two new typed tests pin those exact post-swap shapes rather than depending on table statistics.

I also rechecked the full change map and counterexamples: the standalone AGG anchor consumes MATCH before a later PROJECT can double-rewrite it; the replacement is row-equivalent because both fulltext implementations emit one row per matching doc_id; wrapped MATCH, correlated and uncorrelated aggregates, classic fulltext, fulltext2 INCLUDE filters/aggregates, and existing top-level/IN/derived controls have public SQL coverage. No per-algorithm SQL-layer dispatch or plugin-boundary regression was introduced. The extra planner work is one bounded depth-1 scan check per AGG, with no query-execution hot-path cost.

Exact-head substantive CI is green, and the merge tree against current main is clean; intervening main changes do not overlap this rewrite contract. No remaining correctness, performance, or unhappy-path blocker found.

@mergify mergify Bot added the queued label Sep 2, 2026
@mergify

mergify Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-02 15:00 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-02 15:12 UTC · at b787ad32563aa34c0e2ebaf52fab4dd0b44911b0

This pull request spent 12 minutes 22 seconds in the queue, with no time running CI.

Reason

Pull request #28011 has been merged manually at 0db2d5e

Hint

You were too fast!

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit 0db2d5e into matrixorigin:main Sep 2, 2026
20 of 21 checks passed
@mergify mergify Bot removed the queued label Sep 2, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…t/fulltext2 index (matrixorigin#27962) (matrixorigin#28011)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27962

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



A MATCH() AGAINST() inside a scalar subquery -- e.g. `select (select
count(*) from docs where match(body) against('ready'))` -- failed with
error 20105 ("MATCH() AGAINST() cannot be replaced by FULLTEXT
INDEX..."), even though the same predicate works at top level, in IN
subqueries, and in derived tables.

Root cause: the fulltext MATCH->index rewrite was reachable from only
two anchors -- a PROJECT (resolveFullTextIndexPath) and INNER/SEMI join
children (applyFullTextFiltersForJoinChildren). A scalar subquery
decorrelates into shapes neither anchor covers:
- uncorrelated: JOIN(LEFT) -> AGG -> SCAN(match). The AGG sits under a
2-input JOIN, so the project-anchored single-input walk stops at the
JOIN and never reaches the AGG.
- correlated: AGG -> JOIN(LEFT) -> SCAN(match). The match scan is a
LEFT-join
child, which applyFullTextFiltersForJoinChildren skipped (INNER/SEMI
only).
The un-rewritten MATCH then reached execution and threw 20105.

Fix (two complementary anchors in the shared, algo-agnostic apply path):
- apply_indices.go: add a standalone Node_AGG anchor. When applyIndices
visits an AGG whose (single-input) descendant is a fulltext-indexed scan
carrying MATCH filters, run the existing agg rewrite anchored on the AGG
itself. When the AGG does sit under a PROJECT, this fires first during
child recursion and consumes the scan's MATCH filters, so the later
PROJECT pass no-ops (no double rewrite).
- apply_indices_fulltext.go: accept LEFT/SINGLE joins (the
scalar-subquery decorrelation types) in
applyFullTextFiltersForJoinChildren. The per-child rewrite replaces
`WHERE match` with a 1:1 INNER join to the fulltext-index result on the
pk, which is row-equivalent to the filter it replaces and so does not
change the enclosing join's row-preservation/NULL-extension.

Works for every aggregate (COUNT/MAX/MIN/SUM/AVG), wrapped MATCH
(`match>0`), HAVING, and composes with fulltext2 INCLUDE columns
(in-index prefilter and aggregating an include column) in both
correlated and uncorrelated subqueries. No per-algo switch or SQL-layer
changes; classic fulltext and fulltext2 both benefit. Top-level / IN /
derived paths are unchanged.

Tests:
- Unit: apply_indices_fulltext_subquery_test.go builds both flattened
shapes and asserts the MATCH is served (countReachableFullTextMatches ==
0, one fulltext scan). Each fails without its corresponding anchor.
- BVT:
test/distributed/cases/fulltext2/fulltext2_scalar_subquery.{sql,result}
-- the matrixorigin#27962 repro plus all agg funcs, wrapped, HAVING, INCLUDE
prefilter/agg, and a correlated subquery; baselines
(top-level/IN/derived) retained. Validated -m run -g -n: 100%.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…t/fulltext2 index (matrixorigin#27962) (matrixorigin#28011)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27962

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



A MATCH() AGAINST() inside a scalar subquery -- e.g. `select (select
count(*) from docs where match(body) against('ready'))` -- failed with
error 20105 ("MATCH() AGAINST() cannot be replaced by FULLTEXT
INDEX..."), even though the same predicate works at top level, in IN
subqueries, and in derived tables.

Root cause: the fulltext MATCH->index rewrite was reachable from only
two anchors -- a PROJECT (resolveFullTextIndexPath) and INNER/SEMI join
children (applyFullTextFiltersForJoinChildren). A scalar subquery
decorrelates into shapes neither anchor covers:
- uncorrelated: JOIN(LEFT) -> AGG -> SCAN(match). The AGG sits under a
2-input JOIN, so the project-anchored single-input walk stops at the
JOIN and never reaches the AGG.
- correlated: AGG -> JOIN(LEFT) -> SCAN(match). The match scan is a
LEFT-join
child, which applyFullTextFiltersForJoinChildren skipped (INNER/SEMI
only).
The un-rewritten MATCH then reached execution and threw 20105.

Fix (two complementary anchors in the shared, algo-agnostic apply path):
- apply_indices.go: add a standalone Node_AGG anchor. When applyIndices
visits an AGG whose (single-input) descendant is a fulltext-indexed scan
carrying MATCH filters, run the existing agg rewrite anchored on the AGG
itself. When the AGG does sit under a PROJECT, this fires first during
child recursion and consumes the scan's MATCH filters, so the later
PROJECT pass no-ops (no double rewrite).
- apply_indices_fulltext.go: accept LEFT/SINGLE joins (the
scalar-subquery decorrelation types) in
applyFullTextFiltersForJoinChildren. The per-child rewrite replaces
`WHERE match` with a 1:1 INNER join to the fulltext-index result on the
pk, which is row-equivalent to the filter it replaces and so does not
change the enclosing join's row-preservation/NULL-extension.

Works for every aggregate (COUNT/MAX/MIN/SUM/AVG), wrapped MATCH
(`match>0`), HAVING, and composes with fulltext2 INCLUDE columns
(in-index prefilter and aggregating an include column) in both
correlated and uncorrelated subqueries. No per-algo switch or SQL-layer
changes; classic fulltext and fulltext2 both benefit. Top-level / IN /
derived paths are unchanged.

Tests:
- Unit: apply_indices_fulltext_subquery_test.go builds both flattened
shapes and asserts the MATCH is served (countReachableFullTextMatches ==
0, one fulltext scan). Each fails without its corresponding anchor.
- BVT:
test/distributed/cases/fulltext2/fulltext2_scalar_subquery.{sql,result}
-- the matrixorigin#27962 repro plus all agg funcs, wrapped, HAVING, INCLUDE
prefilter/agg, and a correlated subquery; baselines
(top-level/IN/derived) retained. Validated -m run -g -n: 100%.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…t/fulltext2 index (matrixorigin#27962) (matrixorigin#28011)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27962

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



A MATCH() AGAINST() inside a scalar subquery -- e.g. `select (select
count(*) from docs where match(body) against('ready'))` -- failed with
error 20105 ("MATCH() AGAINST() cannot be replaced by FULLTEXT
INDEX..."), even though the same predicate works at top level, in IN
subqueries, and in derived tables.

Root cause: the fulltext MATCH->index rewrite was reachable from only
two anchors -- a PROJECT (resolveFullTextIndexPath) and INNER/SEMI join
children (applyFullTextFiltersForJoinChildren). A scalar subquery
decorrelates into shapes neither anchor covers:
- uncorrelated: JOIN(LEFT) -> AGG -> SCAN(match). The AGG sits under a
2-input JOIN, so the project-anchored single-input walk stops at the
JOIN and never reaches the AGG.
- correlated: AGG -> JOIN(LEFT) -> SCAN(match). The match scan is a
LEFT-join
child, which applyFullTextFiltersForJoinChildren skipped (INNER/SEMI
only).
The un-rewritten MATCH then reached execution and threw 20105.

Fix (two complementary anchors in the shared, algo-agnostic apply path):
- apply_indices.go: add a standalone Node_AGG anchor. When applyIndices
visits an AGG whose (single-input) descendant is a fulltext-indexed scan
carrying MATCH filters, run the existing agg rewrite anchored on the AGG
itself. When the AGG does sit under a PROJECT, this fires first during
child recursion and consumes the scan's MATCH filters, so the later
PROJECT pass no-ops (no double rewrite).
- apply_indices_fulltext.go: accept LEFT/SINGLE joins (the
scalar-subquery decorrelation types) in
applyFullTextFiltersForJoinChildren. The per-child rewrite replaces
`WHERE match` with a 1:1 INNER join to the fulltext-index result on the
pk, which is row-equivalent to the filter it replaces and so does not
change the enclosing join's row-preservation/NULL-extension.

Works for every aggregate (COUNT/MAX/MIN/SUM/AVG), wrapped MATCH
(`match>0`), HAVING, and composes with fulltext2 INCLUDE columns
(in-index prefilter and aggregating an include column) in both
correlated and uncorrelated subqueries. No per-algo switch or SQL-layer
changes; classic fulltext and fulltext2 both benefit. Top-level / IN /
derived paths are unchanged.

Tests:
- Unit: apply_indices_fulltext_subquery_test.go builds both flattened
shapes and asserts the MATCH is served (countReachableFullTextMatches ==
0, one fulltext scan). Each fails without its corresponding anchor.
- BVT:
test/distributed/cases/fulltext2/fulltext2_scalar_subquery.{sql,result}
-- the matrixorigin#27962 repro plus all agg funcs, wrapped, HAVING, INCLUDE
prefilter/agg, and a correlated subquery; baselines
(top-level/IN/derived) retained. Validated -m run -g -n: 100%.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

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.

3 participants