Repository navigation
fix(plan/fulltext): rewrite MATCH in scalar subqueries to the fulltext/fulltext2 index (#27962) - #28011
Conversation
…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 reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 12 minutes 22 seconds in the queue, with no time running CI. ReasonPull request #28011 has been merged manually at 0db2d5e HintYou were too fast! Tick the box to put this pull request back in the merge queue (same as
|
…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>
…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>
…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>
What type of PR is this?
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:
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):
WHERE matchwith 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: