Skip to content

fix(plan): rewrite MATCH beneath a window operator and OUTER JOIN to the fulltext index (#28974) - #29069

Merged
XuPeng-SH merged 14 commits into
matrixorigin:mainfrom
cpegeric:bug_28974
Sep 21, 2026
Merged

XuPeng-SH merged 14 commits into
matrixorigin:mainfrom
cpegeric:bug_28974

Conversation

@cpegeric

@cpegeric cpegeric commented Sep 17, 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 #28974 #20687

What this PR does / why we need it:

Both issues are the same class of defect: a MATCH() AGAINST() on an indexed column reaches execution as ERROR 20105 because applyIndices did not anchor the fulltext rewrite for that plan shape. The invariant is "no fulltext_match may survive applyIndices"; this PR extends the anchors to two more shapes.

#28974 — MATCH beneath a WINDOW operator

A top-level WHERE MATCH() AGAINST() is served by a FULLTEXT/FULLTEXT2 index, but adding a window function (e.g. ROW_NUMBER() OVER (...)) puts a WINDOW node between the query block and the base scan. applyIndices anchored the fulltext rewrite at PROJECT/SORT/AGG only, so the scan's fulltext_match survived beneath the WINDOW and threw 20105.

  • Added a Node_WINDOW fulltext anchor (mirroring the Node_AGG anchor from [Bug]: fulltext MATCH score remains unrewritten inside aggregate expressions #28681): it builds the index-scan join for the scan's MATCH filters and reparents the window's child onto it. Post-order recursion runs it before the PROJECT pass, which then finds no MATCH and no-ops.
  • OVER (PARTITION BY ...) inserts a Node_PARTITION between the WINDOW and the scan. resolveScanNodeUnderWindow descends that partition to reach the scan, and the rewrite reparents the scan's immediate parent onto the index join so the partition is not dropped.
  • The window anchor publishes its served scores into ftJoinServed, so a MATCH projected in the SELECT list, in an ancestor ORDER BY, or in a stacked outer window is rewritten to the score column instead of surviving as 20105.
  • exprCallsFunc / replaceScoreFnInExprBy now traverse Expr_W (window function, OVER partition/order-by), so a MATCH inside the window spec (OVER (ORDER BY match(...))) is rewritten too. Aggregate/projection exprs never contain an Expr_W, so existing callers are unaffected.
  • View DDL: a WHERE MATCH alongside a window function is now servable, so fulltext_view_ddl / fulltext2_view_ddl accept those views (a MATCH that appears only in an OVER spec with no WHERE seed stays refused, as before).

#20687 — MATCH on the preserved side of an outer JOIN

A LEFT JOIN B WHERE match(A.x) (and the RIGHT / scalar-subquery SINGLE equivalents) also failed with 20105: applyFullTextFiltersForJoinChildren only served the null-extending child of an outer join, never the row-preserved child.

A fulltext_match on a scan's FilterList is a pure filter on that input's own columns, so re-expressing it as the 1-row-per-pk INNER-join semi-join is row-equivalent to the WHERE that placed it there — on the preserved side too. A LEFT JOIN B WHERE match(A.x) becomes (A INNER JOIN A_ft) LEFT JOIN B, so a matcher with no join partner is kept null-extended, not dropped. Both children of LEFT/RIGHT/SINGLE are now eligible; ANTI/MARK/DEDUP and ASOF stay conservative (filtering those inputs is not a pure filter), and FULL OUTER does not exist in MO.

(This is not the index-less full-table-scan case originally described in #20687 — that is infeasible for CJK, which has no whitespace tokenizer without an index; this fix serves MATCH via the existing index on the preserved side of the join.)

How is this PR tested:

  • Unit (pkg/sql/plan): TestFullTextWindowMatchRewritten, TestFullTextWindowPartitionMatchRewritten, ...ProjectedMatchPropagated, ...StackedSpecMatchPropagated, TestFullTextWindowPartitionByMatchServed, TestResolveScanNodeUnderWindow, TestExprCallsFuncTraversesWindowSpec, TestReplaceScoreFnInExprByTraversesWindowSpec, and TestFullTextJoinRewriteServesOuterJoinPreservedChild (LEFT/RIGHT/SINGLE preserved child).
  • BVT (verified for both FULLTEXT and FULLTEXT2): fulltext_window_match / fulltext2_window_match (window + PARTITION BY + projected/ancestor/stacked MATCH), fulltext_view_ddl / fulltext2_view_ddl (view acceptance), and fulltext_join / fulltext2_join (INNER/LEFT/RIGHT/SINGLE + a null-extension counterexample where a partner-less matcher is kept as (id, NULL), with EXPLAIN pinning the index scan nested under the outer join).

🤖 Generated with Claude Code

…dex (matrixorigin#28974)

A top-level WHERE MATCH() AGAINST() predicate is served by a FULLTEXT/FULLTEXT2
index, but adding a window function (e.g. ROW_NUMBER() OVER (...)) put a WINDOW
node between the query block and the base scan. applyIndices anchors the fulltext
rewrite at PROJECT/SORT/AGG only, so the scan's fulltext_match survived beneath the
WINDOW and execution failed with ERROR 20105.

Add a Node_WINDOW fulltext anchor (mirroring the Node_AGG anchor from matrixorigin#28681):
applyIndicesForWindowUsingFullTextIndex builds the index-scan join for the scan's
MATCH filters and reparents the window's child onto it. Post-order recursion runs
this before the PROJECT pass, which then finds no MATCH and no-ops.

Also make replaceScoreFnInExprBy traverse Expr_W (window function, OVER
partition/order-by), so a MATCH that also appears inside the window spec -- e.g.
ROW_NUMBER() OVER (ORDER BY match(...)) with the same MATCH in WHERE -- is rewritten
to the score column instead of surviving as 20105. Aggregate/projection exprs never
contain an Expr_W, so existing callers are unaffected.

Verified live for both FULLTEXT2 and classic FULLTEXT: the window+MATCH query now
returns the ranked rows. Unit test TestFullTextWindowMatchRewritten and BVT
fulltext2_window_match (control, primary, EXPLAIN fulltext2_search, and the
combined window-orderby case) cover it.

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 →

…8974)

Classic-FULLTEXT sibling of fulltext2_window_match: the same window+MATCH rewrite
fix applies to both algorithms (one shared applyIndices path). Covers the control
MATCH, the primary ROW_NUMBER() OVER (...) + WHERE MATCH case, an EXPLAIN assertion
that the fulltext_index_scan TVF is reached beneath the WINDOW, and the combined
case where the same MATCH also appears in the window's OVER order-by.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cpegeric cpegeric changed the title fix(plan): rewrite MATCH beneath a window operator to the fulltext index (#28974) fix(plan): serve fulltext MATCH beneath a window operator and on the preserved side of an outer join (#28974 #20687) Sep 17, 2026
@cpegeric

Copy link
Copy Markdown
Contributor Author

fixed outer join

@cpegeric cpegeric changed the title fix(plan): serve fulltext MATCH beneath a window operator and on the preserved side of an outer join (#28974 #20687) fix(plan): rewrite MATCH beneath a window operator and OUTER JOIN to the fulltext index (#28974) Sep 17, 2026

@aunjgr aunjgr 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 c1e8e53. The full-text rewrite preserves the outer-join side, safely handles the admitted WINDOW/PARTITION/PROJECT chains, performs binding-aware score replacement, and has meaningful result and plan coverage for LEFT, RIGHT, SINGLE, null-extension, partitioned, and stacked-plan cases. I found no blocking correctness issue.

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

Deep review(head c1e8e53)

结论:REQUEST_CHANGES。上次旧 head 的两项 P2 已修复,但发现 1 个新的 P2;未发现 P0/P1。

  • [P2] WINDOW.FilterList 中已被索引服务的 MATCH 没有继续改写。见 apply_indices_fulltext.go:358-367。rewriteWindowMatchesFromServed 只处理 WinSpecList 和紧邻 PARTITION 的 OrderBy,遗漏 windowNode.FilterList。
  • 外层谓词经过 PROJECT 后,score 引用会被展开为原始 MATCH;含 window binding 的条件进入 WINDOW.FilterList 并在窗口后求值,但该独立副本没有被替换。WINDOW 编译时仍会执行 compileRestrict,残留 MATCH 可能返回 20105 unsupported。
  • 典型反例:窗口中计算 MATCH score,外层同时以 rn 和 score 过滤;WHERE MATCH 已被服务,但窗口后的 FilterList 仍保留原始 MATCH。

建议使用同一 binding-aware rewriter 处理窗口后谓词,保持窗口求值位置,并补 typed-plan UT 以及 FULLTEXT/FULLTEXT2 两种索引的结果 BVT。

旧问题复核:PARTITION 下探、OUTER JOIN null-preservation、祖先排序和叠加窗口 score 传播已修复;索引 scope、NULL/空匹配、资源释放未发现新的确定性问题。

当前 head 的 Build、Linux UT、SCA、Coverage、两组 BVT 和 CI Required 均通过,但相关 BVT 没覆盖上述窗口后 FilterList 反例。CI:https://github.com/matrixorigin/matrixone/actions/runs/35242693427

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

Deep re-review at exact head c1e8e53. The previous PARTITION traversal and served-score propagation findings are fixed, and the new outer-join rewrite preserves the nullable-side semantics in the covered LEFT/RIGHT/SINGLE cases. However, one execution-blocking window path remains: a served MATCH copied into WINDOW.FilterList is not rewritten, so compileRestrict can still evaluate raw fulltext_match and return 20105. I reproduced this with a temporary typed-plan regression: after applyIndices, one reachable fulltext_match remained in WINDOW.FilterList while the scan copy had been consumed. The targeted existing window/outer-join tests pass, but they do not cover this post-window predicate shape.

Comment thread pkg/sql/plan/apply_indices_fulltext.go Resolved
cpegeric and others added 2 commits September 18, 2026 10:21
…igin#28974)

rewriteWindowMatchesFromServed rewrote a served fulltext_match in the WINDOW's
WinSpecList and in the adjacent PARTITION order-by, but skipped the window node's
own FilterList. A post-window predicate that also references a window column
(e.g. `rn = 1 OR score > 0`) cannot be pushed below the WINDOW, so it stays on
WINDOW.FilterList and is evaluated after the window (Node_WINDOW runs
compileRestrict after compileWin). Predicate pushdown expands the projected
`score` alias back to the raw MATCH there, so that post-window copy kept a bare
fulltext_match even though the scan's WHERE MATCH was served and removed --
reaching execution as ERROR 20105 for both FULLTEXT and FULLTEXT2.

Apply the same binding-aware rewriter (servedFullTextScoreSameTable) to
windowNode.FilterList, so the post-window copy resolves to the served score
column at the window's evaluation position; a MATCH no served scan answers is
left intact and still raises 20105.

Tests: TestFullTextWindowFilterListMatchRewritten (typed plan; verified to fail
without the fix). BVT fulltext_window_match / fulltext2_window_match add the
subquery-window + post-window `rn = 1 OR score > 0` counterexample, asserting
the rows return and the index scan is used.

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

Copy link
Copy Markdown
Contributor Author

fixed

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

P2 blocker at apply_indices_fulltext.go:366-370: the fix only rewrites MATCH expressions in WINDOW.FilterList. A reachable outer FILTER above WINDOW can still retain a served fulltext_match when it does not reference window columns and cannot be pushed by partition keys. I reproduced this with a real query using score > 0: the optimized plan has one fulltext index scan but still retains one reachable fulltext_match, which enters the unsupported execution path. Please add binding-aware replacement for this independent FILTER while preserving post-window evaluation, and add regression coverage for both fulltext index forms. The current head and required CI were verified at d6fb993.

…ve WINDOW (matrixorigin#28974)

The WINDOW fulltext anchor rewrote served fulltext_match in the window's own
spec/FilterList/PARTITION, but not in a distinct Node_FILTER above the window. An
outer predicate that references neither a window column nor a partition key (e.g.
`... where score > 0` inlined by predicate pushdown to `fulltext_match(...) > 0`)
stays in that independent FILTER and was never rewritten, so the raw fulltext_match
reached execution as error 20105 even though the index scan existed.

Extract rewriteServedMatchesInFilterList (binding-tag-aware; leaves an unserved
MATCH intact so it still raises 20105) and add a Node_FILTER case in applyIndices
that applies it. Post-order recursion guarantees the anchor below has published
its score (ftJoinServed) first; the filter keeps its position, so evaluation stays
post-window. The WINDOW.FilterList loop now shares the same helper.

Adds TestFullTextOuterFilterAboveWindowMatchRewritten (proven to fail without the
fix) and BVT coverage for both fulltext index forms.

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

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

Re-reviewed the full diff and the changes since my prior review at c1e8f7a. The two previously blocking paths are now closed: WINDOW.FilterList MATCH expressions are rewritten by the window anchor, and an independent FILTER above WINDOW is rewritten after child recursion. I also verified the PARTITION/stacked-window/projected-score paths and the preserved side of LEFT/RIGHT/SINGLE joins against the implementation and regression coverage. Focused planner tests for all 11 added cases pass at exact head c9a2b1c; git diff --check also passes. The failing coverage job is from unrelated TestIssue26404MixedDataBranchLifecycleDoesNotDeadlock/pick, while the regular Linux UT and relevant BVT jobs passed. No blocking issue found.

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

Deep review (exact head 8e98b85).\n\nAPPROVE.\n\nReviewed the complete planner, unit-test, and FULLTEXT/FULLTEXT2 BVT diff against base 510e28e. The WINDOW/PARTITION anchor preserves the plan shape while reparenting the scan below the index join, publishes served scores to projections, ancestor/stacked windows, WINDOW.FilterList and independent outer FILTERs, and leaves unsupported MATCH expressions unresolved. The preserved-side LEFT/RIGHT/SINGLE rewrites remain row-preserving and retain null-extended unmatched rows.\n\nThe prior PARTITION, served-score propagation, WINDOW.FilterList and outer-FILTER blockers are closed at this head. Exact-head focused planner tests, FULLTEXT/FULLTEXT2 window/view/join BVTs, CI and diff checks are green. No concrete blocker remains.

@mergify

mergify Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-21 21:03 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-21 21:04 UTC · at 0aae985c24a69eda955c22a4169bb624cabdfeeb

This pull request spent 59 seconds in the queue, with no time running CI.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Reason

Pull request #29069 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

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

  • Requeue this pull request

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants