Skip to content

fix(plan): push the covered fulltext2 score LIMIT without the implicit sort - #30274

Closed
cpegeric wants to merge 1 commit into
matrixorigin:mainfrom
cpegeric:fix_ft2_score_limit
Closed

cpegeric wants to merge 1 commit into
matrixorigin:mainfrom
cpegeric:fix_ft2_score_limit

Conversation

@cpegeric

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 #

Main regression from two PRs that merged close together and conflict: #30193 (77ccd7c438, bound covered FULLTEXT2 score searches) and #30189 (461bc8e349, omit overwritten implicit fulltext ordering).

What this PR does / why we need it:

1. pkg/sql/plan tests do not compile on main.

pkg/sql/plan/apply_indices_fulltext_snapshot_test.go:182:67: not enough arguments in call to builder.tryApplyCoveredFulltext2
	have (..., *plan.Expr, nil)
	want (..., *plan.Expr, *plan.Expr, bool)

#30189 added the trailing orderOverwritten bool parameter; #30193 added a call in TestCoveredFulltext2ScoreLimitOnBoundSQL without it. The call now passes false, like the other calls in the file.

2. The covered FULLTEXT2 score LIMIT is no longer pushed into the search.

With the call compiling, TestCoveredFulltext2ScoreLimitOnBoundSQL/score_alias and /score_alias_offset fail on main (Expected value not to be nil). Query shape, a FULLTEXT2 index with INCLUDE columns:

SELECT id, MATCH(title, body) AGAINST('hello' IN BOOLEAN MODE) AS sc
FROM ft WHERE MATCH(title, body) AGAINST('hello' IN BOOLEAN MODE) AND base_id IS NULL
ORDER BY sc DESC LIMIT 2;

Cause:

Fix: the implicit score sort is optional in pushCoveredFulltext2ScoreLimitFromSort. It only repeats the outer score-DESC order. When it is present, its key is still checked; the project-list checks are unchanged.

Tests

  • TestCoveredFulltext2ScoreLimitOnBoundSQL (all 9 cases) passes; the full pkg/sql/plan package passes.
  • The fulltext BVT has not been run on this branch.

🤖 Generated with Claude Code

…t sort

matrixorigin#30193 pushes an explicit ORDER BY score DESC LIMIT into the covered
fulltext2 search only through SORT -> PROJECT -> implicit score SORT ->
search. matrixorigin#30189 then omits that implicit sort when the explicit ORDER BY
overwrites it, so the push declined and the search ran unbounded.
The implicit sort is now optional in pushCoveredFulltext2ScoreLimitFromSort.

The test call added by matrixorigin#30193 also misses the orderOverwritten argument
added by matrixorigin#30189, so pkg/sql/plan tests did not compile on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mergify mergify Bot added kind/bug Something isn't working kind/test-ci labels Oct 15, 2026
XuPeng-SH pushed a commit that referenced this pull request Oct 15, 2026
## What type of PR is this?

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

## Which issue(s) this PR fixes:

issue #30164

Integration follow-up to #30189 and #30193, based on main `8795f3699b`
after the emergency revert #30285. References the optional-inner-sort
repair in #30274; that reference PR is unchanged.

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

Restore removal of implicit fulltext relevance sorts when an explicit
order replaces them, while preserving safe covered FULLTEXT2 score
candidate limits. The revert restored compilation and Top-K behavior but
also restored the redundant sort reported in #30164.

The score-LIMIT consumer now accepts both `SORT -> PROJECT ->
INDEX_SEARCH_SCAN` and the equivalent shape with an intervening implicit
score sort. A present inner sort must still have the same single
score-DESC key. Direct-column projection, exact-search, filter,
pagination, rank, secondary-key, ASC and overflow barriers remain
enforced. The outer sort retains SQL pagination; the search receives
LIMIT+OFFSET (2 for LIMIT 2, 3 with OFFSET 1). Search-level rank is also
explicitly rejected. Python UDF handlers are conservatively treated as
order-sensitive: the wrapper registry flags cannot establish handler
purity. This applies recursively inside scalar wrappers and preserves
the implicit relevance order before their projections.

Update all covered-rewrite test callers together with the ordering
change. Public parse/bind/optimize regressions cover default implicit
relevance order, explicit MATCH and score aliases, nested pages,
LIMIT/OFFSET and unsafe controls. Typed tests independently cover the
optional sort's equivalence and refusal barriers. Restore #30189's
ordering regressions and extend the canonical candidate-limit BVT with
distinct-score pages and combined candidate-bound/single-sort
assertions.

Validation on `3d263e5c79bef731a1667c8c294708818d932cf6` (local run
content matches this commit):

- Reproduced raw `461bc8e349` planner UT compilation failure at the
missing boolean argument. On an amended baseline containing only that
mechanical argument fix, `score_alias` and `score_alias_offset` fail
their candidate-limit assertions.
- A new public planning regression fails in all 6 direct/wrapped Python
UDF scenarios on the previous PR head `25a66777d2`, and passes with the
local purity-proof repair.
- PASS: fresh native `make -j8 cgo`; focused regressions; complete
`pkg/sql/plan` tests (`-count=1`); owning build and vet; incremental
golangci-lint.
- PASS (reused unchanged consumer evidence): focused FULLTEXT2
search/Top-K/candidate/INCLUDE tests, complete
`pkg/fulltext2/plugin/search`, and complete `pkg/sql/colexec/vectorscan`
tests.
- PASS: default SIMD `mo-service` build and real SQL on a private
macOS/arm64 instance, Go 1.27.1. Toolchain PATH was pinned after the
system launcher's automatic switching failed.
- PASS: normal mo-tester `-n -g -o` comparison of
`fulltext2/fulltext2_candidate_limit.sql`,
`fulltext2/fulltext2_covered.sql`, and
`fulltext/fulltext_overwritten_order.sql`, twice on the same instance:
**180/180 statements per round, zero ignored or abnormal**. Fixtures
were staged byte-for-byte outside `.codex` to avoid mo-tester's
dotted-path rewriting.
- PASS: independent test-database residue checks after each round;
graceful instance shutdown, closed test ports and removal of its data
directory; Python UDF catalog entries are also removed.
- Python handler execution: NOT_RUN. The new SQL regression uses real
CREATE FUNCTION/EXPLAIN without a Python service to assert the ordering
barrier; it does not claim external-handler runtime validation.
- Pending: exact-head Linux CI and formal QA/performance acceptance.
These functional checks do not claim a throughput improvement.

QA required: yes — the full issue's performance acceptance and
exact-head CI remain separate from local functional validation. Ready
for review; #30164 remains open for QA/performance acceptance.
Exact-head CI is still in progress. No scoring, defaults, index format,
cache, remote benchmark or issue-lifecycle changes.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@cpegeric cpegeric closed this Oct 15, 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 kind/test-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant