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));
}
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);
}
Aggregations