efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Query precompiler aliases Skip and Take into a single SQL parameter

Open rockyway opened this issue 1 month ago • 1 comments

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; LIMIT gets 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.Sqlite 10.0.7, Microsoft.EntityFrameworkCore.Tasks 10.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.7 tag.

rockyway avatar Oct 27 '26 13:10 rockyway

I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  • https://github.com/dotnet/efcore/issues/35968

MihuBot avatar Oct 27 '26 13:10 MihuBot