Skip to content

Properly support citext in all string translations #388

Closed

Description

@roji

In #319 and #384, String.Startswith() and String.Contains() translation was fixed to make it compatible with citext. We still have other method translators which haven't be adapted (EndsWith(), Replace()...).

Do a proper pass over everything and make sure everyone works well, add tests as well.

Activity

  1. added this to the Backlog milestone on Jun 10, 2018
  2. chrisckc commented on Mar 30, 2019

    @chrisckc

    @roji It seems that String.Startswith() has been fixed in the use case in #319, but only in the case where the database column starts with the supplied string, not the other way around.
    Maybe String.Contains also has the same issue? i haven't tried that yet.

    Example:
    Where the database contains a records with column CompanyName = "some company"

    This works as it was fixed in #319:

    string companyName = "Some Company";
    company = await Companies.OrderBy(item => item.CompanyName.Length).Where(item => item.CompanyName.StartsWith(companyName)).FirstOrDefaultAsync();
    

    In the output SQL i can see the casts to CITEXT in both the LIKE and LEFT-LENGTH comparisons:

    FROM "Companies" AS item
    WHERE (item."IsArchived" = FALSE) AND (item."CompanyName" LIKE ($1 || CITEXT '%') AND (CAST(LEFT(item."CompanyName", LENGTH($1)) AS citext) = $1))
    ORDER BY LENGTH(item."CompanyName")
    LIMIT 1;
    

    (in the output above i have replaced the EF generated parameter with $1 for testing the query)

    However this fails:

    string companyName = "Some Company";
    company = await Companies.OrderByDescending(item => item.CompanyName.Length).Where(companyName.StartsWith(item.CompanyName)).FirstOrDefaultAsync();
    

    No results are returned and the generated SQL is missing the required cast on the LEFT-LENGTH part of the query:

    FROM "Companies" AS item
    WHERE (item."IsArchived" = FALSE) AND ($1 LIKE (item."CompanyName" || CITEXT '%') AND (LEFT($1, LENGTH(item."CompanyName")) = item."CompanyName"))
    ORDER BY LENGTH(item."CompanyName") DESC
    LIMIT 1;
    

    If i either remove the "AND (LEFT..." comparison or add the required cast it works:
    Fixed version:

    FROM "Companies" AS item
    WHERE (item."IsArchived" = FALSE) AND ($1 LIKE (item."CompanyName" || CITEXT '%') AND (CAST(LEFT($1, LENGTH(item."CompanyName")) AS citext) = item."CompanyName"))
    ORDER BY LENGTH(item."CompanyName") DESC
    LIMIT 1;
    

    For now i will have to implement some kind of workaround until this is fixed.
    I am using Npgsql.EntityFrameworkCore.PostgreSQL version 2.2.0

  3. chrisckc commented on Mar 30, 2019

    @chrisckc

    Using ToLower() on both strings works, but obviously negates the point of using CITEXT

  4. chrisckc commented on Nov 29, 2019

    @chrisckc

    I appears that in 3.1, #319 seems to have regressed, this worked in 2.1 however testing it in 3.1 shows that CiTextColumn.StartsWith("sometext") is no longer case insensitive, i can see that the generated SQL no longer includes the CAST to citext as shown above.

    At least it is now consistent again with CiTextColumn.EndsWith("sometext") which was never fixed.

  5. chrisckc commented on Nov 29, 2019

    @chrisckc

    In 3.1, CiTextColumn.Contains("sometext") still works as it did being case insensitive.

    Also there is no change in the behaviour of the reverse scenarios:
    "sometext".StartsWith(CiTextColumn) is still case sensitive
    "sometext".EndsWith(CiTextColumn) is still case sensitive

    However "sometext".Contains(CiTextColumn) works being case insensitive in 3.1, not sure if this worked in 2.1

  6. chrisckc commented on Nov 29, 2019

    @chrisckc

    Here are some example outputs:
    In the Examples, Vehicle.Make is a citext column

    var test6 = _store.MyEntity.Where(item => item.Vehicle.Make.Contains(testString)).Take(10).ToList(); // works fine
    Generated SQL:
    WHERE STRPOS(v."Make", @__testString_0) > 0
    
    
    var test7 = _store.MyEntity.Where(item => item.Vehicle.Make.StartsWith(testString)).Take(10).ToList(); // does perform as case insensitive
    Generated SQL:
    WHERE (@__testString_0 = '') OR ((v."Make" IS NOT NULL) AND ((v."Make" LIKE v."Make" || '%') AND (LEFT(v."Make", LENGTH(@__testString_0)) = @__testString_0)))
    
    
    var test8 = _store.MyEntity.Where(item => item.Vehicle.Make.EndsWith(testString)).Take(10).ToList(); // does not perform as case insensitive
    Generated SQL:
    WHERE (@__testString_0 = '') OR ((v."Make" IS NOT NULL) AND (RIGHT(v."Make", LENGTH(@__testString_0)) = @__testString_0))
    
    // Reverse scenarios:
    
    var test3 = _store.MyEntity.Where(item => testString.Contains(item.Vehicle.Make)).Take(10).ToList(); // works fine
    Generated SQL:
    WHERE STRPOS(@__testString_0, v."Make") > 0
    
    
    var test4 = _store.MyEntity.Where(item => testString.StartsWith(item.Vehicle.Make)).Take(10).ToList(); // does not perform as case insensitive
    Generated SQL:
    WHERE (v."Make" = '') OR ((v."Make" IS NOT NULL) AND ((@__testString_0 LIKE @__testString_0 || '%') AND (LEFT(@__testString_0, LENGTH(v."Make")) = v."Make")))
    
    
    var test5 = _store.MyEntity.Where(item => testString.EndsWith(item.Vehicle.Make)).Take(10).ToList(); // does not perform as case insensitive
    Generated SQL:
    WHERE (v."Make" = '') OR ((v."Make" IS NOT NULL) AND (RIGHT(@__testString_0, LENGTH(v."Make")) = v."Make"))
    
  7. roji commented on Nov 29, 2019

    @roji
    MemberAuthor

    @chrisckc I'll do my best to look at StartsWith and EndsWith ASAP to make sure that they work as expected in the final 3.1 release.

  8. modified the milestones: Backlog, 3.1.0 on Dec 1, 2019
  9. self-assigned this
    on Dec 1, 2019
  10. added 4 commits that reference this issue on Dec 1, 2019
    f179a35
    e0dd19e
    02318f6
    f3f10f1
  11. chrisckc commented on Dec 2, 2019

    @chrisckc

    My above examples work fine with your commits now, Thanks!

  12. roji commented on Dec 2, 2019

    @roji
    MemberAuthor

    Thanks for confirming @chrisckc!

  13. added a commit that references this issue on Dec 2, 2019
    8364015
  14. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions