Skip to content

Commit

Permalink
Fix operator precedence of AT TIME ZONE (#2987)
Browse files Browse the repository at this point in the history
Fixes #2980

(cherry picked from commit 8402038)
  • Loading branch information
roji committed Feb 21, 2024
1 parent 468c8be commit 1e6c725
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ protected override bool TryGetOperatorInfo(SqlExpression expression, out int pre
PgBinaryExpression => (1000, false),

CollateExpression => (1000, false),
AtTimeZoneExpression => (1000, false),
AtTimeZoneExpression => (1100, false),
InExpression => (900, false),
PgJsonTraversalExpression => (1000, false),
PgArrayIndexExpression => (1500, false),
Expand Down
35 changes: 35 additions & 0 deletions test/EFCore.PG.FunctionalTests/Query/OperatorsQueryNpgsqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,41 @@ LIMIT 2
""");
}

[ConditionalFact]
public virtual async Task AtTimeZone_and_addition()
{
var contextFactory = await InitializeAsync<OperatorsContext>(
seed: context =>
{
context.Set<OperatorEntityDateTime>().AddRange(
new OperatorEntityDateTime { Id = 1, Value = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc) },
new OperatorEntityDateTime { Id = 2, Value = new DateTime(2020, 2, 1, 0, 0, 0, DateTimeKind.Utc) });
context.SaveChanges();
},
onModelCreating: modelBuilder => modelBuilder.Entity<OperatorEntityDateTime>().Property(x => x.Id).ValueGeneratedNever());

await using var context = contextFactory.CreateContext();

var result = await context.Set<OperatorEntityDateTime>()
.Where(b => new DateOnly(2020, 1, 15) > DateOnly.FromDateTime(b.Value.AddDays(1)))
.SingleAsync();

Assert.Equal(1, result.Id);

AssertSql(
"""
SELECT o."Id", o."Value"
FROM "OperatorEntityDateTime" AS o
WHERE DATE '2020-01-15' > CAST((o."Value" + INTERVAL '1 days') AT TIME ZONE 'UTC' AS date)
LIMIT 2
""");
}

public class OperatorEntityDateTime : OperatorEntityBase
{
public DateTime Value { get; set; }
}

protected override void Seed(OperatorsContext ctx)
{
ctx.Set<OperatorEntityString>().AddRange(ExpectedData.OperatorEntitiesString);
Expand Down

0 comments on commit 1e6c725

Please sign in to comment.