Skip to content

= ANY with an AND/OR item omits parentheses, returning wrong results #4122

Description

@jmjrawlings

VisitArrayAny writes the item without parentheses. When the item is an AND/OR expression, = binds tighter than AND, so Postgres applies = ANY to the last operand only. The query runs without error and returns the wrong rows.

bool[] availability = [false];

var unavailable = db.Products
    .Where(p => availability.Contains(p.IsListed && p.InStock));
SQL Result
Expected WHERE (p."IsListed" AND p."InStock") = ANY (@availability) Banana, Cherry, Date
Actual WHERE p."IsListed" AND p."InStock" = ANY (@availability) Banana

Seed: Apple listed and in stock, Banana listed and out of stock, Cherry not listed and in stock, Date neither.

Full repro
using Microsoft.EntityFrameworkCore;

await using var db = new ShopContext();
await db.Database.EnsureDeletedAsync();
await db.Database.EnsureCreatedAsync();

db.Products.AddRange(
    new Product { Name = "Apple", IsListed = true, InStock = true },
    new Product { Name = "Banana", IsListed = true, InStock = false },
    new Product { Name = "Cherry", IsListed = false, InStock = true },
    new Product { Name = "Date", IsListed = false, InStock = false });
await db.SaveChangesAsync();

bool[] availability = [false];

var query = db.Products
    .Where(p => availability.Contains(p.IsListed && p.InStock))
    .Select(p => p.Name);

Console.WriteLine(query.ToQueryString());
Console.WriteLine(string.Join(", ", await query.ToListAsync()));

public class ShopContext : DbContext
{
    public DbSet<Product> Products => Set<Product>();

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseNpgsql("Host=localhost;Username=postgres;Password=postgres;Database=any_repro");
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public bool IsListed { get; set; }
    public bool InStock { get; set; }
}

Cause: NpgsqlQuerySqlGenerator.cs#L890 calls Visit(expression.Item) with no precedence check. The precedence table already ranks PgAnyExpression at 800 and AndAlso/OrElse at 500 (L1636).

Suggested fix, the same pattern as npgsql/npgsql#3927:

var requiresParentheses = RequiresParentheses(expression, expression.Item);
if (requiresParentheses)
{
    Sql.Append("(");
}

Visit(expression.Item);

if (requiresParentheses)
{
    Sql.Append(")");
}

Workaround: with a single picked value, filter on the expression or its negation (p.IsListed && p.InStock or !(p.IsListed && p.InStock)) in place of Contains.

Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3, PostgreSQL 17. Happy to send a PR if that helps.

Activity

  1. transferred this issue fromnpgsql/npgsqlon Nov 10, 2026
  2. added theissue type on Nov 17, 2026
  3. added this to the 10.0.4 milestone on Nov 17, 2026
  4. added a commit that references this issue on Nov 17, 2026
    e90ebc6
  5. added a commit that references this issue on Nov 20, 2026
    6135755
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

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions