Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,11 @@ private ScopedSpan optimizerSpan(PlanOptimizer optimizer)
if (!Span.fromContext(Context.current()).isRecording()) {
return null;
}
String optimizerName = (optimizer instanceof IterativeOptimizer iterative)
? "Iterative:" + iterative.getName()
: optimizer.getClass().getSimpleName();
SpanBuilder builder = plannerContext.getTracer().spanBuilder("optimize")
.setAttribute(TrinoAttributes.OPTIMIZER_NAME, optimizer.getClass().getSimpleName());
if (optimizer instanceof IterativeOptimizer iterative) {
builder.setAttribute(TrinoAttributes.OPTIMIZER_RULES, iterative.getRules().stream()
.map(x -> x.getClass().getSimpleName())
.toList());
}
.setAttribute(TrinoAttributes.OPTIMIZER_NAME, optimizerName);
return scopedSpan(builder.startSpan());
}

Expand Down
104 changes: 86 additions & 18 deletions core/trino-main/src/main/java/io/trino/sql/planner/PlanOptimizers.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class IterativeOptimizer
{
private static final Logger LOG = Logger.get(IterativeOptimizer.class);

private final String name;
private final RuleStatsRecorder stats;
private final StatsCalculator statsCalculator;
private final CostCalculator costCalculator;
Expand All @@ -77,13 +78,14 @@ public class IterativeOptimizer
private final Predicate<Session> useLegacyRules;
private final PlannerContext plannerContext;

public IterativeOptimizer(PlannerContext plannerContext, RuleStatsRecorder stats, StatsCalculator statsCalculator, CostCalculator costCalculator, Set<Rule<?>> rules)
public IterativeOptimizer(String name, PlannerContext plannerContext, RuleStatsRecorder stats, StatsCalculator statsCalculator, CostCalculator costCalculator, Set<Rule<?>> rules)
{
this(plannerContext, stats, statsCalculator, costCalculator, session -> false, ImmutableList.of(), rules);
this(name, plannerContext, stats, statsCalculator, costCalculator, session -> false, ImmutableList.of(), rules);
}

public IterativeOptimizer(PlannerContext plannerContext, RuleStatsRecorder stats, StatsCalculator statsCalculator, CostCalculator costCalculator, Predicate<Session> useLegacyRules, List<PlanOptimizer> legacyRules, Set<Rule<?>> newRules)
public IterativeOptimizer(String name, PlannerContext plannerContext, RuleStatsRecorder stats, StatsCalculator statsCalculator, CostCalculator costCalculator, Predicate<Session> useLegacyRules, List<PlanOptimizer> legacyRules, Set<Rule<?>> newRules)
{
this.name = requireNonNull(name, "name is null");
this.plannerContext = requireNonNull(plannerContext, "plannerContext is null");
this.stats = requireNonNull(stats, "stats is null");
this.statsCalculator = requireNonNull(statsCalculator, "statsCalculator is null");
Expand Down Expand Up @@ -130,6 +132,16 @@ public Result optimizeAndMarkPlanChanges(PlanNode plan, PlanOptimizer.Context co
return new Result(memo.extract(), ImmutableSet.copyOf(changedPlanNodeIds));
}

public IterativeOptimizer withName(String name)
{
return new IterativeOptimizer(name, plannerContext, stats, statsCalculator, costCalculator, useLegacyRules, legacyRules, rules);
}

public String getName()
{
return name;
}

// Used for diagnostics.
public Set<Rule<?>> getRules()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

import io.opentelemetry.api.common.AttributeKey;

import java.util.List;

import static io.opentelemetry.api.common.AttributeKey.booleanKey;
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;

public final class TrinoAttributes
Expand Down Expand Up @@ -47,7 +44,6 @@ private TrinoAttributes() {}
public static final AttributeKey<Boolean> CASCADE = booleanKey("trino.cascade");

public static final AttributeKey<String> OPTIMIZER_NAME = stringKey("trino.optimizer");
public static final AttributeKey<List<String>> OPTIMIZER_RULES = stringArrayKey("trino.optimizer.rules");

public static final AttributeKey<Long> SPLIT_BATCH_MAX_SIZE = longKey("trino.split_batch.max_size");
public static final AttributeKey<Long> SPLIT_BATCH_RESULT_SIZE = longKey("trino.split_batch.result_size");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static void assertPlannerWarnings(PlanTester planTester, @Language("SQL")
if (rules.isPresent()) {
// Warnings from testing rules will be added
planOptimizers = ImmutableList.of(new IterativeOptimizer(
"TestPlannerWarnings",
planTester.getPlannerContext(),
new RuleStatsRecorder(),
planTester.getStatsCalculator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public void testJoinOrderSwitchRule()
"SELECT n.name FROM supplier AS s JOIN nation AS n on s.nationkey = n.nationkey",
session,
ImmutableList.of(new IterativeOptimizer(
"TestJoinOrderSwitch",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down Expand Up @@ -151,6 +152,7 @@ SELECT max(s.nationkey), sum(t.regionkey)
""",
session,
ImmutableList.of(new IterativeOptimizer(
"TestJoinOrderSwitch",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down Expand Up @@ -224,6 +226,7 @@ SELECT max(distinct s.nationkey), sum(distinct t.regionkey)
""",
session,
ImmutableList.of(new IterativeOptimizer(
"TestJoinOrderSwitch",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down Expand Up @@ -260,6 +263,7 @@ public void testNoChangeToRootSubPlanIfStatsAreAccurate()
"SELECT n.name FROM supplier AS s JOIN nation AS n on s.nationkey = n.nationkey",
session,
ImmutableList.of(new IterativeOptimizer(
"TestJoinOrderSwitch",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down Expand Up @@ -328,6 +332,7 @@ SELECT max(s.nationkey), sum(t.regionkey)
""",
session,
ImmutableList.of(new IterativeOptimizer(
"TestJoinOrderSwitch",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down Expand Up @@ -376,6 +381,7 @@ public void testWhenSimilarColumnIsProjectedTwice()
""",
session,
ImmutableList.of(new IterativeOptimizer(
"TestJoinOrderSwitch",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void testDuplicatesInWindowOrderBy()
ImmutableList.of(
new UnaliasSymbolReferences(),
new IterativeOptimizer(
"TestRemoveIdentityProjections",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ protected void assertMinimallyOptimizedPlan(@Language("SQL") String sql, PlanMat
List<PlanOptimizer> optimizers = ImmutableList.of(
new UnaliasSymbolReferences(),
new IterativeOptimizer(
"TestMinimalPlanCleanup",
planTester.getPlannerContext(),
new RuleStatsRecorder(),
planTester.getStatsCalculator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void optimizerQueryRulesStatsCollect()
try (PlanTester planTester = PlanTester.create(sessionBuilder.build())) {
PlanOptimizersStatsCollector planOptimizersStatsCollector = new PlanOptimizersStatsCollector(10);
PlanOptimizer optimizer = new IterativeOptimizer(
"TestRuleStatsCollection",
planTester.getPlannerContext(),
new RuleStatsRecorder(),
planTester.getStatsCalculator(),
Expand Down Expand Up @@ -97,6 +98,7 @@ public void optimizerTimeoutsOnNonConvergingPlan()
ImmutableMap.of());

PlanOptimizer optimizer = new IterativeOptimizer(
"TestTimeoutOnNonConvergingPlan",
planTester.getPlannerContext(),
new RuleStatsRecorder(),
planTester.getStatsCalculator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private void assertUnitPlan(@Language("SQL") String sql, PlanMatchPattern patter
{
List<PlanOptimizer> optimizers = ImmutableList.of(
new IterativeOptimizer(
"TestEliminateSorts",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ private void assertUnitPlan(@Language("SQL") String sql, PlanMatchPattern patter
List<PlanOptimizer> optimizers = ImmutableList.of(
new UnaliasSymbolReferences(),
new IterativeOptimizer(
"TestMergeWindows",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ private void assertUnitPlan(@Language("SQL") String sql, PlanMatchPattern patter
false,
false),
new IterativeOptimizer(
"TestReorderWindows",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ protected void assertPlan(String sql, PlanMatchPattern pattern)
List<PlanOptimizer> optimizers = ImmutableList.of(
new UnaliasSymbolReferences(),
new IterativeOptimizer(
"TestSetFlattening",
getPlanTester().getPlannerContext(),
new RuleStatsRecorder(),
getPlanTester().getStatsCalculator(),
Expand Down