Skip to content
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

StartsWith on citext column does not return results #319

Closed
jcachat opened this issue Mar 1, 2018 · 4 comments
Closed

StartsWith on citext column does not return results #319

jcachat opened this issue Mar 1, 2018 · 4 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@jcachat
Copy link

jcachat commented Mar 1, 2018

I am having an issue trying to do a StartsWith() on a "citext" column. When the case of the search value doesn't match the data, I don't get the correct results returned.

The condition that is being generated is something like this

WHERE ("ec"."Name" LIKE ($1 || '%') AND (LEFT("ec"."Name", LENGTH($1)) = $1))

The problem seems to be caused by the LEFT/LENGTH part. LEFT seems to be returning the value as a normal "text" string and not a "citext" value so it's not doing a case insensitive comparison.

In my searches, I came across this discussion which I assume is why that condition was added.

I can workaround it with a .ToLower() on the column but that defeats the purpose of the citext. Can anything be done about this?

@roji roji added the bug Something isn't working label Mar 1, 2018
@roji roji added this to the 2.0.2 milestone Mar 1, 2018
@roji
Copy link
Member

roji commented Mar 1, 2018

I'll take a look at this. Can you please say which version you're using?

@jcachat
Copy link
Author

jcachat commented Mar 1, 2018

Sorry, I meant to include that and forgot. I'm using version 2.0.1 (the latest) and running 10.1 of PostgreSQL.

@roji roji closed this as completed in 9f8cd0c Mar 5, 2018
@roji
Copy link
Member

roji commented Mar 5, 2018

Your analysis is correct. PostgreSQL's LEFT() unfortunately returns a text when given a citext (i.e. there's no citext overload. LEFT() is used to implement StartsWith() in order to chop the beginning part of the string for comparison (LIKE is used first to use any existing index).

I pushed a commit that will specifically check for citext and cast the result of LEFT() to a citext, so that comparison is case-insensitive. This is somewhat hacky but it works. It will be released with 2.1.0-preview2.

@roji roji modified the milestones: 2.0.2, 2.1.0 Mar 5, 2018
@roji roji self-assigned this Mar 5, 2018
austindrenski pushed a commit to austindrenski/npgsql-efcore.pg that referenced this issue Mar 7, 2018
@chrisckc
Copy link

This appears to have regressed in 3.1
see: #388

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants