Package org.apache.phoenix.schema

Examples of org.apache.phoenix.schema.TableRef


                }
            }
            // Add any dynamic columns to the table declaration
            List<ColumnDef> dynamicColumns = tableNode.getDynamicColumns();
            theTable = addDynamicColumns(dynamicColumns, theTable);
            TableRef tableRef = new TableRef(tableNode.getAlias(), theTable, timeStamp, !dynamicColumns.isEmpty());
            if (logger.isDebugEnabled() && timeStamp != QueryConstants.UNSET_TIMESTAMP) {
                logger.debug("Re-resolved stale table " + fullTableName + " with seqNum " + tableRef.getTable().getSequenceNumber() + " at timestamp " + tableRef.getTable().getTimeStamp() + " with " + tableRef.getTable().getColumns().size() + " columns: " + tableRef.getTable().getColumns());
            }
            return tableRef;
        }
View Full Code Here


        }

        @Override
        public Void visit(NamedTableNode tableNode) throws SQLException {
            String alias = tableNode.getAlias();
            TableRef tableRef = createTableRef(tableNode, true);
            PTable theTable = tableRef.getTable();

            if (alias != null) {
                tableMap.put(alias, tableRef);
            }
View Full Code Here

                    PTableType.SUBQUERY, null, MetaDataProtocol.MIN_TABLE_TIMESTAMP, PTable.INITIAL_SEQ_NUM,
                    null, null, columns, null, Collections.<PTable>emptyList(), false,
                    Collections.<PName>emptyList(), null, null, false, false, null, null, null);
           
            String alias = subselectNode.getAlias();
            TableRef tableRef = new TableRef(alias, t, MetaDataProtocol.MIN_TABLE_TIMESTAMP, false);
            tableMap.put(alias, tableRef);
            tables.add(tableRef);
            return null;
        }
View Full Code Here

        private ColumnFamilyRef resolveColumnFamily(String tableName, String cfName) throws SQLException {
            if (tableName == null) {
                ColumnFamilyRef theColumnFamilyRef = null;
                Iterator<TableRef> iterator = tables.iterator();
                while (iterator.hasNext()) {
                    TableRef tableRef = iterator.next();
                    try {
                        PColumnFamily columnFamily = tableRef.getTable().getColumnFamily(cfName);
                        if (theColumnFamilyRef != null) { throw new TableNotFoundException(cfName); }
                        theColumnFamilyRef = new ColumnFamilyRef(tableRef, columnFamily);
                    } catch (ColumnFamilyNotFoundException e) {}
                }
                if (theColumnFamilyRef != null) { return theColumnFamilyRef; }
                throw new TableNotFoundException(cfName);
            } else {
                TableRef tableRef = resolveTable(null, tableName);
                PColumnFamily columnFamily = tableRef.getTable().getColumnFamily(cfName);
                return new ColumnFamilyRef(tableRef, columnFamily);
            }
        }
View Full Code Here

        @Override
        public ColumnRef resolveColumn(String schemaName, String tableName, String colName) throws SQLException {
            if (tableName == null) {
                int theColumnPosition = -1;
                TableRef theTableRef = null;
                Iterator<TableRef> iterator = tables.iterator();
                while (iterator.hasNext()) {
                    TableRef tableRef = iterator.next();
                    try {
                        PColumn column = tableRef.getTable().getColumn(colName);
                        if (theTableRef != null) { throw new AmbiguousColumnException(colName); }
                        theTableRef = tableRef;
                        theColumnPosition = column.getPosition();
                    } catch (ColumnNotFoundException e) {

                    }
                }
                if (theTableRef != null) { return new ColumnRef(theTableRef, theColumnPosition); }
                throw new ColumnNotFoundException(colName);
            } else {
                try {
                    TableRef tableRef = resolveTable(schemaName, tableName);
                    PColumn column = tableRef.getTable().getColumn(colName);
                    return new ColumnRef(tableRef, column.getPosition());
                } catch (TableNotFoundException e) {
                    // Try using the tableName as a columnFamily reference instead
                    ColumnFamilyRef cfRef = resolveColumnFamily(schemaName, tableName);
                    PColumn column = cfRef.getFamily().getColumn(colName);
View Full Code Here

        }
        return ret;
    }
   
    public static SelectStatement optimize(PhoenixStatement statement, SelectStatement select, final ColumnResolver resolver) throws SQLException {
        TableRef groupByTableRef = null;
        TableRef orderByTableRef = null;
        if (select.getGroupBy() != null && !select.getGroupBy().isEmpty()) {
            ColumnParseNodeVisitor groupByVisitor = new ColumnParseNodeVisitor(resolver);
            for (ParseNode node : select.getGroupBy()) {
                node.accept(groupByVisitor);
            }
            Set<TableRef> set = groupByVisitor.getTableRefSet();
            if (set.size() == 1) {
                groupByTableRef = set.iterator().next();
            }
        } else if (select.getOrderBy() != null && !select.getOrderBy().isEmpty()) {
            ColumnParseNodeVisitor orderByVisitor = new ColumnParseNodeVisitor(resolver);
            for (OrderByNode node : select.getOrderBy()) {
                node.getNode().accept(orderByVisitor);
            }
            Set<TableRef> set = orderByVisitor.getTableRefSet();
            if (set.size() == 1) {
                orderByTableRef = set.iterator().next();
            }
        }
        JoinTable join = compile(statement, select, resolver);
        if (groupByTableRef != null || orderByTableRef != null) {
            QueryCompiler compiler = new QueryCompiler(statement, select, resolver);
            List<Object> binds = statement.getParameters();
            StatementContext ctx = new StatementContext(statement, resolver, new Scan(), new SequenceManager(statement));
            QueryPlan plan = compiler.compileJoinQuery(ctx, binds, join, false);
            TableRef table = plan.getTableRef();
            if (groupByTableRef != null && !groupByTableRef.equals(table)) {
                groupByTableRef = null;
            }
            if (orderByTableRef != null && !orderByTableRef.equals(table)) {
                orderByTableRef = null;
            }           
        }
       
        final Map<TableRef, TableRef> replacement = new HashMap<TableRef, TableRef>();
       
        for (Table table : join.getTables()) {
            if (table.isSubselect())
                continue;
            TableRef tableRef = table.getTableRef();
            List<ParseNode> groupBy = tableRef.equals(groupByTableRef) ? select.getGroupBy() : null;
            List<OrderByNode> orderBy = tableRef.equals(orderByTableRef) ? select.getOrderBy() : null;
            SelectStatement stmt = getSubqueryForOptimizedPlan(select.getHint(), table.getDynamicColumns(), tableRef, join.getColumnRefs(), table.getPreFiltersCombined(), groupBy, orderBy, table.isWildCardSelect(), select.hasSequence());
            QueryPlan plan = statement.getConnection().getQueryServices().getOptimizer().optimize(statement, stmt);
            if (!plan.getTableRef().equals(tableRef)) {
                replacement.put(tableRef, plan.getTableRef());
            }           
        }
       
        if (replacement.isEmpty())
            return select;
       
        List<TableNode> from = select.getFrom();
        List<TableNode> newFrom = Lists.newArrayListWithExpectedSize(from.size());
        for (TableNode node : from) {
            newFrom.add(node.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 NODE_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 NODE_FACTORY.join(joinNode.getType(), lhsReplace, rhsReplace, joinNode.getOnNode());
                }

                @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 NODE_FACTORY.namedTable(alias == null ? null : '"' + alias + '"', getReplacedTableName(replaceRef), namedTableNode.getDynamicColumns());
View Full Code Here

            return origResolver.resolveTable(name.getSchemaName(), name.getTableName());
        }

        @Override
        public Pair<Table, List<JoinSpec>> visit(BindTableNode boundTableNode) throws SQLException {
            TableRef tableRef = resolveTable(boundTableNode.getAlias(), boundTableNode.getName());
            List<AliasedNode> selectNodes = extractFromSelect(select.getSelect(), tableRef, origResolver);
            Table table = new Table(boundTableNode, Collections.<ColumnDef>emptyList(), selectNodes, tableRef);
            return new Pair<Table, List<JoinSpec>>(table, null);
        }
View Full Code Here

        }

        @Override
        public Pair<Table, List<JoinSpec>> visit(NamedTableNode namedTableNode)
                throws SQLException {
            TableRef tableRef = resolveTable(namedTableNode.getAlias(), namedTableNode.getName());
            List<AliasedNode> selectNodes = extractFromSelect(select.getSelect(), tableRef, origResolver);
            Table table = new Table(namedTableNode, namedTableNode.getDynamicColumns(), selectNodes, tableRef);
            return new Pair<Table, List<JoinSpec>>(table, null);
        }
View Full Code Here

        }

        @Override
        public Pair<Table, List<JoinSpec>> visit(DerivedTableNode subselectNode)
                throws SQLException {
            TableRef tableRef = resolveTable(subselectNode.getAlias(), null);
            List<AliasedNode> selectNodes = extractFromSelect(select.getSelect(), tableRef, origResolver);
            Table table = new Table(subselectNode, selectNodes, tableRef);
            return new Pair<Table, List<JoinSpec>>(table, null);
        }
View Full Code Here

                        && joinSpec.getType() != JoinType.Inner)
                    return null;
                vector[i] = true;
                Iterator<TableRef> iter = joinSpec.getDependencies().iterator();
                while (vector[i] == true && iter.hasNext()) {
                    TableRef tableRef = iter.next();
                    if (!tableRef.equals(table.getTableRef())) {
                        vector[i] = false;
                    }
                }
            }
           
View Full Code Here

TOP

Related Classes of org.apache.phoenix.schema.TableRef

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.