Query precompiler aliases Skip and Take into a single SQL parameter
Summary
With precompiled queries, .Skip(skip).Take(take) can produce SQL that uses one parameter for both LIMIT and OFFSET. The interceptor still extracts two values, but the SQL references only one of them, so the query pages wrongly.
Repro
var page = ctx.Set<E>().OrderBy(e => e.Id).Skip(skip).Take(take).ToList();
With skip = 0, take = 3, precompiled and published NativeAOT:
- Expected (JIT): 3 rows
- Actual (precompiled): 0 rows;
LIMITgets the skip value
Analysis
src/EFCore.Design/Query/Internal/PrecompiledQueryCodeGenerator.cs:165-189: before compiling, the precompiler evaluates the query with dummy parameter values. Both dummies here are 0, so the two values are deduplicated into one SQL parameter.
Workaround
Give the generator distinct dummy values. For example, pass Take(Math.Max(1, take)) where take is already validated to be at least 1, which is an identity on valid input.
Other shapes that capture two values with equal dummy values may alias the same way.
Possibly related: #35503.
Environment
- EF Core 10.0.7 (
Microsoft.EntityFrameworkCore.Sqlite10.0.7,Microsoft.EntityFrameworkCore.Tasks10.0.7) - .NET SDK 10.0.403,
PublishAot=true,EFOptimizeContext=true,EFPrecompileQueriesStage=build, win-x64 - Provider: SQLite
- Source references below are to the
v10.0.7tag.
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
- https://github.com/dotnet/efcore/issues/35968