Description
Submitted by: Rajko Thon (rthon)
Attachments:
k4-test.7z
BUG: Reverse like-search with '%' not working
I implemented a table containing words and tried to implement a kind of full-text-search-solution similar to that of Jirí Cincura:
"https://www.tabsoverspaces.com/233577-poor-mans-full-text-using-psql-only-on-firebird".
Unfortunately, there seems to be a bug if you search with like and '%'.
To illustrate the behaviour I created a small sample database.
One table: SYS_WORT.
Two important columns: WORT and WORT_REV, the latter one containing the reversed word.
Indices on: Index
SYS_WORT.WORT UC1 unique constraint
SYS_WORT.WORT IX1 additional function based index on REVERSE(WORT)
SYS_WORT.WORT_REV IX2 index
So the first query demonstrates the behaviour hoped for.
SELECT w.*, char_length(w.wort) len FROM sys_wort w WHERE wort LIKE 'heis%en%' OR wort_rev LIKE 'greb%sie%'
It gets three of the four rows by using indices UC1(WORT) and IX2(WORT_REV)
Since a search-term usually has the letters in normal order, we reverse them.
But this one does not work as expected and does also not use any indices.
SELECT * FROM sys_wort WHERE wort LIKE 'Heis%en%' OR wort LIKE reverse('%eis%berg')
So is reverse('%eis%berg') != 'greb%sie%' ?
You can find endings of words using index IX2 although, without LIKE and any '%', but with STARTING.
SELECT * FROM sys_wort WHERE wort_rev STARTING reverse('berg')
You can also find the same using the additional index IX1 on REVERSE(WORT).
SELECT * FROM sys_wort WHERE reverse(wort) STARTING reverse('berg')
But LIKE again does not use the index IX1.
SELECT * FROM sys_wort WHERE reverse(wort) LIKE reverse('%berg')
So it seems it is not possible to reverse search with LIKE and '%' and using the index at the same time?