Skip to content

Commit

Permalink
Query: Throw error for unhandled derived query root expression
Browse files Browse the repository at this point in the history
Query root design

Core exposes QueryRootExpression which is base class for any query root so we can access EntityType out of it. This is needed for nav expansion and any potential future use case.
Since any derived query root would derive from it, core assembly translates it only when type is exact match to QueryRootExpression.
Relational layer also processes QueryRootExpression when they are mapped to default SqlQuery, which also uses exact type match now.

Apart from QueryRootExpression, no other kind of query root which can appear in different providers should be derivable (else they need to use exact type match too).
This rule makes relational ones sealed class. In case any one needs to derive from it, they need to add additional processing anyway.

Provider specific derived query roots can be non-sealed. If anyone is deriving from it then they should be using their derived provider which process those nodes too and if the derived provider wasn't used and shipped provider is used then it is an error from user perspective. If derived query root is used on other provider (targeting diff database) then it will fail since even the base shipped query root is unknown.

Resolves #26502
  • Loading branch information
smitpatel committed Nov 8, 2021
1 parent c5aa799 commit 78a6b98
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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 FromSqlQueryRootExpression : QueryRootExpression
public sealed class FromSqlQueryRootExpression : QueryRootExpression
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -55,15 +55,15 @@ public class FromSqlQueryRootExpression : QueryRootExpression
/// 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 string Sql { get; }
public string Sql { get; }

/// <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 Argument { get; }
public Expression Argument { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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 TableValuedFunctionQueryRootExpression : QueryRootExpression
public sealed class TableValuedFunctionQueryRootExpression : QueryRootExpression
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -41,15 +41,15 @@ public class TableValuedFunctionQueryRootExpression : QueryRootExpression
/// 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 IStoreFunction Function { get; }
public IStoreFunction Function { get; }

/// <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 IReadOnlyCollection<Expression> Arguments { get; }
public IReadOnlyCollection<Expression> Arguments { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected override Expression VisitExtension(Expression extensionExpression)
_sqlExpressionFactory.Select(
fromSqlQueryRootExpression.EntityType,
new FromSqlExpression(
fromSqlQueryRootExpression.EntityType.GetDefaultMappings().Single().Table.Name.Substring(0, 1)
fromSqlQueryRootExpression.EntityType.GetDefaultMappings().Single().Table.Name[..1]
.ToLowerInvariant(),
fromSqlQueryRootExpression.Sql,
fromSqlQueryRootExpression.Argument)));
Expand Down Expand Up @@ -134,7 +134,8 @@ protected override Expression VisitExtension(Expression extensionExpression)
return CreateShapedQueryExpression(entityType, queryExpression);

case QueryRootExpression queryRootExpression
when queryRootExpression.EntityType.GetSqlQueryMappings().FirstOrDefault(m => m.IsDefaultSqlQueryMapping)?.SqlQuery is
when queryRootExpression.Type == typeof(QueryRootExpression)
&& queryRootExpression.EntityType.GetSqlQueryMappings().FirstOrDefault(m => m.IsDefaultSqlQueryMapping)?.SqlQuery is
ISqlQuery sqlQuery:
return Visit(
new FromSqlQueryRootExpression(
Expand Down
10 changes: 4 additions & 6 deletions src/EFCore/Query/QueryableMethodTranslatingExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,10 @@ protected override Expression VisitExtension(Expression extensionExpression)
{
Check.NotNull(extensionExpression, nameof(extensionExpression));

return extensionExpression switch
{
ShapedQueryExpression _ => extensionExpression,
QueryRootExpression queryRootExpression => CreateShapedQueryExpression(queryRootExpression.EntityType),
_ => base.VisitExtension(extensionExpression),
};
// This has to be exact type match
return extensionExpression.GetType() == typeof(QueryRootExpression)
? CreateShapedQueryExpression(((QueryRootExpression)extensionExpression).EntityType)
: base.VisitExtension(extensionExpression);
}

/// <inheritdoc />
Expand Down

0 comments on commit 78a6b98

Please sign in to comment.