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
4 changes: 4 additions & 0 deletions src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrimitiveCollectionsEntity>().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<PrimitiveCollectionsEntity>().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<PrimitiveCollectionsEntity>().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();
Expand Down Expand Up @@ -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<PrimitiveCollectionsEntity>().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<PrimitiveCollectionsEntity>().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();
Expand Down
Loading