Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Generate condition for table splitting when no required properties #29207

Merged
merged 1 commit into from
Sep 27, 2022
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
31 changes: 26 additions & 5 deletions src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,33 @@ private void AddConditions(SelectExpression selectExpression, IEntityType entity
var table = (firstTable as FromSqlExpression)?.Table ?? ((ITableBasedExpression)firstTable).Table;
if (table.IsOptional(entityType))
{
SqlExpression? predicate = null;
var entityProjectionExpression = GetMappedEntityProjectionExpression(selectExpression);
var predicate = entityType.GetNonPrincipalSharedNonPkProperties(table)
.Where(e => !e.IsNullable)
.Select(e => IsNotNull(e, entityProjectionExpression))
.Aggregate((l, r) => AndAlso(l, r));
selectExpression.ApplyPredicate(predicate);
var requiredNonPkProperties = entityType.GetProperties().Where(p => !p.IsNullable && !p.IsPrimaryKey()).ToList();
if (requiredNonPkProperties.Count > 0)
{
predicate = requiredNonPkProperties.Select(e => IsNotNull(e, entityProjectionExpression))
.Aggregate((l, r) => AndAlso(l, r));
}

var allNonSharedNonPkProperties = entityType.GetNonPrincipalSharedNonPkProperties(table);
// We don't need condition for nullable property if there exist at least one required property which is non shared.
if (allNonSharedNonPkProperties.Count != 0
&& allNonSharedNonPkProperties.All(p => p.IsNullable))
{
var atLeastOneNonNullValueInNullablePropertyCondition = allNonSharedNonPkProperties
.Select(e => IsNotNull(e, entityProjectionExpression))
.Aggregate((a, b) => OrElse(a, b));

predicate = predicate == null
? atLeastOneNonNullValueInNullablePropertyCondition
: AndAlso(predicate, atLeastOneNonNullValueInNullablePropertyCondition);
}

if (predicate != null)
{
selectExpression.ApplyPredicate(predicate);
}
}

bool HasSiblings(IEntityType entityType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,96 @@ await TestHelpers.ExecuteWithStrategyInTransactionAsync(
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Optional_dependent_without_required_property(bool async)
{
var contextFactory = await InitializeAsync<Context29196>(
onConfiguring: e => e.ConfigureWarnings(w => w.Log(RelationalEventId.OptionalDependentWithoutIdentifyingPropertyWarning)));

using (var context = contextFactory.CreateContext())
{
var query = context.DetailedOrders.Where(o => o.Status == OrderStatus.Pending);

var result = async
? await query.ToListAsync()
: query.ToList();
}
}

protected class Context29196 : DbContext
{
public Context29196(DbContextOptions options)
: base(options)
{
}

public DbSet<Order> Orders => Set<Order>();

public DbSet<DetailedOrder> DetailedOrders => Set<DetailedOrder>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<DetailedOrder>(
dob =>
{
dob.ToTable("Orders");
dob.Property(o => o.Status).HasColumnName("Status");
dob.Property(o => o.Version).IsRowVersion().HasColumnName("Version");
});

modelBuilder.Entity<Order>(
ob =>
{
ob.ToTable("Orders");
ob.Property(o => o.Status).HasColumnName("Status");
ob.HasOne(o => o.DetailedOrder).WithOne().HasForeignKey<DetailedOrder>(o => o.Id);
ob.Property<byte[]>("Version").IsRowVersion().HasColumnName("Version");
});
}

public void Seed()
{
Add(
new Order
{
Status = OrderStatus.Pending,
DetailedOrder = new DetailedOrder
{
Status = OrderStatus.Pending,
ShippingAddress = "221 B Baker St, London",
BillingAddress = "11 Wall Street, New York"
}
});

SaveChanges();
}
}

public class DetailedOrder
{
public int Id { get; set; }
public OrderStatus? Status { get; set; }
public string BillingAddress { get; set; }
public string ShippingAddress { get; set; }
public byte[] Version { get; set; }
}

public class Order
{
public int Id { get; set; }
public OrderStatus? Status { get; set; }
public DetailedOrder DetailedOrder { get; set; }
}

public enum OrderStatus
{
Pending,
Shipped
}

public void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());

Expand Down