question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List Contains as part of IQuerable fails in Redshift

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Mar 13, 2022

@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; }
}
1reaction
mattdone01commented, Apr 27, 2021

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

IQuerable .Contains on Array type not working #1278
Hi, We are currently upgrading from .Net Core 2.1 to 3.1 and as part of EF Core 3.1 they have introduced an exception...
Read more >
AWS Amazon Redshift driver bug with lists in clause IN
I'm using spring NamedParameterJdbcDaoSupport class with Redshift and I have a bug/crash when parameters are of type List/Set and have no ...
Read more >
Nested data limitations - Amazon Redshift
A subquery that accesses nested data anywhere other than the FROM clause must return a single value. The only exceptions are (NOT) EXISTS...
Read more >
[Solved]-Filtering IQueryable sub list-Entity Framework
this way you'll have both subitems and items where your criteria is satisfied. ...if you're working with an ienumerable you can do: ienumerable...
Read more >
Insight into Redshift Spectrum query error
Discovered the issue: error is happening because I had more than one type of file (i.e., files of differing layouts) in the same...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found