Skip to content

Commit

Permalink
Simplify analyzer setup and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Nov 28, 2021
1 parent eb5b77d commit 3ac9061
Show file tree
Hide file tree
Showing 26 changed files with 489 additions and 381 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
import com.google.common.util.concurrent.ListenableFuture;
import io.trino.Session;
import io.trino.connector.CatalogName;
import io.trino.cost.StatsCalculator;
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.MaterializedViewDefinition;
import io.trino.metadata.Metadata;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.ViewColumn;
import io.trino.security.AccessControl;
import io.trino.spi.security.GroupProvider;
import io.trino.sql.analyzer.Analysis;
import io.trino.sql.analyzer.Analyzer;
import io.trino.sql.analyzer.AnalyzerFactory;
import io.trino.sql.parser.SqlParser;
import io.trino.sql.tree.CreateMaterializedView;
import io.trino.sql.tree.Expression;
Expand Down Expand Up @@ -53,17 +51,15 @@ public class CreateMaterializedViewTask
private final Metadata metadata;
private final AccessControl accessControl;
private final SqlParser sqlParser;
private final GroupProvider groupProvider;
private final StatsCalculator statsCalculator;
private final AnalyzerFactory analyzerFactory;

@Inject
public CreateMaterializedViewTask(Metadata metadata, AccessControl accessControl, SqlParser sqlParser, GroupProvider groupProvider, StatsCalculator statsCalculator)
public CreateMaterializedViewTask(Metadata metadata, AccessControl accessControl, SqlParser sqlParser, AnalyzerFactory analyzerFactory)
{
this.metadata = requireNonNull(metadata, "metadata is null");
this.accessControl = requireNonNull(accessControl, "accessControl is null");
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null");
this.groupProvider = requireNonNull(groupProvider, "groupProvider is null");
this.statsCalculator = requireNonNull(statsCalculator, "statsCalculator is null");
this.analyzerFactory = requireNonNull(analyzerFactory, "analyzerFactory is null");
}

@Override
Expand All @@ -85,7 +81,7 @@ public ListenableFuture<Void> execute(

String sql = getFormattedSql(statement.getQuery(), sqlParser);

Analysis analysis = new Analyzer(session, metadata, sqlParser, groupProvider, accessControl, Optional.empty(), parameters, parameterLookup, stateMachine.getWarningCollector(), statsCalculator)
Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterLookup, stateMachine.getWarningCollector())
.analyze(statement);

List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@

import com.google.common.util.concurrent.ListenableFuture;
import io.trino.Session;
import io.trino.cost.StatsCalculator;
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.ViewColumn;
import io.trino.metadata.ViewDefinition;
import io.trino.security.AccessControl;
import io.trino.spi.security.GroupProvider;
import io.trino.spi.security.Identity;
import io.trino.sql.analyzer.Analysis;
import io.trino.sql.analyzer.Analyzer;
import io.trino.sql.analyzer.AnalyzerFactory;
import io.trino.sql.parser.SqlParser;
import io.trino.sql.tree.CreateView;
import io.trino.sql.tree.Expression;
Expand All @@ -49,17 +47,15 @@ public class CreateViewTask
private final Metadata metadata;
private final AccessControl accessControl;
private final SqlParser sqlParser;
private final GroupProvider groupProvider;
private final StatsCalculator statsCalculator;
private final AnalyzerFactory analyzerFactory;

@Inject
public CreateViewTask(Metadata metadata, AccessControl accessControl, SqlParser sqlParser, GroupProvider groupProvider, StatsCalculator statsCalculator)
public CreateViewTask(Metadata metadata, AccessControl accessControl, SqlParser sqlParser, AnalyzerFactory analyzerFactory)
{
this.metadata = requireNonNull(metadata, "metadata is null");
this.accessControl = requireNonNull(accessControl, "accessControl is null");
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null");
this.groupProvider = requireNonNull(groupProvider, "groupProvider is null");
this.statsCalculator = requireNonNull(statsCalculator, "statsCalculator is null");
this.analyzerFactory = requireNonNull(analyzerFactory, "analyzerFactory is null");
}

@Override
Expand All @@ -82,7 +78,7 @@ public ListenableFuture<Void> execute(

String sql = getFormattedSql(statement.getQuery(), sqlParser);

Analysis analysis = new Analyzer(session, metadata, sqlParser, groupProvider, accessControl, Optional.empty(), parameters, parameterExtractor(statement, parameters), stateMachine.getWarningCollector(), statsCalculator)
Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterExtractor(statement, parameters), stateMachine.getWarningCollector())
.analyze(statement);

List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.google.common.util.concurrent.ListenableFuture;
import io.airlift.slice.Slice;
import io.trino.Session;
import io.trino.cost.StatsCalculator;
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.metadata.ResolvedFunction;
Expand All @@ -28,7 +27,7 @@
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;
import io.trino.sql.analyzer.Analysis;
import io.trino.sql.analyzer.Analyzer;
import io.trino.sql.analyzer.AnalyzerFactory;
import io.trino.sql.analyzer.CorrelationSupport;
import io.trino.sql.analyzer.ExpressionAnalysis;
import io.trino.sql.analyzer.ExpressionAnalyzer;
Expand All @@ -49,7 +48,6 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TimeZone;

import static com.google.common.base.Preconditions.checkArgument;
Expand All @@ -70,17 +68,17 @@ public class SetTimeZoneTask
private final Metadata metadata;
private final AccessControl accessControl;
private final SqlParser sqlParser;
private final AnalyzerFactory analyzerFactory;
private final GroupProvider groupProvider;
private final StatsCalculator statsCalculator;

@Inject
public SetTimeZoneTask(Metadata metadata, AccessControl accessControl, SqlParser sqlParser, GroupProvider groupProvider, StatsCalculator statsCalculator)
public SetTimeZoneTask(Metadata metadata, AccessControl accessControl, SqlParser sqlParser, AnalyzerFactory analyzerFactory, GroupProvider groupProvider)
{
this.metadata = requireNonNull(metadata, "metadata is null");
this.accessControl = requireNonNull(accessControl, "accessControl is null");
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null");
this.analyzerFactory = requireNonNull(analyzerFactory, "analyzerFactory is null");
this.groupProvider = requireNonNull(groupProvider, "groupProvider is null");
this.statsCalculator = requireNonNull(statsCalculator, "statsCalculator is null");
}

@Override
Expand Down Expand Up @@ -112,7 +110,7 @@ private String getTimeZoneId(
WarningCollector warningCollector)
{
Session session = stateMachine.getSession();
Analysis analysis = analyzeStatement(statement, metadata, accessControl, stateMachine, parameters, session);
Analysis analysis = analyzeStatement(statement, stateMachine, parameters, session);
Scope scope = Scope.builder()
.withRelationType(RelationId.anonymous(), new RelationType())
.build();
Expand Down Expand Up @@ -154,23 +152,15 @@ else if (timeZoneValue instanceof Long) {

private Analysis analyzeStatement(
SetTimeZone statement,
Metadata metadata,
AccessControl accessControl,
QueryStateMachine stateMachine,
List<Expression> parameters,
Session session)
{
return new Analyzer(
return analyzerFactory.createAnalyzer(
session,
metadata,
sqlParser,
groupProvider,
accessControl,
Optional.empty(),
parameters,
parameterExtractor(statement, parameters),
stateMachine.getWarningCollector(),
statsCalculator)
stateMachine.getWarningCollector())
.analyze(statement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,17 @@
import io.trino.metadata.Metadata;
import io.trino.metadata.TableHandle;
import io.trino.operator.ForScheduler;
import io.trino.security.AccessControl;
import io.trino.server.BasicQueryInfo;
import io.trino.server.DynamicFilterService;
import io.trino.server.protocol.Slug;
import io.trino.spi.QueryId;
import io.trino.spi.TrinoException;
import io.trino.spi.security.GroupProvider;
import io.trino.spi.type.TypeOperators;
import io.trino.split.SplitManager;
import io.trino.split.SplitSource;
import io.trino.sql.analyzer.Analysis;
import io.trino.sql.analyzer.Analyzer;
import io.trino.sql.analyzer.QueryExplainer;
import io.trino.sql.parser.SqlParser;
import io.trino.sql.analyzer.AnalyzerFactory;
import io.trino.sql.planner.DistributedExecutionPlanner;
import io.trino.sql.planner.InputExtractor;
import io.trino.sql.planner.LogicalPlanner;
Expand Down Expand Up @@ -140,9 +137,7 @@ private SqlQueryExecution(
Slug slug,
Metadata metadata,
TypeOperators typeOperators,
GroupProvider groupProvider,
AccessControl accessControl,
SqlParser sqlParser,
AnalyzerFactory analyzerFactory,
SplitManager splitManager,
NodePartitioningManager nodePartitioningManager,
NodeScheduler nodeScheduler,
Expand All @@ -154,7 +149,6 @@ private SqlQueryExecution(
ScheduledExecutorService schedulerExecutor,
FailureDetector failureDetector,
NodeTaskMap nodeTaskMap,
QueryExplainer queryExplainer,
ExecutionPolicy executionPolicy,
SplitSchedulerStats schedulerStats,
StatsCalculator statsCalculator,
Expand Down Expand Up @@ -190,7 +184,7 @@ private SqlQueryExecution(
this.stateMachine = requireNonNull(stateMachine, "stateMachine is null");

// analyze query
this.analysis = analyze(preparedQuery, stateMachine, metadata, groupProvider, accessControl, sqlParser, queryExplainer, warningCollector);
this.analysis = analyze(preparedQuery, stateMachine, warningCollector, analyzerFactory);

stateMachine.addStateChangeListener(state -> {
if (!state.isDone()) {
Expand Down Expand Up @@ -246,30 +240,20 @@ private synchronized void unregisterDynamicFilteringQuery(DynamicFiltersStats fi
dynamicFilterService.removeQuery(stateMachine.getQueryId());
}

private Analysis analyze(
private static Analysis analyze(
PreparedQuery preparedQuery,
QueryStateMachine stateMachine,
Metadata metadata,
GroupProvider groupProvider,
AccessControl accessControl,
SqlParser sqlParser,
QueryExplainer queryExplainer,
WarningCollector warningCollector)
WarningCollector warningCollector,
AnalyzerFactory analyzerFactory)
{
stateMachine.beginAnalysis();

requireNonNull(preparedQuery, "preparedQuery is null");
Analyzer analyzer = new Analyzer(
Analyzer analyzer = analyzerFactory.createAnalyzer(
stateMachine.getSession(),
metadata,
sqlParser,
groupProvider,
accessControl,
Optional.of(queryExplainer),
preparedQuery.getParameters(),
parameterExtractor(preparedQuery.getStatement(), preparedQuery.getParameters()),
warningCollector,
statsCalculator);
warningCollector);
Analysis analysis;
try {
analysis = analyzer.analyze(preparedQuery.getStatement());
Expand Down Expand Up @@ -732,16 +716,13 @@ public static class SqlQueryExecutionFactory
private final int scheduleSplitBatchSize;
private final Metadata metadata;
private final TypeOperators typeOperators;
private final GroupProvider groupProvider;
private final AccessControl accessControl;
private final SqlParser sqlParser;
private final AnalyzerFactory analyzerFactory;
private final SplitManager splitManager;
private final NodePartitioningManager nodePartitioningManager;
private final NodeScheduler nodeScheduler;
private final List<PlanOptimizer> planOptimizers;
private final PlanFragmenter planFragmenter;
private final RemoteTaskFactory remoteTaskFactory;
private final QueryExplainer queryExplainer;
private final ExecutorService queryExecutor;
private final ScheduledExecutorService schedulerExecutor;
private final FailureDetector failureDetector;
Expand All @@ -758,9 +739,7 @@ public static class SqlQueryExecutionFactory
QueryManagerConfig config,
Metadata metadata,
TypeOperators typeOperators,
GroupProvider groupProvider,
AccessControl accessControl,
SqlParser sqlParser,
AnalyzerFactory analyzerFactory,
SplitManager splitManager,
NodePartitioningManager nodePartitioningManager,
NodeScheduler nodeScheduler,
Expand All @@ -771,7 +750,6 @@ public static class SqlQueryExecutionFactory
@ForScheduler ScheduledExecutorService schedulerExecutor,
FailureDetector failureDetector,
NodeTaskMap nodeTaskMap,
QueryExplainer queryExplainer,
Map<String, ExecutionPolicy> executionPolicies,
SplitSchedulerStats schedulerStats,
StatsCalculator statsCalculator,
Expand All @@ -785,9 +763,7 @@ public static class SqlQueryExecutionFactory
this.scheduleSplitBatchSize = config.getScheduleSplitBatchSize();
this.metadata = requireNonNull(metadata, "metadata is null");
this.typeOperators = requireNonNull(typeOperators, "typeOperators is null");
this.groupProvider = requireNonNull(groupProvider, "groupProvider is null");
this.accessControl = requireNonNull(accessControl, "accessControl is null");
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null");
this.analyzerFactory = requireNonNull(analyzerFactory, "analyzerFactory is null");
this.splitManager = requireNonNull(splitManager, "splitManager is null");
this.nodePartitioningManager = requireNonNull(nodePartitioningManager, "nodePartitioningManager is null");
this.nodeScheduler = requireNonNull(nodeScheduler, "nodeScheduler is null");
Expand All @@ -797,7 +773,6 @@ public static class SqlQueryExecutionFactory
this.schedulerExecutor = requireNonNull(schedulerExecutor, "schedulerExecutor is null");
this.failureDetector = requireNonNull(failureDetector, "failureDetector is null");
this.nodeTaskMap = requireNonNull(nodeTaskMap, "nodeTaskMap is null");
this.queryExplainer = requireNonNull(queryExplainer, "queryExplainer is null");
this.executionPolicies = requireNonNull(executionPolicies, "executionPolicies is null");
this.planOptimizers = requireNonNull(planOptimizersFactory, "planOptimizersFactory is null").get();
this.statsCalculator = requireNonNull(statsCalculator, "statsCalculator is null");
Expand All @@ -824,9 +799,7 @@ public QueryExecution createQueryExecution(
slug,
metadata,
typeOperators,
groupProvider,
accessControl,
sqlParser,
analyzerFactory,
splitManager,
nodePartitioningManager,
nodeScheduler,
Expand All @@ -838,7 +811,6 @@ public QueryExecution createQueryExecution(
schedulerExecutor,
failureDetector,
nodeTaskMap,
queryExplainer,
executionPolicy,
schedulerStats,
statsCalculator,
Expand Down
Loading

0 comments on commit 3ac9061

Please sign in to comment.