Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redo SQL table alias management #32785

Merged
merged 2 commits into from
Jan 15, 2024
Merged

Redo SQL table alias management #32785

merged 2 commits into from
Jan 15, 2024

Conversation

roji
Copy link
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
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
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
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
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.

@roji roji merged commit ea01b9e into dotnet:main Jan 15, 2024
7 checks passed
@roji roji deleted the TableAliases branch January 15, 2024 12:18
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