Search in sources :

Example 1 with PreparedQuery

use of io.prestosql.execution.QueryPreparer.PreparedQuery in project hetu-core by openlookeng.

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, String slug, SessionContext sessionContext, String inputQuery, ResourceGroupManager<C> resourceGroupManager) {
    String query = inputQuery;
    Session session = null;
    DispatchQuery dispatchQuery = null;
    try {
        if (query.length() > maxQueryLength) {
            int queryLength = query.length();
            query = query.substring(0, maxQueryLength);
            throw new PrestoException(QUERY_TEXT_TOO_LARGE, format("Query text length (%s) exceeds the maximum length (%s)", queryLength, maxQueryLength));
        }
        // decode session
        session = sessionSupplier.createSession(queryId, sessionContext);
        // prepare query
        PreparedQuery preparedQuery = queryPreparer.prepareQuery(session, query);
        // select resource group
        Optional<String> queryType = getQueryType(preparedQuery.getStatement().getClass()).map(Enum::name);
        SelectionContext<C> selectionContext = resourceGroupManager.selectGroup(new SelectionCriteria(sessionContext.getIdentity().getPrincipal().isPresent(), sessionContext.getIdentity().getUser(), Optional.ofNullable(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());
        // mark existing transaction as active
        transactionManager.activateTransaction(session, isTransactionControlStatement(preparedQuery.getStatement()), accessControl);
        dispatchQuery = dispatchQueryFactory.createDispatchQuery(session, query, preparedQuery, slug, selectionContext.getResourceGroupId(), resourceGroupManager);
        boolean queryAdded = queryCreated(dispatchQuery);
        if (queryAdded && !dispatchQuery.isDone()) {
            try {
                resourceGroupManager.submit(dispatchQuery, selectionContext, queryExecutor);
                if (PropertyService.getBooleanProperty(HetuConstant.MULTI_COORDINATOR_ENABLED) && stateUpdater != null) {
                    stateUpdater.registerQuery(StateStoreConstants.QUERY_STATE_COLLECTION_NAME, dispatchQuery);
                }
                if (LOG.isDebugEnabled()) {
                    long now = System.currentTimeMillis();
                    LOG.debug("query:%s submission started at %s, ended at %s, total time use: %sms", dispatchQuery.getQueryId(), new SimpleDateFormat("HH:mm:ss:SSS").format(dispatchQuery.getCreateTime().toDate()), new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(now)), now - dispatchQuery.getCreateTime().getMillis());
                }
            } 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 (dispatchQuery == null) {
            if (session == null) {
                session = Session.builder(new SessionPropertyManager()).setQueryId(queryId).setIdentity(sessionContext.getIdentity()).setSource(sessionContext.getSource()).build();
            }
            DispatchQuery failedDispatchQuery = failedDispatchQueryFactory.createFailedDispatchQuery(session, query, Optional.empty(), throwable);
            queryCreated(failedDispatchQuery);
        } else {
            dispatchQuery.fail(throwable);
        }
    }
}
Also used : PreparedQuery(io.prestosql.execution.QueryPreparer.PreparedQuery) PrestoException(io.prestosql.spi.PrestoException) Date(java.util.Date) SelectionCriteria(io.prestosql.spi.resourcegroups.SelectionCriteria) SessionPropertyManager(io.prestosql.metadata.SessionPropertyManager) SimpleDateFormat(java.text.SimpleDateFormat) Session(io.prestosql.Session)

Example 2 with PreparedQuery

use of io.prestosql.execution.QueryPreparer.PreparedQuery in project hetu-core by openlookeng.

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.prestosql.execution.QueryPreparer.PreparedQuery) AllColumns(io.prestosql.sql.tree.AllColumns) Test(org.testng.annotations.Test)

Example 3 with PreparedQuery

use of io.prestosql.execution.QueryPreparer.PreparedQuery in project hetu-core by openlookeng.

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.prestosql.execution.QueryPreparer) PreparedQuery(io.prestosql.execution.QueryPreparer.PreparedQuery)

Example 4 with PreparedQuery

use of io.prestosql.execution.QueryPreparer.PreparedQuery in project hetu-core by openlookeng.

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();
    QueryExplainer queryExplainer = new QueryExplainer(optimizers, planFragmenter, metadata, accessControl, sqlParser, statsCalculator, costCalculator, dataDefinitionTask, heuristicIndexerManager, cubeManager);
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.of(queryExplainer), preparedQuery.getParameters(), warningCollector, heuristicIndexerManager, cubeManager);
    LogicalPlanner logicalPlanner = new LogicalPlanner(session, optimizers, new PlanSanityChecker(true), idAllocator, metadata, new TypeAnalyzer(sqlParser, metadata), statsCalculator, costCalculator, warningCollector);
    Analysis analysis = analyzer.analyze(preparedQuery.getStatement());
    return logicalPlanner.plan(analysis, false, stage);
}
Also used : QueryPreparer(io.prestosql.execution.QueryPreparer) QueryExplainer(io.prestosql.sql.analyzer.QueryExplainer) LogicalPlanner(io.prestosql.sql.planner.LogicalPlanner) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) Analysis(io.prestosql.sql.analyzer.Analysis) PreparedQuery(io.prestosql.execution.QueryPreparer.PreparedQuery) PlanSanityChecker(io.prestosql.sql.planner.sanity.PlanSanityChecker) TypeAnalyzer(io.prestosql.sql.planner.TypeAnalyzer) Analyzer(io.prestosql.sql.analyzer.Analyzer) TypeAnalyzer(io.prestosql.sql.planner.TypeAnalyzer)

Example 5 with PreparedQuery

use of io.prestosql.execution.QueryPreparer.PreparedQuery in project hetu-core by openlookeng.

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.prestosql.execution.QueryPreparer.PreparedQuery) AllColumns(io.prestosql.sql.tree.AllColumns) Session(io.prestosql.Session) Test(org.testng.annotations.Test)

Aggregations

PreparedQuery (io.prestosql.execution.QueryPreparer.PreparedQuery)5 Session (io.prestosql.Session)2 QueryPreparer (io.prestosql.execution.QueryPreparer)2 AllColumns (io.prestosql.sql.tree.AllColumns)2 Test (org.testng.annotations.Test)2 SessionPropertyManager (io.prestosql.metadata.SessionPropertyManager)1 PrestoException (io.prestosql.spi.PrestoException)1 PlanNodeIdAllocator (io.prestosql.spi.plan.PlanNodeIdAllocator)1 SelectionCriteria (io.prestosql.spi.resourcegroups.SelectionCriteria)1 Analysis (io.prestosql.sql.analyzer.Analysis)1 Analyzer (io.prestosql.sql.analyzer.Analyzer)1 QueryExplainer (io.prestosql.sql.analyzer.QueryExplainer)1 LogicalPlanner (io.prestosql.sql.planner.LogicalPlanner)1 TypeAnalyzer (io.prestosql.sql.planner.TypeAnalyzer)1 PlanSanityChecker (io.prestosql.sql.planner.sanity.PlanSanityChecker)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1