Skip to content

Fall out from #36502 #36550

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -371,8 +371,13 @@ protected override Expression VisitExtension(Expression extensionExpression)

_projectionMapping[_projectionMembers.Peek()] = projection;

return shaper.Update(
new ProjectionBindingExpression(_selectExpression, _projectionMembers.Peek(), typeof(ValueBuffer)));
return shaper
.Update(new ProjectionBindingExpression(_selectExpression, _projectionMembers.Peek(), typeof(ValueBuffer)))
#pragma warning disable EF1001
// This is to handle have correct type for the shaper expression. It is later fixed in MatchTypes.
// This mirrors for structural types what we do for scalars.
.MakeClrTypeNullable();
#pragma warning restore EF1001
}

case IncludeExpression includeExpression:
Expand Down Expand Up @@ -658,7 +663,14 @@ private static Expression MatchTypes(Expression expression, Type targetType)
targetType.MakeNullable() == expression.Type,
$"expression has type {expression.Type.Name}, but must be nullable over {targetType.Name}");

expression = Expression.Convert(expression, targetType);
return expression switch
{
#pragma warning disable EF1001
RelationalStructuralTypeShaperExpression structuralShaper => structuralShaper.MakeClrTypeNonNullable(),
#pragma warning restore EF1001

_ => Expression.Convert(expression, targetType),
};
}

return expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@ bool TryRewriteComplexTypeEquality(bool collection, [NotNullWhen(true)] out SqlE
// into complex properties to generate a flattened list of comparisons.
// The moment we reach a a complex property that's mapped to JSON, we stop and generate a single comparison
// for the whole complex type.
bool TryGenerateComparisons(
IComplexType type,
Expression left,
Expression right,
[NotNullWhen(true)] ref SqlExpression? comparisons)
bool TryGenerateComparisons(IComplexType complexType, Expression left, Expression right, [NotNullWhen(true)] ref SqlExpression? comparisons)
=> TryGenerateComparisonsRec(complexType, left, right, ref comparisons, out _);
bool TryGenerateComparisonsRec(IComplexType type, Expression left, Expression right, [NotNullWhen(true)] ref SqlExpression? comparisons, out bool exitImmediately)
{
exitImmediately = false;

if (type.IsMappedToJson())
{
var leftScalar = Process(left);
Expand Down Expand Up @@ -418,6 +418,17 @@ SqlParameterExpression parameter
{
var comparison = _sqlExpressionFactory.MakeBinary(nodeType, leftTranslation, rightTranslation, boolTypeMapping)!;

// If we have a required property and one of the sides is a constant null,
// we can use just that property and skip comparing the rest of properties.
if (!property.IsNullable
&& (leftTranslation is SqlConstantExpression { Value: null }
|| rightTranslation is SqlConstantExpression { Value: null }))
{
comparisons = comparison;
exitImmediately = true;
return true;
}

comparisons = comparisons is null
? comparison
: nodeType == ExpressionType.Equal
Expand Down Expand Up @@ -447,10 +458,15 @@ SqlParameterExpression parameter

if (nestedLeft is null
|| nestedRight is null
|| !TryGenerateComparisons(complexProperty.ComplexType, nestedLeft, nestedRight, ref comparisons))
|| !TryGenerateComparisonsRec(complexProperty.ComplexType, nestedLeft, nestedRight, ref comparisons, out exitImmediately))
{
return false;
}

if (exitImmediately)
{
return true;
}
}

return comparisons is not null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RelationalStructuralTypeShaperExpression : StructuralTypeShaperExpr
/// <param name="valueBufferExpression">An expression of ValueBuffer to get values for properties of the entity.</param>
/// <param name="nullable">A bool value indicating whether this entity instance can be null.</param>
public RelationalStructuralTypeShaperExpression(ITypeBase structuralType, Expression valueBufferExpression, bool nullable)
: base(structuralType, valueBufferExpression, nullable, materializationCondition: null)
: base(structuralType, valueBufferExpression, nullable)
{
}

Expand All @@ -38,12 +38,14 @@ public RelationalStructuralTypeShaperExpression(ITypeBase structuralType, Expres
/// An expression of <see cref="Func{T,TResult}" /> to determine which entity type to
/// materialize.
/// </param>
/// <param name="clrType">CLR type for this expression as returned from <see cref="Type"/>.</param>
protected RelationalStructuralTypeShaperExpression(
ITypeBase type,
Expression valueBufferExpression,
bool nullable,
LambdaExpression? materializationCondition)
: base(type, valueBufferExpression, nullable, materializationCondition)
LambdaExpression? materializationCondition,
Type clrType)
: base(type, valueBufferExpression, nullable, materializationCondition, clrType)
{
}

Expand Down Expand Up @@ -149,7 +151,7 @@ protected override LambdaExpression GenerateMaterializationCondition(ITypeBase t
/// <inheritdoc />
public override StructuralTypeShaperExpression WithType(ITypeBase type)
=> type != StructuralType
? new RelationalStructuralTypeShaperExpression(type, ValueBufferExpression, IsNullable)
? new RelationalStructuralTypeShaperExpression(type, ValueBufferExpression, IsNullable, materializationCondition: null, type.ClrType)
: this;

/// <inheritdoc />
Expand All @@ -175,12 +177,24 @@ public override StructuralTypeShaperExpression MakeNullable(bool nullable = true
}

// Marking nullable requires re-computation of Discriminator condition
return new RelationalStructuralTypeShaperExpression(StructuralType, newValueBufferExpression, true);
return new RelationalStructuralTypeShaperExpression(StructuralType, newValueBufferExpression, true, materializationCondition: null, Type);
}

/// <inheritdoc />
public override StructuralTypeShaperExpression Update(Expression valueBufferExpression)
=> valueBufferExpression != ValueBufferExpression
? new RelationalStructuralTypeShaperExpression(StructuralType, valueBufferExpression, IsNullable, MaterializationCondition)
? new RelationalStructuralTypeShaperExpression(StructuralType, valueBufferExpression, IsNullable, MaterializationCondition, Type)
: this;

/// <inheritdoc />
public override StructuralTypeShaperExpression MakeClrTypeNullable()
=> Type != Type.MakeNullable()
? new RelationalStructuralTypeShaperExpression(StructuralType, ValueBufferExpression, IsNullable, MaterializationCondition, Type.MakeNullable())
: this;

/// <inheritdoc />
public override StructuralTypeShaperExpression MakeClrTypeNonNullable()
=> Type != Type.UnwrapNullableType()
? new RelationalStructuralTypeShaperExpression(StructuralType, ValueBufferExpression, IsNullable, MaterializationCondition, Type.UnwrapNullableType())
: this;
}
4 changes: 2 additions & 2 deletions src/EFCore/Query/EntityMaterializerSourceParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace Microsoft.EntityFrameworkCore.Query;
/// </summary>
/// <param name="StructuralType">The entity or complex type being materialized.</param>
/// <param name="InstanceName">The name of the instance being materialized.</param>
/// <param name="AllowNullable">Whether nullable result is allowed.</param>
/// <param name="ClrType">CLR type of the result.</param>
/// <param name="QueryTrackingBehavior">
/// The query tracking behavior, or <see langword="null" /> if this materialization is not from a query.
/// </param>
public readonly record struct StructuralTypeMaterializerSourceParameters(
ITypeBase StructuralType,
string InstanceName,
bool? AllowNullable,
Type ClrType,
QueryTrackingBehavior? QueryTrackingBehavior);

/// <summary>
Expand Down
Loading
Loading