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);
}
}
}
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"))));
}
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);
}
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);
}
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"))));
}
Aggregations