Case insensitive query against index
Our current use case is something like:
SELECT * FROM User WHERE any() LIKE ? ORDER BY MailAddress ASC
so, if I search for "terry", I do not get any result, but if I search for "Terry", I do find it. The property has been created as shown below:
userClass.createProperty(USER__FIRST_NAME, OType.STRING).setMandatory(true).setMin("1").setMax("50").createIndex(OClass.INDEX_TYPE.NOTUNIQUE);
Here is another example: I have a field "[email protected]". Today if I search for "[email protected]", I do not find it, but if I search for "[email protected]", I do find it. With this improvement in place, I would expect to find this field "[email protected]" if I searched for "[email protected]".
Usual behavior of SQL query is case sensitive comparison, which means that if we will not exclude such kind of index from SQL optimization, the other queries will be broken, but what is also interesting this query works only if index is present, which contradicts the SQL specification.
@enisher is proposed is to use this case insensitive index in query like this SELECT * FROM User WHERE any().equalsIgnoreCase(?) ORDER BY MailAddress ASC instead of SELECT * FROM User WHERE any() LIKE ? ORDER BY MailAddress ASC .
Which will not break other queries and will not contradict to SQL behavior without index.
It makes handling of composite indexes a bit tricky, but in reality possible to do.
You're right. But equalsIgnoreCase() wouldn't be like "LIKE" but rather like "EQUALS". In this use case Arul used LIKE as for equals because there aren't keyword. And it would very hard to use the index with keyword. So I assume Arul want exact match.
By looking how other SQL impl resolve this I found: http://stackoverflow.com/questions/2876789/case-insensitive-for-sql-like-wildcard-statement
Postgres and Adabas have the ILIKE operator (case-insensitive):
SELECT * FROM User WHERE any() ILIKE ? ORDER BY MailAddress ASC
But at this point we'd need the same for all the other operators like =, >, <, etc. The most common trick for RDBMS SQL users is to use UPPER/LOWER like:
SELECT * FROM User WHERE any().toLowerCase() = ? ORDER BY MailAddress ASC
But the user has to put the lower case key to search in the value passed in "?".
So the @enisher 's idea is good but it will more hard to parse and to recognize to the optimizer.
But if we use LIKE we introduce all issue listed above.
Also if Arul use any indexed fields in other queries, the rest of the queries with equal, they will be broken, do not they ?
Also index right now is not used for LIKE because of possible presence of %, we can introduce index reuse possibility for like if there is no wildcards. But issue with this query is only SQL parser issue, not index issue.
In this case it is better to introduce ILIKE operator as in postrgress and use index for not wildcards only and only for ILIKE (it is very simple to do). and of course we should not forget about composite keys to avoid issue with them too.
This will avoid all tradeoffs but as we discussed we should think about handling of composite keys to avoid possible bugs (in SQL query handling I mean).
@laa so what's your proposal?
HI guys, It seems like we have simple misunderstanding my idea is that usage of ILIKE (which @lvca has written in 2-nd comment) should resolve all issues and be simple to implement.
I don't like the ILIKE proposal because it doesn't work as a LIKE for the Arul's requirements. He needs more a EQUALS operator. So I found something in RDBMS (http://stackoverflow.com/questions/3969059/sql-case-sensitive-string-compare) to solve this problem.
ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)
COLLATE SQL_Latin1_General_CP1_CI_AS
COLLATE (http://dev.mysql.com/doc/refman/5.0/en/charset-collation-effect.html) defines how to compare values. We could support "COLLATE" in property definition like:
ALTER PROPERTY name COLLATE ci
The Collate strategy could be done by using the interface OCollate with 2 default impl:
-
OCaseSensitiveCollate
-
OCaseInsensitiveCollate
interface OCollate extends Comparator
By the way we could extend OCaseInsentiveComparator (the impl is not optimal, I'm going to change it). Now every-time an index has to look for a key we could call:
Object transformedKey = index.getCollate().transform( key );
And at the end we could provide a way to plug own collecting strategies by registering them in the factory.
WDYT?
Luca, You are the best.
+1.
Done. Now IndexDefinition and OProperty have:
OCollate getCollate()
setCollate( OCollate )
setCollate( String )
Collate implementations are pluggable like SQL functions (by using std Java service registry). Bundled there are:
- default
- ci (case insensitive)
When the index is created against one property the collate attribute is inherited by index too.
I've also changed OUser and ORole to use collate=ci.
Hi Luca, I suggest to introduce the collate property at database level too. This will be the default collation used for all string comparisons, if not overrided by the specific collation attribute of the property queried.
Furthermore it could be useful to extend the SQL parser to specify the collation inside the query. This will override both the property and DB COLLATE attributes.
@giastfader this issue has been sponsored by a client and satisfies his requirements. What you propose is good, but it should be another issue ;-)