Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal;
Expand Down Expand Up @@ -386,13 +387,13 @@ public NpgsqlDateTimeMethodTranslator(
// For non-constants, we can't parameterize INTERVAL '1 days'. Instead, we use CAST($1 || ' days' AS interval).
// Note that a make_interval() function also exists, but accepts only int (for all fields except for
// seconds), so we don't use it.
// Note: we instantiate SqlBinaryExpression manually rather than via sqlExpressionFactory because
// of the non-standard Add expression (concatenate int with text)
// Use a PostgreSQL binary expression so nulls propagate, rather than being coalesced to empty strings as in C# concatenation.
// See https://github.com/npgsql/efcore.pg/issues/2889.
return _sqlExpressionFactory.Convert(
new SqlBinaryExpression(
ExpressionType.Add,
new PgUnknownBinaryExpression(
_sqlExpressionFactory.Convert(intervalNum, typeof(string), _textMapping),
_sqlExpressionFactory.Constant(' ' + datePart, _textMapping),
"||",
typeof(string),
_textMapping),
typeof(TimeSpan),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ await AssertQuery(
"""
@years='2'

SELECT o."OrderDate" + CAST(@years::text || ' years' AS interval) AS "OrderDate"
SELECT o."OrderDate" + CAST((@years::text || ' years') AS interval) AS "OrderDate"
FROM "Orders" AS o
WHERE o."OrderDate" IS NOT NULL
""");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.EntityFrameworkCore.TestModels.BasicTypesModel;

namespace Microsoft.EntityFrameworkCore.Query.Translations.Temporal;

public class DateOnlyTranslationsNpgsqlTest : DateOnlyTranslationsTestBase<BasicTypesQueryNpgsqlFixture>
Expand Down Expand Up @@ -105,6 +107,38 @@ WHERE CAST(b."DateOnly" + INTERVAL '3 months' AS date) = DATE '1991-02-10'
""");
}

[ConditionalTheory]
[InlineData(-1, null)]
[InlineData(0, 0)]
[InlineData(1, 8)]
public virtual async Task Add_with_nullable_argument(int id, int? amount)
{
await using var context = Fixture.CreateContext();
var date = Fixture.GetExpectedData().Set<NullableBasicTypesEntity>().Single(b => b.Id == id).DateOnly;

var result = await context.Set<NullableBasicTypesEntity>()
.Where(b => b.Id == id)
.Select(b => new
{
Years = (int?)b.DateOnly!.Value.AddYears(b.Int!.Value).Year,
Months = (int?)b.DateOnly!.Value.AddMonths(b.Int!.Value).Month
})
.SingleAsync();

Assert.Equal(amount.HasValue ? date?.AddYears(amount.Value).Year : null, result.Years);
Assert.Equal(amount.HasValue ? date?.AddMonths(amount.Value).Month : null, result.Months);

AssertSql(
$"""
@id='{id}'

SELECT date_part('year', CAST(n."DateOnly" + CAST((n."Int"::text || ' years') AS interval) AS date))::int AS "Years", date_part('month', CAST(n."DateOnly" + CAST((n."Int"::text || ' months') AS interval) AS date))::int AS "Months"
FROM "NullableBasicTypesEntities" AS n
WHERE n."Id" = @id
LIMIT 2
""");
}

public override async Task AddDays()
{
await base.AddDays();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,35 @@ WHERE date_part('year', (b."DateTime" + INTERVAL '1 years') AT TIME ZONE 'UTC'):
""");
}

[ConditionalTheory]
[InlineData(-1, null)]
[InlineData(0, 0)]
[InlineData(1, 8)]
[InlineData(4, -9)]
public virtual async Task AddDays_with_nullable_argument(int id, int? days)
{
await using var context = Fixture.CreateContext();
var date = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);

var result = await context.Set<NullableBasicTypesEntity>()
.Where(b => b.Id == id)
.Select(b => (DateTime?)date.AddDays(b.Int!.Value))
.SingleAsync();

Assert.Equal(days.HasValue ? date.AddDays(days.Value) : (DateTime?)null, result);

AssertSql(
$"""
@date='2000-01-01T00:00:00.0000000Z' (DbType = DateTime)
@id='{id}'

SELECT @date + CAST((n."Int"::double precision::text || ' days') AS interval)
FROM "NullableBasicTypesEntities" AS n
WHERE n."Id" = @id
LIMIT 2
""");
}

public override async Task Year()
{
await base.Year();
Expand Down
Loading