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

Query: TPC prune unused concrete tables and columns #27993

Merged
merged 1 commit into from
May 11, 2022
Merged

Conversation

smitpatel
Copy link
Contributor

  • Add TpcTablesExpression which holds all union all tables so we can prune unused subqueries
  • Also allow pruning of unused column inside each subquery
  • Assign unique aliases in inner subqueries across whole SelectExpression
  • Apply predicate on discriminator column directly to subqueries by removing unnecessary subqueries

Resolves #27967
Resolves #27957

@smitpatel smitpatel requested a review from a team May 10, 2022 17:43
SELECT [k].[Species], [k].[CountryId], [k].[Name], [k].[EagleId], [k].[IsFlightless], NULL AS [Group], [k].[FoundOn], N'Kiwi' AS [Discriminator]
FROM [Kiwi] AS [k]
) AS [t]
WHERE [t].[CountryId] = 1 AND [t].[Discriminator] = N'Kiwi'");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Table pruning example

@@ -77,10 +74,10 @@ public override async Task Can_use_is_kiwi_in_projection(bool async)
ELSE CAST(0 AS bit)
END
FROM (
SELECT [e].[Species], [e].[CountryId], [e].[Name], [e].[EagleId], [e].[IsFlightless], [e].[Group], NULL AS [FoundOn], N'Eagle' AS [Discriminator]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Column pruning example

@@ -1350,6 +1328,59 @@ public void ApplyPredicate(SqlExpression sqlExpression)
sqlExpression = PushdownIntoSubqueryInternal().Remap(sqlExpression);
}

if ((sqlExpression is SqlBinaryExpression { OperatorType: ExpressionType.Equal }
|| sqlExpression is InExpression { Subquery: null, IsNegated: false })
&& _groupBy.Count == 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only condition in Select before swallowing predicate and pruning table.
@maumar to review.
Limit/Offset are deal with above itself. If they are present, pushdown will happen and we won't find the Tpc table anyway.
Other predicates are fine since they do AND so it will work.
GroupBy is not allowed, also implies no HAVING
DISTINCT is ok because it applies after filtering.
Other tables in SelectExpression doesn't affect the base as predicate applies to whole result. For TPC in joined table, the predicate would be specified before the join (that scenario works already). If predicate is applied to result of join then it becomes complicated to track down TPC because it would be inside subquery in joined table. User can always do filtering before.
Ordering is applied after filtering so that is also fine (unless limit/offset which is already handled).

Do we need anymore condition (certain thing on Select) which makes this optimization incorrect?

Copy link
Contributor

Choose a reason for hiding this comment

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

could be useful to add this comment in the code

- Add TpcTablesExpression which holds all union all tables so we can prune unused subqueries
- Also allow pruning of unused column inside each subquery
- Assign unique aliases in inner subqueries across whole SelectExpression
- Apply predicate on discriminator column directly to subqueries by removing unnecessary subqueries

Resolves #27967
Resolves #27957
Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

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

Another reviewer would be good

UNION ALL
SELECT [o0].[Nickname], [o0].[SquadId], [o0].[AssignedCityName], [o0].[CityOfBirthName], [o0].[FullName], [o0].[HasSoulPatch], [o0].[LeaderNickname], [o0].[LeaderSquadId], [o0].[Rank], N'Officer' AS [Discriminator]
FROM [Officers] AS [o0]
SELECT [t2].[Nickname], [t2].[Rank], [t2].[SquadId], [t2].[FullName], [t2].[LeaderNickname], [t2].[LeaderSquadId]
Copy link
Contributor

Choose a reason for hiding this comment

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

we could lift this case also

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Filed #28009

FROM [Officers] AS [o4]
) AS [t7] ON [s].[Id] = [t7].[SquadId]
SELECT [t10].[Nickname], [t10].[HasSoulPatch], [t10].[SquadId]
FROM (
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

UNION ALL
SELECT [o0].[Nickname], [o0].[SquadId], [o0].[AssignedCityName], [o0].[CityOfBirthName], [o0].[FullName], [o0].[HasSoulPatch], [o0].[LeaderNickname], [o0].[LeaderSquadId], [o0].[Rank], N'Officer' AS [Discriminator]
FROM [Officers] AS [o0]
SELECT [t3].[FullName], [t3].[Nickname], [t3].[SquadId], [t3].[LeaderNickname], [t3].[LeaderSquadId]
Copy link
Contributor

Choose a reason for hiding this comment

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

... and here

@smitpatel smitpatel merged commit 83b5c80 into main May 11, 2022
@smitpatel smitpatel deleted the smit/tptsharpe branch May 11, 2022 23:57
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