Skip to content

Array indexing over SqlExpression not translated #3383

Closed

Description

@kyberias

Using Npgsql.EntityFrameworkCore.PostgreSQL 9.0.1.

I have the following code:

context.MyTable.Select(m => (m.FolderPath != null && m.FolderPath != "") ? EF.Functions.StringToArray(m.FolderPath, "/")[0] : null)
            .Distinct()
            .ToArray();

This gets translated into the following SQL:

SELECT DISTINCT m.folder_path IS NOT NULL AND m.folder_path <> '', string_to_array(m.folder_path, '/')
      FROM MyTable AS m
      WHERE m.storage = @__storage_0

I'm expecting the Select() to return the first element of the split string and the Distinct() to process that but the translated SQL seems to omit the array index [0] altogether and DISTINCT targets the array. This seems incorrect.

Is this a bug and/or is there a way to circumvent it?

Activity

  1. roji commented on Nov 20, 2024

    @roji
    Member

    When trying to repro the above, I'm seeing a translation failure exception rather than the indexing getting omitted. Can you please look at the code below and tweak it to show the omission, or submit a similar repro?

    Attempted repro
    await using var context = new BlogContext();
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();
    
    _ = await context.Blogs.Where(b => EF.Functions.StringToArray(b.Name, "/")[0] == "foo").ToListAsync();
    
    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseNpgsql("Host=localhost;Username=test;Password=test")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
  2. self-assigned this
    on Nov 20, 2024
  3. changed the title [-]Array indexing not translated[/-] [+]Array indexing over SqlExpression not translated[/+] on Nov 20, 2024
  4. kyberias commented on Nov 20, 2024

    @kyberias
    Author

    Here is a complete repro. Note the usage of Select() and Distinct() and the comments.

    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Logging;
    
    await using var context = new BlogContext();
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();
    
    context.Blogs.Add(new Blog
    {
        Name = "a/b/c"
    });
    context.Blogs.Add(new Blog
    {
        Name = "a/c/d"
    });
    await context.SaveChangesAsync();
    
    var a = await context.Blogs.Select(b => EF.Functions.StringToArray(b.Name, "/")[0]).Distinct().ToListAsync();
    
    // Expect to see a single 'a' (due to Distinct()), but the result set has two 'a's
    // SQL executed seems to perform DISTINCT on the whole array, not for first elements of the array.
    
    foreach (var str in a)
    {
        Console.WriteLine(str);
    }
    
    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseNpgsql("Host=localhost;Username=postgres;Password=foobar;Database=BlogTest")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
  5. added theissue type on Jun 14, 2025
  6. added this to the 10.0.1 milestone on Feb 7, 2026
  7. added a commit that references this issue on Feb 7, 2026
    5604097
  8. added a commit that references this issue on Feb 7, 2026
    253d4ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions