-
-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
detect_fts()
identifies the wrong table if tables have names that are subsets of each other
#434
Comments
detect_fts()
identifies the wrong table if tables have names that are subsets of each other
Definitely a bug - thanks for the detailed write-up! You're right, the code at fault is here: sqlite-utils/sqlite_utils/db.py Lines 2213 to 2231 in 1b09538
|
A test that demonstrates the problem: @pytest.mark.parametrize("reverse_order", (True, False))
def test_detect_fts_similar_tables(fresh_db, reverse_order):
# https://github.com/simonw/sqlite-utils/issues/434
table1, table2 = ("demo", "demo2")
if reverse_order:
table1, table2 = table2, table1
fresh_db[table1].insert({"title": "Hello"}).enable_fts(
["title"], fts_version="FTS4"
)
fresh_db[table2].insert({"title": "Hello"}).enable_fts(
["title"], fts_version="FTS4"
)
assert fresh_db[table1].detect_fts() == "{}_fts".format(table1)
assert fresh_db[table2].detect_fts() == "{}_fts".format(table2) The order matters - so this test currently passes in one direction and fails in the other:
|
Since table names can be quoted like this: CREATE VIRTUAL TABLE "searchable_fts"
USING FTS4 (text1, text2, [name with . and spaces], content="searchable") OR like this: CREATE VIRTUAL TABLE "searchable_fts"
USING FTS4 (text1, text2, [name with . and spaces], content=[searchable]) This fix looks to be correct to me (copying from the updated (
"SELECT name FROM sqlite_master\n"
" WHERE rootpage = 0\n"
" AND (\n"
" sql LIKE :like\n"
" OR sql LIKE :like2\n"
" OR (\n"
" tbl_name = :table\n"
" AND sql LIKE '%VIRTUAL TABLE%USING FTS%'\n"
" )\n"
" )",
{
"like": "%VIRTUAL TABLE%USING FTS%content=[dogs]%",
"like2": '%VIRTUAL TABLE%USING FTS%content="dogs"%',
"table": "dogs",
},
) |
Windows 10
Python 3.9.6
When I was running a full text search through the Python library, I noticed that the query was being run on a different full text search table than the one I was trying to search.
I took a look at the following function
sqlite-utils/sqlite_utils/db.py
Line 2213 in 841ad44
and noticed:
My database contains tables with similar names and %{table}% was matching another table that ended differently in its name.
I have included a sample test that shows this occurring:
I search for Marsupials in db["books"] and The Clue of the Broken Blade is returned.
This occurs since the search for Marsupials was "successfully" done against db["booksb"] and rowid 1 is returned. "The Clue of the Broken Blade" has a rowid of 1 in db["books"] and this is what is returned from the search.
The text was updated successfully, but these errors were encountered: