Search in sources :

Example 6 with OrderBy

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

the class TreePrinter method print.

public void print(Node root) {
    AstVisitor<Void, Integer> printer = new DefaultTraversalVisitor<Void, Integer>() {

        @Override
        protected Void visitNode(Node node, Integer indentLevel) {
            throw new UnsupportedOperationException("not yet implemented: " + node);
        }

        @Override
        protected Void visitQuery(Query node, Integer indentLevel) {
            print(indentLevel, "Query ");
            indentLevel++;
            print(indentLevel, "QueryBody");
            process(node.getQueryBody(), indentLevel);
            if (node.getOrderBy().isPresent()) {
                print(indentLevel, "OrderBy");
                process(node.getOrderBy().get(), indentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(indentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        @Override
        protected Void visitQuerySpecification(QuerySpecification node, Integer indentLevel) {
            print(indentLevel, "QuerySpecification ");
            indentLevel++;
            process(node.getSelect(), indentLevel);
            if (node.getFrom().isPresent()) {
                print(indentLevel, "From");
                process(node.getFrom().get(), indentLevel + 1);
            }
            if (node.getWhere().isPresent()) {
                print(indentLevel, "Where");
                process(node.getWhere().get(), indentLevel + 1);
            }
            if (node.getGroupBy().isPresent()) {
                String distinct = "";
                if (node.getGroupBy().get().isDistinct()) {
                    distinct = "[DISTINCT]";
                }
                print(indentLevel, "GroupBy" + distinct);
                for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) {
                    print(indentLevel, "SimpleGroupBy");
                    if (groupingElement instanceof SimpleGroupBy) {
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, indentLevel + 1);
                        }
                    } else if (groupingElement instanceof GroupingSets) {
                        print(indentLevel + 1, "GroupingSets");
                        for (List<Expression> set : ((GroupingSets) groupingElement).getSets()) {
                            print(indentLevel + 2, "GroupingSet[");
                            for (Expression expression : set) {
                                process(expression, indentLevel + 3);
                            }
                            print(indentLevel + 2, "]");
                        }
                    } else if (groupingElement instanceof Cube) {
                        print(indentLevel + 1, "Cube");
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, indentLevel + 1);
                        }
                    } else if (groupingElement instanceof Rollup) {
                        print(indentLevel + 1, "Rollup");
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, indentLevel + 1);
                        }
                    }
                }
            }
            if (node.getHaving().isPresent()) {
                print(indentLevel, "Having");
                process(node.getHaving().get(), indentLevel + 1);
            }
            if (node.getOrderBy().isPresent()) {
                print(indentLevel, "OrderBy");
                process(node.getOrderBy().get(), indentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(indentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        protected Void visitOrderBy(OrderBy node, Integer indentLevel) {
            for (SortItem sortItem : node.getSortItems()) {
                process(sortItem, indentLevel);
            }
            return null;
        }

        @Override
        protected Void visitSelect(Select node, Integer indentLevel) {
            String distinct = "";
            if (node.isDistinct()) {
                distinct = "[DISTINCT]";
            }
            print(indentLevel, "Select" + distinct);
            // visit children
            super.visitSelect(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitAllColumns(AllColumns node, Integer indent) {
            if (node.getPrefix().isPresent()) {
                print(indent, node.getPrefix() + ".*");
            } else {
                print(indent, "*");
            }
            return null;
        }

        @Override
        protected Void visitSingleColumn(SingleColumn node, Integer indent) {
            if (node.getAlias().isPresent()) {
                print(indent, "Alias: " + node.getAlias().get());
            }
            // visit children
            super.visitSingleColumn(node, indent + 1);
            return null;
        }

        @Override
        protected Void visitComparisonExpression(ComparisonExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitComparisonExpression(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitArithmeticBinary(ArithmeticBinaryExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitArithmeticBinary(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitLogicalBinaryExpression(LogicalBinaryExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitLogicalBinaryExpression(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitStringLiteral(StringLiteral node, Integer indentLevel) {
            print(indentLevel, "String[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitBinaryLiteral(BinaryLiteral node, Integer indentLevel) {
            print(indentLevel, "Binary[" + node.toHexString() + "]");
            return null;
        }

        @Override
        protected Void visitBooleanLiteral(BooleanLiteral node, Integer indentLevel) {
            print(indentLevel, "Boolean[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitLongLiteral(LongLiteral node, Integer indentLevel) {
            print(indentLevel, "Long[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitLikePredicate(LikePredicate node, Integer indentLevel) {
            print(indentLevel, "LIKE");
            super.visitLikePredicate(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitIdentifier(Identifier node, Integer indentLevel) {
            QualifiedName resolved = resolvedNameReferences.get(node);
            String resolvedName = "";
            if (resolved != null) {
                resolvedName = "=>" + resolved.toString();
            }
            print(indentLevel, "Identifier[" + node.getValue() + resolvedName + "]");
            return null;
        }

        @Override
        protected Void visitDereferenceExpression(DereferenceExpression node, Integer indentLevel) {
            QualifiedName resolved = resolvedNameReferences.get(node);
            String resolvedName = "";
            if (resolved != null) {
                resolvedName = "=>" + resolved.toString();
            }
            print(indentLevel, "DereferenceExpression[" + node + resolvedName + "]");
            return null;
        }

        @Override
        protected Void visitFunctionCall(FunctionCall node, Integer indentLevel) {
            String name = Joiner.on('.').join(node.getName().getParts());
            print(indentLevel, "FunctionCall[" + name + "]");
            super.visitFunctionCall(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitTable(Table node, Integer indentLevel) {
            String name = Joiner.on('.').join(node.getName().getParts());
            print(indentLevel, "Table[" + name + "]");
            return null;
        }

        @Override
        protected Void visitValues(Values node, Integer indentLevel) {
            print(indentLevel, "Values");
            super.visitValues(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitRow(Row node, Integer indentLevel) {
            print(indentLevel, "Row");
            super.visitRow(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitAliasedRelation(AliasedRelation node, Integer indentLevel) {
            print(indentLevel, "Alias[" + node.getAlias() + "]");
            super.visitAliasedRelation(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitSampledRelation(SampledRelation node, Integer indentLevel) {
            print(indentLevel, "TABLESAMPLE[" + node.getType() + " (" + node.getSamplePercentage() + ")]");
            super.visitSampledRelation(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitTableSubquery(TableSubquery node, Integer indentLevel) {
            print(indentLevel, "SubQuery");
            super.visitTableSubquery(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitInPredicate(InPredicate node, Integer indentLevel) {
            print(indentLevel, "IN");
            super.visitInPredicate(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitSubqueryExpression(SubqueryExpression node, Integer indentLevel) {
            print(indentLevel, "SubQuery");
            super.visitSubqueryExpression(node, indentLevel + 1);
            return null;
        }
    };
    printer.process(root, 0);
}
Also used : ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) Query(com.facebook.presto.sql.tree.Query) Rollup(com.facebook.presto.sql.tree.Rollup) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) Node(com.facebook.presto.sql.tree.Node) Values(com.facebook.presto.sql.tree.Values) AllColumns(com.facebook.presto.sql.tree.AllColumns) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) Identifier(com.facebook.presto.sql.tree.Identifier) List(java.util.List) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) GroupingSets(com.facebook.presto.sql.tree.GroupingSets) OrderBy(com.facebook.presto.sql.tree.OrderBy) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Table(com.facebook.presto.sql.tree.Table) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) DefaultTraversalVisitor(com.facebook.presto.sql.tree.DefaultTraversalVisitor) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) LikePredicate(com.facebook.presto.sql.tree.LikePredicate) TableSubquery(com.facebook.presto.sql.tree.TableSubquery) InPredicate(com.facebook.presto.sql.tree.InPredicate) GroupingElement(com.facebook.presto.sql.tree.GroupingElement) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) BinaryLiteral(com.facebook.presto.sql.tree.BinaryLiteral) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Cube(com.facebook.presto.sql.tree.Cube) Select(com.facebook.presto.sql.tree.Select) Row(com.facebook.presto.sql.tree.Row) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation)

Example 7 with OrderBy

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

the class MaterializedViewQueryOptimizer method visitQuerySpecification.

@Override
protected Node visitQuerySpecification(QuerySpecification node, Void context) {
    if (!node.getFrom().isPresent()) {
        throw new IllegalStateException("Query with no From clause is not rewritable by materialized view");
    }
    Relation relation = node.getFrom().get();
    if (relation instanceof AliasedRelation) {
        removablePrefix = Optional.of(((AliasedRelation) relation).getAlias());
        relation = ((AliasedRelation) relation).getRelation();
    }
    if (!(relation instanceof Table)) {
        throw new SemanticException(NOT_SUPPORTED, node, "Relation other than Table is not supported in query optimizer");
    }
    Table baseTable = (Table) relation;
    if (!removablePrefix.isPresent()) {
        removablePrefix = Optional.of(new Identifier(baseTable.getName().toString()));
    }
    if (node.getGroupBy().isPresent()) {
        ImmutableSet.Builder<Expression> expressionsInGroupByBuilder = ImmutableSet.builder();
        for (GroupingElement element : node.getGroupBy().get().getGroupingElements()) {
            element = removeGroupingElementPrefix(element, removablePrefix);
            Optional<Set<Expression>> groupByOfMaterializedView = materializedViewInfo.getGroupBy();
            if (groupByOfMaterializedView.isPresent()) {
                for (Expression expression : element.getExpressions()) {
                    if (!groupByOfMaterializedView.get().contains(expression) || !materializedViewInfo.getBaseToViewColumnMap().containsKey(expression)) {
                        throw new IllegalStateException(format("Grouping element %s is not present in materialized view groupBy field", element));
                    }
                }
            }
            expressionsInGroupByBuilder.addAll(element.getExpressions());
        }
        expressionsInGroupBy = Optional.of(expressionsInGroupByBuilder.build());
    }
    // TODO: Add HAVING validation to the validator https://github.com/prestodb/presto/issues/16406
    if (node.getHaving().isPresent()) {
        throw new SemanticException(NOT_SUPPORTED, node, "Having clause is not supported in query optimizer");
    }
    if (materializedViewInfo.getWhereClause().isPresent()) {
        if (!node.getWhere().isPresent()) {
            throw new IllegalStateException("Query with no where clause is not rewritable by materialized view with where clause");
        }
        QualifiedObjectName baseTableName = createQualifiedObjectName(session, baseTable, baseTable.getName());
        Optional<TableHandle> tableHandle = metadata.getTableHandle(session, baseTableName);
        if (!tableHandle.isPresent()) {
            throw new SemanticException(MISSING_TABLE, node, "Table does not exist");
        }
        ImmutableList.Builder<Field> fields = ImmutableList.builder();
        for (ColumnHandle columnHandle : metadata.getColumnHandles(session, tableHandle.get()).values()) {
            ColumnMetadata columnMetadata = metadata.getColumnMetadata(session, tableHandle.get(), columnHandle);
            fields.add(Field.newUnqualified(materializedViewInfo.getWhereClause().get().getLocation(), columnMetadata.getName(), columnMetadata.getType()));
        }
        Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields.build())).build();
        // Given base query's filter condition and materialized view's filter condition, the goal is to check if MV's filters contain Base's filters (Base implies MV).
        // Let base query's filter condition be A, and MV's filter condition be B.
        // One way to evaluate A implies B is to evaluate logical expression A^~B and check if the output domain is none.
        // If the resulting domain is none, then A^~B is false. Thus A implies B.
        // For more information and examples: https://fb.quip.com/WwmxA40jLMxR
        // TODO: Implement method that utilizes external SAT solver libraries. https://github.com/prestodb/presto/issues/16536
        RowExpression materializedViewWhereCondition = convertToRowExpression(materializedViewInfo.getWhereClause().get(), scope);
        RowExpression baseQueryWhereCondition = convertToRowExpression(node.getWhere().get(), scope);
        RowExpression rewriteLogicExpression = and(baseQueryWhereCondition, call(baseQueryWhereCondition.getSourceLocation(), "not", new FunctionResolution(metadata.getFunctionAndTypeManager()).notFunction(), materializedViewWhereCondition.getType(), materializedViewWhereCondition));
        RowExpression disjunctiveNormalForm = logicalRowExpressions.convertToDisjunctiveNormalForm(rewriteLogicExpression);
        ExtractionResult<VariableReferenceExpression> result = domainTranslator.fromPredicate(session.toConnectorSession(), disjunctiveNormalForm, BASIC_COLUMN_EXTRACTOR);
        if (!result.getTupleDomain().equals(TupleDomain.none())) {
            throw new IllegalStateException("View filter condition does not contain base query's filter condition");
        }
    }
    return new QuerySpecification((Select) process(node.getSelect(), context), node.getFrom().map(from -> (Relation) process(from, context)), node.getWhere().map(where -> (Expression) process(where, context)), node.getGroupBy().map(groupBy -> (GroupBy) process(groupBy, context)), node.getHaving().map(having -> (Expression) process(having, context)), node.getOrderBy().map(orderBy -> (OrderBy) process(orderBy, context)), node.getOffset(), node.getLimit());
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) WarningCollector(com.facebook.presto.spi.WarningCollector) RowExpressionDomainTranslator(com.facebook.presto.sql.relational.RowExpressionDomainTranslator) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) SqlToRowExpressionTranslator(com.facebook.presto.sql.relational.SqlToRowExpressionTranslator) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ExpressionUtils.removeExpressionPrefix(com.facebook.presto.sql.ExpressionUtils.removeExpressionPrefix) SelectItem(com.facebook.presto.sql.tree.SelectItem) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) ExpressionUtils.removeGroupingElementPrefix(com.facebook.presto.sql.ExpressionUtils.removeGroupingElementPrefix) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) ImmutableSet(com.google.common.collect.ImmutableSet) Query(com.facebook.presto.sql.tree.Query) QueryBody(com.facebook.presto.sql.tree.QueryBody) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) ImmutableMap(com.google.common.collect.ImmutableMap) Node(com.facebook.presto.sql.tree.Node) Set(java.util.Set) NOT_SUPPORTED(com.facebook.presto.sql.analyzer.SemanticErrorCode.NOT_SUPPORTED) GroupingElement(com.facebook.presto.sql.tree.GroupingElement) SortItem(com.facebook.presto.sql.tree.SortItem) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Optional(java.util.Optional) ExpressionUtils.removeSortItemPrefix(com.facebook.presto.sql.ExpressionUtils.removeSortItemPrefix) Select(com.facebook.presto.sql.tree.Select) ExtractionResult(com.facebook.presto.spi.relation.DomainTranslator.ExtractionResult) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) ExpressionUtils.removeSingleColumnPrefix(com.facebook.presto.sql.ExpressionUtils.removeSingleColumnPrefix) Logger(com.facebook.airlift.log.Logger) Table(com.facebook.presto.sql.tree.Table) RowExpressionDeterminismEvaluator(com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) Identifier(com.facebook.presto.sql.tree.Identifier) ImmutableList(com.google.common.collect.ImmutableList) LogicalRowExpressions(com.facebook.presto.expressions.LogicalRowExpressions) BASIC_COLUMN_EXTRACTOR(com.facebook.presto.spi.relation.DomainTranslator.BASIC_COLUMN_EXTRACTOR) Objects.requireNonNull(java.util.Objects.requireNonNull) TableHandle(com.facebook.presto.spi.TableHandle) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) AllColumns(com.facebook.presto.sql.tree.AllColumns) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) MaterializedViewInfo(com.facebook.presto.sql.analyzer.MaterializedViewInformationExtractor.MaterializedViewInfo) RowExpression(com.facebook.presto.spi.relation.RowExpression) GroupBy(com.facebook.presto.sql.tree.GroupBy) OrderBy(com.facebook.presto.sql.tree.OrderBy) Relation(com.facebook.presto.sql.tree.Relation) MISSING_TABLE(com.facebook.presto.sql.analyzer.SemanticErrorCode.MISSING_TABLE) Session(com.facebook.presto.Session) LogicalRowExpressions.and(com.facebook.presto.expressions.LogicalRowExpressions.and) AstVisitor(com.facebook.presto.sql.tree.AstVisitor) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) Expression(com.facebook.presto.sql.tree.Expression) ColumnHandle(com.facebook.presto.spi.ColumnHandle) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ImmutableList(com.google.common.collect.ImmutableList) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) Relation(com.facebook.presto.sql.tree.Relation) Identifier(com.facebook.presto.sql.tree.Identifier) ImmutableSet(com.google.common.collect.ImmutableSet) OrderBy(com.facebook.presto.sql.tree.OrderBy) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Table(com.facebook.presto.sql.tree.Table) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) GroupBy(com.facebook.presto.sql.tree.GroupBy) RowExpression(com.facebook.presto.spi.relation.RowExpression) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) GroupingElement(com.facebook.presto.sql.tree.GroupingElement) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableHandle(com.facebook.presto.spi.TableHandle) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation)

Example 8 with OrderBy

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

the class MaterializedViewQueryOptimizer method visitOrderBy.

@Override
protected Node visitOrderBy(OrderBy node, Void context) {
    ImmutableList.Builder<SortItem> rewrittenOrderBy = ImmutableList.builder();
    for (SortItem sortItem : node.getSortItems()) {
        sortItem = removeSortItemPrefix(sortItem, removablePrefix);
        if (!materializedViewInfo.getBaseToViewColumnMap().containsKey(sortItem.getSortKey())) {
            throw new IllegalStateException(format("Sort key %s is not present in materialized view select fields", sortItem.getSortKey()));
        }
        rewrittenOrderBy.add((SortItem) process(sortItem, context));
    }
    return new OrderBy(rewrittenOrderBy.build());
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) SortItem(com.facebook.presto.sql.tree.SortItem) ImmutableList(com.google.common.collect.ImmutableList)

Example 9 with OrderBy

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

the class TestSqlParser method testAggregationWithOrderBy.

@Test
public void testAggregationWithOrderBy() {
    assertExpression("array_agg(x ORDER BY x DESC)", new FunctionCall(QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(identifier("x"), DESCENDING, UNDEFINED)))), false, ImmutableList.of(identifier("x"))));
    assertStatement("SELECT array_agg(x ORDER BY t.y) FROM t", new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new DereferenceExpression(new Identifier("t"), identifier("y")), ASCENDING, UNDEFINED)))), false, ImmutableList.of(new Identifier("x")))), Optional.of(table(QualifiedName.of("t"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) 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) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 10 with OrderBy

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

the class FunctionCallProvider method getExpectedValue.

public FunctionCall getExpectedValue(SymbolAliases aliases) {
    List<Expression> symbolReferences = toSymbolReferences(args, aliases);
    if (isWindowFunction) {
        return new ExpectedWindowFunctionCall(symbolReferences);
    }
    Optional<OrderBy> orderByClause = Optional.empty();
    if (!orderBy.isEmpty()) {
        orderByClause = Optional.of(new OrderBy(orderBy.stream().map(item -> new SortItem(Symbol.from(aliases.get(item.getField())).toSymbolReference(), item.getOrdering(), item.getNullOrdering())).collect(Collectors.toList())));
    }
    return new FunctionCall(name, Optional.empty(), Optional.empty(), orderByClause, distinct, symbolReferences);
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) SortItem(com.facebook.presto.sql.tree.SortItem) Expression(com.facebook.presto.sql.tree.Expression) FunctionCall(com.facebook.presto.sql.tree.FunctionCall)

Aggregations

OrderBy (com.facebook.presto.sql.tree.OrderBy)10 SortItem (com.facebook.presto.sql.tree.SortItem)9 Query (com.facebook.presto.sql.tree.Query)7 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)7 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)6 Expression (com.facebook.presto.sql.tree.Expression)5 Identifier (com.facebook.presto.sql.tree.Identifier)3 Offset (com.facebook.presto.sql.tree.Offset)3 Session (com.facebook.presto.Session)2 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)2 Metadata (com.facebook.presto.metadata.Metadata)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 TableHandle (com.facebook.presto.spi.TableHandle)2 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)2 AliasedRelation (com.facebook.presto.sql.tree.AliasedRelation)2 AllColumns (com.facebook.presto.sql.tree.AllColumns)2 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)2 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)2 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)2