Search in sources :

Example 1 with Explain

use of com.facebook.presto.sql.tree.Explain in project presto by prestodb.

the class TestSqlParser method testExplain.

@Test
public void testExplain() {
    assertStatement("EXPLAIN SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false, false, ImmutableList.of()));
    assertStatement("EXPLAIN (TYPE LOGICAL) SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false, false, ImmutableList.of(new ExplainType(ExplainType.Type.LOGICAL))));
    assertStatement("EXPLAIN (TYPE LOGICAL, FORMAT TEXT) SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false, false, ImmutableList.of(new ExplainType(ExplainType.Type.LOGICAL), new ExplainFormat(ExplainFormat.Type.TEXT))));
}
Also used : ExplainType(com.facebook.presto.sql.tree.ExplainType) ExplainFormat(com.facebook.presto.sql.tree.ExplainFormat) Explain(com.facebook.presto.sql.tree.Explain) AllColumns(com.facebook.presto.sql.tree.AllColumns) Test(org.testng.annotations.Test)

Example 2 with Explain

use of com.facebook.presto.sql.tree.Explain in project presto by prestodb.

the class TestSqlParser method testAnalyze.

@Test
public void testAnalyze() {
    QualifiedName table = QualifiedName.of("foo");
    assertStatement("ANALYZE foo", new Analyze(table, ImmutableList.of()));
    assertStatement("ANALYZE foo WITH ( \"string\" = 'bar', \"long\" = 42, computed = concat('ban', 'ana'), a = ARRAY[ 'v1', 'v2' ] )", new Analyze(table, ImmutableList.of(new Property(new Identifier("string"), new StringLiteral("bar")), new Property(new Identifier("long"), new LongLiteral("42")), new Property(new Identifier("computed"), new FunctionCall(QualifiedName.of("concat"), ImmutableList.of(new StringLiteral("ban"), new StringLiteral("ana")))), new Property(new Identifier("a"), new ArrayConstructor(ImmutableList.of(new StringLiteral("v1"), new StringLiteral("v2")))))));
    assertStatement("EXPLAIN ANALYZE foo", new Explain(new Analyze(table, ImmutableList.of()), false, false, ImmutableList.of()));
    assertStatement("EXPLAIN ANALYZE ANALYZE foo", new Explain(new Analyze(table, ImmutableList.of()), true, false, ImmutableList.of()));
}
Also used : Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) Explain(com.facebook.presto.sql.tree.Explain) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Property(com.facebook.presto.sql.tree.Property) Analyze(com.facebook.presto.sql.tree.Analyze) Test(org.testng.annotations.Test)

Example 3 with Explain

use of com.facebook.presto.sql.tree.Explain in project presto by prestodb.

the class SqlQueryExecution method doAnalyzeQuery.

private PlanRoot doAnalyzeQuery() {
    // time analysis phase
    stateMachine.beginAnalysis();
    // plan query
    LogicalPlanner logicalPlanner = new LogicalPlanner(false, stateMachine.getSession(), planOptimizers, idAllocator, metadata, sqlParser, statsCalculator, costCalculator, stateMachine.getWarningCollector(), planChecker);
    Plan plan = getSession().getRuntimeStats().profileNanos(LOGICAL_PLANNER_TIME_NANOS, () -> logicalPlanner.plan(analysis));
    queryPlan.set(plan);
    // extract inputs
    List<Input> inputs = new InputExtractor(metadata, stateMachine.getSession()).extractInputs(plan.getRoot());
    stateMachine.setInputs(inputs);
    // extract output
    Optional<Output> output = new OutputExtractor().extractOutput(plan.getRoot());
    stateMachine.setOutput(output);
    // fragment the plan
    // the variableAllocator is finally passed to SqlQueryScheduler for runtime cost-based optimizations
    variableAllocator.set(new PlanVariableAllocator(plan.getTypes().allVariables()));
    SubPlan fragmentedPlan = getSession().getRuntimeStats().profileNanos(FRAGMENT_PLAN_TIME_NANOS, () -> planFragmenter.createSubPlans(stateMachine.getSession(), plan, false, idAllocator, variableAllocator.get(), stateMachine.getWarningCollector()));
    // record analysis time
    stateMachine.endAnalysis();
    boolean explainAnalyze = analysis.getStatement() instanceof Explain && ((Explain) analysis.getStatement()).isAnalyze();
    return new PlanRoot(fragmentedPlan, !explainAnalyze, extractConnectors(analysis));
}
Also used : LogicalPlanner(com.facebook.presto.sql.planner.LogicalPlanner) Explain(com.facebook.presto.sql.tree.Explain) InputExtractor(com.facebook.presto.sql.planner.InputExtractor) Plan(com.facebook.presto.sql.planner.Plan) SubPlan(com.facebook.presto.sql.planner.SubPlan) OutputExtractor(com.facebook.presto.sql.planner.OutputExtractor) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) SubPlan(com.facebook.presto.sql.planner.SubPlan)

Example 4 with Explain

use of com.facebook.presto.sql.tree.Explain in project presto by prestodb.

the class SqlQueryManager method createQuery.

@Override
public QueryInfo createQuery(SessionSupplier sessionSupplier, String query) {
    requireNonNull(sessionSupplier, "sessionFactory is null");
    requireNonNull(query, "query is null");
    checkArgument(!query.isEmpty(), "query must not be empty string");
    QueryId queryId = queryIdGenerator.createNextQueryId();
    Session session = null;
    QueryExecution queryExecution;
    Statement statement;
    try {
        session = sessionSupplier.createSession(queryId, transactionManager, accessControl, sessionPropertyManager);
        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));
        }
        Statement wrappedStatement = sqlParser.createStatement(query);
        statement = unwrapExecuteStatement(wrappedStatement, sqlParser, session);
        List<Expression> parameters = wrappedStatement instanceof Execute ? ((Execute) wrappedStatement).getParameters() : emptyList();
        validateParameters(statement, parameters);
        QueryExecutionFactory<?> queryExecutionFactory = executionFactories.get(statement.getClass());
        if (queryExecutionFactory == null) {
            throw new PrestoException(NOT_SUPPORTED, "Unsupported statement type: " + statement.getClass().getSimpleName());
        }
        if (statement instanceof Explain && ((Explain) statement).isAnalyze()) {
            Statement innerStatement = ((Explain) statement).getStatement();
            if (!(executionFactories.get(innerStatement.getClass()) instanceof SqlQueryExecutionFactory)) {
                throw new PrestoException(NOT_SUPPORTED, "EXPLAIN ANALYZE only supported for statements that are queries");
            }
        }
        queryExecution = queryExecutionFactory.createQueryExecution(queryId, query, session, statement, parameters);
    } catch (ParsingException | PrestoException | SemanticException e) {
        // This is intentionally not a method, since after the state change listener is registered
        // it's not safe to do any of this, and we had bugs before where people reused this code in a method
        URI self = locationFactory.createQueryLocation(queryId);
        // if session creation failed, create a minimal session object
        if (session == null) {
            session = Session.builder(new SessionPropertyManager()).setQueryId(queryId).setIdentity(sessionSupplier.getIdentity()).build();
        }
        Optional<ResourceGroupId> resourceGroup = Optional.empty();
        if (e instanceof QueryQueueFullException) {
            resourceGroup = Optional.of(((QueryQueueFullException) e).getResourceGroup());
        }
        QueryExecution execution = new FailedQueryExecution(queryId, query, resourceGroup, session, self, transactionManager, queryExecutor, metadata, e);
        QueryInfo queryInfo = null;
        try {
            queries.put(queryId, execution);
            queryInfo = execution.getQueryInfo();
            queryMonitor.queryCreatedEvent(queryInfo);
            queryMonitor.queryCompletedEvent(queryInfo);
            stats.queryFinished(queryInfo);
        } finally {
            // execution MUST be added to the expiration queue or there will be a leak
            expirationQueue.add(execution);
        }
        return queryInfo;
    }
    QueryInfo queryInfo = queryExecution.getQueryInfo();
    queryMonitor.queryCreatedEvent(queryInfo);
    queryExecution.addFinalQueryInfoListener(finalQueryInfo -> {
        try {
            QueryInfo info = queryExecution.getQueryInfo();
            stats.queryFinished(info);
            queryMonitor.queryCompletedEvent(info);
        } finally {
            expirationQueue.add(queryExecution);
        }
    });
    addStatsListener(queryExecution);
    queries.put(queryId, queryExecution);
    // start the query in the background
    queueManager.submit(statement, queryExecution, queryExecutor);
    return queryInfo;
}
Also used : Execute(com.facebook.presto.sql.tree.Execute) Optional(java.util.Optional) Statement(com.facebook.presto.sql.tree.Statement) QueryId(com.facebook.presto.spi.QueryId) Explain(com.facebook.presto.sql.tree.Explain) QueryQueueFullException(com.facebook.presto.execution.resourceGroups.QueryQueueFullException) PrestoException(com.facebook.presto.spi.PrestoException) URI(java.net.URI) SqlQueryExecutionFactory(com.facebook.presto.execution.SqlQueryExecution.SqlQueryExecutionFactory) Expression(com.facebook.presto.sql.tree.Expression) ParsingException(com.facebook.presto.sql.parser.ParsingException) SessionPropertyManager(com.facebook.presto.metadata.SessionPropertyManager) Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 5 with Explain

use of com.facebook.presto.sql.tree.Explain in project presto by prestodb.

the class QueryPreparer method prepareQuery.

public PreparedQuery prepareQuery(Session session, Statement wrappedStatement, WarningCollector warningCollector) throws ParsingException, PrestoException, SemanticException {
    Statement statement = wrappedStatement;
    Optional<String> prepareSql = Optional.empty();
    if (statement instanceof Execute) {
        prepareSql = Optional.of(session.getPreparedStatementFromExecute((Execute) statement));
        statement = sqlParser.createStatement(prepareSql.get(), createParsingOptions(session, warningCollector));
    }
    if (statement instanceof Explain && ((Explain) statement).isAnalyze()) {
        Statement innerStatement = ((Explain) statement).getStatement();
        Optional<QueryType> innerQueryType = StatementUtils.getQueryType(innerStatement.getClass());
        if (!innerQueryType.isPresent() || innerQueryType.get() == QueryType.DATA_DEFINITION) {
            throw new PrestoException(NOT_SUPPORTED, "EXPLAIN ANALYZE doesn't support statement type: " + innerStatement.getClass().getSimpleName());
        }
    }
    List<Expression> parameters = ImmutableList.of();
    if (wrappedStatement instanceof Execute) {
        parameters = ((Execute) wrappedStatement).getParameters();
    }
    validateParameters(statement, parameters);
    Optional<String> formattedQuery = Optional.empty();
    if (isLogFormattedQueryEnabled(session)) {
        formattedQuery = Optional.of(getFormattedQuery(statement, parameters));
    }
    return new PreparedQuery(statement, parameters, formattedQuery, prepareSql);
}
Also used : Execute(com.facebook.presto.sql.tree.Execute) Expression(com.facebook.presto.sql.tree.Expression) Statement(com.facebook.presto.sql.tree.Statement) Explain(com.facebook.presto.sql.tree.Explain) PrestoException(com.facebook.presto.spi.PrestoException) QueryType(com.facebook.presto.spi.resourceGroups.QueryType)

Aggregations

Explain (com.facebook.presto.sql.tree.Explain)7 Statement (com.facebook.presto.sql.tree.Statement)4 PrestoException (com.facebook.presto.spi.PrestoException)2 LogicalPlanner (com.facebook.presto.sql.planner.LogicalPlanner)2 Execute (com.facebook.presto.sql.tree.Execute)2 ExplainFormat (com.facebook.presto.sql.tree.ExplainFormat)2 Expression (com.facebook.presto.sql.tree.Expression)2 Test (org.testng.annotations.Test)2 Session (com.facebook.presto.Session)1 QueryPreparer (com.facebook.presto.execution.QueryPreparer)1 PreparedQuery (com.facebook.presto.execution.QueryPreparer.PreparedQuery)1 SqlQueryExecutionFactory (com.facebook.presto.execution.SqlQueryExecution.SqlQueryExecutionFactory)1 QueryQueueFullException (com.facebook.presto.execution.resourceGroups.QueryQueueFullException)1 SessionPropertyManager (com.facebook.presto.metadata.SessionPropertyManager)1 QueryId (com.facebook.presto.spi.QueryId)1 PlanNodeIdAllocator (com.facebook.presto.spi.plan.PlanNodeIdAllocator)1 QueryType (com.facebook.presto.spi.resourceGroups.QueryType)1 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)1 Analysis (com.facebook.presto.sql.analyzer.Analysis)1 Analyzer (com.facebook.presto.sql.analyzer.Analyzer)1