Skip to content

ddl, table, executor: build a FULLTEXT index in TiKV behind a switch - #73088

Draft
terry1purcell wants to merge 10 commits into
pingcap:masterfrom
terry1purcell:fts-pr3-index
Draft

terry1purcell wants to merge 10 commits into
pingcap:masterfrom
terry1purcell:fts-pr3-index

Conversation

@terry1purcell

@terry1purcell terry1purcell commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: ref #70491

Problem Summary:

Depends on #73085 (part 1). Until it merges, this PR also shows its commit; review only the top commit, ddl, table, executor: build a FULLTEXT index in TiKV behind a switch.

This is part 3 of 6 of splitting #71531 (FULLTEXT index in TiKV) into staged PRs:

  1. Parse and analyze search strings and documents (expression: add MATCH ... AGAINST boolean query parsing and text analysis #73085)
  2. Local MATCH ... AGAINST evaluation without an index (expression, planner: evaluate MATCH ... AGAINST IN BOOLEAN MODE in TiDB #73086)
  3. Index format, DDL, write path and ADMIN CHECK (this PR; creation behind a switch)
  4. Posting-list engine (expression/fulltext: evaluate a boolean query over posting lists #73087)
  5. TiKV posting reader, planner and IndexMerge access path (removes the switch) (planner, executor: answer MATCH ... AGAINST from a FULLTEXT index in TiKV #73110)
  6. Planning-time posting-count probe (planner: count FULLTEXT index postings when planning #73114)

What changed and how does it work?

On the classic kernel, a FULLTEXT index is stored in TiKV as a positional inverted index: one KV entry per distinct term per row, whose value holds the term's positions in the document. No query reads the index yet, so creating one requires the new variable tidb_enable_tikv_fulltext_index (default OFF, undocumented). Without it, CREATE TABLE, CREATE INDEX and ALTER TABLE ... ADD fail with the same error as today. Part 5 removes the switch.

  • Metadata: IndexInfo.TiKVFullText records the parser and analyzer settings (token sizes, stopwords, ngram size) when the index is created, with a format version. The session that runs the statement captures them into the ADD INDEX job, so the DDL owner builds with the same settings. WITH PARSER NGRAM selects the ngram parser.
  • Columns: the last index column is tokenized. Any columns before it are ordinary key columns encoded ahead of the term, so one tenant's entries form a contiguous range. Prefix lengths, DESC, GLOBAL, partial-index conditions, expression key parts, non-string or binary columns and local temporary tables are refused.
  • Write path: the index tokenizes the row and writes one entry per term. A NULL, empty or stopword-only document writes none. The mutation checker checks each entry's term and key columns against the row. An index whose format version this binary does not know fails its writes rather than its schema load.
  • ADMIN CHECK TABLE / INDEX checks a FULLTEXT index from the record side: every row's expected entries exist, and every entry belongs to a row. ADMIN CLEANUP INDEX refuses it.
  • DDL guards: creating the index needs DDL job version 2 and every TiDB node to support it. SHOW CREATE TABLE prints it with its parser, BR log restore can rebuild it, and ANALYZE, auto-analyze, TTL and access-path selection skip it.

Notes for reviewers:

  • The variable is visible in SHOW VARIABLES: the Hidden flag is deprecated and no longer hides anything. It is checked only in session DDL paths, so DM and Lightning are not gated.
  • table.IndexKVGenerator gains an error-only generator. Following master's rule that Next advances only on success, it returns its error without advancing.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test

Some ddl and table/tables tests need failpoints; run them with tools/check/failpoint-go-test.sh.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

🤖 Generated with Claude Code

@ti-chi-bot ti-chi-bot Bot added the release-note-none Denotes a PR that doesn't merit a release note. label Nov 12, 2026
@ti-chi-bot

ti-chi-bot Bot commented Nov 12, 2026

Copy link
Copy Markdown

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 12, 2026
@coderabbitai

coderabbitai Bot commented Nov 12, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

terry1purcell and others added 3 commits November 13, 2026 16:11
…ysis

Add the parsing and analysis a FULLTEXT search needs, with no SQL
behaviour change yet:

- pkg/expression/matchagainst parses IN BOOLEAN MODE search strings for
  the STANDARD and NGRAM parsers, following MySQL's operator grammar.
- pkg/expression/fulltext tokenizes documents the way InnoDB's built-in
  and ngram parsers do (token size bounds, the default stopword list,
  ngram positions), and compiles a boolean search into a query that can
  match a document.
- innodb_ft_min_token_size, innodb_ft_max_token_size and
  ngram_token_size become real global variables, bounded as in MySQL, so
  the analyzer reads them; they were read-only placeholders.
- model.FullTextParserTypeNgramV1 names the NGRAM parser.

This is the first part of splitting pingcap#71531 (FULLTEXT index
in TiKV) into staged PRs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oken-size bounds

- NGRAM positions: a word too short to yield a gram left no gap when an
  earlier word had more grams than words, so the distance between the
  grams around it depended on the preceding text. A phrase such as
  "gh x ij" then missed a document containing it, and "gh ij" matched
  it. Positions now come from a running counter: one per gram start, and
  one per word too short for a gram.
- STANDARD parser: a quote left open now runs to the end of the search
  string, and a newline no longer ends it, as in the NGRAM parser. It
  was an error, so a stray quote failed the statement under one parser
  and not the other.
- innodb_ft_min_token_size and innodb_ft_max_token_size are validated
  against each other. Their ranges overlap, and a minimum above the
  maximum made the STANDARD analyzer drop every token, so every search
  silently matched nothing.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… operators

- A required term that is a stopword or outside the token-size bounds
  made the whole search match nothing, while the same term without '+'
  was ignored. It is now dropped, as MySQL transforms '+word +stopword'
  into '+word'; a search left with no positive term still matches
  nothing.
- '~', '<' and '>' were errors. In MySQL they only change a word's
  contribution to the relevance, and MATCH is evaluated here as a filter
  without a score, so the word after them is optional. '(' ')' and '@'
  stay unsupported.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…equired term

UsesPrefixPostings reported a prefix anywhere in the query, including an
optional one in a group with a required child, whose postings are never
read. It now follows the lists PostingReads names.

Also document the two exported NGRAM parsing errors separately.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…GRAM delimiter

A word of Hindi, Thai or vocalized Arabic holds a combining mark, a vowel
sign or a virama, between most of its letters. The tokenizer took only
letters, numbers and '_' as word characters, so it split such a word at
every mark, into single letters that the STANDARD analyzer then dropped
as too short. Marks are now word characters, in the analyzer and in the
NGRAM search-string scanner, which must agree with it.

The NGRAM scanner follows MySQL's fts_get_word, which an index WITH
PARSER ngram is searched through: a prefix operator counts only after a
space, the last of several wins, and a stray one is skipped. '@' is no
operator there, only a delimiter, so it no longer fails the statement
and an address can be searched for.

Also remove code no search string can reach:
- the NGRAM parser's parenthesized groups, which its scanner rejects
  first, and with them ErrUnmatchedLeftParen and ErrUnmatchedRightParen;
- BooleanGroup as a clause expression, BooleanTerm.Ignored and
  BooleanPhrase.Distance, which no parser sets;
- the STANDARD scanner's NUM token, which only followed the '@' the
  parser rejects, and the commented-out parseGroupClause.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ted token sizes

- In an NGRAM boolean search, '>', '<' and '~' after a space are consumed
  as operators, as MySQL's scanner does, so '+>apple' stays required and
  '->apple' excluded instead of both becoming optional.
- The analyzer refuses innodb_ft_min_token_size above
  innodb_ft_max_token_size when it reads them. Each SET checks the other
  variable, but concurrent updates, or updates between the two reads, can
  still give such a pair, and the STANDARD analyzer would then drop every
  token and match nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
terry1purcell and others added 2 commits November 16, 2026 19:55
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On the classic kernel, store a FULLTEXT index in TiKV as a positional
inverted index: one KV entry per distinct term per row, whose value
holds the term's positions in the document. Creating one requires the
new undocumented variable tidb_enable_tikv_fulltext_index (default
OFF); without it CREATE TABLE, CREATE INDEX and ALTER TABLE ADD fail
with the same error as before. The switch is removed once queries can
read the index.

- Metadata: IndexInfo.TiKVFullText snapshots the parser and analyzer
  settings (token sizes, stopwords, ngram size) when the index is
  created, with a format version. The statement's session captures them
  into the ADD INDEX job, so the owner builds with the same settings.
  WITH PARSER NGRAM selects the ngram parser.
- Columns: the last index column is tokenized; any columns before it are
  ordinary key columns encoded ahead of the term, so one tenant's entries
  form a contiguous range. Prefix lengths, DESC, GLOBAL, partial
  conditions, expression key parts, non-string or binary columns and local
  temporary tables are refused.
- Write path: the index tokenizes the row and writes one entry per term;
  a NULL, empty or stopword-only document writes none. The mutation
  checker checks each entry's term and key columns against the row. An
  index whose version this binary does not know fails its writes rather
  than its schema load.
- ADMIN CHECK TABLE / INDEX checks a FULLTEXT index from the record side:
  every row's expected entries exist, and every entry belongs to a row.
  ADMIN CLEANUP INDEX refuses it.
- DDL guards: creating the index needs DDL job version 2 and every TiDB
  node to support it. SHOW CREATE TABLE prints it with its parser, BR log
  restore can rebuild it, and ANALYZE, auto-analyze, TTL and access-path
  selection skip it.

This is part 3 of splitting pingcap#71531 (FULLTEXT index in TiKV)
into staged PRs; it depends only on part 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A relevance score such as BM25 needs, from each posting it reads, the
term's frequency in the document and the document's length. The frequency
is the position count; store the length, in tokens, ahead of it, so a
score can later be computed from the postings alone without reading or
analyzing the document. A row's entries are all rewritten when its
document changes, so the length cannot go stale.

The new layout takes value kind 0x02; the pre-release kind 0x01, without
the length, is refused rather than misread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In MySQL every column of FULLTEXT(a, b) is tokenized and the columns are
searched together as one document. Reading the column list as key columns
ahead of a tokenized last column gave the same definition another meaning:
a ported FULLTEXT(title, body) was silently built as an index on body keyed
by title.

Keep MySQL's meaning for the column list, and name key columns apart:

  FULLTEXT INDEX idx (body) KEY_COLUMNS (tenant_id)

A column list of several columns is refused until searching several
columns as one document is supported. The DDL layer turns KEY_COLUMNS into
key parts ahead of the tokenized column where every caller enters it, so
the stored index and the DDL job arguments are unchanged. SHOW CREATE TABLE
and BR's index-repair SQL print the clause, the former inside a
/*T![fts_key] */ comment that MySQL and older TiDB read as a plain
FULLTEXT(body).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ti-chi-bot

ti-chi-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign bornchanger, joechenrh, terry1purcell, zanmato1984 for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

This branch has not been deployed

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

Labels

component/statistics do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant