Search in sources :

Example 6 with JoinType

use of org.apache.phoenix.parse.JoinTableNode.JoinType in project phoenix by apache.

the class SubqueryRewriter method visitLeave.

@Override
public ParseNode visitLeave(InParseNode node, List<ParseNode> l) throws SQLException {
    boolean isTopNode = topNode == node;
    if (isTopNode) {
        topNode = null;
    }
    SubqueryParseNode subqueryNode = (SubqueryParseNode) l.get(1);
    SelectStatement subquery = fixSubqueryStatement(subqueryNode.getSelectNode());
    String rhsTableAlias = ParseNodeFactory.createTempAlias();
    List<AliasedNode> selectNodes = fixAliasedNodes(subquery.getSelect(), true);
    subquery = NODE_FACTORY.select(subquery, !node.isSubqueryDistinct(), selectNodes);
    ParseNode onNode = getJoinConditionNode(l.get(0), selectNodes, rhsTableAlias);
    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)

Example 7 with JoinType

use of org.apache.phoenix.parse.JoinTableNode.JoinType in project phoenix by apache.

the class SubqueryRewriter method leaveArrayComparisonNode.

protected List<ParseNode> leaveArrayComparisonNode(ParseNode node, List<ParseNode> l) throws SQLException {
    boolean isTopNode = topNode == node;
    if (isTopNode) {
        topNode = null;
    }
    ParseNode firstChild = l.get(0);
    if (!(firstChild instanceof SubqueryParseNode)) {
        return l;
    }
    SubqueryParseNode subqueryNode = (SubqueryParseNode) firstChild;
    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 any/all comparison subquery
        return l;
    }
    ParseNode rhsNode = null;
    boolean isNonGroupByAggregate = subquery.getGroupBy().isEmpty() && subquery.isAggregate();
    List<AliasedNode> aliasedNodes = subquery.getSelect();
    String derivedTableAlias = null;
    if (!subquery.getGroupBy().isEmpty()) {
        derivedTableAlias = ParseNodeFactory.createTempAlias();
        aliasedNodes = fixAliasedNodes(aliasedNodes, false);
    }
    if (aliasedNodes.size() == 1) {
        rhsNode = derivedTableAlias == null ? aliasedNodes.get(0).getNode() : NODE_FACTORY.column(NODE_FACTORY.table(null, derivedTableAlias), aliasedNodes.get(0).getAlias(), null);
    } else {
        List<ParseNode> nodes = Lists.<ParseNode>newArrayListWithExpectedSize(aliasedNodes.size());
        for (AliasedNode aliasedNode : aliasedNodes) {
            nodes.add(derivedTableAlias == null ? aliasedNode.getNode() : NODE_FACTORY.column(NODE_FACTORY.table(null, derivedTableAlias), aliasedNode.getAlias(), null));
        }
        rhsNode = NODE_FACTORY.rowValueConstructor(nodes);
    }
    if (!isNonGroupByAggregate) {
        rhsNode = NODE_FACTORY.function(DistinctValueAggregateFunction.NAME, Collections.singletonList(rhsNode));
    }
    List<AliasedNode> additionalSelectNodes = conditionExtractor.getAdditionalSelectNodes();
    List<AliasedNode> selectNodes = Lists.newArrayListWithExpectedSize(additionalSelectNodes.size() + 1);
    selectNodes.add(NODE_FACTORY.aliasedNode(ParseNodeFactory.createTempAlias(), rhsNode));
    selectNodes.addAll(additionalSelectNodes);
    List<ParseNode> groupbyNodes = Lists.newArrayListWithExpectedSize(additionalSelectNodes.size());
    for (AliasedNode aliasedNode : additionalSelectNodes) {
        groupbyNodes.add(aliasedNode.getNode());
    }
    if (derivedTableAlias == null) {
        subquery = NODE_FACTORY.select(subquery, false, selectNodes, where, groupbyNodes, true);
    } else {
        List<ParseNode> derivedTableGroupBy = Lists.newArrayListWithExpectedSize(subquery.getGroupBy().size() + groupbyNodes.size());
        derivedTableGroupBy.addAll(groupbyNodes);
        derivedTableGroupBy.addAll(subquery.getGroupBy());
        List<AliasedNode> derivedTableSelect = Lists.newArrayListWithExpectedSize(aliasedNodes.size() + selectNodes.size() - 1);
        derivedTableSelect.addAll(aliasedNodes);
        for (int i = 1; i < selectNodes.size(); i++) {
            AliasedNode aliasedNode = selectNodes.get(i);
            String alias = ParseNodeFactory.createTempAlias();
            derivedTableSelect.add(NODE_FACTORY.aliasedNode(alias, aliasedNode.getNode()));
            aliasedNode = NODE_FACTORY.aliasedNode(aliasedNode.getAlias(), NODE_FACTORY.column(NODE_FACTORY.table(null, derivedTableAlias), alias, null));
            selectNodes.set(i, aliasedNode);
            groupbyNodes.set(i - 1, aliasedNode.getNode());
        }
        SelectStatement derivedTableStmt = NODE_FACTORY.select(subquery, subquery.isDistinct(), derivedTableSelect, where, derivedTableGroupBy, true);
        subquery = NODE_FACTORY.select(NODE_FACTORY.derivedTable(derivedTableAlias, derivedTableStmt), subquery.getHint(), false, selectNodes, null, groupbyNodes, null, Collections.<OrderByNode>emptyList(), null, null, subquery.getBindCount(), true, false, Collections.<SelectStatement>emptyList(), subquery.getUdfParseNodes());
    }
    ParseNode onNode = conditionExtractor.getJoinCondition();
    TableNode rhsTable = NODE_FACTORY.derivedTable(rhsTableAlias, subquery);
    JoinType joinType = isTopNode ? JoinType.Inner : JoinType.Left;
    tableNode = NODE_FACTORY.join(joinType, tableNode, rhsTable, onNode, false);
    firstChild = NODE_FACTORY.column(NODE_FACTORY.table(null, rhsTableAlias), selectNodes.get(0).getAlias(), null);
    if (isNonGroupByAggregate) {
        firstChild = NODE_FACTORY.upsertStmtArrayNode(Collections.singletonList(firstChild));
    }
    ComparisonParseNode secondChild = (ComparisonParseNode) l.get(1);
    secondChild = NODE_FACTORY.comparison(secondChild.getFilterOp(), secondChild.getLHS(), NODE_FACTORY.elementRef(Lists.newArrayList(firstChild, NODE_FACTORY.literal(1))));
    return Lists.newArrayList(firstChild, secondChild);
}
Also used : OrderByNode(org.apache.phoenix.parse.OrderByNode) JoinType(org.apache.phoenix.parse.JoinTableNode.JoinType) AliasedNode(org.apache.phoenix.parse.AliasedNode) SelectStatement(org.apache.phoenix.parse.SelectStatement) SubqueryParseNode(org.apache.phoenix.parse.SubqueryParseNode) TableNode(org.apache.phoenix.parse.TableNode) ComparisonParseNode(org.apache.phoenix.parse.ComparisonParseNode) 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)

Aggregations

JoinType (org.apache.phoenix.parse.JoinTableNode.JoinType)7 AliasedNode (org.apache.phoenix.parse.AliasedNode)5 ParseNode (org.apache.phoenix.parse.ParseNode)5 SelectStatement (org.apache.phoenix.parse.SelectStatement)5 SubqueryParseNode (org.apache.phoenix.parse.SubqueryParseNode)5 TableNode (org.apache.phoenix.parse.TableNode)5 AndParseNode (org.apache.phoenix.parse.AndParseNode)4 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)4 ComparisonParseNode (org.apache.phoenix.parse.ComparisonParseNode)4 CompoundParseNode (org.apache.phoenix.parse.CompoundParseNode)4 ExistsParseNode (org.apache.phoenix.parse.ExistsParseNode)4 InParseNode (org.apache.phoenix.parse.InParseNode)4 LiteralParseNode (org.apache.phoenix.parse.LiteralParseNode)4 RowValueConstructorParseNode (org.apache.phoenix.parse.RowValueConstructorParseNode)4 Expression (org.apache.phoenix.expression.Expression)3 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 OrderByNode (org.apache.phoenix.parse.OrderByNode)2 KeyValueSchema (org.apache.phoenix.schema.KeyValueSchema)2