Java Code Examples for org.apache.phoenix.schema.PTable#getBucketNum()

The following examples show how to use org.apache.phoenix.schema.PTable#getBucketNum() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static List<PColumn> getPkColumns(PTable ptable, Connection conn) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    
    if (ptable.getType() == PTableType.INDEX) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
 
Example 2
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Deprecated
private static List<PColumn> getPkColumns(PTable ptable, Connection conn, boolean forDataTable) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    
    if (ptable.getType() == PTableType.INDEX && forDataTable) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
 
Example 3
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Get expression that may be used to evaluate the tenant ID of a given row in a
 * multi-tenant table. Both the SYSTEM.CATALOG table and the SYSTEM.SEQUENCE
 * table are considered multi-tenant.
 * @param conn open Phoenix connection
 * @param fullTableName full table name
 * @return An expression that may be evaluated for a row in the provided table or
 * null if the table is not a multi-tenant table. 
 * @throws SQLException if the table name is not found, a TableNotFoundException
 * is thrown. If a multi-tenant local index is supplied a SQLFeatureNotSupportedException
 * is thrown.
 */
public static Expression getTenantIdExpression(Connection conn, String fullTableName) throws SQLException {
    PTable table = getTable(conn, fullTableName);
    // TODO: consider setting MULTI_TENANT = true for SYSTEM.CATALOG and SYSTEM.SEQUENCE
    if (!SchemaUtil.isMetaTable(table) && !SchemaUtil.isSequenceTable(table) && !table.isMultiTenant()) {
        return null;
    }
    if (table.getIndexType() == IndexType.LOCAL) {
        /*
         * With some hackery, we could deduce the tenant ID from a multi-tenant local index,
         * however it's not clear that we'd want to maintain the same prefixing of the region
         * start key, as the region boundaries may end up being different on a cluster being
         * replicated/backed-up to (which is the use case driving the method).
         */
        throw new SQLFeatureNotSupportedException();
    }
    
    int pkPosition = table.getBucketNum() == null ? 0 : 1;
    List<PColumn> pkColumns = table.getPKColumns();
    return new RowKeyColumnExpression(pkColumns.get(pkPosition), new RowKeyValueAccessor(pkColumns, pkPosition));
}
 
Example 4
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static List<PColumn> getPkColumns(PTable ptable, Connection conn, boolean forDataTable) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    
    if (ptable.getType() == PTableType.INDEX && forDataTable) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
 
Example 5
Source File: IndexUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static byte[][] getViewConstants(PTable dataTable) {
    if (dataTable.getType() != PTableType.VIEW && dataTable.getType() != PTableType.PROJECTED) return null;
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    List<byte[]> viewConstants = new ArrayList<byte[]>();
    List<PColumn> dataPkColumns = dataTable.getPKColumns();
    for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
        PColumn dataPKColumn = dataPkColumns.get(i);
        if (dataPKColumn.getViewConstant() != null) {
            if (IndexUtil.getViewConstantValue(dataPKColumn, ptr)) {
                viewConstants.add(ByteUtil.copyKeyBytesIfNecessary(ptr));
            } else {
                throw new IllegalStateException();
            }
        }
    }
    return viewConstants.isEmpty() ? null : viewConstants
            .toArray(new byte[viewConstants.size()][]);
}
 
Example 6
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static Expression getFirstPKColumnExpression(PTable table) throws SQLException {
    if (table.getIndexType() == IndexType.LOCAL) {
        /*
         * With some hackery, we could deduce the tenant ID from a multi-tenant local index,
         * however it's not clear that we'd want to maintain the same prefixing of the region
         * start key, as the region boundaries may end up being different on a cluster being
         * replicated/backed-up to (which is the use case driving the method).
         */
        throw new SQLFeatureNotSupportedException();
    }
    
    // skip salt and viewIndexId columns.
    int pkPosition = (table.getBucketNum() == null ? 0 : 1) + (table.getViewIndexId() == null ? 0 : 1);
    List<PColumn> pkColumns = table.getPKColumns();
    return new RowKeyColumnExpression(pkColumns.get(pkPosition), new RowKeyValueAccessor(pkColumns, pkPosition));
}
 
Example 7
Source File: ExpressionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Called by visitor to resolve a column expression node into a column reference.
 * Derived classes may use this as a hook to trap all column resolves.
 * @param node a column expression node
 * @return a resolved ColumnRef
 * @throws SQLException if the column expression node does not refer to a known/unambiguous column
 */
protected ColumnRef resolveColumn(ColumnParseNode node) throws SQLException {
    ColumnRef ref = null;
    try {
        ref = context.getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
    } catch (ColumnNotFoundException e) {
        // Rather than not use a local index when a column not contained by it is referenced, we
        // join back to the data table in our coprocessor since this is a relatively cheap
        // operation given that we know the join is local.
        if (context.getCurrentTable().getTable().getIndexType() == IndexType.LOCAL) {
            try {
                return new LocalIndexDataColumnRef(context, context.getCurrentTable(), node.getName());
            } catch (ColumnFamilyNotFoundException c) {
                throw e;
            }
        } else {
            throw e;
        }
    }
    PTable table = ref.getTable();
    int pkPosition = ref.getPKSlotPosition();
    // Disallow explicit reference to salting column, tenant ID column, and index ID column
    if (pkPosition >= 0) {
        boolean isSalted = table.getBucketNum() != null;
        boolean isMultiTenant = context.getConnection().getTenantId() != null && table.isMultiTenant();
        boolean isSharedViewIndex = table.getViewIndexId() != null;
        int minPosition = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
        if (pkPosition < minPosition) {
            throw new ColumnNotFoundException(table.getSchemaName().getString(), table.getTableName().getString(), null, ref.getColumn().getName().getString());
        }
    }
    return ref;
}
 
Example 8
Source File: FromCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
protected PTable addDynamicColumns(List<ColumnDef> dynColumns, PTable theTable)
        throws SQLException {
    if (!dynColumns.isEmpty()) {
        List<PColumn> existingColumns = theTable.getColumns();
        // Need to skip the salting column, as it's handled in the PTable builder call below
        List<PColumn> allcolumns = new ArrayList<>(
                theTable.getBucketNum() == null ? existingColumns :
                        existingColumns.subList(1, existingColumns.size()));
        // Position still based on with the salting columns
        int position = existingColumns.size();
        PName defaultFamilyName = PNameFactory.newName(SchemaUtil.getEmptyColumnFamily(theTable));
        for (ColumnDef dynColumn : dynColumns) {
            PName familyName = defaultFamilyName;
            PName name = PNameFactory.newName(dynColumn.getColumnDefName().getColumnName());
            String family = dynColumn.getColumnDefName().getFamilyName();
            if (family != null) {
                theTable.getColumnFamily(family); // Verifies that column family exists
                familyName = PNameFactory.newName(family);
            }
            allcolumns.add(new PColumnImpl(name, familyName, dynColumn.getDataType(), dynColumn.getMaxLength(),
                    dynColumn.getScale(), dynColumn.isNull(), position, dynColumn.getSortOrder(), dynColumn.getArraySize(), null, false, dynColumn.getExpression(), false, true, Bytes.toBytes(dynColumn.getColumnDefName().getColumnName()),
                HConstants.LATEST_TIMESTAMP));
            position++;
        }
        theTable = PTableImpl.builderWithColumns(theTable, allcolumns)
                .build();
    }
    return theTable;
}
 
Example 9
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private byte[][] getViewConstants(PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    byte[][] viewConstants = null;
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
    return viewConstants;
}
 
Example 10
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Encode the primary key values from the table as a byte array. The values must
 * be in the same order as the primary key constraint. If the connection and
 * table are both tenant-specific, the tenant ID column must not be present in
 * the values.
 * @param conn an open connection
 * @param fullTableName the full table name
 * @param values the values of the primary key columns ordered in the same order
 *  as the primary key constraint
 * @return the encoded byte array
 * @throws SQLException if the table cannot be found or the incorrect number of
 *  of values are provided
 * @see #decodePK(Connection, String, byte[]) to decode the byte[] back to the
 *  values
 */
@Deprecated
public static byte[] encodePK(Connection conn, String fullTableName, Object[] values) throws SQLException {
    PTable table = getTable(conn, fullTableName);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    int offset = (table.getBucketNum() == null ? 0 : 1) + (table.isMultiTenant() && pconn.getTenantId() != null ? 1 : 0);
    List<PColumn> pkColumns = table.getPKColumns();
    if (pkColumns.size() - offset != values.length) {
        throw new SQLException("Expected " + (pkColumns.size() - offset) + " but got " + values.length);
    }
    PDataType type = null;
    TrustedByteArrayOutputStream output = new TrustedByteArrayOutputStream(table.getRowKeySchema().getEstimatedValueLength());
    try {
        for (int i = offset; i < pkColumns.size(); i++) {
            if (type != null && !type.isFixedWidth()) {
                output.write(QueryConstants.SEPARATOR_BYTE);
            }
            type = pkColumns.get(i).getDataType();

            //for fixed width data types like CHAR and BINARY, we need to pad values to be of max length.
            Object paddedObj = type.pad(values[i - offset], pkColumns.get(i).getMaxLength());
            byte[] value = type.toBytes(paddedObj);
            output.write(value);
        }
        return output.toByteArray();
    } finally {
        try {
            output.close();
        } catch (IOException e) {
            throw new RuntimeException(e); // Impossible
        }
    }
}
 
Example 11
Source File: IndexExpressionParseNodeRewriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public IndexExpressionParseNodeRewriter(PTable index, String alias, PhoenixConnection connection, Map<String, UDFParseNode> udfParseNodes) throws SQLException {
      indexedParseNodeToColumnParseNodeMap = Maps.newHashMapWithExpectedSize(index.getColumns().size());
      NamedTableNode tableNode = NamedTableNode.create(alias,
              TableName.create(index.getParentSchemaName().getString(), index.getParentTableName().getString()),
              Collections.<ColumnDef> emptyList());
      ColumnResolver dataResolver = FromCompiler.getResolver(tableNode, connection, udfParseNodes);
      StatementContext context = new StatementContext(new PhoenixStatement(connection), dataResolver);
      IndexStatementRewriter rewriter = new IndexStatementRewriter(dataResolver, null, true);
      ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
      int indexPosOffset = (index.getBucketNum() == null ? 0 : 1) + (index.isMultiTenant() ? 1 : 0) + (index.getViewIndexId() == null ? 0 : 1);
      List<PColumn> pkColumns = index.getPKColumns();
for (int i=indexPosOffset; i<pkColumns.size(); ++i) {
      	PColumn column = pkColumns.get(i);
      	String expressionStr = IndexUtil.getIndexColumnExpressionStr(column);
          ParseNode expressionParseNode  = SQLParser.parseCondition(expressionStr);
          String colName = "\"" + column.getName().getString() + "\"";
          Expression dataExpression = expressionParseNode.accept(expressionCompiler);
          PDataType expressionDataType = dataExpression.getDataType();
          ParseNode indexedParseNode = expressionParseNode.accept(rewriter);
          PDataType indexColType = IndexUtil.getIndexColumnDataType(dataExpression.isNullable(), expressionDataType);
          ParseNode columnParseNode = new ColumnParseNode(alias!=null ? TableName.create(null, alias) : null, colName, null);
          if ( indexColType != expressionDataType) {
              columnParseNode = NODE_FACTORY.cast(columnParseNode, expressionDataType, null, null);
          }
          indexedParseNodeToColumnParseNodeMap.put(indexedParseNode, columnParseNode);
      }
  }
 
Example 12
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static int getMinPKOffset(PTable table, PName tenantId) {
    // In SELECT *, don't include tenant column or index ID column for tenant connection
    int posOffset = table.getBucketNum() == null ? 0 : 1;
    if (table.isMultiTenant() && tenantId != null) {
        posOffset++;
    }
    if (table.getViewIndexId() != null) {
        posOffset++;
    }
    return posOffset;
}
 
Example 13
Source File: ExpressionUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * For table with salted/multiTenant/viewIndexId,some leading rowkey columns should be skipped.
 * @param table
 * @param phoenixConnection
 * @return
 */
public static int getRowKeyColumnOffset(PTable table, PhoenixConnection phoenixConnection) {
    boolean isSalted = table.getBucketNum() != null;
    boolean isMultiTenant = phoenixConnection.getTenantId() != null && table.isMultiTenant();
    boolean isSharedViewIndex = table.getViewIndexId() != null;
    return (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
}
 
Example 14
Source File: TrackOrderPreservingExpressionCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
TrackOrderPreservingExpressionCompiler(StatementContext context, GroupBy groupBy, int expectedEntrySize, Ordering ordering, TupleProjector tupleProjector) {
    super(context, groupBy);
    PTable table = context.getResolver().getTables().get(0).getTable();
    boolean isSalted = table.getBucketNum() != null;
    boolean isMultiTenant = context.getConnection().getTenantId() != null && table.isMultiTenant();
    boolean isSharedViewIndex = table.getViewIndexId() != null;
    // TODO: util for this offset, as it's computed in numerous places
    positionOffset = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
    entries = Lists.newArrayListWithExpectedSize(expectedEntrySize);
    this.ordering = ordering;
    this.tupleProjector = tupleProjector;
}
 
Example 15
Source File: ScanUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static final boolean canQueryBeExecutedSerially(PTable table, OrderBy orderBy, StatementContext context) {
    /*
     * If ordering by columns not on the PK axis, we can't execute a query serially because we
     * need to do a merge sort across all the scans which isn't possible with SerialIterators.
     * Similar reasoning follows for salted and local index tables when ordering rows in a row
     * key order. Serial execution is OK in other cases since SerialIterators will execute scans
     * in the correct order.
     */
    if (!orderBy.getOrderByExpressions().isEmpty()
            || ((table.getBucketNum() != null || table.getIndexType() == IndexType.LOCAL) && shouldRowsBeInRowKeyOrder(
                orderBy, context))) {
        return false;
    }
    return true;
}
 
Example 16
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static short getMaxKeySeq(PTable table) {
    int offset = 0;
    if (table.getBucketNum() != null) {
        offset++;
    }
    // TODO: for tenant-specific table on tenant-specific connection,
    // we should subtract one for tenant column and another one for
    // index ID
    return (short)(table.getPKColumns().size() - offset);
}
 
Example 17
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private byte[][] getViewConstants(PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    byte[][] viewConstants = null;
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
    return viewConstants;
}
 
Example 18
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private static void setValues(byte[][] values, int[] pkSlotIndex, int[] columnIndexes,
        PTable table, MultiRowMutationState mutation,
        PhoenixStatement statement, boolean useServerTimestamp, IndexMaintainer maintainer,
        byte[][] viewConstants, byte[] onDupKeyBytes, int numSplColumns) throws SQLException {
    long columnValueSize = 0;
    Map<PColumn,byte[]> columnValues = Maps.newHashMapWithExpectedSize(columnIndexes.length);
    byte[][] pkValues = new byte[table.getPKColumns().size()][];
    // If the table uses salting, the first byte is the salting byte, set to an empty array
    // here and we will fill in the byte later in PRowImpl.
    if (table.getBucketNum() != null) {
        pkValues[0] = new byte[] {0};
    }
    for(int i = 0; i < numSplColumns; i++) {
        pkValues[i + (table.getBucketNum() != null ? 1 : 0)] = values[i];
    }
    Long rowTimestamp = null; // case when the table doesn't have a row timestamp column
    RowTimestampColInfo rowTsColInfo = new RowTimestampColInfo(useServerTimestamp, rowTimestamp);
    for (int i = 0, j = numSplColumns; j < values.length; j++, i++) {
        byte[] value = values[j];
        PColumn column = table.getColumns().get(columnIndexes[i]);
        if (SchemaUtil.isPKColumn(column)) {
            pkValues[pkSlotIndex[i]] = value;
            if (SchemaUtil.getPKPosition(table, column) == table.getRowTimestampColPos()) {
                if (!useServerTimestamp) {
                    PColumn rowTimestampCol = table.getPKColumns().get(table.getRowTimestampColPos());
                    rowTimestamp = PLong.INSTANCE.getCodec().decodeLong(value, 0, rowTimestampCol.getSortOrder());
                    if (rowTimestamp < 0) {
                        throw new IllegalDataException("Value of a column designated as ROW_TIMESTAMP cannot be less than zero");
                    }
                    rowTsColInfo = new RowTimestampColInfo(useServerTimestamp, rowTimestamp);
                } 
            }
        } else {
            columnValues.put(column, value);
            columnValueSize += (column.getEstimatedSize() + value.length);
        }
    }
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    table.newKey(ptr, pkValues);
    if (table.getIndexType() == IndexType.LOCAL && maintainer != null) {
        byte[] rowKey = maintainer.buildDataRowKey(ptr, viewConstants);
        HRegionLocation region =
                statement.getConnection().getQueryServices()
                        .getTableRegionLocation(table.getParentName().getBytes(), rowKey);
        byte[] regionPrefix =
                region.getRegion().getStartKey().length == 0 ? new byte[region
                        .getRegion().getEndKey().length] : region.getRegion()
                        .getStartKey();
        if (regionPrefix.length != 0) {
            ptr.set(ScanRanges.prefixKey(ptr.get(), 0, ptr.getLength(), regionPrefix,
                regionPrefix.length));
        }
    } 
    mutation.put(ptr, new RowMutationState(columnValues, columnValueSize, statement.getConnection().getStatementExecutionCounter(), rowTsColInfo, onDupKeyBytes));
}
 
Example 19
Source File: PhoenixDatabaseMetaData.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public ResultSet getPrimaryKeys(String catalog, String schemaName, String tableName)
        throws SQLException {
    if (tableName == null || tableName.length() == 0) {
        return emptyResultSet;
    }
    String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    PTable table = PhoenixRuntime.getTableNoCache(connection, fullTableName);
    boolean isSalted = table.getBucketNum() != null;
    boolean tenantColSkipped = false;
    List<PColumn> pkColumns = table.getPKColumns();
    List<PColumn> sorderPkColumns =
            Lists.newArrayList(pkColumns.subList(isSalted ? 1 : 0, pkColumns.size()));
    // sort the columns by name
    Collections.sort(sorderPkColumns, new Comparator<PColumn>(){
        @Override public int compare(PColumn c1, PColumn c2) {
            return c1.getName().getString().compareTo(c2.getName().getString());
        }
    });

    try {
    List<Tuple> tuples = Lists.newArrayListWithExpectedSize(10);
    ResultSet rs = getTables(catalog, schemaName, tableName, null);
    while (rs.next()) {
        String tenantId = rs.getString(TABLE_CAT);
        for (PColumn column : sorderPkColumns) {
            String columnName = column.getName().getString();
            // generate row key
            // TENANT_ID, TABLE_SCHEM, TABLE_NAME , COLUMN_NAME are row key columns
            byte[] rowKey =
                    SchemaUtil.getColumnKey(tenantId, schemaName, tableName, columnName, null);

            // add one cell for each column info
            List<Cell> cells = Lists.newArrayListWithCapacity(8);
            // KEY_SEQ_COLUMN
            byte[] keySeqBytes = ByteUtil.EMPTY_BYTE_ARRAY;
            int pkPos = pkColumns.indexOf(column);
            if (pkPos != -1) {
                short keySeq =
                        (short) (pkPos + 1 - (isSalted ? 1 : 0) - (tenantColSkipped ? 1 : 0));
                keySeqBytes = PSmallint.INSTANCE.toBytes(keySeq);
            }
            cells.add(PhoenixKeyValueUtil.newKeyValue(rowKey, TABLE_FAMILY_BYTES, KEY_SEQ_BYTES,
                MetaDataProtocol.MIN_TABLE_TIMESTAMP, keySeqBytes));
            // PK_NAME
            cells.add(PhoenixKeyValueUtil.newKeyValue(rowKey, TABLE_FAMILY_BYTES, PK_NAME_BYTES,
                MetaDataProtocol.MIN_TABLE_TIMESTAMP, table.getPKName() != null
                        ? table.getPKName().getBytes() : ByteUtil.EMPTY_BYTE_ARRAY));
            // ASC_OR_DESC
            char sortOrder = column.getSortOrder() == SortOrder.ASC ? 'A' : 'D';
            cells.add(PhoenixKeyValueUtil.newKeyValue(rowKey, TABLE_FAMILY_BYTES,
                ASC_OR_DESC_BYTES, MetaDataProtocol.MIN_TABLE_TIMESTAMP,
                Bytes.toBytes(sortOrder)));
            // DATA_TYPE
            cells.add(PhoenixKeyValueUtil.newKeyValue(rowKey, TABLE_FAMILY_BYTES, DATA_TYPE_BYTES,
                MetaDataProtocol.MIN_TABLE_TIMESTAMP,
                PInteger.INSTANCE.toBytes(column.getDataType().getResultSetSqlType())));
            // TYPE_NAME
            cells.add(PhoenixKeyValueUtil.newKeyValue(rowKey, TABLE_FAMILY_BYTES,
                Bytes.toBytes(TYPE_NAME), MetaDataProtocol.MIN_TABLE_TIMESTAMP,
                column.getDataType().getSqlTypeNameBytes()));
            // COLUMN_SIZE
            cells.add(
                PhoenixKeyValueUtil.newKeyValue(rowKey, TABLE_FAMILY_BYTES, COLUMN_SIZE_BYTES,
                    MetaDataProtocol.MIN_TABLE_TIMESTAMP,
                    column.getMaxLength() != null
                            ? PInteger.INSTANCE.toBytes(column.getMaxLength())
                            : ByteUtil.EMPTY_BYTE_ARRAY));
            // TYPE_ID
            cells.add(PhoenixKeyValueUtil.newKeyValue(rowKey, TABLE_FAMILY_BYTES,
                Bytes.toBytes(TYPE_ID), MetaDataProtocol.MIN_TABLE_TIMESTAMP,
                PInteger.INSTANCE.toBytes(column.getDataType().getSqlType())));
            // VIEW_CONSTANT
            cells.add(PhoenixKeyValueUtil.newKeyValue(rowKey, TABLE_FAMILY_BYTES, VIEW_CONSTANT_BYTES,
                MetaDataProtocol.MIN_TABLE_TIMESTAMP, column.getViewConstant() != null
                        ? column.getViewConstant() : ByteUtil.EMPTY_BYTE_ARRAY));
            Collections.sort(cells, new CellComparatorImpl());
            Tuple tuple = new MultiKeyValueTuple(cells);
            tuples.add(tuple);
        }
    }
    return new PhoenixResultSet(new MaterializedResultIterator(tuples),
            GET_PRIMARY_KEYS_ROW_PROJECTOR,
            new StatementContext(new PhoenixStatement(connection), false));
    } finally {
        if (connection.getAutoCommit()) {
            connection.commit();
        }
    }
}
 
Example 20
Source File: PhoenixMetadata.java    From presto with Apache License 2.0 4 votes vote down vote up
private Map<String, Object> getTableProperties(ConnectorSession session, JdbcTableHandle handle)
{
    ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();

    try (PhoenixConnection connection = phoenixClient.getConnection(JdbcIdentity.from(session));
            HBaseAdmin admin = connection.getQueryServices().getAdmin()) {
        String schemaName = toPhoenixSchemaName(Optional.ofNullable(handle.getSchemaName())).orElse(null);
        PTable table = getTable(connection, SchemaUtil.getTableName(schemaName, handle.getTableName()));

        boolean salted = table.getBucketNum() != null;
        StringJoiner joiner = new StringJoiner(",");
        List<PColumn> pkColumns = table.getPKColumns();
        for (PColumn pkColumn : pkColumns.subList(salted ? 1 : 0, pkColumns.size())) {
            joiner.add(pkColumn.getName().getString());
        }
        properties.put(PhoenixTableProperties.ROWKEYS, joiner.toString());

        if (table.getBucketNum() != null) {
            properties.put(PhoenixTableProperties.SALT_BUCKETS, table.getBucketNum());
        }
        if (table.isWALDisabled()) {
            properties.put(PhoenixTableProperties.DISABLE_WAL, table.isWALDisabled());
        }
        if (table.isImmutableRows()) {
            properties.put(PhoenixTableProperties.IMMUTABLE_ROWS, table.isImmutableRows());
        }

        String defaultFamilyName = QueryConstants.DEFAULT_COLUMN_FAMILY;
        if (table.getDefaultFamilyName() != null) {
            defaultFamilyName = table.getDefaultFamilyName().getString();
            properties.put(PhoenixTableProperties.DEFAULT_COLUMN_FAMILY, defaultFamilyName);
        }

        HTableDescriptor tableDesc = admin.getTableDescriptor(table.getPhysicalName().getBytes());

        HColumnDescriptor[] columnFamilies = tableDesc.getColumnFamilies();
        for (HColumnDescriptor columnFamily : columnFamilies) {
            if (columnFamily.getNameAsString().equals(defaultFamilyName)) {
                if (!"NONE".equals(columnFamily.getBloomFilterType().toString())) {
                    properties.put(PhoenixTableProperties.BLOOMFILTER, columnFamily.getBloomFilterType().toString());
                }
                if (columnFamily.getMaxVersions() != 1) {
                    properties.put(PhoenixTableProperties.VERSIONS, columnFamily.getMaxVersions());
                }
                if (columnFamily.getMinVersions() > 0) {
                    properties.put(PhoenixTableProperties.MIN_VERSIONS, columnFamily.getMinVersions());
                }
                if (!columnFamily.getCompression().toString().equals("NONE")) {
                    properties.put(PhoenixTableProperties.COMPRESSION, columnFamily.getCompression().toString());
                }
                if (columnFamily.getTimeToLive() < FOREVER) {
                    properties.put(PhoenixTableProperties.TTL, columnFamily.getTimeToLive());
                }
                break;
            }
        }
    }
    catch (IOException | SQLException e) {
        throw new PrestoException(PHOENIX_METADATA_ERROR, "Couldn't get Phoenix table properties", e);
    }
    return properties.build();
}