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

Đang mở Phù hợp với người mới
#4,122 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp, postgresql

Hướng nghiên cứu

Bắt đầu tại src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs, ở phần triển khai VisitArrayAny quanh dòng 890, sau đó kiểm tra cách xử lý thứ tự ưu tiên hiện có và các bài kiểm thử tương ứng. Tái hiện sự cố bằng truy vấn Contains được cung cấp và xác minh SQL được tạo nhóm biểu thức AND trước = ANY; thêm hoặc cập nhật một bài kiểm thử hồi quy để xác nhận truy vấn trả về các hàng như mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C#
Star
1.8k
Fork
268
Merge trung bình
2 giờ 35 phút
Pull request đã merge (30 ngày)
136

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của npgsql/efcore.pg

Tất cả issue của npgsql/efcore.pg

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.