Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jan 15, 2024
1 parent 72af13f commit 973daee
Show file tree
Hide file tree
Showing 84 changed files with 17,322 additions and 17,309 deletions.
1 change: 1 addition & 0 deletions EFCore.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<s:Boolean x:Key="/Default/UserDictionary/Words/=sqlite/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=subqueries/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=subquery/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=subquery_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=transactionality/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unconfigured/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unignore/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,13 @@ when entityQueryRootExpression.GetType() == typeof(EntityQueryRootExpression)
RelationalStrings.SqlQueryUnmappedType(sqlQueryRootExpression.ElementType.DisplayName()));
}

var alias = _sqlAliasManager.GenerateTableAlias("t");
var selectExpression = new SelectExpression(
_sqlAliasManager,
new FromSqlExpression(alias, sqlQueryRootExpression.Sql, sqlQueryRootExpression.Argument), SqlQuerySingleColumnAlias,
new FromSqlExpression(
_sqlAliasManager.GenerateTableAlias("sql"),
sqlQueryRootExpression.Sql,
sqlQueryRootExpression.Argument),
SqlQuerySingleColumnAlias,
sqlQueryRootExpression.Type, typeMapping);

Expression shaperExpression = new ProjectionBindingExpression(
Expand Down Expand Up @@ -287,25 +290,28 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
// Attempt to translate access into a primitive collection property (i.e. array column)
if (_sqlTranslator.TryBindMember(_sqlTranslator.Visit(source), member, out var translatedExpression, out var property)
&& property is IProperty { IsPrimitiveCollection: true } regularProperty
&& translatedExpression is SqlExpression sqlExpression)
&& translatedExpression is SqlExpression sqlExpression
&& TranslatePrimitiveCollection(
sqlExpression, regularProperty, _sqlAliasManager.GenerateTableAlias(GenerateTableAlias(sqlExpression))) is
{ } primitiveCollectionTranslation)
{
var tableAlias = sqlExpression switch
{
ColumnExpression c => c.Name,
JsonScalarExpression { Path: [.., { PropertyName: string propertyName }] } => propertyName,
_ => "j"
};
return primitiveCollectionTranslation;
}

tableAlias = _sqlAliasManager.GenerateTableAlias(tableAlias);
return null;

if (TranslatePrimitiveCollection(sqlExpression, regularProperty, tableAlias) is
{ } primitiveCollectionTranslation)
string GenerateTableAlias(SqlExpression sqlExpression)
=> sqlExpression switch
{
return primitiveCollectionTranslation;
}
}
ColumnExpression c => c.Name,
JsonScalarExpression jsonScalar
=> jsonScalar.Path.LastOrDefault(s => s.PropertyName is not null) is PathSegment lastPropertyNameSegment
? lastPropertyNameSegment.PropertyName!
: GenerateTableAlias(jsonScalar.Json),
ScalarSubqueryExpression scalarSubquery => scalarSubquery.Subquery.Projection[0].Alias,

return null;
_ => "collection"
};
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,12 @@ public TpcTableExpressionRemovingExpressionVisitor(SqlAliasManager sqlAliasManag
var result = subSelectExpressions[0];
for (var i = 1; i < subSelectExpressions.Count; i++)
{
var setOperationAlias = _sqlAliasManager.GenerateTableAlias("t");
var source1 = result;
var source2 = subSelectExpressions[i];
RemapProjections(reindexingMap, source2);
var generatedSelectExpression = new SelectExpression(alias: null, _sqlAliasManager);

var setOperationAlias = _sqlAliasManager.GenerateTableAlias("union");
var unionExpression = new UnionExpression(setOperationAlias, source1, source2, distinct: false);
var tableReferenceExpression = new TableReferenceExpression(generatedSelectExpression, setOperationAlias);
generatedSelectExpression._tables.Add(unionExpression);
Expand Down
14 changes: 10 additions & 4 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ _projectionMapping[new ProjectionMember()] =
selectExpression._mutable = false;
}

// We only assign unique alias to Tpc
var tableAlias = _sqlAliasManager.GenerateTableAlias("t");
var tableAlias = _sqlAliasManager.GenerateTableAlias("table");
var tpcTables = new TpcTablesExpression(tableAlias, entityType, subSelectExpressions);
var tpcTableReference = new TableReferenceExpression(this, tableAlias);
_tables.Add(tpcTables);
Expand Down Expand Up @@ -1118,6 +1117,10 @@ public Expression ApplyProjection(
&& (containsSingleResult || containsCollection))
{
// SingleResult can lift collection from inner
// Note that we create a CloningExpressionVisitor without a SQL alias manager - this means that aliases won't get uniquified
// as expressions are being cloned. Since we're cloning here to get a completely separate (split) query, that makes sense
// as we don't want aliases to be unique across different queries (but in other contexts, when the cloned fragment gets
// integrated back into the same query (e.g. GroupBy) we do want to uniquify aliases).
cloningExpressionVisitor = new CloningExpressionVisitor(sqlAliasManager: null);
}

Expand Down Expand Up @@ -2581,7 +2584,7 @@ private void ApplySetOperation(
throw new InvalidOperationException(RelationalStrings.ProjectionMappingCountMismatch);
}

var setOperationAlias = _sqlAliasManager.GenerateTableAlias("t");
var setOperationAlias = _sqlAliasManager.GenerateTableAlias(setOperationType.ToString());
var tableReferenceExpression = new TableReferenceExpression(this, setOperationAlias);

foreach (var (projectionMember, expression1, expression2) in select1._projectionMapping.Join(
Expand Down Expand Up @@ -3969,7 +3972,10 @@ public void PushdownIntoSubquery()
/// <param name="liftOrderings">Whether orderings on the query should be lifted out of the subquery.</param>
private SqlRemappingVisitor PushdownIntoSubqueryInternal(bool liftOrderings = true)
{
var subqueryAlias = _sqlAliasManager.GenerateTableAlias("t");
// If there's just one table in the select being pushed down, bubble up that table's name as the subquery's alias.
var subqueryAlias =
_sqlAliasManager.GenerateTableAlias(_tables is [{ Alias: string singleTableAlias }] ? singleTableAlias : "subquery");

var subquery = new SelectExpression(
subqueryAlias, [], _tables.ToList(), _tableReferences.ToList(), _groupBy.ToList(),
_orderings.ToList(), GetAnnotations(), _sqlAliasManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ public override async Task Update_complex_type_to_another_database_complex_type_
@__p_0='1'

UPDATE [c0]
SET [c0].[ShippingAddress_AddressLine1] = [t].[BillingAddress_AddressLine1],
[c0].[ShippingAddress_AddressLine2] = [t].[BillingAddress_AddressLine2],
[c0].[ShippingAddress_Tags] = [t].[BillingAddress_Tags],
[c0].[ShippingAddress_ZipCode] = [t].[BillingAddress_ZipCode],
[c0].[ShippingAddress_Country_Code] = [t].[ShippingAddress_Country_Code],
[c0].[ShippingAddress_Country_FullName] = [t].[ShippingAddress_Country_FullName]
SET [c0].[ShippingAddress_AddressLine1] = [c1].[BillingAddress_AddressLine1],
[c0].[ShippingAddress_AddressLine2] = [c1].[BillingAddress_AddressLine2],
[c0].[ShippingAddress_Tags] = [c1].[BillingAddress_Tags],
[c0].[ShippingAddress_ZipCode] = [c1].[BillingAddress_ZipCode],
[c0].[ShippingAddress_Country_Code] = [c1].[ShippingAddress_Country_Code],
[c0].[ShippingAddress_Country_FullName] = [c1].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c0]
INNER JOIN (
SELECT [c].[Id], [c].[BillingAddress_AddressLine1], [c].[BillingAddress_AddressLine2], [c].[BillingAddress_Tags], [c].[BillingAddress_ZipCode], [c].[ShippingAddress_Country_Code], [c].[ShippingAddress_Country_FullName]
FROM [Customer] AS [c]
ORDER BY [c].[Id]
OFFSET @__p_0 ROWS
) AS [t] ON [c0].[Id] = [t].[Id]
) AS [c1] ON [c0].[Id] = [c1].[Id]
""");
}

Expand Down
Loading

0 comments on commit 973daee

Please sign in to comment.