Search in sources :

Example 1 with SelectItem

use of io.trino.sql.tree.SelectItem 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 2 with SelectItem

use of io.trino.sql.tree.SelectItem 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)

Example 3 with SelectItem

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

the class QueryRewriter method checksumSql.

private String checksumSql(List<Column> columns, QualifiedName table) throws QueryRewriteException {
    if (columns.isEmpty()) {
        throw new QueryRewriteException("Table " + table + " has no columns");
    }
    ImmutableList.Builder<SelectItem> selectItems = ImmutableList.builder();
    for (Column column : columns) {
        Expression expression = new Identifier(column.getName());
        if (column.isApproximateType()) {
            expression = new FunctionCall(QualifiedName.of("round"), ImmutableList.of(expression, new LongLiteral(Integer.toString(doublePrecision))));
        }
        selectItems.add(new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(expression))));
    }
    Select select = new Select(false, selectItems.build());
    return formatSql(new QuerySpecification(select, Optional.of(new Table(table)), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : DropTable(io.trino.sql.tree.DropTable) CreateTable(io.trino.sql.tree.CreateTable) Table(io.trino.sql.tree.Table) LongLiteral(io.trino.sql.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) SingleColumn(io.trino.sql.tree.SingleColumn) QuerySpecification(io.trino.sql.tree.QuerySpecification) Identifier(io.trino.sql.tree.Identifier) SingleColumn(io.trino.sql.tree.SingleColumn) Expression(io.trino.sql.tree.Expression) SelectItem(io.trino.sql.tree.SelectItem) CreateTableAsSelect(io.trino.sql.tree.CreateTableAsSelect) Select(io.trino.sql.tree.Select) FunctionCall(io.trino.sql.tree.FunctionCall)

Aggregations

Expression (io.trino.sql.tree.Expression)3 QuerySpecification (io.trino.sql.tree.QuerySpecification)3 Select (io.trino.sql.tree.Select)3 SelectItem (io.trino.sql.tree.SelectItem)3 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)2 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)2 CreateTableAsSelect (io.trino.sql.tree.CreateTableAsSelect)2 DereferenceExpression (io.trino.sql.tree.DereferenceExpression)2 LogicalExpression (io.trino.sql.tree.LogicalExpression)2 SearchedCaseExpression (io.trino.sql.tree.SearchedCaseExpression)2 SingleColumn (io.trino.sql.tree.SingleColumn)2 ImmutableList (com.google.common.collect.ImmutableList)1 AliasedRelation (io.trino.sql.tree.AliasedRelation)1 ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)1 ArithmeticUnaryExpression (io.trino.sql.tree.ArithmeticUnaryExpression)1 BindExpression (io.trino.sql.tree.BindExpression)1 CreateTable (io.trino.sql.tree.CreateTable)1 DropTable (io.trino.sql.tree.DropTable)1 FunctionCall (io.trino.sql.tree.FunctionCall)1 GroupBy (io.trino.sql.tree.GroupBy)1