Search in sources :

Example 1 with OffsetNode

use of org.apache.phoenix.parse.OffsetNode in project phoenix by apache.

the class OffsetCompiler method compile.

public static Integer compile(StatementContext context, FilterableStatement statement) throws SQLException {
    OffsetNode offsetNode = statement.getOffset();
    if (offsetNode == null) {
        return null;
    }
    OffsetParseNodeVisitor visitor = new OffsetParseNodeVisitor(context);
    offsetNode.getOffsetParseNode().accept(visitor);
    return visitor.getOffset();
}
Also used : OffsetNode(org.apache.phoenix.parse.OffsetNode)

Example 2 with OffsetNode

use of org.apache.phoenix.parse.OffsetNode in project phoenix by apache.

the class SubselectRewriter method flatten.

private SelectStatement flatten(SelectStatement select, SelectStatement subselect) throws SQLException {
    // Replace aliases in sub-select first.
    subselect = ParseNodeRewriter.rewrite(subselect, this);
    ParseNode whereRewrite = subselect.getWhere();
    List<ParseNode> groupByRewrite = subselect.getGroupBy();
    ParseNode havingRewrite = subselect.getHaving();
    List<OrderByNode> orderByRewrite = subselect.getOrderBy();
    LimitNode limitRewrite = subselect.getLimit();
    OffsetNode offsetRewrite = subselect.getOffset();
    HintNode hintRewrite = subselect.getHint();
    boolean isDistinctRewrite = subselect.isDistinct();
    boolean isAggregateRewrite = subselect.isAggregate();
    ParseNode where = select.getWhere();
    if (where != null) {
        if (subselect.getLimit() != null || (subselect.isAggregate() && subselect.getGroupBy().isEmpty())) {
            return select;
        }
        ParseNode postFilter = where.accept(this);
        if (subselect.getGroupBy().isEmpty()) {
            whereRewrite = whereRewrite == null ? postFilter : NODE_FACTORY.and(Arrays.<ParseNode>asList(whereRewrite, postFilter));
        } else {
            havingRewrite = havingRewrite == null ? postFilter : NODE_FACTORY.and(Arrays.<ParseNode>asList(havingRewrite, postFilter));
        }
    }
    if (select.isDistinct()) {
        if (subselect.getLimit() != null || subselect.isAggregate() || subselect.isDistinct()) {
            return select;
        }
        isDistinctRewrite = true;
        orderByRewrite = null;
    }
    if (select.isAggregate()) {
        if (subselect.getLimit() != null || subselect.isAggregate() || subselect.isDistinct()) {
            return select;
        }
        isAggregateRewrite = true;
        orderByRewrite = null;
    }
    List<ParseNode> groupBy = select.getGroupBy();
    if (!groupBy.isEmpty()) {
        if (subselect.getLimit() != null || subselect.isAggregate() || subselect.isDistinct()) {
            return select;
        }
        groupByRewrite = Lists.<ParseNode>newArrayListWithExpectedSize(groupBy.size());
        for (ParseNode node : groupBy) {
            groupByRewrite.add(node.accept(this));
        }
        if (select.getHaving() != null) {
            havingRewrite = select.getHaving().accept(this);
        }
        orderByRewrite = null;
    }
    List<AliasedNode> selectNodes = select.getSelect();
    List<AliasedNode> selectNodesRewrite = Lists.newArrayListWithExpectedSize(selectNodes.size());
    for (AliasedNode aliasedNode : selectNodes) {
        ParseNode node = aliasedNode.getNode();
        if (node instanceof WildcardParseNode || (node instanceof TableWildcardParseNode && ((TableWildcardParseNode) node).getTableName().equals(tableAlias))) {
            for (AliasedNode aNode : subselect.getSelect()) {
                String alias = aNode.getAlias();
                String aliasRewrite = alias == null ? null : SchemaUtil.getColumnName(tableAlias, alias);
                selectNodesRewrite.add(NODE_FACTORY.aliasedNode(aliasRewrite, aNode.getNode()));
            }
        } else {
            selectNodesRewrite.add(NODE_FACTORY.aliasedNode(aliasedNode.getAlias(), node.accept(this)));
        }
    }
    List<OrderByNode> orderBy = select.getOrderBy();
    if (!orderBy.isEmpty()) {
        if (subselect.getLimit() != null) {
            return select;
        }
        orderByRewrite = Lists.newArrayListWithExpectedSize(orderBy.size());
        for (OrderByNode orderByNode : orderBy) {
            ParseNode node = orderByNode.getNode();
            orderByRewrite.add(NODE_FACTORY.orderBy(node.accept(this), orderByNode.isNullsLast(), orderByNode.isAscending()));
        }
    }
    OffsetNode offset = select.getOffset();
    if (offsetRewrite != null || (limitRewrite != null && offset != null)) {
        return select;
    } else {
        offsetRewrite = offset;
    }
    LimitNode limit = select.getLimit();
    if (limit != null) {
        if (limitRewrite == null) {
            limitRewrite = limit;
        } else {
            Integer limitValue = LimitCompiler.compile(null, select);
            Integer limitValueSubselect = LimitCompiler.compile(null, subselect);
            if (limitValue != null && limitValueSubselect != null) {
                limitRewrite = limitValue < limitValueSubselect ? limit : limitRewrite;
            } else {
                return select;
            }
        }
    }
    HintNode hint = select.getHint();
    if (hint != null) {
        hintRewrite = hintRewrite == null ? hint : HintNode.combine(hint, hintRewrite);
    }
    SelectStatement stmt = NODE_FACTORY.select(subselect.getFrom(), hintRewrite, isDistinctRewrite, selectNodesRewrite, whereRewrite, groupByRewrite, havingRewrite, orderByRewrite, limitRewrite, offsetRewrite, select.getBindCount(), isAggregateRewrite, select.hasSequence(), select.getSelects(), select.getUdfParseNodes());
    if (tableAlias != null) {
        this.removeAlias = true;
        stmt = ParseNodeRewriter.rewrite(stmt, this);
    }
    return stmt;
}
Also used : OffsetNode(org.apache.phoenix.parse.OffsetNode) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) OrderByNode(org.apache.phoenix.parse.OrderByNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) AliasedNode(org.apache.phoenix.parse.AliasedNode) SelectStatement(org.apache.phoenix.parse.SelectStatement) LimitNode(org.apache.phoenix.parse.LimitNode) HintNode(org.apache.phoenix.parse.HintNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) ParseNode(org.apache.phoenix.parse.ParseNode)

Aggregations

OffsetNode (org.apache.phoenix.parse.OffsetNode)2 AliasedNode (org.apache.phoenix.parse.AliasedNode)1 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)1 HintNode (org.apache.phoenix.parse.HintNode)1 LimitNode (org.apache.phoenix.parse.LimitNode)1 OrderByNode (org.apache.phoenix.parse.OrderByNode)1 ParseNode (org.apache.phoenix.parse.ParseNode)1 SelectStatement (org.apache.phoenix.parse.SelectStatement)1 TableWildcardParseNode (org.apache.phoenix.parse.TableWildcardParseNode)1 WildcardParseNode (org.apache.phoenix.parse.WildcardParseNode)1