lance icon indicating copy to clipboard operation
lance copied to clipboard

FTS index params: max_token_length=0 builds an index that matches nothing; ngram-only options silently accepted with the code analyzer

Open wjones127 opened this issue 2 months ago • 1 comments

Bug bash: Needs team input — design question

Bug bash priority: Medium · critical-fix: no From the FTS for Code bug bash (2026-10-01), findings: tokenization-8.md, enterprisecreate-6.md. Repros ran against released artifacts (pylance 12.0.0 / 13.0.0b18, lancedb 0.39.0 / 0.40.0b12, @lancedb/lancedb 0.40.0-beta.12) unless stated. $S is the bug bash scratch dir.

Decision needed: the validation contract (adds new errors).

Options:

  1. Reject max_token_length=0, and reject ngram-only options unless base_tokenizer="ngram".
  2. Define 0 as "no limit". This would also give SQL/REST a way to express unlimited.
  3. Leave the behavior and document it.

Recommendation: 1 for the ngram options. Pick 1 or 2 for 0, coordinated with the Enterprise request for an unlimited max_token_length.

  • Category: bug (low) / docs gap
  • Environment: pylance local, Python 3.12, Linux aarch64
  • Artifacts: pylance 13.0.0b18 and 12.0.0 (identical output)
  • Target tracker: GitHub lance-format/lance

Repro

$S/hunt/tokenization/r8.py:

import lance, pyarrow as pa, tempfile
print("pylance", lance.__version__)
tbl = pa.table({"id": [0], "code": ["getUserName"]})
def c(**kw):
    ds = lance.write_dataset(tbl, tempfile.mkdtemp())
    try:
        ds.create_scalar_index("code", "INVERTED", **kw)
        print(f"create OK  {kw} -> 'getUserName' finds {ds.to_table(full_text_query='getUserName', columns=['id','_score'])['id'].to_pylist()}")
    except Exception as e:
        print(f"create ERR {kw} -> {type(e).__name__}: {e}")
    try:
        lance.tokenize("getUserName", **kw); print(f"tokenize OK {kw}")
    except Exception as e:
        print(f"tokenize ERR {kw} -> {type(e).__name__}: {e}")
c(split_identifiers=True)                                     # rejected: code flag on text
c(analyzer="code", min_ngram_length=2, prefix_only=True)      # ngram-only flags on code: accepted
c(analyzer="code", max_token_length=0)                        # indexes nothing
c(analyzer="code", split_identifiers=None)                    # None
c(analyzer="code", stem=None)

Output:

pylance 13.0.0-beta.18
create ERR {'split_identifiers': True} -> ValueError: code analyzer flags require analyzer='code'
tokenize ERR {'split_identifiers': True} -> ValueError: code analyzer flags require analyzer='code'
create OK  {'analyzer': 'code', 'min_ngram_length': 2, 'prefix_only': True} -> 'getUserName' finds [0]
tokenize OK {'analyzer': 'code', 'min_ngram_length': 2, 'prefix_only': True}
create OK  {'analyzer': 'code', 'max_token_length': 0} -> 'getUserName' finds []
tokenize OK {'analyzer': 'code', 'max_token_length': 0}
create ERR {'analyzer': 'code', 'split_identifiers': None} -> TypeError: 'None' is not an instance of 'bool'
tokenize OK {'analyzer': 'code', 'split_identifiers': None}
create ERR {'analyzer': 'code', 'stem': None} -> TypeError: 'None' is not an instance of 'bool'
tokenize OK {'analyzer': 'code', 'stem': None}
  • Code flags on a text analyzer are a hard error. The reverse, ngram-only options (min_ngram_length, max_ngram_length, prefix_only) on the code analyzer, is silently accepted and has no effect.
  • max_token_length=0 builds an index containing no tokens. Every query returns nothing, with no error.
  • lance.tokenize docs say options "set to None use the selected analyzer profile's default" and that it "accepts the tokenizer-related options supported by create_scalar_index" (src/lance/docs/src/guide/tokenizer.md:31-47). But create_scalar_index(..., split_identifiers=None) / stem=None raise TypeError: 'None' is not an instance of 'bool'. Code that builds kwargs from a config with None for "default" works for tokenize and fails for index creation.

Expected behavior

Options that don't apply to the selected analyzer are rejected consistently in both directions (or at least warned). max_token_length must be ≥ 1 (or None). None means "profile default" on both APIs.

Evidence: the text→code direction already errors with code analyzer flags require analyzer='code' (src/lance/python/src/fts.rs:78-97). tokenize declares every option as Option<...> (fts.rs:186-226).

Suspected root cause

FtsTokenizerOptions::apply (src/lance/python/src/fts.rs:62-151) checks only code flags against non-code analyzers. Ngram fields are applied unconditionally (fts.rs:130-138), and InvertedIndexParams::validate (src/lance/rust/lance-index/src/scalar/inverted/tokenizer.rs:1125-1136) does not check them either. extract_kwarg::<T> (fts.rs:10-18) extracts T, not Option<T>, so an explicit Python None from create_scalar_index kwargs (src/lance/python/src/dataset.rs:2740) is a type error, while tokenize takes typed Option parameters.

Fix direction

Extract Option<T> in extract_kwarg (treat None as omitted, keeping the explicit-None meaning for max_token_length/custom_stop_words). Reject ngram options unless base_tokenizer is ngram, and reject max_token_length=0.


Additional reproduction (enterprisecreate-6.md)

  • Category: bug
  • Environment: lancedb server single node (SQL and REST); pylance local; Linux arm64
  • Artifacts: CLI release-2026-10-01.148d419ac8.verified (lance v13.0.0-beta.18); pylance 13.0.0b18
  • Target tracker: GitHub lance-format/lance (validation), LanceDB Enterprise message also misleading (Linear Enterprise)

Repro

SQL  WITH (base_tokenizer = 'code', max_token_length = 0)
  sql: CREATE INDEX
  idx_version: 3 details: {... 'max_token_length': 0 ...}
  q: {'user': [], 'getusername': [], 'xml': [], 'config': [], 'running': [], 'the': [], ...}   (every query empty)
REST {"base_tokenizer": "code", "max_token_length": 0}
  rest: 200 → same details, every query empty
pylance create_scalar_index("code", "INVERTED", base_tokenizer="code", max_token_length=0)
  details max_token_length: 0, every query -> []

($S/hunt/enterprisecreate/{sqlmatrix.py,restmatrix.py,pl.py}.)

The LanceDB Enterprise SQL error text for a bad value says "Expected a positive integer", yet it accepts 0.

Expected behavior

Reject max_token_length=0 with an invalid-input error. Alternatively, define 0 as "no limit", which would also give LanceDB Enterprise a way to express no limit (see enterprisecreate-5).

Suspected root cause

InvertedIndexParams::max_token_length stores the value unchecked (src/lance rust/lance-index/src/scalar/inverted/tokenizer.rs:859-862). build() adds RemoveLongFilter::limit(0), which drops every token (tokenizer.rs:1079-1084). validate() does not check it (tokenizer.rs:1125-1137). LanceDB Enterprise parse_usize_option accepts 0 (engine.rs:4338-4355).

Fix direction

Validate max_token_length >= 1 in Lance validate() (or document 0 = unlimited). Make the LanceDB Enterprise SQL message match.

wjones127 avatar Oct 01 '26 22:10 wjones127

I reproduced the local Python cases on main 71a1b7658 with a fresh native build (Python 3.12.15, pylance 15.0.0-beta.1):

  • split_identifiers=None and stem=None work with tokenize, but create_scalar_index raises TypeError.
  • min_ngram_length=2, prefix_only=True is accepted with analyzer="code" by both APIs.
  • max_token_length=0 produces no tokens and no indexed-query matches.
  • Explicit max_token_length=None retains a 40-byte token in both tokenization and indexed search.

I would like to contribute a focused validation fix with regression coverage through both APIs. Is the intended contract to reject zero, or to make it unlimited? For the other cases, I propose treating ordinary explicit None values as omitted while preserving the special None meanings for max_token_length and custom_stop_words, and rejecting explicitly supplied ngram-only options outside the ngram tokenizer. Please let me know if someone is already implementing this or if you prefer a different scope.

These results cover local Lance only; I have not exercised the Enterprise SQL/REST behavior. No production patch has been started.

AI assistance: Codex helped prepare this reproduction and message; the reported cases were executed locally against the source build.

karthiksenv avatar Oct 26 '26 04:10 karthiksenv