Skip to content

Return -1 from SQLite String.IndexOf(value, startIndex) when the valu… - #38729

Open
ilkertskn wants to merge 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlite-indexof-starting-position-not-found
Open

Return -1 from SQLite String.IndexOf(value, startIndex) when the valu…#38729
ilkertskn wants to merge 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlite-indexof-starting-position-not-found

Conversation

@ilkertskn

@ilkertskn ilkertskn commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

…e is not found

  • The translation computed (instr(substr(...)) - 1) + startIndex, which yields startIndex - 1 instead of -1 when instr returns 0 (the value is not found), so IndexOf produced a wrong non-negative result
  • Wrap the result in a CASE that returns -1 when instr is 0, matching String.IndexOf
  • Add a test for the not-found case and update the affected SQL baselines
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

…e is not found

- The translation computed (instr(substr(...)) - 1) + startIndex, which yields startIndex - 1 instead
  of -1 when instr returns 0 (the value is not found), so IndexOf produced a wrong non-negative result
- Wrap the result in a CASE that returns -1 when instr is 0, matching String.IndexOf
- Add a test for the not-found case and update the affected SQL baselines
@ilkertskn
ilkertskn requested a review from a team as a code owner July 31, 2026 21:36
@ilkertskn

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@AndriySvyryd
AndriySvyryd requested a review from Copilot July 31, 2026 22:28
@AndriySvyryd AndriySvyryd self-assigned this Jul 31, 2026
@AndriySvyryd AndriySvyryd added this to the 12.0.0 milestone Jul 31, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes SQLite translation of string.IndexOf(value, startIndex) so that it correctly returns -1 when the searched value isn’t found (SQLite instr() returns 0, which previously caused the expression to yield startIndex - 1).

Changes:

  • Update SQLite IndexOf(..., startIndex) translation to wrap instr(substr(...)) in a CASE returning -1 when instr() returns 0.
  • Update existing SQL baselines for IndexOf starting-position tests to reflect the new CASE translation.
  • Add a new functional test covering the “not found => -1” case for IndexOf with a starting position.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
test/EFCore.Sqlite.FunctionalTests/Query/Translations/StringTranslationsSqliteTest.cs Updates SQL baselines for starting-position IndexOf and adds a new test asserting -1 when not found.
src/EFCore.Sqlite.Core/Query/Internal/Translators/SqliteStringMethodTranslator.cs Fixes the SQLite translation for IndexOf(value, startIndex) by guarding the instr() result with a CASE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants