Search in sources :

Example 16 with TableName

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

the class StatementNormalizer method normalize.

/**
     * Rewrite the select statement by switching any constants to the right hand side
     * of the expression.
     * @param statement the select statement
     * @param resolver 
     * @return new select statement
     * @throws SQLException 
     */
public static SelectStatement normalize(SelectStatement statement, ColumnResolver resolver) throws SQLException {
    boolean multiTable = statement.isJoin();
    // Replace WildcardParse with a list of TableWildcardParseNode for multi-table queries
    if (multiTable) {
        List<AliasedNode> selectNodes = statement.getSelect();
        List<AliasedNode> normSelectNodes = selectNodes;
        for (int i = 0; i < selectNodes.size(); i++) {
            AliasedNode aliasedNode = selectNodes.get(i);
            ParseNode selectNode = aliasedNode.getNode();
            if (selectNode == WildcardParseNode.INSTANCE) {
                if (selectNodes == normSelectNodes) {
                    normSelectNodes = Lists.newArrayList(selectNodes.subList(0, i));
                }
                List<TableName> tableNames = statement.getFrom().accept(new TableNameVisitor());
                for (TableName tableName : tableNames) {
                    TableWildcardParseNode node = NODE_FACTORY.tableWildcard(tableName);
                    normSelectNodes.add(NODE_FACTORY.aliasedNode(null, node));
                }
            } else if (selectNodes != normSelectNodes) {
                normSelectNodes.add(aliasedNode);
            }
        }
        if (selectNodes != normSelectNodes) {
            statement = NODE_FACTORY.select(statement.getFrom(), statement.getHint(), statement.isDistinct(), normSelectNodes, statement.getWhere(), statement.getGroupBy(), statement.getHaving(), statement.getOrderBy(), statement.getLimit(), statement.getOffset(), statement.getBindCount(), statement.isAggregate(), statement.hasSequence(), statement.getSelects(), statement.getUdfParseNodes());
        }
    }
    return rewrite(statement, new StatementNormalizer(resolver, statement.getSelect().size(), multiTable));
}
Also used : TableName(org.apache.phoenix.parse.TableName) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) BetweenParseNode(org.apache.phoenix.parse.BetweenParseNode) FamilyWildcardParseNode(org.apache.phoenix.parse.FamilyWildcardParseNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) LessThanOrEqualParseNode(org.apache.phoenix.parse.LessThanOrEqualParseNode) ComparisonParseNode(org.apache.phoenix.parse.ComparisonParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) AliasedNode(org.apache.phoenix.parse.AliasedNode)

Example 17 with TableName

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

the class SubqueryRewriter method getJoinConditionNode.

private ParseNode getJoinConditionNode(ParseNode lhs, List<AliasedNode> rhs, String rhsTableAlias) throws SQLException {
    List<ParseNode> lhsNodes;
    if (lhs instanceof RowValueConstructorParseNode) {
        lhsNodes = ((RowValueConstructorParseNode) lhs).getChildren();
    } else {
        lhsNodes = Collections.singletonList(lhs);
    }
    if (lhsNodes.size() != (rhs.size() - 1))
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.SUBQUERY_RETURNS_DIFFERENT_NUMBER_OF_FIELDS).build().buildException();
    int count = lhsNodes.size();
    TableName rhsTableName = NODE_FACTORY.table(null, rhsTableAlias);
    List<ParseNode> equalNodes = Lists.newArrayListWithExpectedSize(count);
    for (int i = 0; i < count; i++) {
        ParseNode rhsNode = NODE_FACTORY.column(rhsTableName, rhs.get(i + 1).getAlias(), null);
        equalNodes.add(NODE_FACTORY.equal(lhsNodes.get(i), rhsNode));
    }
    return count == 1 ? equalNodes.get(0) : NODE_FACTORY.and(equalNodes);
}
Also used : TableName(org.apache.phoenix.parse.TableName) 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) RowValueConstructorParseNode(org.apache.phoenix.parse.RowValueConstructorParseNode) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Aggregations

TableName (org.apache.phoenix.parse.TableName)17 ParseNode (org.apache.phoenix.parse.ParseNode)11 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)8 TableWildcardParseNode (org.apache.phoenix.parse.TableWildcardParseNode)6 WildcardParseNode (org.apache.phoenix.parse.WildcardParseNode)6 HashMap (java.util.HashMap)5 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)5 ComparisonParseNode (org.apache.phoenix.parse.ComparisonParseNode)5 ArrayList (java.util.ArrayList)4 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)4 AndParseNode (org.apache.phoenix.parse.AndParseNode)4 PLong (org.apache.phoenix.schema.types.PLong)4 Map (java.util.Map)3 Scan (org.apache.hadoop.hbase.client.Scan)3 ColumnResolver (org.apache.phoenix.compile.ColumnResolver)3 MutationState (org.apache.phoenix.execute.MutationState)3 Expression (org.apache.phoenix.expression.Expression)3 AliasedNode (org.apache.phoenix.parse.AliasedNode)3 ColumnDef (org.apache.phoenix.parse.ColumnDef)3 NamedTableNode (org.apache.phoenix.parse.NamedTableNode)3