use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class CoordinatorModule method setup.
@Override
protected void setup(Binder binder) {
httpServerBinder(binder).bindResource("/ui", "webapp").withWelcomeFile("index.html");
httpServerBinder(binder).bindResource("/tableau", "webapp/tableau");
// discovery server
install(installModuleIf(EmbeddedDiscoveryConfig.class, EmbeddedDiscoveryConfig::isEnabled, new EmbeddedDiscoveryModule()));
// presto coordinator announcement
discoveryBinder(binder).bindHttpAnnouncement("presto-coordinator");
// statement resource
jsonCodecBinder(binder).bindJsonCodec(QueryInfo.class);
jsonCodecBinder(binder).bindJsonCodec(TaskInfo.class);
jsonCodecBinder(binder).bindJsonCodec(QueryResults.class);
jsonCodecBinder(binder).bindJsonCodec(SelectedRole.class);
jaxrsBinder(binder).bind(QueuedStatementResource.class);
jaxrsBinder(binder).bind(ExecutingStatementResource.class);
binder.bind(StatementHttpExecutionMBean.class).in(Scopes.SINGLETON);
newExporter(binder).export(StatementHttpExecutionMBean.class).withGeneratedName();
// resource for serving static content
jaxrsBinder(binder).bind(WebUiResource.class);
// failure detector
binder.install(new FailureDetectorModule());
jaxrsBinder(binder).bind(NodeResource.class);
jaxrsBinder(binder).bind(WorkerResource.class);
httpClientBinder(binder).bindHttpClient("workerInfo", ForWorkerInfo.class);
// query monitor
jsonCodecBinder(binder).bindJsonCodec(OperatorInfo.class);
configBinder(binder).bindConfig(QueryMonitorConfig.class);
binder.bind(QueryMonitor.class).in(Scopes.SINGLETON);
// query manager
jaxrsBinder(binder).bind(QueryResource.class);
jaxrsBinder(binder).bind(StageResource.class);
jaxrsBinder(binder).bind(QueryStateInfoResource.class);
jaxrsBinder(binder).bind(ResourceGroupStateInfoResource.class);
binder.bind(QueryIdGenerator.class).in(Scopes.SINGLETON);
binder.bind(QueryManager.class).to(SqlQueryManager.class).in(Scopes.SINGLETON);
newExporter(binder).export(QueryManager.class).withGeneratedName();
binder.bind(QueryPreparer.class).in(Scopes.SINGLETON);
binder.bind(SessionSupplier.class).to(QuerySessionSupplier.class).in(Scopes.SINGLETON);
binder.bind(InternalResourceGroupManager.class).in(Scopes.SINGLETON);
newExporter(binder).export(InternalResourceGroupManager.class).withGeneratedName();
binder.bind(ResourceGroupManager.class).to(InternalResourceGroupManager.class);
binder.bind(LegacyResourceGroupConfigurationManager.class).in(Scopes.SINGLETON);
binder.bind(RetryCircuitBreaker.class).in(Scopes.SINGLETON);
newExporter(binder).export(RetryCircuitBreaker.class).withGeneratedName();
binder.bind(LocalQueryProvider.class).in(Scopes.SINGLETON);
jaxrsBinder(binder).bind(TaskInfoResource.class);
// dispatcher
binder.bind(DispatchManager.class).in(Scopes.SINGLETON);
binder.bind(FailedDispatchQueryFactory.class).in(Scopes.SINGLETON);
binder.bind(DispatchExecutor.class).in(Scopes.SINGLETON);
// local dispatcher
binder.bind(DispatchQueryFactory.class).to(LocalDispatchQueryFactory.class);
// cluster memory manager
binder.bind(ClusterMemoryManager.class).in(Scopes.SINGLETON);
binder.bind(ClusterMemoryPoolManager.class).to(ClusterMemoryManager.class).in(Scopes.SINGLETON);
httpClientBinder(binder).bindHttpClient("memoryManager", ForMemoryManager.class).withTracing().withConfigDefaults(config -> {
config.setRequestTimeout(new Duration(10, SECONDS));
});
bindLowMemoryKiller(LowMemoryKillerPolicy.NONE, NoneLowMemoryKiller.class);
bindLowMemoryKiller(LowMemoryKillerPolicy.TOTAL_RESERVATION, TotalReservationLowMemoryKiller.class);
bindLowMemoryKiller(LowMemoryKillerPolicy.TOTAL_RESERVATION_ON_BLOCKED_NODES, TotalReservationOnBlockedNodesLowMemoryKiller.class);
newExporter(binder).export(ClusterMemoryManager.class).withGeneratedName();
// node monitor
binder.bind(ClusterSizeMonitor.class).in(Scopes.SINGLETON);
// statistics calculator
binder.install(new StatsCalculatorModule());
// cost calculator
binder.bind(TaskCountEstimator.class).in(Scopes.SINGLETON);
binder.bind(CostCalculator.class).to(CostCalculatorUsingExchanges.class).in(Scopes.SINGLETON);
binder.bind(CostCalculator.class).annotatedWith(EstimatedExchanges.class).to(CostCalculatorWithEstimatedExchanges.class).in(Scopes.SINGLETON);
binder.bind(CostComparator.class).in(Scopes.SINGLETON);
// cluster statistics
jaxrsBinder(binder).bind(ClusterStatsResource.class);
// planner
binder.bind(PlanFragmenter.class).in(Scopes.SINGLETON);
binder.bind(PlanOptimizers.class).in(Scopes.SINGLETON);
// query explainer
binder.bind(QueryExplainer.class).in(Scopes.SINGLETON);
// explain analyze
binder.bind(ExplainAnalyzeContext.class).in(Scopes.SINGLETON);
// execution scheduler
binder.bind(RemoteTaskFactory.class).to(HttpRemoteTaskFactory.class).in(Scopes.SINGLETON);
newExporter(binder).export(RemoteTaskFactory.class).withGeneratedName();
binder.bind(RemoteTaskStats.class).in(Scopes.SINGLETON);
newExporter(binder).export(RemoteTaskStats.class).withGeneratedName();
httpClientBinder(binder).bindHttpClient("scheduler", ForScheduler.class).withTracing().withFilter(GenerateTraceTokenRequestFilter.class).withConfigDefaults(config -> {
config.setRequestTimeout(new Duration(10, SECONDS));
config.setMaxConnectionsPerServer(250);
});
binder.bind(ScheduledExecutorService.class).annotatedWith(ForScheduler.class).toInstance(newSingleThreadScheduledExecutor(threadsNamed("stage-scheduler")));
// query execution
binder.bind(ExecutorService.class).annotatedWith(ForQueryExecution.class).toInstance(newCachedThreadPool(threadsNamed("query-execution-%s")));
binder.bind(QueryExecutionMBean.class).in(Scopes.SINGLETON);
newExporter(binder).export(QueryExecutionMBean.class).as(generatedNameOf(QueryExecution.class));
MapBinder<Class<? extends Statement>, QueryExecutionFactory<?>> executionBinder = newMapBinder(binder, new TypeLiteral<Class<? extends Statement>>() {
}, new TypeLiteral<QueryExecution.QueryExecutionFactory<?>>() {
});
binder.bind(SplitSchedulerStats.class).in(Scopes.SINGLETON);
newExporter(binder).export(SplitSchedulerStats.class).withGeneratedName();
binder.bind(SqlQueryExecutionFactory.class).in(Scopes.SINGLETON);
binder.bind(SectionExecutionFactory.class).in(Scopes.SINGLETON);
Set<Map.Entry<Class<? extends Statement>, QueryType>> queryTypes = getAllQueryTypes().entrySet();
// bind sql query statements to SqlQueryExecutionFactory
queryTypes.stream().filter(entry -> entry.getValue() != QueryType.DATA_DEFINITION).forEach(entry -> executionBinder.addBinding(entry.getKey()).to(SqlQueryExecutionFactory.class).in(Scopes.SINGLETON));
binder.bind(PartialResultQueryManager.class).in(Scopes.SINGLETON);
// bind data definition statements to DataDefinitionExecutionFactory
queryTypes.stream().filter(entry -> entry.getValue() == QueryType.DATA_DEFINITION && !isSessionTransactionControlStatement(entry.getKey())).forEach(entry -> executionBinder.addBinding(entry.getKey()).to(DDLDefinitionExecutionFactory.class).in(Scopes.SINGLETON));
binder.bind(DDLDefinitionExecutionFactory.class).in(Scopes.SINGLETON);
// bind session Control statements to SessionTransactionExecutionFactory
queryTypes.stream().filter(entry -> (entry.getValue() == QueryType.DATA_DEFINITION && isSessionTransactionControlStatement(entry.getKey()))).forEach(entry -> executionBinder.addBinding(entry.getKey()).to(SessionDefinitionExecutionFactory.class).in(Scopes.SINGLETON));
binder.bind(SessionDefinitionExecutionFactory.class).in(Scopes.SINGLETON);
// helper class binding data definition tasks and statements
PrestoDataDefBindingHelper.bindDDLDefinitionTasks(binder);
PrestoDataDefBindingHelper.bindTransactionControlDefinitionTasks(binder);
MapBinder<String, ExecutionPolicy> executionPolicyBinder = newMapBinder(binder, String.class, ExecutionPolicy.class);
executionPolicyBinder.addBinding("all-at-once").to(AllAtOnceExecutionPolicy.class);
executionPolicyBinder.addBinding("phased").to(PhasedExecutionPolicy.class);
configBinder(binder).bindConfig(NodeResourceStatusConfig.class);
binder.bind(NodeResourceStatusProvider.class).to(NodeResourceStatus.class).in(Scopes.SINGLETON);
newOptionalBinder(binder, ResourceManagerProxy.class);
install(installModuleIf(ServerConfig.class, ServerConfig::isResourceManagerEnabled, rmBinder -> {
httpClientBinder(rmBinder).bindHttpClient("resourceManager", ForResourceManager.class);
rmBinder.bind(ResourceManagerProxy.class).in(Scopes.SINGLETON);
}));
// cleanup
binder.bind(ExecutorCleanup.class).in(Scopes.SINGLETON);
}
use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class QueryRewriter method rewriteQuery.
public QueryObjectBundle rewriteQuery(@Language("SQL") String query, ClusterType clusterType) {
checkState(prefixes.containsKey(clusterType), "Unsupported cluster type: %s", clusterType);
Statement statement = sqlParser.createStatement(query, PARSING_OPTIONS);
QualifiedName prefix = prefixes.get(clusterType);
List<Property> properties = tableProperties.get(clusterType);
if (statement instanceof CreateTableAsSelect) {
CreateTableAsSelect createTableAsSelect = (CreateTableAsSelect) statement;
QualifiedName temporaryTableName = generateTemporaryName(Optional.of(createTableAsSelect.getName()), prefix);
return new QueryObjectBundle(temporaryTableName, ImmutableList.of(), new CreateTableAsSelect(temporaryTableName, createTableAsSelect.getQuery(), createTableAsSelect.isNotExists(), applyPropertyOverride(createTableAsSelect.getProperties(), properties), createTableAsSelect.isWithData(), createTableAsSelect.getColumnAliases(), createTableAsSelect.getComment()), ImmutableList.of(new DropTable(temporaryTableName, true)), clusterType);
}
if (statement instanceof Insert) {
Insert insert = (Insert) statement;
QualifiedName originalTableName = insert.getTarget();
QualifiedName temporaryTableName = generateTemporaryName(Optional.of(originalTableName), prefix);
return new QueryObjectBundle(temporaryTableName, ImmutableList.of(new CreateTable(temporaryTableName, ImmutableList.of(new LikeClause(originalTableName, Optional.of(INCLUDING))), false, properties, Optional.empty())), new Insert(temporaryTableName, insert.getColumns(), insert.getQuery()), ImmutableList.of(new DropTable(temporaryTableName, true)), clusterType);
}
if (statement instanceof Query) {
QualifiedName temporaryTableName = generateTemporaryName(Optional.empty(), prefix);
ResultSetMetaData metadata = getResultMetadata((Query) statement);
List<Identifier> columnAliases = generateStorageColumnAliases(metadata);
Query rewrite = rewriteNonStorableColumns((Query) statement, metadata);
return new QueryObjectBundle(temporaryTableName, ImmutableList.of(), new CreateTableAsSelect(temporaryTableName, rewrite, false, properties, true, Optional.of(columnAliases), Optional.empty()), ImmutableList.of(new DropTable(temporaryTableName, true)), clusterType);
}
if (statement instanceof CreateView) {
CreateView createView = (CreateView) statement;
QualifiedName temporaryViewName = generateTemporaryName(Optional.empty(), prefix);
ImmutableList.Builder<Statement> setupQueries = ImmutableList.builder();
// Otherwise, do not pre-create temporary view.
try {
String createExistingViewQuery = getOnlyElement(prestoAction.execute(new ShowCreate(VIEW, createView.getName()), REWRITE, SHOW_CREATE_VIEW_CONVERTER).getResults());
CreateView createExistingView = (CreateView) sqlParser.createStatement(createExistingViewQuery, PARSING_OPTIONS);
setupQueries.add(new CreateView(temporaryViewName, createExistingView.getQuery(), false, createExistingView.getSecurity()));
} catch (QueryException e) {
// no-op
}
return new QueryObjectBundle(temporaryViewName, setupQueries.build(), new CreateView(temporaryViewName, createView.getQuery(), createView.isReplace(), createView.getSecurity()), ImmutableList.of(new DropView(temporaryViewName, true)), clusterType);
}
if (statement instanceof CreateTable) {
CreateTable createTable = (CreateTable) statement;
QualifiedName temporaryTableName = generateTemporaryName(Optional.empty(), prefix);
return new QueryObjectBundle(temporaryTableName, ImmutableList.of(), new CreateTable(temporaryTableName, createTable.getElements(), createTable.isNotExists(), applyPropertyOverride(createTable.getProperties(), properties), createTable.getComment()), ImmutableList.of(new DropTable(temporaryTableName, true)), clusterType);
}
throw new IllegalStateException(format("Unsupported query type: %s", statement.getClass()));
}
use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class ExplainVerification method getQueryRewrite.
@Override
protected QueryBundle getQueryRewrite(ClusterType clusterType) {
Statement statement = sqlParser.createStatement(getSourceQuery().getQuery(clusterType), PARSING_OPTIONS);
Explain explain = new Explain(statement, false, false, ImmutableList.of(new ExplainFormat(JSON)));
return new QueryBundle(ImmutableList.of(), explain, ImmutableList.of(), clusterType);
}
use of com.facebook.presto.sql.tree.Statement in project presto by prestodb.
the class DdlVerification method verify.
@Override
@SuppressWarnings("unchecked")
protected DdlMatchResult verify(QueryObjectBundle control, QueryObjectBundle test, Optional<QueryResult<Void>> controlQueryResult, Optional<QueryResult<Void>> testQueryResult, ChecksumQueryContext controlChecksumQueryContext, ChecksumQueryContext testChecksumQueryContext) {
Statement controlChecksumQuery = getChecksumQuery(control);
Statement testChecksumQuery = getChecksumQuery(test);
controlChecksumQueryContext.setChecksumQuery(formatSql(controlChecksumQuery));
testChecksumQueryContext.setChecksumQuery(formatSql(testChecksumQuery));
String controlChecksum = getOnlyElement(callAndConsume(() -> getHelperAction().execute(controlChecksumQuery, CONTROL_CHECKSUM, checksumConverter), stats -> stats.getQueryStats().map(QueryStats::getQueryId).ifPresent(controlChecksumQueryContext::setChecksumQueryId)).getResults());
String testChecksum = getOnlyElement(callAndConsume(() -> getHelperAction().execute(testChecksumQuery, TEST_CHECKSUM, checksumConverter), stats -> stats.getQueryStats().map(QueryStats::getQueryId).ifPresent(testChecksumQueryContext::setChecksumQueryId)).getResults());
S controlObject;
S testObject;
try {
controlObject = (S) sqlParser.createStatement(controlChecksum, PARSING_OPTIONS);
} catch (ParsingException e) {
return new DdlMatchResult(CONTROL_NOT_PARSABLE, Optional.of(e), controlChecksum, testChecksum);
}
try {
testObject = (S) sqlParser.createStatement(testChecksum, PARSING_OPTIONS);
} catch (ParsingException e) {
return new DdlMatchResult(TEST_NOT_PARSABLE, Optional.of(e), controlChecksum, testChecksum);
}
return new DdlMatchResult(match(controlObject, testObject, control, test) ? MATCH : MISMATCH, Optional.empty(), controlChecksum, testChecksum);
}
Aggregations