Skip to content

Commit

Permalink
Query: Don't read default value from databases (#21668)
Browse files Browse the repository at this point in the history
In 3.x when reading values from database, we started returning default value of expected type if the value was null in the database. That means if we needed to read int then database value was null then we got 0 back. Which is not correct as we should throw exception for the case.

The fix here is,
- All value read from databases are of nullable type as database can return null.
- Since all the bindings with database are nullable now,
  - When doing member access we add null check for value types and return nullable of return type
  - When doing method call we add null check for instance. All parameters we convert to original types.
  - All other expressions, where they need to match the type to reconstruct the original tree, adds convert nodes.
- For translation of Single/SingleOrDefault (and friends)
  - For scalar subquery
    - We add coalesce with default value for SingleOrDefault
    - We don't add coalesce for Single case and assume there will be non-null value coming out of database which we will try to assign.
  - For non-scalar subquery
    - We add default value in client side in shaper for SingleOrDefault
    - We throw exception Sequence contains no elements for Single case
  - Above rules are only when not returning entity type. When returning entity type, we don't have enough information to differentiate the case. Also above behavior is just side-effect of assigning values to properties which cannot take null values.
- When doing DefaultIfEmpty over a non-reference type, navigation expansion will add client side coalesce so that we can move Selector after DefaultIfEmpty

Resolves #20633
Resolves #20959
Resolves #21002
  • Loading branch information
smitpatel committed Jul 18, 2020
1 parent 2c0ee81 commit 3ebfde7
Show file tree
Hide file tree
Showing 59 changed files with 1,046 additions and 605 deletions.
43 changes: 0 additions & 43 deletions src/EFCore.Cosmos/Query/CosmosQueryTranslationPostprocessor.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq.Expressions;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class CosmosQueryTranslationPostprocessor : QueryTranslationPostprocessor
{
private readonly ISqlExpressionFactory _sqlExpressionFactory;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public CosmosQueryTranslationPostprocessor(
[NotNull] QueryTranslationPostprocessorDependencies dependencies,
[NotNull] ISqlExpressionFactory sqlExpressionFactory,
[NotNull] QueryCompilationContext queryCompilationContext)
: base(dependencies, queryCompilationContext)
{
Check.NotNull(sqlExpressionFactory, nameof(sqlExpressionFactory));

_sqlExpressionFactory = sqlExpressionFactory;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Expression Process(Expression query)
{
query = base.Process(query);
query = new CosmosValueConverterCompensatingExpressionVisitor(_sqlExpressionFactory).Visit(query);

return query;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CosmosProjectionBindingRemovingExpressionVisitor(
{
_selectExpression = selectExpression;
}

protected override ProjectionExpression GetProjection(ProjectionBindingExpression projectionBindingExpression)
=> _selectExpression.Projection[GetProjectionIndex(projectionBindingExpression)];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,16 @@ MethodInfo GetMethod()
return new EntityReferenceExpression(subqueryTranslation);
}

var shaperExpression = subqueryTranslation.ShaperExpression;
if (shaperExpression is UnaryExpression unaryExpression
&& unaryExpression.NodeType == ExpressionType.Convert
&& unaryExpression.Type.MakeNullable() == unaryExpression.Operand.Type)
{
shaperExpression = unaryExpression.Operand;
}

#pragma warning disable IDE0046 // Convert to conditional expression
if (!(subqueryTranslation.ShaperExpression is ProjectionBindingExpression projectionBindingExpression))
if (!(shaperExpression is ProjectionBindingExpression projectionBindingExpression))
#pragma warning restore IDE0046 // Convert to conditional expression
{
return null;
Expand Down Expand Up @@ -869,7 +877,6 @@ private Expression TryBindMember(Expression source, MemberIdentity member, Type
if (property != null)
{
return BindProperty(entityReferenceExpression, property, type);

}

AddTranslationErrorDetails(
Expand Down
Loading

0 comments on commit 3ebfde7

Please sign in to comment.