`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by field
Maintainer thường phản hồi trong vòng 1 ngày
Đá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
- 70/100
Hướng nghiên cứu
Các bản dịch nằm trong src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlTimeSpanMemberTranslator.cs (lines 47-53, 67-75) và src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeMemberTranslatorPlugin.cs (lines 119-122, 234). Thay floor bằng trunc và bọc các khoảng thời gian đã tính toán trong justify_interval() trước khi đọc các trường. Xác nhận với repro của issue rằng các span âm khớp với .NET và các khoảng thời gian được chuẩn hóa.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
| Environment | |
|---|---|
| PostgreSQL | 18.6 (postgres:18), session TimeZone Etc/UTC |
| .NET 10 | runtime 10.0.11, Npgsql.EntityFrameworkCore.PostgreSQL(.NodaTime) 10.0.3, EF Core 10.0.4 |
| .NET 11 | runtime 11.0.0-rc.2, Npgsql.EntityFrameworkCore.PostgreSQL(.NodaTime) 11.0.0-rc.2, EF Core 11.0.0-rc.2 |
| Also | Npgsql 10.0.3 on both lines, NodaTime 3.2.2, macOS arm64 |
Both lines print the same output. main at 150011d has the same code.
.NET and NodaTime truncate the components of a negative span toward zero. The translations floor them. And an interval computed in SQL keeps its days and time fields apart (1 day -02:00:00), so .Days and .Hours read those raw fields.
| LINQ | Value | .NET | PostgreSQL | SQL |
|---|---|---|---|---|
r.Span.Seconds |
TimeSpan −1.5 s |
−1 | −2 | floor(date_part('second', r."Span"))::int |
r.Duration.Seconds |
Duration −1.5 s |
−1 | −2 | floor(date_part('second', r."Duration"))::int |
(r.SpanA + r.SpanB).Days / .Hours |
1 day + (−2 h) | 0 / 22 | 1 / −2 | floor(date_part('day', r."SpanA" + r."SpanB"))::int |
(r.DurationA + r.DurationB).Days / .Hours |
1 day + (−1 h) | 0 / 23 | 1 / −1 | date_part('day', r."DurationA" + r."DurationB")::int |
Repro
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NodaTime;
await using var context = new TestContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Rows.Add(new Row
{
Span = TimeSpan.FromMilliseconds(-1500),
Duration = Duration.FromMilliseconds(-1500),
SpanA = TimeSpan.FromDays(1), SpanB = TimeSpan.FromHours(-2),
DurationA = Duration.FromDays(1), DurationB = Duration.FromHours(-1),
});
await context.SaveChangesAsync();
var row = await context.Rows.AsNoTracking().SingleAsync();
// 1. .NET and NodaTime truncate a negative component toward zero; the translation floors it
Console.WriteLine($"TimeSpan(-1.5 s).Seconds: .NET {row.Span.Seconds}, database {await context.Rows.Select(r => r.Span.Seconds).SingleAsync()}");
Console.WriteLine($"Duration(-1.5 s).Seconds: .NET {row.Duration.Seconds}, database {await context.Rows.Select(r => r.Duration.Seconds).SingleAsync()}");
// 2. An interval computed in SQL keeps its days and time fields apart (1 day + -2 h is '1 day -02:00:00')
Console.WriteLine($"(1 day + -2 h).Days/.Hours as TimeSpan: .NET {(row.SpanA + row.SpanB).Days}/{(row.SpanA + row.SpanB).Hours}, database {await context.Rows.Select(r => (r.SpanA + r.SpanB).Days).SingleAsync()}/{await context.Rows.Select(r => (r.SpanA + r.SpanB).Hours).SingleAsync()}");
Console.WriteLine($"(1 day + -1 h).Days/.Hours as Duration: .NET {(row.DurationA + row.DurationB).Days}/{(row.DurationA + row.DurationB).Hours}, database {await context.Rows.Select(r => (r.DurationA + r.DurationB).Days).SingleAsync()}/{await context.Rows.Select(r => (r.DurationA + r.DurationB).Hours).SingleAsync()}");
public class TestContext : DbContext
{
public DbSet<Row> Rows => Set<Row>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"), o => o.UseNodaTime())
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Row
{
public int Id { get; set; }
public TimeSpan Span { get; set; }
public Duration Duration { get; set; }
public TimeSpan SpanA { get; set; }
public TimeSpan SpanB { get; set; }
public Duration DurationA { get; set; }
public Duration DurationB { get; set; }
}
Output, the same on both lines:
TimeSpan(-1.5 s).Seconds: .NET -1, database -2
Duration(-1.5 s).Seconds: .NET -1, database -2
(1 day + -2 h).Days/.Hours as TimeSpan: .NET 0/22, database 1/-2
(1 day + -1 h).Days/.Hours as Duration: .NET 0/23, database 1/-1
TimeSpan: NpgsqlTimeSpanMemberTranslator.cs#L47-L53, withFloorat NpgsqlTimeSpanMemberTranslator.cs#L67-L75.Millisecondsis affected the same way.- NodaTime:
Durationat NpgsqlNodaTimeMemberTranslatorPlugin.cs#L119-L122, andPeriod.Secondsat NpgsqlNodaTimeMemberTranslatorPlugin.cs#L234.
trunc instead of floor matches .NET for negative values. justify_interval() normalizes a computed interval before its fields are read.
- Ngôn ngữ chính
- C#
- Star
- 1.8k
- Fork
- 268
- Merge trung bình
- 2 giờ 35 phút
- Pull request đã merge (30 ngày)
- 136
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của npgsql/efcore.pg
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSONCó thể đã có người làm @Ahmed7991 đã nhận 5 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
npgsql/efcore.pg#4093 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01Có thể đã có người làm @Sorbitol đã nhận 12 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 30/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never matchCó thể đã có người làm @Mr444-dev đã nhận 7 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
npgsql/efcore.pg#4099 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Array translations: chained `Skip`/`Take` return an empty array, `int.MaxValue` bounds overflow, and `Intersect().Any()` ignores NULL elementsCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của npgsql/efcore.pg
Issue tương tự
-
Issue-Bug Needs-Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
JiaFeiMiao-K-Cat/DiscImageStudio#281 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area/access-management Frontend onboarding candidate :handshake:
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Altinn/altinn-auth#4941 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
aws/aws-sdk-net#4586 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Kryptos-FR/MarkView.Avalonia#269 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày