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 @@ -41,7 +41,7 @@ protected override Expression VisitExtension(Expression expression)
{
switch (expression)
{
case PgUnnestExpression unnestExpression
case PgUnnestExpression { Array.TypeMapping: null } unnestExpression
when TryGetInferredTypeMapping(unnestExpression.Alias, unnestExpression.ColumnName, out var elementTypeMapping):
{
var collectionTypeMapping = _typeMappingSource.FindMapping(unnestExpression.Array.Type, _model, elementTypeMapping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,70 @@ public class Entity
public string? Value { get; set; }
}
}

[ConditionalFact]
public virtual async Task Set_operations_over_array_columns_with_different_element_mappings()
{
var contextFactory = await InitializeAsync<Context3940>(
seed: async context =>
{
context.Orders.AddRange(
new Context3940.Order { Id = 1, Tags = ["a", "x"], Labels = ["b"] },
new Context3940.Order { Id = 2, Tags = ["a"], Labels = ["a", "x"] },
new Context3940.Order { Id = 3, Tags = ["a"], Labels = ["a"] });
await context.SaveChangesAsync();
});

await using var context = contextFactory.CreateContext();

var union = await context.Orders.Where(o => o.Tags.Union(o.Labels).Contains("x"))
.OrderBy(o => o.Id).Select(o => o.Id).ToArrayAsync();
var except = await context.Orders.Where(o => o.Tags.Except(o.Labels).Any()).Select(o => o.Id).ToArrayAsync();

Assert.Equal([1, 2], union);
Assert.Equal([1], except);

AssertSql(
"""
SELECT o."Id"
FROM "Orders" AS o
WHERE 'x' IN (
SELECT t.value
FROM unnest(o."Tags") AS t(value)
UNION
SELECT l.value
FROM unnest(o."Labels") AS l(value)
)
ORDER BY o."Id" NULLS FIRST
""",
//
"""
SELECT o."Id"
FROM "Orders" AS o
WHERE EXISTS (
SELECT 1
FROM (
SELECT t.value
FROM unnest(o."Tags") AS t(value)
EXCEPT
SELECT l.value
FROM unnest(o."Labels") AS l(value)
) AS e)
""");
}

protected class Context3940(DbContextOptions options) : DbContext(options)
{
public DbSet<Order> Orders => Set<Order>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Order>().PrimitiveCollection(o => o.Labels).ElementType().HasMaxLength(10);

public class Order
{
public int Id { get; set; }
public List<string> Tags { get; set; } = [];
public List<string> Labels { get; set; } = [];
}
}
}
Loading