Skip to content

Commit

Permalink
Various code cleanup/styling for Cosmos query pipeline (#33637)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored May 1, 2024
1 parent 8ed790a commit 1e16f86
Show file tree
Hide file tree
Showing 40 changed files with 743 additions and 1,223 deletions.
47 changes: 11 additions & 36 deletions src/EFCore.Cosmos/Query/Internal/CollectionShaperExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,44 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal;
/// 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 CollectionShaperExpression : Expression, IPrintableExpression
public class CollectionShaperExpression(
Expression projection,
Expression innerShaper,
INavigationBase? navigation,
Type elementType)
: Expression, IPrintableExpression
{
/// <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 CollectionShaperExpression(
Expression projection,
Expression innerShaper,
INavigationBase? navigation,
Type elementType)
{
Check.NotNull(projection, nameof(projection));
Check.NotNull(innerShaper, nameof(innerShaper));

Projection = projection;
InnerShaper = innerShaper;
Navigation = navigation;
ElementType = elementType;
}

/// <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 virtual Expression Projection { get; }
public virtual Expression Projection { get; } = projection;

/// <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 virtual Expression InnerShaper { get; }
public virtual Expression InnerShaper { get; } = innerShaper;

/// <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 virtual INavigationBase? Navigation { get; }
public virtual INavigationBase? Navigation { get; } = navigation;

/// <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 virtual Type ElementType { get; }
public virtual Type ElementType { get; } = elementType;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -90,8 +74,6 @@ public override Type Type
/// </summary>
protected override Expression VisitChildren(ExpressionVisitor visitor)
{
Check.NotNull(visitor, nameof(visitor));

var projection = visitor.Visit(Projection);
var innerShaper = visitor.Visit(InnerShaper);

Expand All @@ -107,14 +89,9 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
public virtual CollectionShaperExpression Update(
Expression projection,
Expression innerShaper)
{
Check.NotNull(projection, nameof(projection));
Check.NotNull(innerShaper, nameof(innerShaper));

return projection != Projection || innerShaper != InnerShaper
=> projection != Projection || innerShaper != InnerShaper
? new CollectionShaperExpression(projection, innerShaper, Navigation, ElementType)
: this;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -124,8 +101,6 @@ public virtual CollectionShaperExpression Update(
/// </summary>
void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
{
Check.NotNull(expressionPrinter, nameof(expressionPrinter));

expressionPrinter.AppendLine("CollectionShaper:");
using (expressionPrinter.Indent())
{
Expand Down
17 changes: 2 additions & 15 deletions src/EFCore.Cosmos/Query/Internal/CosmosDateTimeMemberTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,8 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
/// 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 CosmosDateTimeMemberTranslator : IMemberTranslator
public class CosmosDateTimeMemberTranslator(ISqlExpressionFactory sqlExpressionFactory) : IMemberTranslator
{
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 CosmosDateTimeMemberTranslator(ISqlExpressionFactory 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
Expand All @@ -41,7 +28,7 @@ public CosmosDateTimeMemberTranslator(ISqlExpressionFactory sqlExpressionFactory
|| declaringType == typeof(DateTimeOffset))
&& member.Name == nameof(DateTime.UtcNow))
{
return _sqlExpressionFactory.Function(
return sqlExpressionFactory.Function(
"GetCurrentDateTime",
[],
returnType);
Expand Down
19 changes: 3 additions & 16 deletions src/EFCore.Cosmos/Query/Internal/CosmosEqualsTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,8 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
/// 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 CosmosEqualsTranslator : IMethodCallTranslator
public class CosmosEqualsTranslator(ISqlExpressionFactory sqlExpressionFactory) : IMethodCallTranslator
{
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 CosmosEqualsTranslator(ISqlExpressionFactory 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
Expand Down Expand Up @@ -60,8 +47,8 @@ public CosmosEqualsTranslator(ISqlExpressionFactory sqlExpressionFactory)
return left.Type.UnwrapNullableType() == right.Type.UnwrapNullableType()
|| (right.Type == typeof(object) && (right is SqlParameterExpression or SqlConstantExpression))
|| (left.Type == typeof(object) && (left is SqlParameterExpression or SqlConstantExpression))
? _sqlExpressionFactory.Equal(left, right)
: _sqlExpressionFactory.Constant(false);
? sqlExpressionFactory.Equal(left, right)
: sqlExpressionFactory.Constant(false);
}

return null;
Expand Down
19 changes: 3 additions & 16 deletions src/EFCore.Cosmos/Query/Internal/CosmosMathTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
/// 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 CosmosMathTranslator : IMethodCallTranslator
public class CosmosMathTranslator(ISqlExpressionFactory sqlExpressionFactory) : IMethodCallTranslator
{
private static readonly Dictionary<MethodInfo, string> SupportedMethodTranslations = new()
{
Expand Down Expand Up @@ -71,19 +71,6 @@ public class CosmosMathTranslator : IMethodCallTranslator
{ typeof(float).GetRuntimeMethod(nameof(float.RadiansToDegrees), [typeof(float)])!, "DEGREES" },
};

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 CosmosMathTranslator(ISqlExpressionFactory 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
Expand All @@ -102,9 +89,9 @@ public CosmosMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
? ExpressionExtensions.InferTypeMapping(arguments[0])
: ExpressionExtensions.InferTypeMapping(arguments[0], arguments[1]);

var newArguments = arguments.Select(e => _sqlExpressionFactory.ApplyTypeMapping(e, typeMapping!));
var newArguments = arguments.Select(e => sqlExpressionFactory.ApplyTypeMapping(e, typeMapping!));

return _sqlExpressionFactory.Function(
return sqlExpressionFactory.Function(
sqlFunctionName,
newArguments,
method.ReturnType,
Expand Down
16 changes: 2 additions & 14 deletions src/EFCore.Cosmos/Query/Internal/CosmosQueryCompilationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,9 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
/// 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 CosmosQueryCompilationContext : QueryCompilationContext
public class CosmosQueryCompilationContext(QueryCompilationContextDependencies dependencies, bool async)
: QueryCompilationContext(dependencies, async)
{
/// <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 CosmosQueryCompilationContext(
QueryCompilationContextDependencies dependencies,
bool async)
: base(dependencies, async)
{
}

/// <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
Expand Down
21 changes: 5 additions & 16 deletions src/EFCore.Cosmos/Query/Internal/CosmosQueryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,16 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
/// 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 CosmosQueryContext : QueryContext
public class CosmosQueryContext(
QueryContextDependencies dependencies,
ICosmosClientWrapper cosmosClient)
: QueryContext(dependencies)
{
/// <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 CosmosQueryContext(
QueryContextDependencies dependencies,
ICosmosClientWrapper cosmosClient)
: base(dependencies)
{
CosmosClient = cosmosClient;
}

/// <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 virtual ICosmosClientWrapper CosmosClient { get; }
public virtual ICosmosClientWrapper CosmosClient { get; } = cosmosClient;
}
24 changes: 5 additions & 19 deletions src/EFCore.Cosmos/Query/Internal/CosmosQueryContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,14 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
/// 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 CosmosQueryContextFactory : IQueryContextFactory
public class CosmosQueryContextFactory(
QueryContextDependencies dependencies,
ICosmosClientWrapper cosmosClient) : IQueryContextFactory
{
private readonly ICosmosClientWrapper _cosmosClient;

/// <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 CosmosQueryContextFactory(
QueryContextDependencies dependencies,
ICosmosClientWrapper cosmosClient)
{
Dependencies = dependencies;
_cosmosClient = cosmosClient;
}

/// <summary>
/// Dependencies for this service.
/// </summary>
protected virtual QueryContextDependencies Dependencies { get; }
protected virtual QueryContextDependencies Dependencies { get; } = dependencies;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -41,5 +27,5 @@ public CosmosQueryContextFactory(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual QueryContext Create()
=> new CosmosQueryContext(Dependencies, _cosmosClient);
=> new CosmosQueryContext(Dependencies, cosmosClient);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,9 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
/// 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 CosmosQueryMetadataExtractingExpressionVisitor : ExpressionVisitor
public class CosmosQueryMetadataExtractingExpressionVisitor(CosmosQueryCompilationContext cosmosQueryCompilationContext)
: ExpressionVisitor
{
private readonly CosmosQueryCompilationContext _cosmosQueryCompilationContext;

/// <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 CosmosQueryMetadataExtractingExpressionVisitor(CosmosQueryCompilationContext cosmosQueryCompilationContext)
{
_cosmosQueryCompilationContext = cosmosQueryCompilationContext;
}

/// <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
Expand All @@ -37,7 +25,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
{
var innerQueryable = Visit(methodCallExpression.Arguments[0]);

_cosmosQueryCompilationContext.PartitionKeyFromExtension = methodCallExpression.Arguments[1].GetConstantValue<string>();
cosmosQueryCompilationContext.PartitionKeyFromExtension = methodCallExpression.Arguments[1].GetConstantValue<string>();

return innerQueryable;
}
Expand Down
Loading

0 comments on commit 1e16f86

Please sign in to comment.