efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Query: Translate byte array.IndexOf

Open smitpatel opened this issue 6 years ago • 11 comments

smitpatel avatar Dec 12 '19 02:12 smitpatel

CharIndex over varbinary(max) returns bigint rather than int.

smitpatel avatar Dec 13 '19 20:12 smitpatel

[Edited name] I'm on vacation, and I want to get some things to do, can I start with this one, @roji / @smitpatel any observation?

ralmsdeveloper avatar Jun 12 '20 08:06 ralmsdeveloper

Nothing in particular @ralmsdeveloper, just to pay close attention to specific return types (bigint/int) as @smitpatel wrote above.

roji avatar Jun 12 '20 10:06 roji

Hi @roji / @smitpatel

I am new to the EFCore repo and have just understood some basic things about the translation (above linked PR helped with that a bit). I would like to work on this one if it's not being worked on by any one else.

Here's my initial understanding of this issue (atleast for SqlServer):

  • Need to add a specific translator for the ByteArray for the 2 overloads of Array.IndexOf method in SqlServerByteArrayMethodTranslator
  • In case the declaring type is varbinary(max) cast the return value as int before returning

Haven't looked into sqlLite for this yet, but it should be easily portable. Before that I wanted to check if I'm on the right track for this?

nikhil197 avatar Aug 15 '24 14:08 nikhil197

@nikhil197 overall yes, but what are you planning to translate the method call to exactly? And why "cast the return value as int"? Note that you'll also need to add proper test coverage too.

I'd suggest on concentrating on a SQL Server implementation first, it's also OK if support is added only for SQL Server initially without SQLite (although support in both is obviously better).

roji avatar Aug 17 '24 16:08 roji

@roji I have tried implementing similar to what has already been done for the string.IndexOf methods. Passing values to CHARINDEX (with conversions, if needed) and then returning value - 1 (since CHARINDEX returns 1-based values). This is the final translation for the Array.IndexOf method for byte[], with a starting position: CAST(CHARINDEX(CAST(@__b_0 AS varbinary(max)), [s].[Banner], @__startPos_1 + 1) AS int) - 1

And casting to int is only when varbinary(max) is the underlying type of the column because as mentioned above, CHARINDEX will return bigint. So to match that to the method which returns int, I'm using a cast.

And I have tried adding tests as well. I was starting to look into the SQLite change, but I can raise the PR for the changes I have done so far.

nikhil197 avatar Aug 17 '24 16:08 nikhil197

Hi, I'd like to work on this if no one is currently working on it.

There's an existing PR (#34457) by @nikhil197 — thank you for the groundwork on this! Since it appears to have gone inactive, I'd like to submit a fresh implementation that builds on that work and incorporates @roji review feedback:

  • Type safety: checking that the array argument is actually byte[] before translating
  • No separate shared ArrayMethods class — method matching is done inline within each translator
  • Reduced code duplication for the varbinary(max) bigint/int branching
  • DB-neutral test naming (no varbinary_max in test names)

My implementation covers:

  • SQL Server: CHARINDEX with CAST(... AS int) for varbinary(max), both 2-param and 3-param overloads
  • SQLite: instr for 2-param, CASE WHEN + substr for 3-param (since instr doesn't support a start position natively)

I'll open a PR shortly. Please let me know if there are any concerns.

yykkibbb avatar Feb 17 '26 14:02 yykkibbb

My bad for not re-reviewing #34457 again in a timely manner, I was too busy with other things. @nikhil197 if you wish to rebase/continue work on #34457, please feel free to do so and I'll take care to review and merge it. Otherwise, I'm happy to review a new PR that takes everything in #34457 into account.

roji avatar Feb 17 '26 15:02 roji

Hi @roji , Yes I will continue this again. I was busy with some other stuff. I will rebase my PR .. WIll submit it soon. Thanks

nikhil197 avatar Mar 28 '26 10:03 nikhil197

Hi, I’m interested in completing #19287 as my first EF Core contribution. I noticed PR #34457 has been inactive for some time. Would it be appropriate for me to start a fresh implementation based on the current main branch?

ivanPeshterskii avatar Aug 11 '26 21:08 ivanPeshterskii

@ivanPeshterskii Sure

AndriySvyryd avatar Nov 18 '26 00:11 AndriySvyryd