Search in sources :

Example 11 with TableNode

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

the class QueryOptimizer method rewriteQueryWithIndexReplacement.

private static SelectStatement rewriteQueryWithIndexReplacement(final PhoenixConnection connection, final ColumnResolver resolver, final SelectStatement select, final Map<TableRef, TableRef> replacement) throws SQLException {
    TableNode from = select.getFrom();
    TableNode newFrom = from.accept(new TableNodeVisitor<TableNode>() {

        private TableRef resolveTable(String alias, TableName name) throws SQLException {
            if (alias != null)
                return resolver.resolveTable(null, alias);
            return resolver.resolveTable(name.getSchemaName(), name.getTableName());
        }

        private TableName getReplacedTableName(TableRef tableRef) {
            String schemaName = tableRef.getTable().getSchemaName().getString();
            return TableName.create(schemaName.length() == 0 ? null : schemaName, tableRef.getTable().getTableName().getString());
        }

        @Override
        public TableNode visit(BindTableNode boundTableNode) throws SQLException {
            TableRef tableRef = resolveTable(boundTableNode.getAlias(), boundTableNode.getName());
            TableRef replaceRef = replacement.get(tableRef);
            if (replaceRef == null)
                return boundTableNode;
            String alias = boundTableNode.getAlias();
            return FACTORY.bindTable(alias == null ? null : '"' + alias + '"', getReplacedTableName(replaceRef));
        }

        @Override
        public TableNode visit(JoinTableNode joinNode) throws SQLException {
            TableNode lhs = joinNode.getLHS();
            TableNode rhs = joinNode.getRHS();
            TableNode lhsReplace = lhs.accept(this);
            TableNode rhsReplace = rhs.accept(this);
            if (lhs == lhsReplace && rhs == rhsReplace)
                return joinNode;
            return FACTORY.join(joinNode.getType(), lhsReplace, rhsReplace, joinNode.getOnNode(), joinNode.isSingleValueOnly());
        }

        @Override
        public TableNode visit(NamedTableNode namedTableNode) throws SQLException {
            TableRef tableRef = resolveTable(namedTableNode.getAlias(), namedTableNode.getName());
            TableRef replaceRef = replacement.get(tableRef);
            if (replaceRef == null)
                return namedTableNode;
            String alias = namedTableNode.getAlias();
            return FACTORY.namedTable(alias == null ? null : '"' + alias + '"', getReplacedTableName(replaceRef), namedTableNode.getDynamicColumns(), namedTableNode.getTableSamplingRate());
        }

        @Override
        public TableNode visit(DerivedTableNode subselectNode) throws SQLException {
            return subselectNode;
        }
    });
    if (from == newFrom) {
        return select;
    }
    SelectStatement indexSelect = IndexStatementRewriter.translate(FACTORY.select(select, newFrom), resolver, replacement);
    for (TableRef indexTableRef : replacement.values()) {
        // replace expressions with corresponding matching columns for functional indexes
        indexSelect = ParseNodeRewriter.rewrite(indexSelect, new IndexExpressionParseNodeRewriter(indexTableRef.getTable(), indexTableRef.getTableAlias(), connection, indexSelect.getUdfParseNodes()));
    }
    return indexSelect;
}
Also used : SQLException(java.sql.SQLException) JoinTableNode(org.apache.phoenix.parse.JoinTableNode) TableName(org.apache.phoenix.parse.TableName) BindTableNode(org.apache.phoenix.parse.BindTableNode) SelectStatement(org.apache.phoenix.parse.SelectStatement) DerivedTableNode(org.apache.phoenix.parse.DerivedTableNode) TableNode(org.apache.phoenix.parse.TableNode) JoinTableNode(org.apache.phoenix.parse.JoinTableNode) NamedTableNode(org.apache.phoenix.parse.NamedTableNode) BindTableNode(org.apache.phoenix.parse.BindTableNode) DerivedTableNode(org.apache.phoenix.parse.DerivedTableNode) NamedTableNode(org.apache.phoenix.parse.NamedTableNode) IndexExpressionParseNodeRewriter(org.apache.phoenix.parse.IndexExpressionParseNodeRewriter) TableRef(org.apache.phoenix.schema.TableRef)

Aggregations

TableNode (org.apache.phoenix.parse.TableNode)11 SelectStatement (org.apache.phoenix.parse.SelectStatement)9 ParseNode (org.apache.phoenix.parse.ParseNode)8 AndParseNode (org.apache.phoenix.parse.AndParseNode)7 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)7 ComparisonParseNode (org.apache.phoenix.parse.ComparisonParseNode)6 DerivedTableNode (org.apache.phoenix.parse.DerivedTableNode)6 AliasedNode (org.apache.phoenix.parse.AliasedNode)5 BindTableNode (org.apache.phoenix.parse.BindTableNode)5 JoinTableNode (org.apache.phoenix.parse.JoinTableNode)5 JoinType (org.apache.phoenix.parse.JoinTableNode.JoinType)5 NamedTableNode (org.apache.phoenix.parse.NamedTableNode)5 SubqueryParseNode (org.apache.phoenix.parse.SubqueryParseNode)5 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 TableRef (org.apache.phoenix.schema.TableRef)4 EqualParseNode (org.apache.phoenix.parse.EqualParseNode)3