Search in sources :

Example 1 with PreparedQuery

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);
}
Also used : QueryPreparer(io.trino.execution.QueryPreparer) PreparedQuery(io.trino.execution.QueryPreparer.PreparedQuery)

Example 2 with PreparedQuery

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"))));
}
Also used : PreparedQuery(io.trino.execution.QueryPreparer.PreparedQuery) AllColumns(io.trino.sql.tree.AllColumns) Test(org.testng.annotations.Test)

Example 3 with PreparedQuery

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"))));
}
Also used : PreparedQuery(io.trino.execution.QueryPreparer.PreparedQuery) AllColumns(io.trino.sql.tree.AllColumns) Session(io.trino.Session) Test(org.testng.annotations.Test)

Example 4 with PreparedQuery

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);
    }
}
Also used : Optional(java.util.Optional) PreparedQuery(io.trino.execution.QueryPreparer.PreparedQuery) SelectionCriteria(io.trino.spi.resourcegroups.SelectionCriteria) TrinoException(io.trino.spi.TrinoException) Session(io.trino.Session)

Example 5 with PreparedQuery

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);
}
Also used : QueryPreparer(io.trino.execution.QueryPreparer) LogicalPlanner(io.trino.sql.planner.LogicalPlanner) PlanNodeIdAllocator(io.trino.sql.planner.PlanNodeIdAllocator) Analysis(io.trino.sql.analyzer.Analysis) PreparedQuery(io.trino.execution.QueryPreparer.PreparedQuery) PlanSanityChecker(io.trino.sql.planner.sanity.PlanSanityChecker) Analyzer(io.trino.sql.analyzer.Analyzer) TypeAnalyzer(io.trino.sql.planner.TypeAnalyzer) TypeAnalyzer(io.trino.sql.planner.TypeAnalyzer) StatementAnalyzerFactory(io.trino.sql.analyzer.StatementAnalyzerFactory) AnalyzerFactory(io.trino.sql.analyzer.AnalyzerFactory)

Aggregations

PreparedQuery (io.trino.execution.QueryPreparer.PreparedQuery)5 Session (io.trino.Session)2 QueryPreparer (io.trino.execution.QueryPreparer)2 AllColumns (io.trino.sql.tree.AllColumns)2 Test (org.testng.annotations.Test)2 TrinoException (io.trino.spi.TrinoException)1 SelectionCriteria (io.trino.spi.resourcegroups.SelectionCriteria)1 Analysis (io.trino.sql.analyzer.Analysis)1 Analyzer (io.trino.sql.analyzer.Analyzer)1 AnalyzerFactory (io.trino.sql.analyzer.AnalyzerFactory)1 StatementAnalyzerFactory (io.trino.sql.analyzer.StatementAnalyzerFactory)1 LogicalPlanner (io.trino.sql.planner.LogicalPlanner)1 PlanNodeIdAllocator (io.trino.sql.planner.PlanNodeIdAllocator)1 TypeAnalyzer (io.trino.sql.planner.TypeAnalyzer)1 PlanSanityChecker (io.trino.sql.planner.sanity.PlanSanityChecker)1 Optional (java.util.Optional)1