Repository navigation
Description
As part of #431 I'm working on the translation for string indexers.
I'm looking for opinions on whether the translation should be mapped to substr or get_byte.
Issue
Surprisingly, it appears that C# expression trees represent comparisons of char comparisons of int:
src.Where(x => x.SomeString[0] == 'T')
// expression: Convert(x.SomeString.get_Chars(0), Int32)This makes it hard to generate the SQL one would expect, because it would require intercepting an int meant to be a char, and converting it back. So instead, you end up with:
-- what we would like to see:
WHERE substr(x."SomeString", 1, 1) = 'T'
-- what we actually see:
WHERE substr(x."SomeString", 1, 1) = 84
-- note that this actually throws a syntax error
-- => 'operator does not exist: text = integer'To avoid the operator error, we generate:
WHERE ascii(substr(x."SomeString", 1, 1)) = 84And if the comparison is to a string, it expands to this:
src.Where(x => x.SomeString[0].ToString() == "T")WHERE CAST(ascii(substr(x."SomeString", 1, 1)) AS text) = 'T'All of which is a very complicated way of indexing a string.
Proposal
I'm weighing the possibility of using get_byte instead of substr for string indexing operations.
It would be a nice parallel to byte[] which maps to bytea with indexing mapped to get_byte. This would then reserve translations of substr for cases of actual string manipulation.
It would still involve a cast of text -> bytea, but its zero-indexed and doesn't need a length parameter.
-- current
WHERE ascii(substr(x."SomeString", 1, 1)) = 84
-- proposed
WHERE get_byte(x."SomeString"::bytea, 0) = 84
WHERE get_byte(CAST(x."SomeString" AS bytea), 0) = 84Currently, we use CAST(value AS type) syntax, but this could be a special case where we use the shorthand value::type instead. (These shorthand casts are achievable with a CustomUnaryExpression configured as a postfix operator.)
Overall, this is a matter of style and clarity. Wrapping substr in ascii seems to work fine, but it makes for some busy-looking SQL.
Activity
roji commentedon Jun 10, 2018
The
ascii(substr())translation you mention as "current" isn't implemented in the provider, right? I mean, it's something you're working with in your branch as opposed to current EF Core behavior? Just asking because I don't recall ever implementing it.Interesting about the expression generated for string character indexing, I'm also surprised by it. although your
get_byte()proposal above does seem slightly more readable (but not by much!), I think we'd better avoid it for efficiency reasons. The cast tobytealooks to me like it could be bad - you're probably asking PostgreSQL to copy the entire string in memory, where thesubstr+asciisolution doesn't. So to be safe I'd go withsubstr.roji commentedon Jun 17, 2018
@austindrenski anything else we should discuss here or should this issue be closed?
austindrenski commentedon Jun 17, 2018
@roji Apologies for letting this sit for a week—was tied up on another project.
Oops, I should have worded that better. Yes, I meant "currently implemented in my working tree".
TBH, after a few days away, I agree. The readability would still be lackluster.
Might just be better to see what can be done on the C# expression tree side (e.g. find
char -> bytecases and cast back tocharas appropriate).Ah. I hadn't considered a server-side copy.
That works for me. Appreciate the advice!