Test bug: Include_collection_with_cross_apply_with_filter relies on nondeterministic ORDER BY with Take

Đang mở Phù hợp với người mới
#39,848 1 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 2 ngày

@Mohamedadel-prog đang làm issue này rồi.

Từ ngày 29/11/2026.

Đá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
66/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
Lĩnh vực
databases, testing

Hướng nghiên cứu

Đọc test/EFCore.Specification.Tests/Query/NorthwindIncludeQueryTestBase.cs, đặc biệt là Include_collection_with_cross_apply_with_filter và các biến thể outer_apply liền kề. Kiểm tra cách các bài kiểm thử này xác nhận các collection được include và xác định liệu có thể thêm thứ tự duy nhất hoặc làm cho assertion không phụ thuộc vào thứ tự hay không. Hoàn thành khi các bài kiểm thử không còn phụ thuộc vào kết quả đồng hạng của ORDER BY; chạy các bài kiểm thử truy vấn liên quan để xác minh.

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

Mô tả

Description

NorthwindIncludeQueryTestBase.Include_collection_with_cross_apply_with_filter (and the outer_apply variants next to it) builds the query as:

from c in ss.Set<Customer>().Include(c => c.Orders)
from o in ss.Set<Order>().Where(o => o.CustomerID == c.CustomerID)
                        .OrderBy(o => c.CustomerID).Take(5)
where c.CustomerID.StartsWith("F")
select c

c.CustomerID is constant inside the correlated subquery. Therefore ORDER BY c.CustomerID does not define a unique order of Orders, while Take(5) depends on which five rows the relational provider returns. LINQ-to-Objects uses a stable sort and picks the first five source rows; SQL does not guarantee that.

Source: https://github.com/dotnet/efcore/blob/v9.0.14/test/EFCore.Specification.Tests/Query/NorthwindIncludeQueryTestBase.cs#L315-L346

Why this is not a provider requirement

SQL-92 §13.1, General Rules, item 2:

If an <order by clause> is not specified, then the table specified by the <cursor specification> is T and the ordering of rows in T is implementation-dependent.

SQL-92, Implementation-defined elements, item 14:

If the <declare cursor> does not include an <order by clause>, or includes an <order by clause> that does not specify the order of the rows completely, then the rows of the table have an order that is defined only to the extent that the <order by clause> specifies an order and is otherwise implementation-dependent.

PostgreSQL documentation (SELECT, ORDER BY / LIMIT):

If they are equal according to all specified expressions, they are returned in an implementation-dependent order.

It is even possible for repeated executions of the same LIMIT query to return different subsets of the rows of a table, if there is not an ORDER BY to enforce selection of a deterministic subset. Again, this is not a bug; determinism of the results is simply not guaranteed in such a case.

Codd's relational model defines a relation as a set of n-tuples; a set has no intrinsic row order, and Codd explicitly lists ordering dependence as something applications must not rely on.

Observed effect

Against a relational provider whose engine does not guarantee stable ties, the first query can pick an arbitrary set of five OrderIDs. In split-query mode the included Orders collection is then not attached to the expected parent rows, and AssertIncludeCollection fails with e.g. Expected: 5, Actual: 0. The same query can also answer differently between executions.

Suggested fix

Make the test query deterministic by adding a unique tiebreaker:

.OrderBy(o => c.CustomerID).ThenBy(o => o.OrderID).Take(5)

or assert the included collection in an order-insensitive way. The test should not require a relational engine to reproduce LINQ-to-Objects' stable tie ordering.

This is a test-specification issue, not a requirement for providers to emulate LINQ stable sort.

Ngôn ngữ chính
C#
Star
14.8k
Fork
3.5k
Merge trung bình
2 ngày 22 giờ
Pull request đã merge (30 ngày)
158

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

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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 dotnet/efcore

Tất cả issue của dotnet/efcore

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.