From b7d5dac6c2be61d9a0ac79e4fa8dd4f49ab40545 Mon Sep 17 00:00:00 2001 From: Pengyi Peng Date: Sat, 14 Nov 2026 02:55:32 +0800 Subject: [PATCH 1/2] Preserve one-shot column iterables in full-text search queries --- sqlite_utils/db.py | 1 + tests/test_fts_search_iterables.py | 85 ++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/test_fts_search_iterables.py diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index c011d9bb0..499279682 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -3960,6 +3960,7 @@ def search_sql( original_quoted = quote_identifier(original) columns_sql = "*" columns_with_prefix_sql = f"{original_quoted}.*" + columns = list(columns) if columns is not None else None if columns: columns_sql = ",\n ".join(quote_identifier(c) for c in columns) columns_with_prefix_sql = ",\n ".join( diff --git a/tests/test_fts_search_iterables.py b/tests/test_fts_search_iterables.py new file mode 100644 index 000000000..900156d8e --- /dev/null +++ b/tests/test_fts_search_iterables.py @@ -0,0 +1,85 @@ +import pytest + + +def _generator(values): + yield from values + + +@pytest.fixture(params=["FTS4", "FTS5"]) +def searchable_table(fresh_db, request): + table = fresh_db["books"] + table.insert_all( + [ + {"title": "sqlite first", "author": "A", "notes": "one"}, + {"title": "sqlite second", "author": "B", "notes": "two"}, + {"title": "sqlite third", "author": "C", "notes": "three"}, + ] + ) + table.enable_fts(["title"], fts_version=request.param) + return table + + +@pytest.mark.parametrize("entrypoint", ["search_sql", "search"]) +@pytest.mark.parametrize("names", [[], ["title"], ["author", "title"]]) +@pytest.mark.parametrize( + "factory", + [list, tuple, iter, _generator], + ids=["list", "tuple", "iterator", "generator"], +) +def test_search_column_iterables(searchable_table, entrypoint, names, factory): + columns = factory(names) + kwargs = dict( + columns=columns, + order_by='"original".rowid', + limit=2, + offset=1, + where="rowid >= :minimum", + ) + if entrypoint == "search_sql": + sql = searchable_table.search_sql(**kwargs) + actual = list(searchable_table.db.query(sql, {"query": "sqlite", "minimum": 1})) + else: + actual = list( + searchable_table.search("sqlite", where_args={"minimum": 1}, **kwargs) + ) + + expected = [ + {"rowid": 2, "title": "sqlite second", "author": "B", "notes": "two"}, + {"rowid": 3, "title": "sqlite third", "author": "C", "notes": "three"}, + ] + if names: + expected = [{name: row[name] for name in names} for row in expected] + assert actual == expected + assert [list(row) for row in actual] == [list(row) for row in expected] + if isinstance(columns, (list, tuple)): + assert list(columns) == names + + +def test_search_none_columns_still_returns_all_columns(searchable_table): + results = list(searchable_table.search("sqlite", columns=None, order_by="rowid")) + assert len(results) == 3 + assert list(results[0]) == ["rowid", "title", "author", "notes"] + + +def test_search_iterator_with_rank(searchable_table): + results = list( + searchable_table.search("sqlite", columns=iter(["title"]), include_rank=True) + ) + assert {row["title"] for row in results} == { + "sqlite first", + "sqlite second", + "sqlite third", + } + assert all(list(row) == ["title", "rank"] for row in results) + assert all(isinstance(row["rank"], float) for row in results) + + +@pytest.mark.parametrize("fts", ["FTS4", "FTS5"]) +def test_search_iterator_quoted_columns_and_alias_collision(fresh_db, fts): + table = fresh_db["original"] + table.insert({"text": "sqlite", 'with "quote': "A", "with space": "B"}) + table.enable_fts(["text"], fts_version=fts) + columns = ('with "quote', "with space") + expected = [{'with "quote': "A", "with space": "B"}] + assert list(table.search("sqlite", columns=columns)) == expected + assert list(table.search("sqlite", columns=iter(columns))) == expected From e7a3b6db7cd95eec05cf5a12dc11384a2f6bc920 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 29 Nov 2026 13:47:10 -0800 Subject: [PATCH 2/2] Replace new test file with a single test in test_fts.py One test in the existing FTS test file covers the fix: search() with an iterator for columns= returns the selected columns. Co-Authored-By: Claude Fable 5.2 --- tests/test_fts.py | 10 ++++ tests/test_fts_search_iterables.py | 85 ------------------------------ 2 files changed, 10 insertions(+), 85 deletions(-) delete mode 100644 tests/test_fts_search_iterables.py diff --git a/tests/test_fts.py b/tests/test_fts.py index 60c77269e..00d6cbbf2 100644 --- a/tests/test_fts.py +++ b/tests/test_fts.py @@ -112,6 +112,16 @@ def test_search_duplicate_columns_are_deduped(fresh_db): ] +def test_search_columns_accepts_iterator(fresh_db): + # https://github.com/simonw/sqlite-utils/issues/942 + table = fresh_db.table("t") + table.insert_all(search_records) + table.enable_fts(["text", "country"]) + assert list(table.search("tanuki", columns=iter(["text", "country"]))) == [ + {"text": "tanuki are running tricksters", "country": "Japan"} + ] + + def test_search_limit_offset(fresh_db): table = fresh_db.table("t") table.insert_all(search_records) diff --git a/tests/test_fts_search_iterables.py b/tests/test_fts_search_iterables.py deleted file mode 100644 index 900156d8e..000000000 --- a/tests/test_fts_search_iterables.py +++ /dev/null @@ -1,85 +0,0 @@ -import pytest - - -def _generator(values): - yield from values - - -@pytest.fixture(params=["FTS4", "FTS5"]) -def searchable_table(fresh_db, request): - table = fresh_db["books"] - table.insert_all( - [ - {"title": "sqlite first", "author": "A", "notes": "one"}, - {"title": "sqlite second", "author": "B", "notes": "two"}, - {"title": "sqlite third", "author": "C", "notes": "three"}, - ] - ) - table.enable_fts(["title"], fts_version=request.param) - return table - - -@pytest.mark.parametrize("entrypoint", ["search_sql", "search"]) -@pytest.mark.parametrize("names", [[], ["title"], ["author", "title"]]) -@pytest.mark.parametrize( - "factory", - [list, tuple, iter, _generator], - ids=["list", "tuple", "iterator", "generator"], -) -def test_search_column_iterables(searchable_table, entrypoint, names, factory): - columns = factory(names) - kwargs = dict( - columns=columns, - order_by='"original".rowid', - limit=2, - offset=1, - where="rowid >= :minimum", - ) - if entrypoint == "search_sql": - sql = searchable_table.search_sql(**kwargs) - actual = list(searchable_table.db.query(sql, {"query": "sqlite", "minimum": 1})) - else: - actual = list( - searchable_table.search("sqlite", where_args={"minimum": 1}, **kwargs) - ) - - expected = [ - {"rowid": 2, "title": "sqlite second", "author": "B", "notes": "two"}, - {"rowid": 3, "title": "sqlite third", "author": "C", "notes": "three"}, - ] - if names: - expected = [{name: row[name] for name in names} for row in expected] - assert actual == expected - assert [list(row) for row in actual] == [list(row) for row in expected] - if isinstance(columns, (list, tuple)): - assert list(columns) == names - - -def test_search_none_columns_still_returns_all_columns(searchable_table): - results = list(searchable_table.search("sqlite", columns=None, order_by="rowid")) - assert len(results) == 3 - assert list(results[0]) == ["rowid", "title", "author", "notes"] - - -def test_search_iterator_with_rank(searchable_table): - results = list( - searchable_table.search("sqlite", columns=iter(["title"]), include_rank=True) - ) - assert {row["title"] for row in results} == { - "sqlite first", - "sqlite second", - "sqlite third", - } - assert all(list(row) == ["title", "rank"] for row in results) - assert all(isinstance(row["rank"], float) for row in results) - - -@pytest.mark.parametrize("fts", ["FTS4", "FTS5"]) -def test_search_iterator_quoted_columns_and_alias_collision(fresh_db, fts): - table = fresh_db["original"] - table.insert({"text": "sqlite", 'with "quote': "A", "with space": "B"}) - table.enable_fts(["text"], fts_version=fts) - columns = ('with "quote', "with space") - expected = [{'with "quote': "A", "with space": "B"}] - assert list(table.search("sqlite", columns=columns)) == expected - assert list(table.search("sqlite", columns=iter(columns))) == expected