-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Don't read default value from databases (#21668)
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
Showing
59 changed files
with
1,046 additions
and
605 deletions.
There are no files selected for viewing
43 changes: 0 additions & 43 deletions
43
src/EFCore.Cosmos/Query/CosmosQueryTranslationPostprocessor.cs
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
src/EFCore.Cosmos/Query/Internal/CosmosQueryTranslationPostprocessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.