Skip to content

Redo SQL table alias management#32785

Merged
roji merged 2 commits intodotnet:mainfrom
roji:TableAliases
Jan 15, 2024
Merged

Redo SQL table alias management#32785
roji merged 2 commits intodotnet:mainfrom
roji:TableAliases

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented Jan 11, 2024

Note to reviewers: since we now generate unique aliases early - before constructing the table - and not later in a separate visitation, the alias ordering is changing (deeper nested tables have lower numbers); that's why so many SQL baselines are changing in this PR.

Closes #32784
Fixes #32757
Part of #32778
Fixes #26104

@roji roji requested a review from a team January 11, 2024 12:26
/// <returns>An expression representing a SELECT in a SQL tree.</returns>
SelectExpression Select(SqlExpression? projection);
[EntityFrameworkInternal]
SelectExpression Select(SqlExpression? projection, SqlAliasManager sqlAliasManager);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the longer-term plan, these are temporary - Select shouldn't be constructing tables within itself as it's doing now, but those tables should rather be constructed outside, i.e. in RelationalQueryableMethodTranslatingEV.

query = new SqlExpressionSimplifyingExpressionVisitor(RelationalDependencies.SqlExpressionFactory, _useRelationalNulls)
.Visit(query);
query = new RelationalValueConverterCompensatingExpressionVisitor(RelationalDependencies.SqlExpressionFactory).Visit(query);
var query5 = new SqlExpressionSimplifyingExpressionVisitor(RelationalDependencies.SqlExpressionFactory, _useRelationalNulls)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to make it easier to debug and compare multiple versions of the tree in the various phases.

/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public void ChangeTableAlias(int tableIndex, string newAlias, HashSet<TableReferenceExpression> visitedTableReferences)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very temporary hack to allow SqlAliasManager to change the alias during post-processing. The subsequent PR will remove TableReferenceExpression altogether and this will go away.

new SelectExpressionMutableVerifyingExpressionVisitor().Visit(query);
// Verifies that all table aliases are uniquely assigned without skipping over
// Which points to possible mutation of a SelectExpression being used in multiple places.
new TableAliasVerifyingExpressionVisitor().Visit(query);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This verification visitor no longer makes sense, but I'll be introducing something better to check correct table/column scoping, after TableReferenceExpression is gone.

Comment thread test/EFCore.SqlServer.FunctionalTests/Query/JsonQuerySqlServerTest.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlExpressions/SelectExpression.Helper.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs Outdated
Comment thread src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Comment thread src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
@roji roji merged commit ea01b9e into dotnet:main Jan 15, 2024
@roji roji deleted the TableAliases branch January 15, 2024 12:18
Comment thread src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
ranma42 added a commit to ranma42/efcore that referenced this pull request May 24, 2024
cincuranet pushed a commit that referenced this pull request May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants