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