Skip to content

[Bug]: scalar, anti, and correlated subqueries prevent FULLTEXT MATCH index rewrite #29079

Closed

Description

@Ariznawlll

Description

An otherwise indexable MATCH() AGAINST() query is rejected when the same query block also contains a scalar subquery, a NOT EXISTS anti-subquery, or a correlated EXISTS subquery. The additional subquery does not need to contain MATCH, reference the fulltext table, or affect fulltext membership.

This affects both classic FULLTEXT and FULLTEXT2 indexes. The same scalar subqueries work when MATCH is removed.

Environment

  • Branch: main
  • Commit: 01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90
  • Deployment: local 2-CN / 1-TN MatrixOne
  • Date: 2026-09-18

Reproduction

CREATE DATABASE ft_scalar_subquery;
USE ft_scalar_subquery;
SET experimental_fulltext2_index = 1;

CREATE TABLE docs(id BIGINT PRIMARY KEY, body TEXT);
INSERT INTO docs VALUES
  (1, 'needle anchor'),
  (2, 'needle anchor'),
  (3, 'filler');

CREATE TABLE q(name VARCHAR(10) PRIMARY KEY, n INT);
INSERT INTO q VALUES ('lo', 0);

CREATE FULLTEXT2 INDEX ft ON docs(body);

-- Works and uses fulltext2_search.
SELECT id
FROM docs
WHERE MATCH(body) AGAINST('+needle +anchor' IN BOOLEAN MODE)
ORDER BY id;

-- Each independent scalar-subquery placement is rejected.
SELECT id
FROM docs
WHERE MATCH(body) AGAINST('+needle +anchor' IN BOOLEAN MODE)
  AND id > (SELECT n FROM q WHERE name = 'lo')
ORDER BY id;

SELECT id, (SELECT n FROM q WHERE name = 'lo') AS marker
FROM docs
WHERE MATCH(body) AGAINST('+needle +anchor' IN BOOLEAN MODE)
ORDER BY id;

SELECT id
FROM docs
WHERE MATCH(body) AGAINST('+needle +anchor' IN BOOLEAN MODE)
ORDER BY (SELECT n FROM q WHERE name = 'lo'), id;

SELECT id
FROM docs
WHERE MATCH(body) AGAINST('+needle +anchor' IN BOOLEAN MODE)
  AND NOT EXISTS (SELECT 1 FROM q WHERE name = 'missing')
ORDER BY id;

SELECT d.id
FROM docs d
WHERE MATCH(d.body) AGAINST('+needle +anchor' IN BOOLEAN MODE)
  AND EXISTS (SELECT 1 FROM q WHERE q.n < d.id)
ORDER BY d.id;

Actual behavior

All scalar, anti, and correlated forms above return:

ERROR 20105 (HY000): not supported: MATCH() AGAINST() function cannot be replaced by FULLTEXT INDEX and full table scan with fulltext search is not supported yet.

The same result occurs on both CNs and with a classic FULLTEXT(body) index.

The failure is independent of vector search. A vector-valued scalar subquery in ORDER BY l2_distance(...) exposes it as well, but a plain table without MATCH accepts that expression and returns the expected rows.

Expected behavior

The independent scalar subquery should not prevent the indexed MATCH predicate from being rewritten. The queries should return the same fulltext matches as the direct control, with the scalar value applied in its normal projection, predicate, or ordering position.

Reduced matrix

Shape FULLTEXT2 classic FULLTEXT
Direct indexed MATCH returns rows returns rows
Scalar subquery in WHERE 20105 20105
Scalar subquery in projection 20105 20105
Scalar subquery in ORDER BY 20105 20105
Independent NOT EXISTS 20105 not required to reproduce
Correlated EXISTS 20105 not required to reproduce
Independent EXISTS returns rows not required to reproduce
Independent IN returns rows not required to reproduce
Same scalar subquery without MATCH returns rows returns rows

Each case was repeated through both CN endpoints with identical behavior.

Code analysis

resolveFullTextIndexPath in pkg/sql/plan/apply_indices.go only recognizes a direct scan/sort path or a single-input aggregate ancestor chain. These subqueries introduce SINGLE/ANTI or correlated join/apply shapes, so this path is not selected. The join fallback in applyFullTextFiltersForJoinChildren only rewrites selected join types and eligible children that are themselves direct TABLE_SCAN nodes. The fulltext predicate under the rejected shapes remains unreplaced and reaches the intentional 20105 fallback in pkg/sql/plan/function/func_fulltext.go. The independent EXISTS and IN controls show that the working SEMI shape already has a rewrite path.

Related but not duplicate: #27962 fixed MATCH located inside a scalar aggregate subquery. This issue has a top-level indexed MATCH; an unrelated scalar subquery elsewhere in that query block disables the rewrite. #27962 is closed and its original reproducer still works on this commit.

Regression coverage

Add planner and distributed SQL coverage for classic FULLTEXT and FULLTEXT2 with scalar subqueries in projection, WHERE, and ORDER BY; add FULLTEXT2 coverage for independent/correlated EXISTS, NOT EXISTS, and IN. Keep the direct indexed and subquery-without-MATCH controls.

Activity

  1. added this to the MO-202609 milestone on Sep 18, 2026
  2. added
    kind/bug
    on Sep 18, 2026
  3. added theissue type on Sep 18, 2026
  4. changed the title [-][Bug]: independent scalar subqueries prevent FULLTEXT MATCH index rewrite[/-] [+][Bug]: scalar, anti, and correlated subqueries prevent FULLTEXT MATCH index rewrite[/+] on Sep 18, 2026
  5. Ariznawlll commented on Sep 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:5be6cd90b501da225be591ffffe6000bf01f515e
    • 部署方式:本地单机(1 CN / 1 TN / 1 LogService)
    • 测试日期:2026-09-27

    原问题验证

    按 issue 原始 SQL 在最新 main 上独立执行 3 次,结果 3/3 符合修复预期;确认修复提交 271c3f1599951980488c18d4b9cc343b638e05a1 已包含在当前 main。

    Happy Path 覆盖

    1. FULLTEXT2 与 classic FULLTEXT 的 MATCH 查询在 WHERE、SELECT、ORDER BY 中带 scalar subquery 时均返回 id 1、2。

    Unhappy Path 覆盖

    1. 两种索引均覆盖 NOT EXISTS 与 correlated EXISTS,未再落入 20105 未重写路径。

    白盒验证

    核对修复提交 271c3f1599951980488c18d4b9cc343b638e05a1、对应 BVT/UT 以及当前 main 的祖先关系;本次同时完成公开 SQL 协议验收。

    回归测试

    已加入 MOTR PR #204 的 issue_29079_29276_fulltext_query_contracts.test,MOTR 提交 c7aeb11c;本地基线比对连续 3 次通过。

    测试证据

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions