Skip to content

String indexing translation #450

Closed
Closed

Description

@austindrenski

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)) = 84

And 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) = 84

Currently, 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

  1. roji commented on Jun 10, 2018

    @roji
    Member

    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 to bytea looks to me like it could be bad - you're probably asking PostgreSQL to copy the entire string in memory, where the substr+ascii solution doesn't. So to be safe I'd go with substr.

  2. roji commented on Jun 17, 2018

    @roji
    Member

    @austindrenski anything else we should discuss here or should this issue be closed?

  3. austindrenski commented on Jun 17, 2018

    @austindrenski
    ContributorAuthor

    @roji Apologies for letting this sit for a week—was tied up on another project.

    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.

    Oops, I should have worded that better. Yes, I meant "currently implemented in my working tree".

    your get_byte() proposal above does seem slightly more readable (but not by much!)

    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 -> byte cases and cast back to char as appropriate).

    The cast to bytea looks to me like it could be bad - you're probably asking PostgreSQL to copy the entire string in memory, where the substr+ascii solution doesn't.

    Ah. I hadn't considered a server-side copy.

    So to be safe I'd go with substr.

    That works for me. Appreciate the advice!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions