-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add table stats cache #12196
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
Add table stats cache #12196
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.cost; | ||
|
|
||
| import io.trino.Session; | ||
| import io.trino.metadata.Metadata; | ||
| import io.trino.metadata.TableHandle; | ||
| import io.trino.spi.statistics.TableStatistics; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class CachingTableStatsProvider | ||
| implements TableStatsProvider | ||
| { | ||
| private final Metadata metadata; | ||
| private final Map<TableHandle, TableStatistics> cache = new HashMap<>(); | ||
|
|
||
| public CachingTableStatsProvider(Metadata metadata) | ||
| { | ||
| this.metadata = requireNonNull(metadata, "metadata is null"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Squash "Address comments" with respective commits. (You did rebase anyway, and split the commit into two in this PR, so keeping some changes as a fixup commit doesn't make review easier) |
||
| } | ||
|
|
||
| @Override | ||
| public TableStatistics getTableStatistics(Session session, TableHandle tableHandle) | ||
| { | ||
| TableStatistics stats = cache.get(tableHandle); | ||
| if (stats == null) { | ||
| stats = metadata.getTableStatistics(session, tableHandle); | ||
| cache.put(tableHandle, stats); | ||
| } | ||
| return stats; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,16 +28,18 @@ public interface StatsCalculator | |
| * @param sourceStats The stats provider for any child nodes' stats, if needed to compute stats for the {@code node} | ||
| * @param lookup Lookup to be used when resolving source nodes, allowing stats calculation to work within {@link IterativeOptimizer} | ||
| * @param types The type provider for all symbols in the scope. | ||
| * @param tableStatsProvider The table stats provider. | ||
| */ | ||
| PlanNodeStatsEstimate calculateStats( | ||
| PlanNode node, | ||
| StatsProvider sourceStats, | ||
| Lookup lookup, | ||
| Session session, | ||
| TypeProvider types); | ||
| TypeProvider types, | ||
| TableStatsProvider tableStatsProvider); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need a new entity here, the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can easily imagine The currently existing Now, these two concepts are different, but not exclusionary:
Now the question is, whether per-PlanNode basis and per-TableHandle basis are significantly different.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Actually they would be different. Plan nodes have no equality, they compare by identity. Upon each exit from IterativeOptimizer (exit from Memo), a fully new plan structure is created. This can be improved a bit (eg produce new plan only if anything changed), but still would break identity-based caching, except for root nodes (table scans). Thus, the solution would look more generic, but would not actually be. |
||
|
|
||
| static StatsCalculator noopStatsCalculator() | ||
| { | ||
| return (node, sourceStats, lookup, ignore, types) -> PlanNodeStatsEstimate.unknown(); | ||
| return (node, sourceStats, lookup, ignore, types, tableStatsProvider) -> PlanNodeStatsEstimate.unknown(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A connector creates new TableHandles during various
ConnectorMetadata.apply*calls.A table handle may become "seen" in the plan and then discarded, make obsolete.
I think we should use weak keys here. Otherwise we need to size this as a regular cache.
(
WeakHashMapprovides weak keys with equality-based lookup, so i'd recommend that)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand this comment.. My understanding is that
CachingTableStatsProvideris created per query and its lifecycle is within query planning. So the cache here can be GCed after the query planning. What issue did you see with it?