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

SQL Server: translation for the PATINDEX function #33702

Closed
roji opened this issue May 11, 2024 · 6 comments
Closed

SQL Server: translation for the PATINDEX function #33702

roji opened this issue May 11, 2024 · 6 comments
Labels
area-query area-sqlserver closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported good first issue This issue should be relatively straightforward to fix. type-enhancement
Milestone

Comments

@roji
Copy link
Member

roji commented May 11, 2024

SQL Server has PATINDEX, which allows getting the index of a LIKE-like pattern (as opposed to just checking whether the pattern matches). EF6 had SqlFunctions.PatIndex.

Raised by @irg1008 in #6159.

@imangd
Copy link
Contributor

imangd commented May 30, 2024

Hi @roji , I would be glad to work on this issue as my first contribution if it hasn't been worked on yet :)

@roji
Copy link
Member Author

roji commented May 30, 2024

Sure thing, feel free to submit a PR!

@imangd
Copy link
Contributor

imangd commented Jun 1, 2024

Thank you @roji! I'd like to share my plan and hopefully receive feedback/insights on whether or not I'm on the right track. Based on the discussion on 6159 and what I have learned from the codebase so far, I believe I should add a new PatIndex method to DbFunctionsExtensions and add a translation for that method in SqlServerStringMethodTranslator. Does that sound right?

@roji
Copy link
Member Author

roji commented Jun 1, 2024

The PatIndex would go on SqlServerDbFunctionsExtensions, not DbFunctionsExtensions, as it's a SQL Server-specific thing. And yeah, implementing the translation in SqlServerStringMethodTranslator makes sense.

@smnsht
Copy link
Contributor

smnsht commented Jun 1, 2024

Hi, @roji

I have a working branch (ready for PR) for this issue with a decent number of unity tests.
(started my work without announcing it)

In my solution, "PATINDEX" resides with SqlServerFullTextSearchFunctionsTranslator, and handles queries like

var result = context.Employees
    .Where(
        c => EF.Functions.PatIndex("%London%", c.City) > 0
            && EF.Functions.PatIndex("%Manager%", c.Title, "Latin1_General_BIN") > 0)
    .FirstOrDefault();

I added unit tests similar to those that exist for EF.Functions.FreeText, replacing language with collate. All works.

SqlServerStringMethodTranslator translates string methods, like str.Trim(), str.TrimLeft(), etc...
Can you confirm it is the place for patindex?

Shall I move my changes to SqlServerStringMethodTranslator?

@roji
Copy link
Member Author

roji commented Jun 1, 2024

Shall I move my changes to SqlServerStringMethodTranslator?

Yes. "full text" in the SQL Server context means something very specific (see docs), and PATINDEX isn't part of that. In any case, where exactly the translator lives isn't very important.

@cincuranet cincuranet added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 14, 2024
@cincuranet cincuranet modified the milestones: Backlog, 9.0.0-preview6 Jun 14, 2024
@roji roji changed the title Translation for the SQL Server PATINDEX function SQL Server: translation for the PATINDEX function Sep 23, 2024
@roji roji modified the milestones: 9.0.0-preview6, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query area-sqlserver closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported good first issue This issue should be relatively straightforward to fix. type-enhancement
Projects
None yet
Development

No branches or pull requests

5 participants