Search in sources :

Example 1 with ExistsParseNode

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

the class SubqueryRewriter method visitLeave.

@Override
public ParseNode visitLeave(ExistsParseNode node, List<ParseNode> l) throws SQLException {
    boolean isTopNode = topNode == node;
    if (isTopNode) {
        topNode = null;
    }
    SubqueryParseNode subqueryNode = (SubqueryParseNode) l.get(0);
    SelectStatement subquery = fixSubqueryStatement(subqueryNode.getSelectNode());
    String rhsTableAlias = ParseNodeFactory.createTempAlias();
    JoinConditionExtractor conditionExtractor = new JoinConditionExtractor(subquery, resolver, connection, rhsTableAlias);
    ParseNode where = subquery.getWhere() == null ? null : subquery.getWhere().accept(conditionExtractor);
    if (where == subquery.getWhere()) {
        // non-correlated EXISTS subquery, add LIMIT 1
        subquery = NODE_FACTORY.select(subquery, NODE_FACTORY.limit(NODE_FACTORY.literal(1)));
        subqueryNode = NODE_FACTORY.subquery(subquery, false);
        node = NODE_FACTORY.exists(subqueryNode, node.isNegate());
        return super.visitLeave(node, Collections.<ParseNode>singletonList(subqueryNode));
    }
    List<AliasedNode> additionalSelectNodes = conditionExtractor.getAdditionalSelectNodes();
    List<AliasedNode> selectNodes = Lists.newArrayListWithExpectedSize(additionalSelectNodes.size() + 1);
    selectNodes.add(NODE_FACTORY.aliasedNode(ParseNodeFactory.createTempAlias(), LiteralParseNode.ONE));
    selectNodes.addAll(additionalSelectNodes);
    subquery = NODE_FACTORY.select(subquery, true, selectNodes, where);
    ParseNode onNode = conditionExtractor.getJoinCondition();
    TableNode rhsTable = NODE_FACTORY.derivedTable(rhsTableAlias, subquery);
    JoinType joinType = isTopNode ? (node.isNegate() ? JoinType.Anti : JoinType.Semi) : JoinType.Left;
    ParseNode ret = isTopNode ? null : NODE_FACTORY.isNull(NODE_FACTORY.column(NODE_FACTORY.table(null, rhsTableAlias), selectNodes.get(0).getAlias(), null), !node.isNegate());
    tableNode = NODE_FACTORY.join(joinType, tableNode, rhsTable, onNode, false);
    return ret;
}
Also used : SelectStatement(org.apache.phoenix.parse.SelectStatement) SubqueryParseNode(org.apache.phoenix.parse.SubqueryParseNode) TableNode(org.apache.phoenix.parse.TableNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) AndParseNode(org.apache.phoenix.parse.AndParseNode) ExistsParseNode(org.apache.phoenix.parse.ExistsParseNode) SubqueryParseNode(org.apache.phoenix.parse.SubqueryParseNode) RowValueConstructorParseNode(org.apache.phoenix.parse.RowValueConstructorParseNode) CompoundParseNode(org.apache.phoenix.parse.CompoundParseNode) ComparisonParseNode(org.apache.phoenix.parse.ComparisonParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) InParseNode(org.apache.phoenix.parse.InParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) JoinType(org.apache.phoenix.parse.JoinTableNode.JoinType) AliasedNode(org.apache.phoenix.parse.AliasedNode)

Aggregations

AliasedNode (org.apache.phoenix.parse.AliasedNode)1 AndParseNode (org.apache.phoenix.parse.AndParseNode)1 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)1 ComparisonParseNode (org.apache.phoenix.parse.ComparisonParseNode)1 CompoundParseNode (org.apache.phoenix.parse.CompoundParseNode)1 ExistsParseNode (org.apache.phoenix.parse.ExistsParseNode)1 InParseNode (org.apache.phoenix.parse.InParseNode)1 JoinType (org.apache.phoenix.parse.JoinTableNode.JoinType)1 LiteralParseNode (org.apache.phoenix.parse.LiteralParseNode)1 ParseNode (org.apache.phoenix.parse.ParseNode)1 RowValueConstructorParseNode (org.apache.phoenix.parse.RowValueConstructorParseNode)1 SelectStatement (org.apache.phoenix.parse.SelectStatement)1 SubqueryParseNode (org.apache.phoenix.parse.SubqueryParseNode)1 TableNode (org.apache.phoenix.parse.TableNode)1