use of io.trino.execution.QueryPreparer.PreparedQuery in project trino by trinodb.
the class LocalQueryRunner method createPlan.
public Plan createPlan(Session session, @Language("SQL") String sql, LogicalPlanner.Stage stage, boolean forceSingleNode, WarningCollector warningCollector) {
PreparedQuery preparedQuery = new QueryPreparer(sqlParser).prepareQuery(session, sql);
assertFormattedSql(sqlParser, createParsingOptions(session), preparedQuery.getStatement());
return createPlan(session, sql, getPlanOptimizers(forceSingleNode), stage, warningCollector);
}
use of io.trino.execution.QueryPreparer.PreparedQuery in project trino by trinodb.
the class TestQueryPreparer method testSelectStatement.
@Test
public void testSelectStatement() {
PreparedQuery preparedQuery = QUERY_PREPARER.prepareQuery(TEST_SESSION, "SELECT * FROM foo");
assertEquals(preparedQuery.getStatement(), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo"))));
}
use of io.trino.execution.QueryPreparer.PreparedQuery in project trino by trinodb.
the class TestQueryPreparer method testExecuteStatement.
@Test
public void testExecuteStatement() {
Session session = testSessionBuilder().addPreparedStatement("my_query", "SELECT * FROM foo").build();
PreparedQuery preparedQuery = QUERY_PREPARER.prepareQuery(session, "EXECUTE my_query");
assertEquals(preparedQuery.getStatement(), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo"))));
}
use of io.trino.execution.QueryPreparer.PreparedQuery in project trino by trinodb.
the class DispatchManager method createQueryInternal.
/**
* Creates and registers a dispatch query with the query tracker. This method will never fail to register a query with the query
* tracker. If an error occurs while creating a dispatch query, a failed dispatch will be created and registered.
*/
private <C> void createQueryInternal(QueryId queryId, Slug slug, SessionContext sessionContext, String query, ResourceGroupManager<C> resourceGroupManager) {
Session session = null;
PreparedQuery preparedQuery = null;
try {
if (query.length() > maxQueryLength) {
int queryLength = query.length();
query = query.substring(0, maxQueryLength);
throw new TrinoException(QUERY_TEXT_TOO_LARGE, format("Query text length (%s) exceeds the maximum length (%s)", queryLength, maxQueryLength));
}
// decode session
session = sessionSupplier.createSession(queryId, sessionContext);
// check query execute permissions
accessControl.checkCanExecuteQuery(sessionContext.getIdentity());
// prepare query
preparedQuery = queryPreparer.prepareQuery(session, query);
// select resource group
Optional<String> queryType = getQueryType(preparedQuery.getStatement()).map(Enum::name);
SelectionContext<C> selectionContext = resourceGroupManager.selectGroup(new SelectionCriteria(sessionContext.getIdentity().getPrincipal().isPresent(), sessionContext.getIdentity().getUser(), sessionContext.getIdentity().getGroups(), sessionContext.getSource(), sessionContext.getClientTags(), sessionContext.getResourceEstimates(), queryType));
// apply system default session properties (does not override user set properties)
session = sessionPropertyDefaults.newSessionWithDefaultProperties(session, queryType, selectionContext.getResourceGroupId());
DispatchQuery dispatchQuery = dispatchQueryFactory.createDispatchQuery(session, sessionContext.getTransactionId(), query, preparedQuery, slug, selectionContext.getResourceGroupId());
boolean queryAdded = queryCreated(dispatchQuery);
if (queryAdded && !dispatchQuery.isDone()) {
try {
resourceGroupManager.submit(dispatchQuery, selectionContext, dispatchExecutor);
} catch (Throwable e) {
// dispatch query has already been registered, so just fail it directly
dispatchQuery.fail(e);
}
}
} catch (Throwable throwable) {
// creation must never fail, so register a failed query in this case
if (session == null) {
session = Session.builder(sessionPropertyManager).setQueryId(queryId).setIdentity(sessionContext.getIdentity()).setSource(sessionContext.getSource().orElse(null)).build();
}
Optional<String> preparedSql = Optional.ofNullable(preparedQuery).flatMap(PreparedQuery::getPrepareSql);
DispatchQuery failedDispatchQuery = failedDispatchQueryFactory.createFailedDispatchQuery(session, query, preparedSql, Optional.empty(), throwable);
queryCreated(failedDispatchQuery);
}
}
use of io.trino.execution.QueryPreparer.PreparedQuery in project trino by trinodb.
the class LocalQueryRunner method createPlan.
public Plan createPlan(Session session, @Language("SQL") String sql, List<PlanOptimizer> optimizers, LogicalPlanner.Stage stage, WarningCollector warningCollector) {
PreparedQuery preparedQuery = new QueryPreparer(sqlParser).prepareQuery(session, sql);
assertFormattedSql(sqlParser, createParsingOptions(session), preparedQuery.getStatement());
PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
AnalyzerFactory analyzerFactory = createAnalyzerFactory(createQueryExplainerFactory(optimizers));
Analyzer analyzer = analyzerFactory.createAnalyzer(session, preparedQuery.getParameters(), parameterExtractor(preparedQuery.getStatement(), preparedQuery.getParameters()), warningCollector);
LogicalPlanner logicalPlanner = new LogicalPlanner(session, optimizers, new PlanSanityChecker(true), idAllocator, getPlannerContext(), new TypeAnalyzer(plannerContext, statementAnalyzerFactory), statsCalculator, costCalculator, warningCollector);
Analysis analysis = analyzer.analyze(preparedQuery.getStatement());
// make LocalQueryRunner always compute plan statistics for test purposes
return logicalPlanner.plan(analysis, stage);
}
Aggregations