Skip to content
Closed
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 @@ -25,6 +25,7 @@
import io.trino.sql.PlannerContext;
import io.trino.sql.analyzer.Analysis;
import io.trino.sql.analyzer.AnalyzerFactory;
import io.trino.sql.analyzer.QueryAnalyzerFactory;
import io.trino.sql.parser.SqlParser;
import io.trino.sql.tree.CreateMaterializedView;
import io.trino.sql.tree.Expression;
Expand Down Expand Up @@ -53,20 +54,23 @@ public class CreateMaterializedViewTask
private final SqlParser sqlParser;
private final AnalyzerFactory analyzerFactory;
private final MaterializedViewPropertyManager materializedViewPropertyManager;
private final QueryAnalyzerFactory queryAnalyzerFactory;

@Inject
public CreateMaterializedViewTask(
PlannerContext plannerContext,
AccessControl accessControl,
SqlParser sqlParser,
AnalyzerFactory analyzerFactory,
MaterializedViewPropertyManager materializedViewPropertyManager)
MaterializedViewPropertyManager materializedViewPropertyManager,
QueryAnalyzerFactory queryAnalyzerFactory)
{
this.plannerContext = requireNonNull(plannerContext, "plannerContext is null");
this.accessControl = requireNonNull(accessControl, "accessControl is null");
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null");
this.analyzerFactory = requireNonNull(analyzerFactory, "analyzerFactory is null");
this.materializedViewPropertyManager = requireNonNull(materializedViewPropertyManager, "materializedViewPropertyManager is null");
this.queryAnalyzerFactory = requireNonNull(queryAnalyzerFactory, "queryAnalyzerFactory is null");
}

@Override
Expand All @@ -88,7 +92,7 @@ public ListenableFuture<Void> execute(

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

Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterLookup, stateMachine.getWarningCollector())
Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterLookup, stateMachine.getWarningCollector(), queryAnalyzerFactory)
.analyze(statement);

List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.trino.spi.security.Identity;
import io.trino.sql.analyzer.Analysis;
import io.trino.sql.analyzer.AnalyzerFactory;
import io.trino.sql.analyzer.QueryAnalyzerFactory;
import io.trino.sql.parser.SqlParser;
import io.trino.sql.tree.CreateView;
import io.trino.sql.tree.Expression;
Expand All @@ -50,14 +51,16 @@ public class CreateViewTask
private final AccessControl accessControl;
private final SqlParser sqlParser;
private final AnalyzerFactory analyzerFactory;
private final QueryAnalyzerFactory queryAnalyzerFactory;

@Inject
public CreateViewTask(Metadata metadata, AccessControl accessControl, SqlParser sqlParser, AnalyzerFactory analyzerFactory)
public CreateViewTask(Metadata metadata, AccessControl accessControl, SqlParser sqlParser, AnalyzerFactory analyzerFactory, QueryAnalyzerFactory queryAnalyzerFactory)
{
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.queryAnalyzerFactory = requireNonNull(queryAnalyzerFactory, "queryAnalyzerFactory is null");
}

@Override
Expand Down Expand Up @@ -92,7 +95,7 @@ else if (metadata.getTableHandle(session, name).isPresent()) {

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

Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterExtractor(statement, parameters), stateMachine.getWarningCollector())
Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterExtractor(statement, parameters), stateMachine.getWarningCollector(), queryAnalyzerFactory)
.analyze(statement);

List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import io.trino.sql.analyzer.Analysis;
import io.trino.sql.analyzer.Analyzer;
import io.trino.sql.analyzer.AnalyzerFactory;
import io.trino.sql.analyzer.QueryAnalyzerFactory;
import io.trino.sql.planner.InputExtractor;
import io.trino.sql.planner.LogicalPlanner;
import io.trino.sql.planner.NodePartitioningManager;
Expand Down Expand Up @@ -135,6 +136,7 @@ private SqlQueryExecution(
Slug slug,
PlannerContext plannerContext,
AnalyzerFactory analyzerFactory,
QueryAnalyzerFactory queryAnalyzerFactory,
SplitSourceFactory splitSourceFactory,
NodePartitioningManager nodePartitioningManager,
NodeScheduler nodeScheduler,
Expand Down Expand Up @@ -190,7 +192,7 @@ private SqlQueryExecution(
this.stateMachine = requireNonNull(stateMachine, "stateMachine is null");

// analyze query
this.analysis = analyze(preparedQuery, stateMachine, warningCollector, analyzerFactory);
this.analysis = analyze(preparedQuery, stateMachine, warningCollector, analyzerFactory, queryAnalyzerFactory);

stateMachine.addStateChangeListener(state -> {
if (!state.isDone()) {
Expand Down Expand Up @@ -254,7 +256,8 @@ private static Analysis analyze(
PreparedQuery preparedQuery,
QueryStateMachine stateMachine,
WarningCollector warningCollector,
AnalyzerFactory analyzerFactory)
AnalyzerFactory analyzerFactory,
QueryAnalyzerFactory queryAnalyzerFactory)
{
stateMachine.beginAnalysis();

Expand All @@ -263,7 +266,8 @@ private static Analysis analyze(
stateMachine.getSession(),
preparedQuery.getParameters(),
parameterExtractor(preparedQuery.getStatement(), preparedQuery.getParameters()),
warningCollector);
warningCollector,
queryAnalyzerFactory);
Analysis analysis;
try {
analysis = analyzer.analyze(preparedQuery.getStatement());
Expand Down Expand Up @@ -710,6 +714,7 @@ public static class SqlQueryExecutionFactory
private final int scheduleSplitBatchSize;
private final PlannerContext plannerContext;
private final AnalyzerFactory analyzerFactory;
private final QueryAnalyzerFactory queryAnalyzerFactory;
private final SplitSourceFactory splitSourceFactory;
private final NodePartitioningManager nodePartitioningManager;
private final NodeScheduler nodeScheduler;
Expand Down Expand Up @@ -739,6 +744,7 @@ public static class SqlQueryExecutionFactory
QueryManagerConfig config,
PlannerContext plannerContext,
AnalyzerFactory analyzerFactory,
QueryAnalyzerFactory queryAnalyzerFactory,
SplitSourceFactory splitSourceFactory,
NodePartitioningManager nodePartitioningManager,
NodeScheduler nodeScheduler,
Expand Down Expand Up @@ -769,6 +775,7 @@ public static class SqlQueryExecutionFactory
this.scheduleSplitBatchSize = config.getScheduleSplitBatchSize();
this.plannerContext = requireNonNull(plannerContext, "plannerContext is null");
this.analyzerFactory = requireNonNull(analyzerFactory, "analyzerFactory is null");
this.queryAnalyzerFactory = requireNonNull(queryAnalyzerFactory, "queryAnalyzerFactory is null");
this.splitSourceFactory = requireNonNull(splitSourceFactory, "splitSourceFactory is null");
this.nodePartitioningManager = requireNonNull(nodePartitioningManager, "nodePartitioningManager is null");
this.nodeScheduler = requireNonNull(nodeScheduler, "nodeScheduler is null");
Expand Down Expand Up @@ -811,6 +818,7 @@ public QueryExecution createQueryExecution(
slug,
plannerContext,
analyzerFactory,
queryAnalyzerFactory,
splitSourceFactory,
nodePartitioningManager,
nodeScheduler,
Expand Down
2 changes: 2 additions & 0 deletions core/trino-main/src/main/java/io/trino/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public interface Metadata

Optional<TableHandle> getTableHandleForStatisticsCollection(Session session, QualifiedObjectName tableName, Map<String, Object> analyzeProperties);

Optional<TableHandle> getTableHandleForStatisticsCollection(Session session, QualifiedObjectName tableName, TupleDomain<ColumnHandle> tupleDomain);

Optional<TableExecuteHandle> getTableHandleForExecute(
Session session,
TableHandle tableHandle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,29 @@ public Optional<TableHandle> getTableHandleForStatisticsCollection(Session sessi
return Optional.empty();
}

@Override
public Optional<TableHandle> getTableHandleForStatisticsCollection(Session session, QualifiedObjectName tableName, TupleDomain<ColumnHandle> tupleDomain)
{
requireNonNull(tableName, "tableName is null");
requireNonNull(tupleDomain, "tupleDomain is null");

Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, tableName.getCatalogName());
if (catalog.isPresent()) {
CatalogMetadata catalogMetadata = catalog.get();
CatalogName catalogName = catalogMetadata.getConnectorId(session, tableName);
ConnectorMetadata metadata = catalogMetadata.getMetadataFor(session, catalogName);

ConnectorTableHandle tableHandle = metadata.getTableHandleForStatisticsCollection(session.toConnectorSession(catalogName), tableName.asSchemaTableName(), tupleDomain);
if (tableHandle != null) {
return Optional.of(new TableHandle(
catalogName,
tableHandle,
catalogMetadata.getTransactionHandleFor(catalogName)));
}
}
return Optional.empty();
}

@Override
public Optional<TableExecuteHandle> getTableHandleForExecute(Session session, TableHandle tableHandle, String procedure, Map<String, Object> executeProperties)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import io.trino.spi.VersionEmbedder;
import io.trino.spi.memory.ClusterMemoryPoolManager;
import io.trino.sql.analyzer.AnalyzerFactory;
import io.trino.sql.analyzer.QueryAnalyzerFactory;
import io.trino.sql.analyzer.QueryExplainerFactory;
import io.trino.sql.planner.OptimizerStatsMBeanExporter;
import io.trino.sql.planner.PlanFragmenter;
Expand Down Expand Up @@ -275,6 +276,9 @@ protected void setup(Binder binder)
// query explainer
binder.bind(QueryExplainerFactory.class).in(Scopes.SINGLETON);

// query analyzer
binder.bind(QueryAnalyzerFactory.class).in(Scopes.SINGLETON);

// explain analyze
binder.bind(ExplainAnalyzeContext.class).in(Scopes.SINGLETON);

Expand Down
15 changes: 10 additions & 5 deletions core/trino-main/src/main/java/io/trino/sql/analyzer/Analysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public class Analysis
private Optional<Insert> insert = Optional.empty();
private Optional<RefreshMaterializedViewAnalysis> refreshMaterializedView = Optional.empty();
private Optional<QualifiedObjectName> delegatedRefreshMaterializedView = Optional.empty();
private Optional<TableHandle> analyzeTarget = Optional.empty();
private Optional<List<TableHandle>> analyzeTargets = Optional.empty();
private Optional<List<ColumnSchema>> updatedColumns = Optional.empty();

private final QueryType queryType;
Expand Down Expand Up @@ -611,6 +611,11 @@ public Collection<TableHandle> getTables()
.collect(toImmutableList());
}

public Collection<NodeRef<Table>> getTableNodeRefs()
{
return ImmutableList.copyOf(tables.keySet());
}

public void registerTable(
Table table,
Optional<TableHandle> handle,
Expand Down Expand Up @@ -722,14 +727,14 @@ public ColumnHandle getColumn(Field field)
return columns.get(field);
}

public Optional<TableHandle> getAnalyzeTarget()
public Optional<List<TableHandle>> getAnalyzeTargets()
{
return analyzeTarget;
return analyzeTargets;
}

public void setAnalyzeTarget(TableHandle analyzeTarget)
public void setAnalyzeTargets(List<TableHandle> analyzeTargets)
{
this.analyzeTarget = Optional.of(analyzeTarget);
this.analyzeTargets = Optional.of(analyzeTargets);
}

public void setCreate(Create create)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Analyzer
private final Map<NodeRef<Parameter>, Expression> parameterLookup;
private final WarningCollector warningCollector;
private final StatementRewrite statementRewrite;
private final QueryAnalyzerFactory queryAnalyzerFactory;

Analyzer(
Session session,
Expand All @@ -55,7 +56,8 @@ public class Analyzer
List<Expression> parameters,
Map<NodeRef<Parameter>, Expression> parameterLookup,
WarningCollector warningCollector,
StatementRewrite statementRewrite)
StatementRewrite statementRewrite,
QueryAnalyzerFactory queryAnalyzerFactory)
{
this.session = requireNonNull(session, "session is null");
this.analyzerFactory = requireNonNull(analyzerFactory, "analyzerFactory is null");
Expand All @@ -64,6 +66,7 @@ public class Analyzer
this.parameterLookup = parameterLookup;
this.warningCollector = requireNonNull(warningCollector, "warningCollector is null");
this.statementRewrite = requireNonNull(statementRewrite, "statementRewrite is null");
this.queryAnalyzerFactory = requireNonNull(queryAnalyzerFactory, "queryAnalyzerFactory is null");
}

public Analysis analyze(Statement statement)
Expand All @@ -73,9 +76,9 @@ public Analysis analyze(Statement statement)

public Analysis analyze(Statement statement, QueryType queryType)
{
Statement rewrittenStatement = statementRewrite.rewrite(analyzerFactory, session, statement, parameters, parameterLookup, warningCollector);
Statement rewrittenStatement = statementRewrite.rewrite(analyzerFactory, session, statement, parameters, parameterLookup, warningCollector, queryAnalyzerFactory);
Analysis analysis = new Analysis(rewrittenStatement, parameterLookup, queryType);
StatementAnalyzer analyzer = statementAnalyzerFactory.createStatementAnalyzer(analysis, session, warningCollector, CorrelationSupport.ALLOWED);
StatementAnalyzer analyzer = statementAnalyzerFactory.createStatementAnalyzer(analysis, session, Optional.of(queryAnalyzerFactory.createQueryAnalyzer(analyzerFactory, statementAnalyzerFactory)), warningCollector, CorrelationSupport.ALLOWED);
analyzer.analyze(rewrittenStatement, Optional.empty());

// check column access permissions for each table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public Analyzer createAnalyzer(
Session session,
List<Expression> parameters,
Map<NodeRef<Parameter>, Expression> parameterLookup,
WarningCollector warningCollector)
WarningCollector warningCollector,
QueryAnalyzerFactory queryAnalyzerFactory)
{
return new Analyzer(
session,
Expand All @@ -52,6 +53,7 @@ public Analyzer createAnalyzer(
parameters,
parameterLookup,
warningCollector,
statementRewrite);
statementRewrite,
queryAnalyzerFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ private ExpressionAnalyzer(
StatementAnalyzerFactory statementAnalyzerFactory,
Analysis analysis,
Session session,
Optional<QueryAnalyzer> queryAnalyzer,
TypeProvider types,
WarningCollector warningCollector)
{
Expand All @@ -322,6 +323,7 @@ private ExpressionAnalyzer(
(node, correlationSupport) -> statementAnalyzerFactory.createStatementAnalyzer(
analysis,
session,
queryAnalyzer,
warningCollector,
correlationSupport),
session,
Expand Down Expand Up @@ -2759,10 +2761,11 @@ public static ExpressionAnalysis analyzePatternRecognitionExpression(
Scope scope,
Analysis analysis,
Expression expression,
Optional<QueryAnalyzer> queryAnalyzer,
WarningCollector warningCollector,
Set<String> labels)
{
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, TypeProvider.empty(), warningCollector);
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, queryAnalyzer, TypeProvider.empty(), warningCollector);
analyzer.analyze(expression, scope, labels);

updateAnalysis(analysis, analyzer, session, accessControl);
Expand All @@ -2784,14 +2787,15 @@ public static ExpressionAnalysis analyzeExpressions(
PlannerContext plannerContext,
StatementAnalyzerFactory statementAnalyzerFactory,
AccessControl accessControl,
Optional<QueryAnalyzer> queryAnalyzer,
TypeProvider types,
Iterable<Expression> expressions,
Map<NodeRef<Parameter>, Expression> parameters,
WarningCollector warningCollector,
QueryType queryType)
{
Analysis analysis = new Analysis(null, parameters, queryType);
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, types, warningCollector);
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, queryAnalyzer, types, warningCollector);
for (Expression expression : expressions) {
analyzer.analyze(
expression,
Expand Down Expand Up @@ -2820,10 +2824,11 @@ public static ExpressionAnalysis analyzeExpression(
Scope scope,
Analysis analysis,
Expression expression,
Optional<QueryAnalyzer> queryAnalyzer,
WarningCollector warningCollector,
CorrelationSupport correlationSupport)
{
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, TypeProvider.empty(), warningCollector);
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, queryAnalyzer, TypeProvider.empty(), warningCollector);
analyzer.analyze(expression, scope, correlationSupport);

updateAnalysis(analysis, analyzer, session, accessControl);
Expand All @@ -2848,12 +2853,13 @@ public static ExpressionAnalysis analyzeWindow(
AccessControl accessControl,
Scope scope,
Analysis analysis,
Optional<QueryAnalyzer> queryAnalyzer,
WarningCollector warningCollector,
CorrelationSupport correlationSupport,
ResolvedWindow window,
Node originalNode)
{
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, TypeProvider.empty(), warningCollector);
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(plannerContext, accessControl, statementAnalyzerFactory, analysis, session, queryAnalyzer, TypeProvider.empty(), warningCollector);
analyzer.analyzeWindow(window, scope, originalNode, correlationSupport);

updateAnalysis(analysis, analyzer, session, accessControl);
Expand Down
Loading