NorthwindSplitIncludeQuery test depend on specific implicit ordering and are hard to fix #26715
Labels
area-groupby
area-query
area-test
customer-reported
punted-for-7.0
Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints.
type-bug
Milestone
@roji The issues can be found in the NorthwindSplitIncludeQueryMySqlTest.cs file.
Its test cases will run fine on MySQL 8 without having to override anything from the base class, because MySQL 8 implicitly sorts/returns the
OrderDetails
entities as SQL Server (and Postgres) does.But MariaDB does not.
Let's look at the
Include_collection_SelectMany_GroupBy_Select
test case for example.LINQ query
Generated SQL (SQL Server)
Generated SQL (MySQL/MariaDB)
The generated SQL queries for MSSQL and MySQL (and MariaDB) are the same.
Let's only look at the first one of the two generated SQL queries, and its most inner subquery:
The LINQ query uses
.Select(e => e.OrderBy(o => o.OrderID).FirstOrDefault())
.This is being translated into a
ROW_NUMBER() OVER(PARTITION BY `o1`.`OrderID` ORDER BY `o1`.`OrderID`) AS `row`
clause, so that theFirstOrDefault()
part of the LINQ query can later use therow
column to figure out what the first record (with theWHERE `t1`.`row` <= 1
clause).The issue is, that
ORDER BY `o1`.`OrderID`
is not ordered by enough columns to be deterministic. Let's take a look at the first row returned for this inner most subquery for MySQL and MariaDB respectively:MySQL
MariaDB
MariaDB is highly optimized and if you don't order something explicitly, you might not just get an unordered result, but it could even be in a non-deterministic order (over multiple runs).
To make the query deterministic, it actually needs to be
ROW_NUMBER() OVER(PARTITION BY `o1`.`OrderID` ORDER BY `o1`.`OrderID`, `o2`.`OrderID`, `o2`.`ProductID`) AS `row`
.And ordering the
Orders.OrderDetails
collection in the LINQ query is tricky.Originally posted by @lauxjpn in PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1553 (comment)
Include provider and version information
EF Core version: 6.0.0
The text was updated successfully, but these errors were encountered: