Skip to content

Commit 27649af

Browse files
committed
Build and print the new IR program for each query
The new IR building and printing is tested in src/test/java/io/trino/sql/dialect/trino However, those unit tests have limited serialization capacity. Full serialization requires injected dependencies. With this change, each query run on the server will be rewritten into a new IR program and printed on the console. It will use dependencies for full serialization. Note that the queries are captured in SqlQueryExecution after they are planned and optimized, and right before they are fragmented. This is the moment where CTE reuse will kick in.
1 parent 61a2bee commit 27649af

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

core/trino-main/src/main/java/io/trino/execution/SqlQueryExecution.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import io.trino.sql.analyzer.Analysis;
6262
import io.trino.sql.analyzer.Analyzer;
6363
import io.trino.sql.analyzer.AnalyzerFactory;
64+
import io.trino.sql.dialect.trino.ProgramBuilder;
65+
import io.trino.sql.newir.FormatOptions;
6466
import io.trino.sql.planner.AdaptivePlanner;
6567
import io.trino.sql.planner.InputExtractor;
6668
import io.trino.sql.planner.LogicalPlanner;
@@ -148,6 +150,7 @@ public class SqlQueryExecution
148150
private final EventDrivenTaskSourceFactory eventDrivenTaskSourceFactory;
149151
private final TaskDescriptorStorage taskDescriptorStorage;
150152
private final PlanOptimizersStatsCollector planOptimizersStatsCollector;
153+
private final FormatOptions formatOptions;
151154

152155
private SqlQueryExecution(
153156
PreparedQuery preparedQuery,
@@ -185,7 +188,8 @@ private SqlQueryExecution(
185188
SqlTaskManager coordinatorTaskManager,
186189
ExchangeManagerRegistry exchangeManagerRegistry,
187190
EventDrivenTaskSourceFactory eventDrivenTaskSourceFactory,
188-
TaskDescriptorStorage taskDescriptorStorage)
191+
TaskDescriptorStorage taskDescriptorStorage,
192+
FormatOptions formatOptions)
189193
{
190194
try (SetThreadName _ = new SetThreadName("Query-" + stateMachine.getQueryId())) {
191195
this.slug = requireNonNull(slug, "slug is null");
@@ -240,6 +244,7 @@ private SqlQueryExecution(
240244
this.eventDrivenTaskSourceFactory = requireNonNull(eventDrivenTaskSourceFactory, "taskSourceFactory is null");
241245
this.taskDescriptorStorage = requireNonNull(taskDescriptorStorage, "taskDescriptorStorage is null");
242246
this.planOptimizersStatsCollector = requireNonNull(planOptimizersStatsCollector, "planOptimizersStatsCollector is null");
247+
this.formatOptions = requireNonNull(formatOptions, "formatOptions is null");
243248
}
244249
}
245250

@@ -503,6 +508,13 @@ private PlanRoot doPlanQuery(CachingTableStatsProvider tableStatsProvider)
503508
Plan plan = logicalPlanner.plan(analysis);
504509
queryPlan.set(plan);
505510

511+
try {
512+
System.out.println(ProgramBuilder.buildProgram(plan.getRoot()).print(1, formatOptions));
513+
}
514+
catch (UnsupportedOperationException e) {
515+
System.out.println("query not supported in the new IR: " + e.getMessage());
516+
}
517+
506518
// fragment the plan
507519
SubPlan fragmentedPlan;
508520
try (var _ = scopedSpan(tracer, "fragment-plan")) {
@@ -809,6 +821,7 @@ public static class SqlQueryExecutionFactory
809821
private final ExchangeManagerRegistry exchangeManagerRegistry;
810822
private final EventDrivenTaskSourceFactory eventDrivenTaskSourceFactory;
811823
private final TaskDescriptorStorage taskDescriptorStorage;
824+
private final FormatOptions formatOptions;
812825

813826
@Inject
814827
SqlQueryExecutionFactory(
@@ -841,7 +854,8 @@ public static class SqlQueryExecutionFactory
841854
SqlTaskManager coordinatorTaskManager,
842855
ExchangeManagerRegistry exchangeManagerRegistry,
843856
EventDrivenTaskSourceFactory eventDrivenTaskSourceFactory,
844-
TaskDescriptorStorage taskDescriptorStorage)
857+
TaskDescriptorStorage taskDescriptorStorage,
858+
FormatOptions formatOptions)
845859
{
846860
this.tracer = requireNonNull(tracer, "tracer is null");
847861
this.schedulerStats = requireNonNull(schedulerStats, "schedulerStats is null");
@@ -875,6 +889,7 @@ public static class SqlQueryExecutionFactory
875889
this.exchangeManagerRegistry = requireNonNull(exchangeManagerRegistry, "exchangeManagerRegistry is null");
876890
this.eventDrivenTaskSourceFactory = requireNonNull(eventDrivenTaskSourceFactory, "eventDrivenTaskSourceFactory is null");
877891
this.taskDescriptorStorage = requireNonNull(taskDescriptorStorage, "taskDescriptorStorage is null");
892+
this.formatOptions = requireNonNull(formatOptions, "formatOptions is null");
878893
}
879894

880895
@Override
@@ -925,7 +940,8 @@ public QueryExecution createQueryExecution(
925940
coordinatorTaskManager,
926941
exchangeManagerRegistry,
927942
eventDrivenTaskSourceFactory,
928-
taskDescriptorStorage);
943+
taskDescriptorStorage,
944+
formatOptions);
929945
}
930946
}
931947
}

0 commit comments

Comments
 (0)