efcore.pg: List Contains as part of IQuerable fails in Redshift

It appears that a query with a list that should generate an In or Any statement is failing. I am using redshift but I don;t even think it hits the server. It is failing inside the compiler. The error I get is : ‘22P03: invalid array flags’

Example EF IQueryable statement:

public async Task<List<Domain.Entities.Security>> GetSecuritiesByCodes(List<int> securityIds)
    {
        var securityModels = await _context.Securities
            .Where(w => w.IsDeleted == false && securityIds.Contains(w.Id))
            .ToListAsync();
    
        return securityModels;
    }

SQL generated by EF IQueryable statement

SELECT s."Id", s."Code", s."IsDeleted", s."Name", s."PrimaryBenchmarkProductId", s."SecurityTypeId"
FROM performance."Securities" AS s
WHERE NOT (s."IsDeleted") AND s."Id" = ANY ($1)

I can’t determine what $1 gets compiled as but it doesn’t seem to hit the server. If you substitute $1 with ‘{1,2,3,4,5,6,7,8,9,10,11}’ then the query works fine and return the right results. Here is a snapshot of the params. image

Redshift Table Definition

CREATE TABLE performance.securities (
id int4 NOT NULL,
code varchar(256) NOT NULL,
"name" varchar(256) NULL,
securitytypeid int4 NULL,
primarybenchmarkproductid int4 NULL,
isdeleted bool NOT NULL DEFAULT 0
)

Connection String “Host=.ap-southeast-2.redshift.amazonaws.com;Port=5439;Database=dev;Username=awsuser;Password=;Server Compatibility Mode=Redshift;”,

I am using EF core 5.0.2 and Npgssql 5.0.2

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@sep2 that is not what I’m seeing with the code sample below: without Redshift, the provider generates an ANY expression from the LINQ query. Please open a new issue with a runnable code sample which demonstrates the issue.

Attempted repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

var guids = new Guid[]
{
    new("ab02c141-7706-4570-8893-ae60fa741f81"),
    new("0e89bc4e-63dd-4cb4-be17-7108d3e7a90b")
};
_ = await ctx.Blogs.Where(b => guids.Contains(b.Guid)).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();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

public class Blog
{
    public int Id { get; set; }
    public Guid Guid { get; set; }
}

@roji I can confirm that the issue has been fixed, although the fix is quite a heavy one as it involves upgrading to dotnet5, upgrading all the ef core resources to ef core 6 daily build versions, but that should all resolve itself over time. Thanks again for your help with this.

Matt

That’s impressive @roji . Very much appreciated. I’ll report back if I find any issues with the new build but many thanks again!