Package com.salesforce.phoenix.schema

Examples of com.salesforce.phoenix.schema.TableRef


      private PTableWrapper table;
      private List<TableRef> tableRefs;
     
      private JoinedTableColumnResolver(PTableWrapper table) {
        this.table = table;
        TableRef tableRef = new TableRef(null, table.getTable(), 0, false);
        this.tableRefs = ImmutableList.of(tableRef);
      }
View Full Code Here


    @Override
    public ColumnRef resolveColumn(String schemaName, String tableName,
        String colName) throws SQLException {
      String name = getProjectedColumnName(schemaName, tableName, colName);
      TableRef tableRef = tableRefs.get(0);
      try {
        PColumn column = tableRef.getTable().getColumn(name);
        return new ColumnRef(tableRef, column.getPosition());
      } catch (ColumnNotFoundException e) {
        List<String> names = table.getMappedColumnName(name);
        if (names.size() == 1) {
          PColumn column = tableRef.getTable().getColumn(names.get(0));
          return new ColumnRef(tableRef, column.getPosition());         
        }
       
        if (names.size() > 1) {
          throw new AmbiguousColumnException(name);
View Full Code Here

    public MutationPlan compile(DeleteStatement delete) throws SQLException {
        final PhoenixConnection connection = statement.getConnection();
        final boolean isAutoCommit = connection.getAutoCommit();
        final ConnectionQueryServices services = connection.getQueryServices();
        final ColumnResolver resolver = FromCompiler.getResolver(delete, connection);
        final TableRef tableRef = resolver.getTables().get(0);
        PTable table = tableRef.getTable();
        if (table.getType() == PTableType.VIEW && table.getViewType().isReadOnly()) {
            throw new ReadOnlyTableException(table.getSchemaName().getString(),table.getTableName().getString());
        }
       
        final boolean hasLimit = delete.getLimit() != null;
        boolean noQueryReqd = !hasLimit && !hasImmutableIndex(tableRef);
        boolean runOnServer = isAutoCommit && noQueryReqd;
        HintNode hint = delete.getHint();
        if (runOnServer && !delete.getHint().hasHint(Hint.USE_INDEX_OVER_DATA_TABLE)) {
            hint = HintNode.create(hint, Hint.USE_DATA_OVER_INDEX_TABLE);
        }

        List<AliasedNode> aliasedNodes = Lists.newArrayListWithExpectedSize(table.getPKColumns().size());
        boolean isSalted = table.getBucketNum() != null;
        boolean isMultiTenant = connection.getTenantId() != null && table.isMultiTenant();
        for (int i = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0); i < table.getPKColumns().size(); i++) {
            PColumn column = table.getPKColumns().get(i);
            aliasedNodes.add(FACTORY.aliasedNode(null, FACTORY.column(null, '"' + column.getName().getString() + '"', null)));
        }
        SelectStatement select = FACTORY.select(
                Collections.singletonList(delete.getTable()),
                hint, false, aliasedNodes, delete.getWhere(),
                Collections.<ParseNode>emptyList(), null,
                delete.getOrderBy(), delete.getLimit(),
                delete.getBindCount(), false);
        DeletingParallelIteratorFactory parallelIteratorFactory = hasLimit ? null : new DeletingParallelIteratorFactory(connection, tableRef);
        final QueryPlan plan = new QueryOptimizer(services).optimize(select, statement, Collections.<PColumn>emptyList(), parallelIteratorFactory);
        if (!plan.getTableRef().equals(tableRef)) {
            runOnServer = false;
            noQueryReqd = false;
        }
       
        final int maxSize = services.getProps().getInt(QueryServices.MAX_MUTATION_SIZE_ATTRIB,QueryServicesOptions.DEFAULT_MAX_MUTATION_SIZE);
        if (hasImmutableIndexWithKeyValueColumns(tableRef)) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.NO_DELETE_IF_IMMUTABLE_INDEX).setSchemaName(tableRef.getTable().getSchemaName().getString())
            .setTableName(tableRef.getTable().getTableName().getString()).build().buildException();
        }
       
        final StatementContext context = plan.getContext();
        // If we're doing a query for a single row with no where clause, then we don't need to contact the server at all.
        // A simple check of the none existence of a where clause in the parse node is not sufficient, as the where clause
        // may have been optimized out.
        if (noQueryReqd && context.isSingleRowScan()) {
            final ImmutableBytesPtr key = new ImmutableBytesPtr(context.getScan().getStartRow());
            return new MutationPlan() {

                @Override
                public ParameterMetaData getParameterMetaData() {
                    return context.getBindManager().getParameterMetaData();
                }

                @Override
                public MutationState execute() {
                    Map<ImmutableBytesPtr,Map<PColumn,byte[]>> mutation = Maps.newHashMapWithExpectedSize(1);
                    mutation.put(key, PRow.DELETE_MARKER);
                    return new MutationState(tableRef, mutation, 0, maxSize, connection);
                }

                @Override
                public ExplainPlan getExplainPlan() throws SQLException {
                    return new ExplainPlan(Collections.singletonList("DELETE SINGLE ROW"));
                }

                @Override
                public PhoenixConnection getConnection() {
                    return connection;
                }
            };
        } else if (runOnServer) {
            // TODO: better abstraction
            Scan scan = context.getScan();
            scan.setAttribute(UngroupedAggregateRegionObserver.DELETE_AGG, QueryConstants.TRUE);

            // Build an ungrouped aggregate query: select COUNT(*) from <table> where <where>
            // The coprocessor will delete each row returned from the scan
            // Ignoring ORDER BY, since with auto commit on and no limit makes no difference
            SelectStatement aggSelect = SelectStatement.create(SelectStatement.COUNT_ONE, delete.getHint());
            final RowProjector projector = ProjectionCompiler.compile(context, aggSelect, GroupBy.EMPTY_GROUP_BY);
            final QueryPlan aggPlan = new AggregatePlan(context, select, tableRef, projector, null, OrderBy.EMPTY_ORDER_BY, null, GroupBy.EMPTY_GROUP_BY, null);
            return new MutationPlan() {

                @Override
                public PhoenixConnection getConnection() {
                    return connection;
                }

                @Override
                public ParameterMetaData getParameterMetaData() {
                    return context.getBindManager().getParameterMetaData();
                }

                @Override
                public MutationState execute() throws SQLException {
                    // TODO: share this block of code with UPSERT SELECT
                    ImmutableBytesWritable ptr = context.getTempPtr();
                    tableRef.getTable().getIndexMaintainers(ptr);
                    ServerCache cache = null;
                    try {
                        if (ptr.getLength() > 0) {
                            IndexMetaDataCacheClient client = new IndexMetaDataCacheClient(connection, tableRef);
                            cache = client.addIndexMetadataCache(context.getScanRanges(), ptr);
View Full Code Here

            assert (jNode instanceof JoinTableNode);
            TableNode tNode = ((JoinTableNode) jNode).getTable();
            for (JoinTable jTable : join.getJoinTables()) {
                if (jTable.getTableNode() != tNode)
                    continue;
                TableRef table = jTable.getTable();
                SelectStatement stmt = getSubqueryForOptimizedPlan(select, table, join.columnRefs, jTable.getPreFiltersCombined());
                QueryPlan plan = context.getConnection().getQueryServices().getOptimizer().optimize(stmt, statement);
                if (!plan.getTableRef().equals(table)) {
                    TableNodeRewriter rewriter = new TableNodeRewriter(plan.getTableRef());
                    jNode.accept(rewriter);
                    newFrom.add(rewriter.getReplacedTableNode());
                    replacement.put(table, plan.getTableRef());
                } else {
                    newFrom.add(jNode);
                }
            }
        }
        // get optimized plan for main table
        TableRef table = join.getMainTable();
        SelectStatement stmt = getSubqueryForOptimizedPlan(select, table, join.columnRefs, join.getPreFiltersCombined());
        QueryPlan plan = context.getConnection().getQueryServices().getOptimizer().optimize(stmt, statement);
        if (!plan.getTableRef().equals(table)) {
            TableNodeRewriter rewriter = new TableNodeRewriter(plan.getTableRef());
            from.get(0).accept(rewriter);
View Full Code Here

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

           }
           Long scn = connection.getSCN();
           PTable theTable = new PTableImpl(table.getName().getSchemaName(), table.getName().getTableName(), scn == null ? HConstants.LATEST_TIMESTAMP : scn, families);
           theTable = this.addDynamicColumns(table.getDynamicColumns(), theTable);
           alias = null;
           tableRefs = ImmutableList.of(new TableRef(alias, theTable, timeStamp, !table.getDynamicColumns().isEmpty()));
       }
View Full Code Here

            TableName tableNameNode = table.getName();
            String schemaName = tableNameNode.getSchemaName();
            String tableName = tableNameNode.getTableName();
            SQLException sqlE = null;
            long timeStamp = QueryConstants.UNSET_TIMESTAMP;
            TableRef tableRef = null;
            boolean retry = true;
            boolean didRetry = false;
            MetaDataMutationResult result = null;
            String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
            while (true) {
                try {
                    if (!updateCacheOnlyIfAutoCommit || connection.getAutoCommit()) {
                        retry = false; // No reason to retry after this
                        result = client.updateCache(schemaName, tableName);
                        timeStamp = result.getMutationTime();
                    }
                    PTable theTable;
                    try {
                        theTable = connection.getPMetaData().getTable(fullTableName);
                    } catch (TableNotFoundException e) {
                        if (allowMultiTenant && result != null && result.getTable() != null) {
                            theTable = result.getTable();
                        } else {
                            throw e;
                        }
                    }
                    // If dynamic columns have been specified add them to the table declaration
                    if (!table.getDynamicColumns().isEmpty()) {
                        theTable = this.addDynamicColumns(table.getDynamicColumns(), theTable);
                    }
                    tableRef = new TableRef(alias, theTable, timeStamp, !table.getDynamicColumns().isEmpty());
                    if (didRetry && logger.isDebugEnabled()) {
                        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());
                    }
                    break;
                } catch (TableNotFoundException e) {
                    sqlE = new TableNotFoundException(e,timeStamp);
                }
View Full Code Here

    }

    @Override
    public ColumnRef resolveColumn(String schemaName, String tableName,
        String colName) throws SQLException {
      TableRef tableRef = tableRefs.get(0);
      boolean resolveCF = false;
      if (schemaName != null || tableName != null) {
          String resolvedTableName = tableRef.getTable().getTableName().getString();
          String resolvedSchemaName = tableRef.getTable().getSchemaName().getString();
          if (schemaName != null && tableName != null) {
                    if ( ! ( schemaName.equals(resolvedSchemaName&&
                             tableName.equals(resolvedTableName) )) {
                        if (!(resolveCF = schemaName.equals(alias))) {
                            throw new ColumnNotFoundException(schemaName, tableName, null, colName);
                        }
                    }
          } else { // schemaName == null && tableName != null
                    if (tableName != null && !tableName.equals(alias) && (!tableName.equals(resolvedTableName) || !resolvedSchemaName.equals(""))) {
                        resolveCF = true;
                   }
          }
         
      }
          PColumn column = resolveCF
                  ? tableRef.getTable().getColumnFamily(tableName).getColumn(colName)
              : tableRef.getTable().getColumn(colName);
            return new ColumnRef(tableRef, column.getPosition());
    }
View Full Code Here

            // If dynamic columns have been specified add them to the table declaration
            if (!dynamicColumnDefs.isEmpty()) {
                theTable = this.addDynamicColumns(dynamicColumnDefs, theTable);
            }
            TableRef tableRef = new TableRef(alias, theTable, timeStamp, !dynamicColumnDefs.isEmpty());
            return tableRef;
        }
View Full Code Here

            String schemaName = namedTableNode.getName().getSchemaName();

            String alias = namedTableNode.getAlias();
            List<ColumnDef> dynamicColumnDefs = namedTableNode.getDynamicColumns();

            TableRef tableRef = createTableRef(alias, schemaName, tableName, dynamicColumnDefs);
            PTable theTable = tableRef.getTable();

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

TOP

Related Classes of com.salesforce.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.