Array translations: chained `Skip`/`Take` return an empty array, `int.MaxValue` bounds overflow, and `Intersect().Any()` ignores NULL elements
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
- #4100 de @CaffeinatedCoder — fechado sem integrar
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 30/100
Direção de pesquisa
Start from src/EFCore.PG/Query/Internal/NpgsqlQueryableMethodTranslatingExpressionVisitor.cs, reading lines 1031-1073 (Skip), 1094-1108 (Take), 1544-1547 (bounds) and 665-668 (Intersect). Run the C# repro in the issue to see the three mismatches. Done means generated SQL uses parenthesized slices, checked bounds, and NULL-safe Intersect so database counts match .NET.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
| Environment | |
|---|---|
| PostgreSQL | 18.6 (postgres:18), session TimeZone Etc/UTC |
| .NET 10 | runtime 10.0.11, Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3, EF Core 10.0.4 |
| .NET 11 | runtime 11.0.0-rc.2, Npgsql.EntityFrameworkCore.PostgreSQL 11.0.0-rc.2, EF Core 11.0.0-rc.2 |
| Also | Npgsql 10.0.3 on both lines, macOS arm64 |
Both lines print the same output. main at 150011d has the same code.
| LINQ | Row | .NET | PostgreSQL | SQL |
|---|---|---|---|---|
r.Ints.Take(2).Skip(1).Count() |
{1,2,3} |
1 | 0 | cardinality(r."Ints"[:2][2:]) |
r.Ints.Skip(int.MaxValue).Count() |
{1,2,3} |
0 | 3 | cardinality(r."Ints"[-2147483648:]) |
r.A.Intersect(r.B).Any() |
{1,NULL,3}, {NULL,5} |
true | false | r."A" && r."B" |
Repro
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
await using var context = new TestContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Rows.Add(new Row { Ints = [1, 2, 3], A = [1, null, 3], B = [null, 5] });
await context.SaveChangesAsync();
var row = await context.Rows.AsNoTracking().SingleAsync();
// 1. a[:2][2:] is a two-dimensional slice of a one-dimensional array in PostgreSQL
Console.WriteLine($"Ints.Take(2).Skip(1).Count(): .NET {row.Ints.Take(2).Skip(1).Count()}, database {await context.Rows.Select(r => r.Ints.Take(2).Skip(1).Count()).SingleAsync()}");
// 2. int.MaxValue + 1 overflows into the slice bound
Console.WriteLine($"Ints.Skip(int.MaxValue).Count(): .NET {row.Ints.Skip(int.MaxValue).Count()}, database {await context.Rows.Select(r => r.Ints.Skip(int.MaxValue).Count()).SingleAsync()}");
// 3. && never matches NULL elements; Intersect does
Console.WriteLine($"A.Intersect(B).Any() with a NULL in both: .NET {row.A.Intersect(row.B).Any()}, database {await context.Rows.Select(r => r.A.Intersect(r.B).Any()).SingleAsync()}");
public class TestContext : DbContext
{
public DbSet<Row> Rows => Set<Row>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"))
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Row
{
public int Id { get; set; }
public int[] Ints { get; set; } = [];
public int?[] A { get; set; } = [];
public int?[] B { get; set; } = [];
}
Output, the same on both lines:
Ints.Take(2).Skip(1).Count(): .NET 1, database 0
Ints.Skip(int.MaxValue).Count(): .NET 0, database 3
A.Intersect(B).Any() with a NULL in both: .NET True, database False
- Chained slices. PostgreSQL reads
a[:2][2:]as a two-dimensional slice of a one-dimensional array, which is empty.(a[:2])[2:]is{2}.TranslateTakemerges into an existing slice (NpgsqlQueryableMethodTranslatingExpressionVisitor.cs#L1094-L1108), butTranslateSkipwraps it (NpgsqlQueryableMethodTranslatingExpressionVisitor.cs#L1031-L1073), and the SQL generator doesn't parenthesize a slice inside a slice. - Overflow. The constant bounds are computed with unchecked
intarithmetic:Convert.ToInt32(constant.Value) + 1(NpgsqlQueryableMethodTranslatingExpressionVisitor.cs#L1544-L1547) andlowerBoundValue + takeCount - 1(NpgsqlQueryableMethodTranslatingExpressionVisitor.cs#L1106). Intersect().Any()becomes&&, which never matches NULL elements. The comment at NpgsqlQueryableMethodTranslatingExpressionVisitor.cs#L665-L668 says that's why it has to be guarded, but column arrays with nullable elements still take the&&path.
- Linguagem predominante
- C#
- Estrelas
- 1.8k
- Forks
- 268
- Merge médio
- 2h 35min
- PRs com merge (30d)
- 136
Preparar o ambiente
Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de npgsql/efcore.pg
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 82/100
Mantenedores costumam responder em até 1 dia
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSONTalvez já em andamento @Ahmed7991 assumiu há 5 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
npgsql/efcore.pg#4093 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by fieldTalvez já em andamento @jeremy-morren assumiu há 11 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
Mantenedores costumam responder em até 1 dia
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01Talvez já em andamento @Sorbitol assumiu há 12 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 30/100
Mantenedores costumam responder em até 1 dia
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never matchTalvez já em andamento @Mr444-dev assumiu há 7 dias. Aberta
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 45/100
npgsql/efcore.pg#4099 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
Todas as issues de npgsql/efcore.pg
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 66/100
sebastienros/fluid#1273 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 82/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
Mantenedores costumam responder em até 1 dia
-
[Feature]: Add Ukrainian (Українська) as a target language for the Translator (Ollama) engineAbertaenhancement
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 73/100
KnifeLemon/Filee#1344 ·
Mantenedores costumam responder em até 1 dia
-
Issue-Bug Needs-Triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
Mantenedores costumam responder em até 2 dias