-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Adaptive planning framework in FTE #20276
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
02befd8
Add RemoteSourceStatsRule based on stage runtime statistics
gaurav8297 33fd38b
Make PlanFragmentIdAllocator configurable in Fragmenter
gaurav8297 83eb052
Handle RemoteSourceNode in PlanFragmenter
gaurav8297 3821baa
Introduce AdaptivePlanner and AdaptivePlanNode
gaurav8297 f838c09
Implement AdaptivePartitioning rule through planner
gaurav8297 69f01b3
Use CachedTableStatsProvider across query
gaurav8297 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
core/trino-main/src/main/java/io/trino/cost/RemoteSourceStatsRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* 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.cost.StatsCalculator.Context; | ||
import io.trino.execution.scheduler.OutputDataSizeEstimate; | ||
import io.trino.matching.Pattern; | ||
import io.trino.spi.type.FixedWidthType; | ||
import io.trino.spi.type.Type; | ||
import io.trino.sql.planner.PlanFragment; | ||
import io.trino.sql.planner.Symbol; | ||
import io.trino.sql.planner.TypeProvider; | ||
import io.trino.sql.planner.plan.PlanFragmentId; | ||
import io.trino.sql.planner.plan.PlanNode; | ||
import io.trino.sql.planner.plan.RemoteSourceNode; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static com.google.common.base.Verify.verify; | ||
import static io.trino.cost.PlanNodeStatsEstimateMath.addStatsAndMaxDistinctValues; | ||
import static io.trino.execution.scheduler.faulttolerant.OutputStatsEstimator.OutputStatsEstimateResult; | ||
import static io.trino.sql.planner.plan.Patterns.remoteSourceNode; | ||
import static io.trino.util.MoreMath.firstNonNaN; | ||
import static java.lang.Double.NaN; | ||
import static java.lang.Double.isNaN; | ||
|
||
public class RemoteSourceStatsRule | ||
extends SimpleStatsRule<RemoteSourceNode> | ||
{ | ||
private static final Pattern<RemoteSourceNode> PATTERN = remoteSourceNode(); | ||
|
||
public RemoteSourceStatsRule(StatsNormalizer normalizer) | ||
{ | ||
super(normalizer); | ||
} | ||
|
||
@Override | ||
public Pattern<RemoteSourceNode> getPattern() | ||
{ | ||
return PATTERN; | ||
} | ||
|
||
@Override | ||
protected Optional<PlanNodeStatsEstimate> doCalculate(RemoteSourceNode node, Context context) | ||
{ | ||
Optional<PlanNodeStatsEstimate> estimate = Optional.empty(); | ||
RuntimeInfoProvider runtimeInfoProvider = context.runtimeInfoProvider(); | ||
|
||
for (int i = 0; i < node.getSourceFragmentIds().size(); i++) { | ||
PlanFragmentId planFragmentId = node.getSourceFragmentIds().get(i); | ||
OutputStatsEstimateResult stageRuntimeStats = runtimeInfoProvider.getRuntimeOutputStats(planFragmentId); | ||
|
||
PlanNodeStatsEstimate stageEstimatedStats = getEstimatedStats(runtimeInfoProvider, context.statsProvider(), planFragmentId); | ||
PlanNodeStatsEstimate adjustedStageStats = adjustStats( | ||
node.getOutputSymbols(), | ||
context.types(), | ||
stageRuntimeStats, | ||
stageEstimatedStats); | ||
|
||
estimate = estimate | ||
.map(planNodeStatsEstimate -> addStatsAndMaxDistinctValues(planNodeStatsEstimate, adjustedStageStats)) | ||
.or(() -> Optional.of(adjustedStageStats)); | ||
} | ||
|
||
verify(estimate.isPresent()); | ||
return estimate; | ||
} | ||
|
||
private PlanNodeStatsEstimate getEstimatedStats( | ||
RuntimeInfoProvider runtimeInfoProvider, | ||
StatsProvider statsProvider, | ||
PlanFragmentId fragmentId) | ||
{ | ||
PlanFragment fragment = runtimeInfoProvider.getPlanFragment(fragmentId); | ||
PlanNode fragmentRoot = fragment.getRoot(); | ||
PlanNodeStatsEstimate estimate = fragment.getStatsAndCosts().getStats().get(fragmentRoot.getId()); | ||
// We will not have stats for the root node in a PlanFragment if collect_plan_statistics_for_all_queries | ||
// is disabled and query isn't an explain analyze. | ||
if (estimate != null && !estimate.isOutputRowCountUnknown()) { | ||
return estimate; | ||
} | ||
return statsProvider.getStats(fragmentRoot); | ||
} | ||
|
||
private PlanNodeStatsEstimate adjustStats( | ||
List<Symbol> outputs, | ||
TypeProvider typeProvider, | ||
OutputStatsEstimateResult runtimeStats, | ||
PlanNodeStatsEstimate estimateStats) | ||
{ | ||
if (runtimeStats.isUnknown()) { | ||
return estimateStats; | ||
} | ||
|
||
// We prefer runtime stats over estimated stats, because runtime stats are more accurate. | ||
OutputDataSizeEstimate outputDataSizeEstimate = runtimeStats.outputDataSizeEstimate(); | ||
gaurav8297 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PlanNodeStatsEstimate.Builder result = PlanNodeStatsEstimate.builder() | ||
.setOutputRowCount(runtimeStats.outputRowCountEstimate()); | ||
|
||
double fixedWidthTypeSize = 0; | ||
double variableTypeValuesCount = 0; | ||
|
||
for (Symbol outputSymbol : outputs) { | ||
Type type = typeProvider.get(outputSymbol); | ||
SymbolStatsEstimate symbolStatistics = estimateStats.getSymbolStatistics(outputSymbol); | ||
double nullsFraction = firstNonNaN(symbolStatistics.getNullsFraction(), 0d); | ||
double numberOfNonNullRows = runtimeStats.outputRowCountEstimate() * (1.0 - nullsFraction); | ||
|
||
if (type instanceof FixedWidthType) { | ||
fixedWidthTypeSize += numberOfNonNullRows * ((FixedWidthType) type).getFixedSize(); | ||
} | ||
else { | ||
variableTypeValuesCount += numberOfNonNullRows; | ||
} | ||
} | ||
|
||
double runtimeOutputDataSize = outputDataSizeEstimate.getTotalSizeInBytes(); | ||
double variableTypeValueAverageSize = NaN; | ||
if (variableTypeValuesCount > 0 && runtimeOutputDataSize > fixedWidthTypeSize) { | ||
variableTypeValueAverageSize = (runtimeOutputDataSize - fixedWidthTypeSize) / variableTypeValuesCount; | ||
} | ||
|
||
for (Symbol outputSymbol : outputs) { | ||
SymbolStatsEstimate symbolStatistics = estimateStats.getSymbolStatistics(outputSymbol); | ||
Type type = typeProvider.get(outputSymbol); | ||
if (!(isNaN(variableTypeValueAverageSize) || type instanceof FixedWidthType)) { | ||
symbolStatistics = SymbolStatsEstimate.buildFrom(symbolStatistics) | ||
.setAverageRowSize(variableTypeValueAverageSize) | ||
.build(); | ||
} | ||
result.addSymbolStatistics(outputSymbol, symbolStatistics); | ||
} | ||
|
||
return result.build(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
core/trino-main/src/main/java/io/trino/cost/RuntimeInfoProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.sql.planner.PlanFragment; | ||
import io.trino.sql.planner.plan.PlanFragmentId; | ||
|
||
import java.util.List; | ||
|
||
import static io.trino.execution.scheduler.faulttolerant.OutputStatsEstimator.OutputStatsEstimateResult; | ||
|
||
/** | ||
* Provides runtime information from FTE execution. This is used to re-optimize the plan based | ||
* on the actual runtime statistics. | ||
*/ | ||
public interface RuntimeInfoProvider | ||
{ | ||
OutputStatsEstimateResult getRuntimeOutputStats(PlanFragmentId planFragmentId); | ||
|
||
PlanFragment getPlanFragment(PlanFragmentId planFragmentId); | ||
|
||
List<PlanFragment> getAllPlanFragments(); | ||
|
||
static RuntimeInfoProvider noImplementation() | ||
{ | ||
return new RuntimeInfoProvider() | ||
{ | ||
@Override | ||
public OutputStatsEstimateResult getRuntimeOutputStats(PlanFragmentId planFragmentId) | ||
{ | ||
throw new UnsupportedOperationException("RuntimeInfoProvider is not implemented"); | ||
} | ||
|
||
@Override | ||
public PlanFragment getPlanFragment(PlanFragmentId planFragmentId) | ||
{ | ||
throw new UnsupportedOperationException("RuntimeInfoProvider is not implemented"); | ||
} | ||
|
||
@Override | ||
public List<PlanFragment> getAllPlanFragments() | ||
{ | ||
throw new UnsupportedOperationException("RuntimeInfoProvider is not implemented"); | ||
} | ||
}; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
core/trino-main/src/main/java/io/trino/cost/StaticRuntimeInfoProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.trino.sql.planner.PlanFragment; | ||
import io.trino.sql.planner.plan.PlanFragmentId; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static io.trino.execution.scheduler.faulttolerant.OutputStatsEstimator.OutputStatsEstimateResult; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class StaticRuntimeInfoProvider | ||
implements RuntimeInfoProvider | ||
{ | ||
private final Map<PlanFragmentId, OutputStatsEstimateResult> runtimeOutputStats; | ||
private final Map<PlanFragmentId, PlanFragment> planFragments; | ||
|
||
public StaticRuntimeInfoProvider( | ||
Map<PlanFragmentId, OutputStatsEstimateResult> runtimeOutputStats, | ||
Map<PlanFragmentId, PlanFragment> planFragments) | ||
{ | ||
this.runtimeOutputStats = ImmutableMap.copyOf(requireNonNull(runtimeOutputStats, "runtimeOutputStats is null")); | ||
this.planFragments = ImmutableMap.copyOf(requireNonNull(planFragments, "planFragments is null")); | ||
} | ||
|
||
@Override | ||
public OutputStatsEstimateResult getRuntimeOutputStats(PlanFragmentId planFragmentId) | ||
{ | ||
return runtimeOutputStats.getOrDefault(planFragmentId, OutputStatsEstimateResult.unknown()); | ||
} | ||
|
||
@Override | ||
public PlanFragment getPlanFragment(PlanFragmentId planFragmentId) | ||
{ | ||
PlanFragment planFragment = planFragments.get(planFragmentId); | ||
requireNonNull(planFragment, "planFragment must not be null: %s".formatted(planFragmentId)); | ||
return planFragment; | ||
} | ||
|
||
@Override | ||
public List<PlanFragment> getAllPlanFragments() | ||
{ | ||
return ImmutableList.copyOf(planFragments.values()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we refactor the code which governs what is put in
fragment.getStatsAndCosts().getStats()
so we always have some (possibly empty)PlanNodeStatsEstimate
in map so we can skip a null check?