Skip to content

Commit fa8d783

Browse files
rojiCopilot
andcommitted
Fix GIN index detection for views
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 60f6593 commit fa8d783

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

src/EFCore.PG/Query/Internal/NpgsqlQueryableMethodTranslatingExpressionVisitor.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,12 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
703703
return array switch
704704
{
705705
// For array columns which have a GIN index, we translate to array containment (with @>) which uses that index.
706-
ColumnExpression { Column: IColumn column }
707-
when column.Table.Indexes
708-
.Any(i =>
709-
i.Columns.Count > 0
710-
&& i.Columns[0] == column
711-
&& i.MappedIndexes.Any(mi => mi.GetMethod()?.Equals("GIN", StringComparison.OrdinalIgnoreCase) == true))
706+
ColumnExpression { Column: IColumnBase column }
707+
when column.PropertyMappings.Any(
708+
m => m.Property.GetContainingIndexes().Any(
709+
i => i.Properties.Count > 0
710+
&& i.Properties[0] == m.Property
711+
&& i.GetMethod()?.Equals("GIN", StringComparison.OrdinalIgnoreCase) == true))
712712
=> BuildSimplifiedShapedQuery(
713713
source,
714714
_sqlExpressionFactory.Contains(

test/EFCore.PG.FunctionalTests/Query/PrimitiveCollectionsQueryNpgsqlTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,6 +2753,26 @@ LIMIT 2
27532753
""");
27542754
}
27552755

2756+
[ConditionalFact]
2757+
public virtual async Task View_column_collection_Contains_with_GIN_index_uses_containment()
2758+
{
2759+
var contextFactory = await InitializeNonSharedTest<TestContext>(
2760+
onModelCreating: mb => mb.Entity<TestEntity>()
2761+
.ToView("TestView")
2762+
.HasIndex(e => e.Ints)
2763+
.HasMethod("GIN"));
2764+
2765+
await using var context = contextFactory.CreateDbContext();
2766+
2767+
Assert.Equal(
2768+
"""
2769+
SELECT t."Id", t."Ints"
2770+
FROM "TestView" AS t
2771+
WHERE t."Ints" @> ARRAY[4]::integer[]
2772+
""",
2773+
context.Set<TestEntity>().Where(c => c.Ints!.Contains(4)).ToQueryString());
2774+
}
2775+
27562776
[ConditionalFact]
27572777
public virtual async Task Column_collection_Contains_with_btree_index_does_not_use_containment()
27582778
{

0 commit comments

Comments
 (0)