Search in sources :

Example 26 with Statement

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

the class SqlFormatterUtil method getFormattedSql.

public static String getFormattedSql(Statement statement, SqlParser sqlParser, Optional<List<Expression>> parameters) {
    String sql = SqlFormatter.formatSql(statement, parameters);
    // verify round-trip
    Statement parsed;
    try {
        ParsingOptions parsingOptions = new ParsingOptions(REJECT);
        parsed = sqlParser.createStatement(sql, parsingOptions);
    } catch (ParsingException e) {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Formatted query does not parse: " + statement);
    }
    if (!statement.equals(parsed)) {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Query does not round-trip: " + statement);
    }
    return sql;
}
Also used : ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) Statement(com.facebook.presto.sql.tree.Statement) ParsingException(com.facebook.presto.sql.parser.ParsingException) PrestoException(com.facebook.presto.spi.PrestoException)

Example 27 with Statement

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

the class AbstractAnalyzerTest method analyze.

private void analyze(Session clientSession, WarningCollector warningCollector, @Language("SQL") String query) {
    transaction(transactionManager, accessControl).singleStatement().readUncommitted().readOnly().execute(clientSession, session -> {
        Analyzer analyzer = AbstractAnalyzerTest.createAnalyzer(session, metadata, warningCollector);
        Statement statement = SQL_PARSER.createStatement(query);
        analyzer.analyze(statement);
    });
}
Also used : Statement(com.facebook.presto.sql.tree.Statement)

Example 28 with Statement

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

the class TestUtilizedColumnsAnalyzer method assertUtilizedTableColumns.

private void assertUtilizedTableColumns(@Language("SQL") String query, Map<QualifiedObjectName, Set<String>> expected) {
    transaction(transactionManager, accessControl).singleStatement().readUncommitted().readOnly().execute(CLIENT_SESSION, session -> {
        Analyzer analyzer = createAnalyzer(session, metadata, WarningCollector.NOOP);
        Statement statement = SQL_PARSER.createStatement(query);
        Analysis analysis = analyzer.analyze(statement);
        assertEquals(analysis.getUtilizedTableColumnReferences().values().stream().findFirst().get(), expected);
    });
}
Also used : Statement(com.facebook.presto.sql.tree.Statement)

Example 29 with Statement

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

the class LocalQueryRunner method createPlan.

public Plan createPlan(Session session, @Language("SQL") String sql, List<PlanOptimizer> optimizers, LogicalPlanner.Stage stage, WarningCollector warningCollector) {
    Statement wrappedStatement = sqlParser.createStatement(sql, createParsingOptions(session, warningCollector));
    PreparedQuery preparedQuery = new QueryPreparer(sqlParser).prepareQuery(session, wrappedStatement, warningCollector);
    assertFormattedSql(sqlParser, createParsingOptions(session), preparedQuery.getStatement());
    PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
    QueryExplainer queryExplainer = new QueryExplainer(optimizers, planFragmenter, metadata, accessControl, sqlParser, statsCalculator, costCalculator, dataDefinitionTask, distributedPlanChecker);
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.of(queryExplainer), preparedQuery.getParameters(), warningCollector);
    LogicalPlanner logicalPlanner = new LogicalPlanner(wrappedStatement instanceof Explain, session, optimizers, singleNodePlanChecker, idAllocator, metadata, sqlParser, statsCalculator, costCalculator, warningCollector);
    Analysis analysis = analyzer.analyze(preparedQuery.getStatement());
    return logicalPlanner.plan(analysis, stage);
}
Also used : QueryPreparer(com.facebook.presto.execution.QueryPreparer) QueryExplainer(com.facebook.presto.sql.analyzer.QueryExplainer) LogicalPlanner(com.facebook.presto.sql.planner.LogicalPlanner) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) Statement(com.facebook.presto.sql.tree.Statement) Analysis(com.facebook.presto.sql.analyzer.Analysis) Explain(com.facebook.presto.sql.tree.Explain) PreparedQuery(com.facebook.presto.execution.QueryPreparer.PreparedQuery) Analyzer(com.facebook.presto.sql.analyzer.Analyzer)

Example 30 with Statement

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

the class TestSqlParser method assertInvalidStatement.

private static void assertInvalidStatement(String expression, String expectedErrorMessageRegex) {
    try {
        Statement result = SQL_PARSER.createStatement(expression, ParsingOptions.builder().build());
        fail("Expected to throw ParsingException for input:[" + expression + "], but got: " + result);
    } catch (ParsingException e) {
        if (!e.getErrorMessage().matches(expectedErrorMessageRegex)) {
            fail(format("Expected error message to match '%s', but was: '%s'", expectedErrorMessageRegex, e.getErrorMessage()));
        }
    }
}
Also used : Statement(com.facebook.presto.sql.tree.Statement)

Aggregations

Statement (com.facebook.presto.sql.tree.Statement)34 SqlQueryManager.unwrapExecuteStatement (com.facebook.presto.execution.SqlQueryManager.unwrapExecuteStatement)7 Test (org.testng.annotations.Test)7 Session (com.facebook.presto.Session)6 PrestoException (com.facebook.presto.spi.PrestoException)6 Execute (com.facebook.presto.sql.tree.Execute)5 Expression (com.facebook.presto.sql.tree.Expression)5 Analysis (com.facebook.presto.sql.analyzer.Analysis)4 Analyzer (com.facebook.presto.sql.analyzer.Analyzer)4 ParsingException (com.facebook.presto.sql.parser.ParsingException)4 Explain (com.facebook.presto.sql.tree.Explain)4 FeaturesConfig (com.facebook.presto.sql.analyzer.FeaturesConfig)3 QueryExplainer (com.facebook.presto.sql.analyzer.QueryExplainer)3 CreateView (com.facebook.presto.sql.tree.CreateView)3 TransactionManager (com.facebook.presto.transaction.TransactionManager)3 ImmutableList (com.google.common.collect.ImmutableList)3 QueryPreparer (com.facebook.presto.execution.QueryPreparer)2 SqlQueryExecutionFactory (com.facebook.presto.execution.SqlQueryExecution.SqlQueryExecutionFactory)2 QueryStats (com.facebook.presto.jdbc.QueryStats)2 QueryType (com.facebook.presto.spi.resourceGroups.QueryType)2