From 81c79f7e1bbc8c520b98dfb125458277a36a5df2 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Tue, 17 Nov 2026 16:08:39 +0100 Subject: [PATCH] Fix chained array subscripts and nullable intersections Parenthesize slices before further indexing or slicing, and preserve INTERSECT null semantics when both inputs have nullable elements. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Query/Internal/NpgsqlQuerySqlGenerator.cs | 4 + ...yableMethodTranslatingExpressionVisitor.cs | 1 + .../PrimitiveCollectionsQueryNpgsqlTest.cs | 100 ++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs index 2a32637cd..7b960f25b 100644 --- a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs +++ b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs @@ -1569,6 +1569,10 @@ protected override bool RequiresParentheses(SqlExpression outerExpression, SqlEx || outerPrecedence >= innerPrecedence; } + // Consecutive subscripts access multiple dimensions rather than the result of the slice. + case PgArraySliceExpression when outerExpression is PgArrayIndexExpression or PgArraySliceExpression: + return true; + // PG requires function calls to be wrapped in parentheses before indexing on the returned array: // (string_to_array(c."ContactName", ' '))[1] case SqlFunctionExpression when outerExpression is PgArrayIndexExpression: diff --git a/src/EFCore.PG/Query/Internal/NpgsqlQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.PG/Query/Internal/NpgsqlQueryableMethodTranslatingExpressionVisitor.cs index ceb1f13bd..67d44b53f 100644 --- a/src/EFCore.PG/Query/Internal/NpgsqlQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.PG/Query/Internal/NpgsqlQueryableMethodTranslatingExpressionVisitor.cs @@ -691,6 +691,7 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr && projectedColumn1.TableAlias == table1.Alias && projectedColumn2.TableAlias == table2.Alias // Unlike INTERSECT, array overlap doesn't match null elements. + && (!projectedColumn1.IsNullable || !projectedColumn2.IsNullable) && (table1 is PgUnnestExpression { Array: { TypeMapping: not NpgsqlMultirangeTypeMapping } array1 } || (projectedColumn1 is { Name: "Value", IsNullable: false } && source1.TryConvertToArray(out array1, ignoreOrderings: true))) diff --git a/test/EFCore.PG.FunctionalTests/Query/PrimitiveCollectionsQueryNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/PrimitiveCollectionsQueryNpgsqlTest.cs index 053df8570..78b39ee52 100644 --- a/test/EFCore.PG.FunctionalTests/Query/PrimitiveCollectionsQueryNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/PrimitiveCollectionsQueryNpgsqlTest.cs @@ -1649,6 +1649,46 @@ public override async Task Column_collection_Skip_Take() """); } + [ConditionalFact] + public virtual async Task Column_collection_Take_Skip() + { + await AssertQuery( + ss => ss.Set().Select(c => new { c.Id, Count = c.Ints.Take(2).Skip(1).Count() })); + + AssertSql( + """ +SELECT p."Id", cardinality((p."Ints"[:2])[2:]) AS "Count" +FROM "PrimitiveCollectionsEntity" AS p +"""); + } + + [ConditionalFact] + public virtual async Task Column_collection_Skip_Skip() + { + await AssertQuery( + ss => ss.Set().Select(c => new { c.Id, Count = c.Ints.Skip(1).Skip(1).Count() })); + + AssertSql( + """ +SELECT p."Id", cardinality((p."Ints"[2:])[2:]) AS "Count" +FROM "PrimitiveCollectionsEntity" AS p +"""); + } + + [ConditionalFact] + public virtual async Task Column_collection_Take_ElementAt() + { + await AssertQuery( + ss => ss.Set().Where(c => c.Ints.Length > 0).Select(c => c.Ints.Take(2).ElementAt(0))); + + AssertSql( + """ +SELECT (p."Ints"[:2])[1] +FROM "PrimitiveCollectionsEntity" AS p +WHERE cardinality(p."Ints") > 0 +"""); + } + public override async Task Column_collection_Where_Skip() { await base.Column_collection_Where_Skip(); @@ -2158,6 +2198,66 @@ FROM unnest(p."NullableInts") AS n(value) """); } + [ConditionalFact] + public virtual async Task Column_collection_Intersect_nullable_column_collection_Any() + { + await AssertQuery( + ss => ss.Set().Select(c => new { c.Id, Result = c.NullableInts.Intersect(c.NullableInts).Any() })); + + AssertSql( + """ +SELECT p."Id", EXISTS ( + SELECT 1 + FROM ( + SELECT n.value + FROM unnest(p."NullableInts") AS n(value) + INTERSECT + SELECT n0.value + FROM unnest(p."NullableInts") AS n0(value) + ) AS i) AS "Result" +FROM "PrimitiveCollectionsEntity" AS p +"""); + } + + [ConditionalFact] + public virtual async Task Column_collection_Intersect_nullable_parameter_collection_Any() + { + int?[] ints = [null, 999]; + + await AssertQuery( + ss => ss.Set().Select(c => new + { + c.Id, + Result = c.NullableInts.Intersect(ints).Any(), + Reversed = ints.Intersect(c.NullableInts).Any() + })); + + AssertSql( + """ +@ints={ NULL +'999' } (DbType = Object) + +SELECT p."Id", EXISTS ( + SELECT 1 + FROM ( + SELECT n.value + FROM unnest(p."NullableInts") AS n(value) + INTERSECT + SELECT i.value + FROM unnest(@ints) AS i(value) + ) AS i0) AS "Result", EXISTS ( + SELECT 1 + FROM ( + SELECT i1.value + FROM unnest(@ints) AS i1(value) + INTERSECT + SELECT n0.value + FROM unnest(p."NullableInts") AS n0(value) + ) AS i2) AS "Reversed" +FROM "PrimitiveCollectionsEntity" AS p +"""); + } + public override async Task Inline_collection_Except_column_collection() { await base.Inline_collection_Except_column_collection();