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
Expand Up @@ -175,7 +175,10 @@ public NpgsqlGeometryMemberTranslator(
SqlExpression Function(string name, SqlExpression[] arguments, Type returnType, RelationalTypeMapping? typeMapping = null)
=> _sqlExpressionFactory.Function(
name, arguments,
nullable: true, argumentsPropagateNullability: TrueArrays[arguments.Length],
nullable: true,
argumentsPropagateNullability: name is "ST_X" or "ST_Y" or "ST_Z" or "ST_M" or "ST_StartPoint" or "ST_EndPoint"
? [false]
: TrueArrays[arguments.Length],
returnType, typeMapping);

RelationalTypeMapping ResultGeometryMapping()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var t when typeof(Geometry).IsAssignableFrom(t) && instance is not null
"ST_GeometryN",
[collection, OneBased(index)],
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
argumentsPropagateNullability: [false, false],
method.ReturnType,
geometryTypeMapping),

Expand Down Expand Up @@ -260,7 +260,10 @@ var t when typeof(Geometry).IsAssignableFrom(t) && instance is not null
SqlExpression Function(string name, SqlExpression[] arguments, Type returnType, RelationalTypeMapping? typeMapping = null)
=> _sqlExpressionFactory.Function(
name, arguments,
nullable: true, argumentsPropagateNullability: TrueArrays[arguments.Length],
nullable: true,
argumentsPropagateNullability: name is "ST_GeometryN" or "ST_InteriorRingN" or "ST_PointN"
? [false, false]
: TrueArrays[arguments.Length],
returnType, typeMapping);

RelationalTypeMapping ResultGeometryMapping()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ SqlExpression Lower()
"lower",
[instance],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
argumentsPropagateNullability: [false],
typeof(LocalDate),
_dateTypeMapping);

Expand All @@ -217,7 +217,7 @@ SqlExpression Upper()
"upper",
[instance],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
argumentsPropagateNullability: [false],
typeof(LocalDate),
_dateTypeMapping);
}
Expand Down Expand Up @@ -300,7 +300,7 @@ private SqlExpression GetDatePartExpressionDouble(
"date_part",
[_sqlExpressionFactory.Constant(partName), instance],
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
argumentsPropagateNullability: [false, false],
typeof(double));

if (floor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ SqlExpression LocalNow()
"date_part",
[_sqlExpressionFactory.Constant(partName), instance],
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
argumentsPropagateNullability: FalseArrays[2],
Comment thread
roji marked this conversation as resolved.
Resolved
typeof(double));

if (floor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public NpgsqlLTreeTranslator(
"lca",
[arguments[0]],
nullable: true,
TrueArrays[1],
FalseArrays[1],
typeof(LTree),
_ltreeTypeMapping),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public NpgsqlRangeTranslator(
member.Name == nameof(NpgsqlRange<int>.LowerBound) ? "lower" : "upper",
[instance],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
argumentsPropagateNullability: FalseArrays[1],
returnType,
typeMapping);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ PgExpressionType.Contains when Left.TypeMapping is NpgsqlHstoreTypeMapping && Ri

PgExpressionType.HstoreGetValue => "->",

PgExpressionType.ArrayConcat => "||",

_ => throw new ArgumentOutOfRangeException($"Unhandled operator type: {OperatorType}")
})
.Append(" ");
Expand Down
9 changes: 9 additions & 0 deletions src/EFCore.PG/Query/Expressions/PgExpressionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,13 @@ public enum PgExpressionType
HstoreGetValue, // ->

#endregion Hstore

#region Array

/// <summary>
/// Represents a PostgreSQL operator for concatenating arrays.
/// </summary>
ArrayConcat, // ||

#endregion Array
}
3 changes: 3 additions & 0 deletions src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ when binaryExpression.Left.TypeMapping is NpgsqlHstoreTypeMapping

PgExpressionType.HstoreGetValue => "->",

PgExpressionType.ArrayConcat => "||",

PgExpressionType.CubeNthCoordinate => "->",
PgExpressionType.CubeNthCoordinateKnn => "~>",
PgExpressionType.CubeDistanceTaxicab => "<#>",
Expand Down Expand Up @@ -1644,6 +1646,7 @@ ExpressionType.Add when GetOperator(sqlBinaryExpression) == " || " => (1000, tru

// There's an "any other operator" category in the PG operator precedence table, we assign that a numeric value of 1000.
// TODO: Some operators here may be associative
PgBinaryExpression { OperatorType: PgExpressionType.ArrayConcat } => (1000, true),
PgBinaryExpression or PgRegexMatchExpression => (1000, false),

CollateExpression => (1000, false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ protected override ShapedQueryExpression TranslateConcat(ShapedQueryExpression s
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
var tableAlias = ((SelectExpression)source1.QueryExpression).Tables.Single().Alias!;
var selectExpression = new SelectExpression(
[new PgUnnestExpression(tableAlias, _sqlExpressionFactory.Add(array1, array2), "value")],
[new PgUnnestExpression(
tableAlias, _sqlExpressionFactory.MakePostgresBinary(PgExpressionType.ArrayConcat, array1, array2), "value")],
new ColumnExpression("value", tableAlias, projectedColumn1.Type, inferredTypeMapping, projectedColumn1.IsNullable || projectedColumn2.IsNullable),
identifier: [GenerateOrdinalityIdentifier(tableAlias)],
_queryCompilationContext.SqlAliasManager);
Expand Down Expand Up @@ -1203,7 +1204,7 @@ [new PgUnnestExpression(tableAlias, sliceExpression, "value")],
"array_remove",
[array, translatedItemToFilterOut],
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
argumentsPropagateNullability: [true, false],
array.Type,
array.TypeMapping);

Expand Down
3 changes: 3 additions & 0 deletions src/EFCore.PG/Query/Internal/NpgsqlSqlNullabilityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ protected virtual SqlExpression VisitPostgresBinary(

nullable = binaryExpression.OperatorType switch
{
// Array concatenation treats a null operand as an empty array.
PgExpressionType.ArrayConcat => leftNullable && rightNullable,

// Missing keys and stored null values produce null even for non-null operands.
PgExpressionType.HstoreGetValue => true,

Expand Down
1 change: 1 addition & 0 deletions src/EFCore.PG/Query/NpgsqlSqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ private SqlExpression ApplyTypeMappingOnPostgresBinary(
case PgExpressionType.RangeExcept:
case PgExpressionType.TextSearchAnd:
case PgExpressionType.TextSearchOr:
case PgExpressionType.ArrayConcat:
{
inferredTypeMapping = typeMapping ?? ExpressionExtensions.InferTypeMapping(left, right);
resultType = inferredTypeMapping?.ClrType ?? left.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,7 @@ public override async Task GroupBy_complex_key_aggregate_2(bool async)
SELECT o0."Key" AS "Month", COALESCE(sum(o0."OrderID"), 0)::int AS "Total", (
SELECT COALESCE(sum(o1."OrderID"), 0)::int
FROM "Orders" AS o1
WHERE date_part('month', o1."OrderDate")::int = o0."Key" OR (o1."OrderDate" IS NULL AND o0."Key" IS NULL)) AS "Payment"
WHERE date_part('month', o1."OrderDate")::int = o0."Key" OR (date_part('month', o1."OrderDate") IS NULL AND o0."Key" IS NULL)) AS "Payment"
FROM (
SELECT o."OrderID", date_part('month', o."OrderDate")::int AS "Key"
FROM "Orders" AS o
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2680,7 +2680,7 @@ public override async Task Project_collection_of_datetimes_filtered()
LEFT JOIN LATERAL (
SELECT d.value, d.ordinality
FROM unnest(p."DateTimes") WITH ORDINALITY AS d(value)
WHERE date_part('day', d.value AT TIME ZONE 'UTC')::int <> 1
WHERE date_part('day', d.value AT TIME ZONE 'UTC')::int <> 1 OR date_part('day', d.value AT TIME ZONE 'UTC') IS NULL
) AS d0 ON TRUE
ORDER BY p."Id" NULLS FIRST, d0.ordinality NULLS FIRST
""");
Expand Down Expand Up @@ -2823,7 +2823,7 @@ LEFT JOIN LATERAL unnest(p."Ints") WITH ORDINALITY AS i0(value) ON TRUE
LEFT JOIN LATERAL (
SELECT d.value, d.ordinality
FROM unnest(p."DateTimes") WITH ORDINALITY AS d(value)
WHERE date_part('day', d.value AT TIME ZONE 'UTC')::int <> 1
WHERE date_part('day', d.value AT TIME ZONE 'UTC')::int <> 1 OR date_part('day', d.value AT TIME ZONE 'UTC') IS NULL
) AS d1 ON TRUE
LEFT JOIN LATERAL (
SELECT d0.value, d0.ordinality
Expand Down Expand Up @@ -3015,6 +3015,77 @@ WHERE cardinality(array_remove(p."Ints", 1)) = 1
""");
}

[ConditionalFact]
public async Task Array_remove_with_nullable_item()
{
await AssertQuery(
ss => ss.Set<PrimitiveCollectionsEntity>().Select(e => new
{
e.Id,
NotEqual = e.Strings.Where(s => s != e.NullableString).Count() != e.Strings.Length
}));

AssertSql(
"""
SELECT p."Id", cardinality(array_remove(p."Strings", p."NullableString")) <> cardinality(p."Strings") AS "NotEqual"
FROM "PrimitiveCollectionsEntity" AS p
""");
}

[ConditionalFact]
public async Task Concat_nullable_arrays()
{
var contextFactory = await InitializeNonSharedTest<TestContext>(
seed: context =>
{
context.AddRange(new TestEntity { Id = 1 }, new TestEntity { Id = 2, Ints = [7, 8] });
return context.SaveChangesAsync();
});

await using var context = contextFactory.CreateDbContext();

int[] ints = [7, 8];

var query =
from left in context.Set<TestEntity>()
from right in context.Set<TestEntity>()
orderby left.Id, right.Id
select new
{
Count = (int?)left.Ints!.Concat(right.Ints!).Count(),
LeftNotEqual = left.Ints!.Concat(ints).Count() != 2,
RightNotEqual = ints.Concat(right.Ints!).Count() != 2
};

var results = await query.Select(e => new
{
e.Count,
e.LeftNotEqual,
e.RightNotEqual,
IsNull = e.Count == null,
Equal = e.Count == 2,
NotEqual = e.Count != 2
}).ToListAsync();

Assert.Equal([null, 2, 2, 4], results.Select(e => e.Count));
Assert.Equal([false, false, true, true], results.Select(e => e.LeftNotEqual));
Assert.Equal([false, true, false, true], results.Select(e => e.RightNotEqual));
Assert.Equal([true, false, false, false], results.Select(e => e.IsNull));
Assert.Equal([false, true, true, false], results.Select(e => e.Equal));
Assert.Equal([true, false, false, true], results.Select(e => e.NotEqual));

AssertSql(
"""
@ints={ '7'
'8' } (DbType = Object)

SELECT cardinality(t."Ints" || t0."Ints") AS "Count", cardinality(t."Ints" || @ints) <> 2 AS "LeftNotEqual", cardinality(@ints || t0."Ints") <> 2 AS "RightNotEqual", t."Ints" || t0."Ints" IS NULL AS "IsNull", cardinality(t."Ints" || t0."Ints") = 2 AND t."Ints" || t0."Ints" IS NOT NULL AS "Equal", cardinality(t."Ints" || t0."Ints") <> 2 OR t."Ints" || t0."Ints" IS NULL AS "NotEqual"
FROM "TestEntity" AS t
CROSS JOIN "TestEntity" AS t0
ORDER BY t."Id" NULLS FIRST, t0."Id" NULLS FIRST
""");
}

public override async Task Parameter_collection_of_structs_Contains_nullable_struct()
{
await base.Parameter_collection_of_structs_Contains_nullable_struct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,93 @@ await AssertQuery(
""");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Missing_point_ordinates_not_equal(bool async)
{
await AssertQuery(
async,
ss => ss.Set<PointEntity>()
.Where(e => e.Point != null && e.Point.Z != 0 && e.Point.M != 0)
Comment thread
roji marked this conversation as resolved.
Resolved
Comment thread
roji marked this conversation as resolved.
Resolved
.Select(e => e.Id));

AssertSql(
"""
SELECT p."Id"
FROM "PointEntity" AS p
WHERE p."Point" IS NOT NULL AND (ST_Z(p."Point") <> 0.0 OR ST_Z(p."Point") IS NULL) AND (ST_M(p."Point") <> 0.0 OR ST_M(p."Point") IS NULL)
""");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Empty_line_endpoints_are_null(bool async)
{
await using var context = Fixture.CreateContext();
await context.Database.CreateExecutionStrategy().ExecuteAsync(
async () =>
{
await using var transaction = await context.Database.BeginTransactionAsync();

var entity = await context.Set<LineStringEntity>().AsTracking().SingleAsync(e => e.Id == 1);
entity.LineString = Fixture.GeometryFactory.CreateLineString();
await context.SaveChangesAsync();
Fixture.TestSqlLoggerFactory.Clear();

var query = context.Set<LineStringEntity>()
.Where(e => e.LineString != null && e.LineString.StartPoint == null && e.LineString.EndPoint == null)
.Select(e => e.Id);

Assert.Equal(1, async ? await query.SingleAsync() : query.Single());

AssertSql(
"""
SELECT l."Id"
FROM "LineStringEntity" AS l
WHERE l."LineString" IS NOT NULL AND ST_StartPoint(l."LineString") IS NULL AND ST_EndPoint(l."LineString") IS NULL
LIMIT 2
""");
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task GetGeometryN_out_of_range_is_null(bool async)
{
await AssertQuery(
async,
ss => ss.Set<MultiLineStringEntity>()
.Where(e => e.MultiLineString != null && e.MultiLineString.GetGeometryN(99) == null)
.Select(e => e.Id),
expectedQuery: ss => ss.Set<MultiLineStringEntity>().Where(e => e.MultiLineString != null).Select(e => e.Id));

AssertSql(
"""
SELECT m."Id"
FROM "MultiLineStringEntity" AS m
WHERE m."MultiLineString" IS NOT NULL AND ST_GeometryN(m."MultiLineString", 100) IS NULL
""");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Geometry_collection_indexer_out_of_range_is_null(bool async)
{
await AssertQuery(
async,
ss => ss.Set<MultiLineStringEntity>()
.Where(e => e.MultiLineString != null && e.MultiLineString[99] == null)
.Select(e => e.Id),
expectedQuery: ss => ss.Set<MultiLineStringEntity>().Where(e => e.MultiLineString != null).Select(e => e.Id));

AssertSql(
"""
SELECT m."Id"
FROM "MultiLineStringEntity" AS m
WHERE m."MultiLineString" IS NOT NULL AND ST_GeometryN(m."MultiLineString", 100) IS NULL
""");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,23 @@ WHERE lca(ARRAY[l."LTree",'Top.Hobbies']::ltree[]) = 'Top'
""");
}

[ConditionalFact]
public void LongestCommonAncestor_is_null()
{
using var context = CreateContext();

Assert.Equal(
7,
context.LTreeEntities.Select(l => (LTree?)LTree.LongestCommonAncestor(l.LTree, "")).Count(l => l == null));

AssertSql(
"""
SELECT count(*)::int
FROM "LTreeEntities" AS l
WHERE lca(ARRAY[l."LTree",'']::ltree[]) IS NULL
""");
}

#region Support

protected LTreeQueryContext CreateContext()
Expand Down
Loading
Loading