Search in sources :

Example 1 with Select

use of io.trino.sql.tree.Select in project trino by trinodb.

the class TestSqlParser method testShowStatsForQuery.

@Test
public void testShowStatsForQuery() {
    String[] tableNames = { "t", "s.t", "c.s.t" };
    for (String fullName : tableNames) {
        QualifiedName qualifiedName = makeQualifiedName(fullName);
        // Simple SELECT
        assertStatement(format("SHOW STATS FOR (SELECT * FROM %s)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.empty()));
        // SELECT with predicate
        assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of(new ComparisonExpression(ComparisonExpression.Operator.GREATER_THAN, new Identifier("field"), new LongLiteral("0")))));
        // SELECT with more complex predicate
        assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0 or field < 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of(LogicalExpression.or(new ComparisonExpression(ComparisonExpression.Operator.GREATER_THAN, new Identifier("field"), new LongLiteral("0")), new ComparisonExpression(ComparisonExpression.Operator.LESS_THAN, new Identifier("field"), new LongLiteral("0"))))));
    }
    // SELECT with LIMIT
    assertThat(statement("SHOW STATS FOR (SELECT * FROM t LIMIT 10)")).isEqualTo(new ShowStats(Optional.of(location(1, 1)), new TableSubquery(new Query(location(1, 17), Optional.empty(), new QuerySpecification(location(1, 17), new Select(location(1, 17), false, ImmutableList.of(new AllColumns(location(1, 24), Optional.empty(), ImmutableList.of()))), Optional.of(new Table(location(1, 31), QualifiedName.of(ImmutableList.of(new Identifier(location(1, 31), "t", false))))), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.of(new Limit(location(1, 33), new LongLiteral(location(1, 39), "10")))), Optional.empty(), Optional.empty(), Optional.empty()))));
    // SELECT with ORDER BY ... LIMIT
    assertThat(statement("SHOW STATS FOR (SELECT * FROM t ORDER BY field LIMIT 10)")).isEqualTo(new ShowStats(Optional.of(location(1, 1)), new TableSubquery(new Query(location(1, 17), Optional.empty(), new QuerySpecification(location(1, 17), new Select(location(1, 17), false, ImmutableList.of(new AllColumns(location(1, 24), Optional.empty(), ImmutableList.of()))), Optional.of(new Table(location(1, 31), QualifiedName.of(ImmutableList.of(new Identifier(location(1, 31), "t", false))))), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.of(new OrderBy(location(1, 33), ImmutableList.of(new SortItem(location(1, 42), new Identifier(location(1, 42), "field", false), ASCENDING, UNDEFINED)))), Optional.empty(), Optional.of(new Limit(location(1, 48), new LongLiteral(location(1, 54), "10")))), Optional.empty(), Optional.empty(), Optional.empty()))));
    // SELECT with WITH
    assertThat(statement("SHOW STATS FOR (\n" + "   WITH t AS (SELECT 1 )\n" + "   SELECT * FROM t)")).isEqualTo(new ShowStats(Optional.of(location(1, 1)), new TableSubquery(new Query(location(2, 4), Optional.of(new With(location(2, 4), false, ImmutableList.of(new WithQuery(location(2, 9), new Identifier(location(2, 9), "t", false), new Query(location(2, 15), Optional.empty(), new QuerySpecification(location(2, 15), new Select(location(2, 15), false, ImmutableList.of(new SingleColumn(location(2, 22), new LongLiteral(location(2, 22), "1"), Optional.empty()))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty())))), new QuerySpecification(location(3, 4), new Select(location(3, 4), false, ImmutableList.of(new AllColumns(location(3, 11), Optional.empty(), ImmutableList.of()))), Optional.of(new Table(location(3, 18), QualifiedName.of(ImmutableList.of(new Identifier(location(3, 18), "t", false))))), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()))));
}
Also used : OrderBy(io.trino.sql.tree.OrderBy) CreateTable(io.trino.sql.tree.CreateTable) DropTable(io.trino.sql.tree.DropTable) Table(io.trino.sql.tree.Table) TruncateTable(io.trino.sql.tree.TruncateTable) RenameTable(io.trino.sql.tree.RenameTable) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) LongLiteral(io.trino.sql.tree.LongLiteral) QualifiedName(io.trino.sql.tree.QualifiedName) AllColumns(io.trino.sql.tree.AllColumns) SingleColumn(io.trino.sql.tree.SingleColumn) TableSubquery(io.trino.sql.tree.TableSubquery) With(io.trino.sql.tree.With) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QuerySpecification(io.trino.sql.tree.QuerySpecification) SortItem(io.trino.sql.tree.SortItem) QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) ShowStats(io.trino.sql.tree.ShowStats) WithQuery(io.trino.sql.tree.WithQuery) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) Select(io.trino.sql.tree.Select) Limit(io.trino.sql.tree.Limit) Test(org.junit.jupiter.api.Test)

Example 2 with Select

use of io.trino.sql.tree.Select in project trino by trinodb.

the class QueryUtil method emptyQuery.

// TODO pass column types
public static Query emptyQuery(List<String> columns) {
    Select select = selectList(columns.stream().map(column -> new SingleColumn(new NullLiteral(), QueryUtil.identifier(column))).toArray(SelectItem[]::new));
    Optional<Expression> where = Optional.of(FALSE_LITERAL);
    return query(new QuerySpecification(select, Optional.empty(), where, Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : QuerySpecification(io.trino.sql.tree.QuerySpecification) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) Expression(io.trino.sql.tree.Expression) SelectItem(io.trino.sql.tree.SelectItem) Select(io.trino.sql.tree.Select) SingleColumn(io.trino.sql.tree.SingleColumn) NullLiteral(io.trino.sql.tree.NullLiteral)

Example 3 with Select

use of io.trino.sql.tree.Select in project trino by trinodb.

the class TestSqlParser method testQueryPeriod.

@Test
public void testQueryPeriod() {
    Expression rangeValue = new TimestampLiteral(location(1, 37), "2021-03-01 00:00:01");
    QueryPeriod queryPeriod = new QueryPeriod(location(1, 17), QueryPeriod.RangeType.TIMESTAMP, rangeValue);
    Table table = new Table(location(1, 15), qualifiedName(location(1, 15), "t"), queryPeriod);
    assertThat(statement("SELECT * FROM t FOR TIMESTAMP AS OF TIMESTAMP '2021-03-01 00:00:01'")).isEqualTo(new Query(location(1, 1), Optional.empty(), new QuerySpecification(location(1, 1), new Select(location(1, 1), false, ImmutableList.of(new AllColumns(location(1, 8), Optional.empty(), ImmutableList.of()))), Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    rangeValue = new StringLiteral(location(1, 35), "version1");
    queryPeriod = new QueryPeriod(new NodeLocation(1, 17), QueryPeriod.RangeType.VERSION, rangeValue);
    table = new Table(location(1, 15), qualifiedName(location(1, 15), "t"), queryPeriod);
    assertThat(statement("SELECT * FROM t FOR VERSION AS OF 'version1'")).isEqualTo(new Query(location(1, 1), Optional.empty(), new QuerySpecification(location(1, 1), new Select(location(1, 1), false, ImmutableList.of(new AllColumns(location(1, 8), Optional.empty(), ImmutableList.of()))), Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : QuerySpecification(io.trino.sql.tree.QuerySpecification) CreateTable(io.trino.sql.tree.CreateTable) DropTable(io.trino.sql.tree.DropTable) Table(io.trino.sql.tree.Table) TruncateTable(io.trino.sql.tree.TruncateTable) RenameTable(io.trino.sql.tree.RenameTable) TimestampLiteral(io.trino.sql.tree.TimestampLiteral) QueryPeriod(io.trino.sql.tree.QueryPeriod) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) StringLiteral(io.trino.sql.tree.StringLiteral) NodeLocation(io.trino.sql.tree.NodeLocation) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) Select(io.trino.sql.tree.Select) AllColumns(io.trino.sql.tree.AllColumns) Test(org.junit.jupiter.api.Test)

Example 4 with Select

use of io.trino.sql.tree.Select in project trino by trinodb.

the class TestSqlParser method testAllRowsReference.

@Test
public void testAllRowsReference() {
    assertThatThrownBy(() -> SQL_PARSER.createStatement("SELECT 1 + A.*", new ParsingOptions(REJECT))).isInstanceOf(ParsingException.class).hasMessageMatching("line 1:13: mismatched input '.'.*");
    assertThat(statement("SELECT A.*")).ignoringLocation().isEqualTo(simpleQuery(new Select(false, ImmutableList.of(new AllColumns(new Identifier("A"), ImmutableList.of())))));
}
Also used : QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) Select(io.trino.sql.tree.Select) AllColumns(io.trino.sql.tree.AllColumns) Test(org.junit.jupiter.api.Test)

Example 5 with Select

use of io.trino.sql.tree.Select in project trino by trinodb.

the class AstBuilder method visitQuerySpecification.

@Override
public Node visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) {
    Optional<Relation> from = Optional.empty();
    List<SelectItem> selectItems = visit(context.selectItem(), SelectItem.class);
    List<Relation> relations = visit(context.relation(), Relation.class);
    if (!relations.isEmpty()) {
        // synthesize implicit join nodes
        Iterator<Relation> iterator = relations.iterator();
        Relation relation = iterator.next();
        while (iterator.hasNext()) {
            relation = new Join(getLocation(context), Join.Type.IMPLICIT, relation, iterator.next(), Optional.empty());
        }
        from = Optional.of(relation);
    }
    return new QuerySpecification(getLocation(context), new Select(getLocation(context.SELECT()), isDistinct(context.setQuantifier()), selectItems), from, visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), visit(context.windowDefinition(), WindowDefinition.class), Optional.empty(), Optional.empty(), Optional.empty());
}
Also used : QuerySpecification(io.trino.sql.tree.QuerySpecification) AliasedRelation(io.trino.sql.tree.AliasedRelation) SampledRelation(io.trino.sql.tree.SampledRelation) Relation(io.trino.sql.tree.Relation) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) GroupBy(io.trino.sql.tree.GroupBy) SimpleGroupBy(io.trino.sql.tree.SimpleGroupBy) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) TryExpression(io.trino.sql.tree.TryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) SelectItem(io.trino.sql.tree.SelectItem) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) Select(io.trino.sql.tree.Select) NaturalJoin(io.trino.sql.tree.NaturalJoin) Join(io.trino.sql.tree.Join) WindowDefinition(io.trino.sql.tree.WindowDefinition)

Aggregations

Select (io.trino.sql.tree.Select)7 QuerySpecification (io.trino.sql.tree.QuerySpecification)6 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)5 CreateTableAsSelect (io.trino.sql.tree.CreateTableAsSelect)5 Expression (io.trino.sql.tree.Expression)5 AllColumns (io.trino.sql.tree.AllColumns)4 DereferenceExpression (io.trino.sql.tree.DereferenceExpression)4 Identifier (io.trino.sql.tree.Identifier)4 LogicalExpression (io.trino.sql.tree.LogicalExpression)4 SingleColumn (io.trino.sql.tree.SingleColumn)4 Table (io.trino.sql.tree.Table)4 ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)3 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)3 CreateTable (io.trino.sql.tree.CreateTable)3 DropTable (io.trino.sql.tree.DropTable)3 LongLiteral (io.trino.sql.tree.LongLiteral)3 SelectItem (io.trino.sql.tree.SelectItem)3 Test (org.junit.jupiter.api.Test)3 QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)2 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)2