Search in sources :

Example 1 with With

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

the class TestSqlParser method testWith.

@Test
public void testWith() {
    assertStatement("WITH a (t, u) AS (SELECT * FROM x), b AS (SELECT * FROM y) TABLE z", new Query(Optional.of(new With(false, ImmutableList.of(new WithQuery(identifier("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("x"))), Optional.of(ImmutableList.of(identifier("t"), identifier("u")))), new WithQuery(identifier("b"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("y"))), Optional.empty())))), new Table(QualifiedName.of("z")), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("WITH RECURSIVE a AS (SELECT * FROM x) TABLE y", new Query(Optional.of(new With(true, ImmutableList.of(new WithQuery(identifier("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("x"))), Optional.empty())))), new Table(QualifiedName.of("y")), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Table(com.facebook.presto.sql.tree.Table) RenameTable(com.facebook.presto.sql.tree.RenameTable) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) AllColumns(com.facebook.presto.sql.tree.AllColumns) With(com.facebook.presto.sql.tree.With) Test(org.testng.annotations.Test)

Example 2 with With

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

the class TestSqlParser method testCreateTableAsWith.

@Test
public void testCreateTableAsWith() {
    String queryParenthesizedWith = "CREATE TABLE foo " + "AS " + "( WITH t(x) AS (VALUES 1) " + "TABLE t ) " + "WITH NO DATA";
    String queryUnparenthesizedWith = "CREATE TABLE foo " + "AS " + "WITH t(x) AS (VALUES 1) " + "TABLE t " + "WITH NO DATA";
    String queryParenthesizedWithHasAlias = "CREATE TABLE foo(a) " + "AS " + "( WITH t(x) AS (VALUES 1) " + "TABLE t ) " + "WITH NO DATA";
    String queryUnparenthesizedWithHasAlias = "CREATE TABLE foo(a) " + "AS " + "WITH t(x) AS (VALUES 1) " + "TABLE t " + "WITH NO DATA";
    QualifiedName table = QualifiedName.of("foo");
    Query query = new Query(Optional.of(new With(false, ImmutableList.of(new WithQuery(identifier("t"), query(new Values(ImmutableList.of(new LongLiteral("1")))), Optional.of(ImmutableList.of(identifier("x"))))))), new Table(QualifiedName.of("t")), Optional.empty(), Optional.empty(), Optional.empty());
    assertStatement(queryParenthesizedWith, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty()));
    assertStatement(queryUnparenthesizedWith, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty()));
    assertStatement(queryParenthesizedWithHasAlias, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("a"))), Optional.empty()));
    assertStatement(queryUnparenthesizedWithHasAlias, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("a"))), Optional.empty()));
}
Also used : CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Table(com.facebook.presto.sql.tree.Table) RenameTable(com.facebook.presto.sql.tree.RenameTable) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) WithQuery(com.facebook.presto.sql.tree.WithQuery) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Values(com.facebook.presto.sql.tree.Values) With(com.facebook.presto.sql.tree.With) Test(org.testng.annotations.Test)

Example 3 with With

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

the class LimitQueryDeterminismAnalyzer method analyzeInternal.

private LimitQueryDeterminismAnalysis analyzeInternal() {
    if (!enabled) {
        return NOT_RUN;
    }
    Query query;
    // A query is rewritten to either an Insert or a CreateTableAsSelect
    if (statement instanceof Insert) {
        query = ((Insert) statement).getQuery();
    } else if (statement instanceof CreateTableAsSelect) {
        query = ((CreateTableAsSelect) statement).getQuery();
    } else {
        return NOT_RUN;
    }
    // Flatten TableSubquery
    if (query.getQueryBody() instanceof TableSubquery) {
        Optional<With> with = query.getWith();
        while (query.getQueryBody() instanceof TableSubquery) {
            // ORDER BY and LIMIT must be empty according to syntax
            if (query.getOrderBy().isPresent() || query.getLimit().isPresent()) {
                return NOT_RUN;
            }
            query = ((TableSubquery) query.getQueryBody()).getQuery();
            // WITH must be empty according to syntax
            if (query.getWith().isPresent()) {
                return NOT_RUN;
            }
        }
        query = new Query(with, query.getQueryBody(), query.getOrderBy(), query.getOffset(), query.getLimit());
    }
    if (query.getQueryBody() instanceof QuerySpecification) {
        return analyzeQuerySpecification(query.getWith(), (QuerySpecification) query.getQueryBody());
    }
    return analyzeQuery(query);
}
Also used : QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Insert(com.facebook.presto.sql.tree.Insert) TableSubquery(com.facebook.presto.sql.tree.TableSubquery) With(com.facebook.presto.sql.tree.With)

Aggregations

QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)3 Query (com.facebook.presto.sql.tree.Query)3 With (com.facebook.presto.sql.tree.With)3 CreateTable (com.facebook.presto.sql.tree.CreateTable)2 CreateTableAsSelect (com.facebook.presto.sql.tree.CreateTableAsSelect)2 DropTable (com.facebook.presto.sql.tree.DropTable)2 RenameTable (com.facebook.presto.sql.tree.RenameTable)2 Table (com.facebook.presto.sql.tree.Table)2 WithQuery (com.facebook.presto.sql.tree.WithQuery)2 Test (org.testng.annotations.Test)2 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)1 AllColumns (com.facebook.presto.sql.tree.AllColumns)1 Identifier (com.facebook.presto.sql.tree.Identifier)1 Insert (com.facebook.presto.sql.tree.Insert)1 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)1 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)1 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)1 TableSubquery (com.facebook.presto.sql.tree.TableSubquery)1 Values (com.facebook.presto.sql.tree.Values)1