org.apache.phoenix.schema.PTable Java Examples

The following examples show how to use org.apache.phoenix.schema.PTable. 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: TephraTransactionContext.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public Table getTransactionalTableWriter(PhoenixConnection connection, PTable table, Table htable, boolean isIndex) throws SQLException {
    // If we have indexes, wrap the HTable in a delegate HTable that
    // will attach the necessary index meta data in the event of a
    // rollback
    TransactionAwareHTable transactionAwareHTable;
    // Don't add immutable indexes (those are the only ones that would participate
    // during a commit), as we don't need conflict detection for these.
    if (isIndex) {
        transactionAwareHTable = new TransactionAwareHTable(htable, TxConstants.ConflictDetection.NONE);
        transactionAwareHTable.startTx(getTransaction());
    } else {
        htable = new RollbackHookHTableWrapper(htable, table, connection);
        transactionAwareHTable = new TransactionAwareHTable(htable, table.isImmutableRows() ? TxConstants.ConflictDetection.NONE : TxConstants.ConflictDetection.ROW);
        // Even for immutable, we need to do this so that an abort has the state
        // necessary to generate the rows to delete.
        this.addTransactionAware(transactionAwareHTable);
    }
    return transactionAwareHTable;
}
 
Example #2
Source File: CsvBulkLoadTool.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Get the index tables of current data table
 * @throws java.sql.SQLException
 */
private List<TargetTableRef> getIndexTables(Connection conn, String schemaName, String qualifiedTableName)
    throws SQLException {
    PTable table = PhoenixRuntime.getTable(conn, qualifiedTableName);
    List<TargetTableRef> indexTables = new ArrayList<TargetTableRef>();
    for(PTable indexTable : table.getIndexes()){
        if (indexTable.getIndexType() == IndexType.LOCAL) {
            indexTables.add(
                    new TargetTableRef(getQualifiedTableName(schemaName,
                            indexTable.getTableName().getString()),
                            MetaDataUtil.getLocalIndexTableName(qualifiedTableName)));
        } else {
            indexTables.add(new TargetTableRef(getQualifiedTableName(schemaName,
                    indexTable.getTableName().getString())));
        }
    }
    return indexTables;
}
 
Example #3
Source File: MetaDataEndpointImpl.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private PTable buildDeletedTable(byte[] key, ImmutableBytesPtr cacheKey, HRegion region,
    long clientTimeStamp) throws IOException {
    if (clientTimeStamp == HConstants.LATEST_TIMESTAMP) {
        return null;
    }

    Scan scan = MetaDataUtil.newTableRowsScan(key, clientTimeStamp, HConstants.LATEST_TIMESTAMP);
    scan.setFilter(new FirstKeyOnlyFilter());
    scan.setRaw(true);
    List<Cell> results = Lists.<Cell> newArrayList();
    try (RegionScanner scanner = region.getScanner(scan);) {
      scanner.next(results);
    }
    // HBase ignores the time range on a raw scan (HBASE-7362)
    if (!results.isEmpty() && results.get(0).getTimestamp() > clientTimeStamp) {
        Cell kv = results.get(0);
        if (kv.getTypeByte() == Type.Delete.getCode()) {
            Cache<ImmutableBytesPtr, PTable> metaDataCache =
                    GlobalCache.getInstance(this.env).getMetaDataCache();
            PTable table = newDeletedTableMarker(kv.getTimestamp());
            metaDataCache.put(cacheKey, table);
            return table;
        }
    }
    return null;
}
 
Example #4
Source File: IndexScrutinyMapper.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static String getSourceTableName(PTable pSourceTable, boolean isNamespaceEnabled) {
    String sourcePhysicalName = pSourceTable.getPhysicalName().getString();
    String physicalTable, table, schema;
    if (pSourceTable.getType() == PTableType.VIEW
            || MetaDataUtil.isViewIndex(sourcePhysicalName)) {
        // in case of view and view index ptable, getPhysicalName() returns hbase tables
        // i.e. without _IDX_ and with _IDX_ respectively
        physicalTable = sourcePhysicalName;
    } else {
        table = pSourceTable.getTableName().toString();
        schema = pSourceTable.getSchemaName().toString();
        physicalTable = SchemaUtil
                .getPhysicalHBaseTableName(schema, table, isNamespaceEnabled).toString();
    }
    return physicalTable;
}
 
Example #5
Source File: FromCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public Void visit(NamedTableNode tableNode) throws SQLException {
    String alias = tableNode.getAlias();
    TableRef tableRef = createTableRef(connectionSchemaName, tableNode, true, false);
    PTable theTable = tableRef.getTable();

    if (alias != null) {
        tableMap.put(alias, tableRef);
    }

    String name = theTable.getName().getString();
    //avoid having one name mapped to two identical TableRef.
    if (alias == null || !alias.equals(name)) {
    	tableMap.put(name, tableRef);
    }
    tables.add(tableRef);
    return null;
}
 
Example #6
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the column info for the given column for the given table.
 *
 * @param table
 * @param columnName User-specified column name. May be family-qualified or bare.
 * @return columnInfo associated with the column in the table
 * @throws SQLException if parameters are null or if column is not found or if column is ambiguous.
 */
public static ColumnInfo getColumnInfo(PTable table, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    PColumn pColumn = null;
    if (columnName.contains(QueryConstants.NAME_SEPARATOR)) {
        String[] tokens = columnName.split(QueryConstants.NAME_SEPARATOR_REGEX);
        if (tokens.length!=2) {
            throw new SQLException(String.format("Unable to process column %s, expected family-qualified name.",columnName));
        }
        String familyName = tokens[0];
        String familyColumn = tokens[1];
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(familyColumn);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return getColumnInfo(pColumn);
}
 
Example #7
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the column info for the given column for the given table.
 *
 * @param table
 * @param columnName User-specified column name. May be family-qualified or bare.
 * @return columnInfo associated with the column in the table
 * @throws SQLException if parameters are null or if column is not found or if column is ambiguous.
 */
public static ColumnInfo getColumnInfo(PTable table, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    PColumn pColumn = null;
    if (columnName.contains(QueryConstants.NAME_SEPARATOR)) {
        String[] tokens = columnName.split(QueryConstants.NAME_SEPARATOR_REGEX);
        if (tokens.length!=2) {
            throw new SQLException(String.format("Unable to process column %s, expected family-qualified name.",columnName));
        }
        String familyName = tokens[0];
        String familyColumn = tokens[1];
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getColumn(familyColumn);
    } else {
        pColumn = table.getColumn(columnName);
    }
    return getColumnInfo(pColumn);
}
 
Example #8
Source File: PartialIndexRebuilderIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testRegionsOnlineCheck() throws Throwable {
    String schemaName = generateUniqueName();
    String tableName = generateUniqueName();
    final String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    PTableKey key = new PTableKey(null,fullTableName);
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PMetaData metaCache = conn.unwrap(PhoenixConnection.class).getMetaDataCache();
        conn.createStatement().execute("CREATE TABLE " + fullTableName + "(k VARCHAR PRIMARY KEY)");
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('a')");
        conn.commit();
        Configuration conf = conn.unwrap(PhoenixConnection.class).getQueryServices().getConfiguration();
        PTable table = metaCache.getTableRef(key).getTable();
        assertTrue(MetaDataUtil.tableRegionsOnline(conf, table));
        try (Admin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin()) {
            admin.disableTable(TableName.valueOf(fullTableName));
            assertFalse(MetaDataUtil.tableRegionsOnline(conf, table));
            admin.enableTable(TableName.valueOf(fullTableName));
        }
        assertTrue(MetaDataUtil.tableRegionsOnline(conf, table));
    }
}
 
Example #9
Source File: ImmutableTablePropertiesIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testImmutableProperty() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String immutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
    String mutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
    try (Connection conn = DriverManager.getConnection(getUrl(), props);) {
        Statement stmt = conn.createStatement();
        // create table with immutable table property set to true
        String ddl = "CREATE TABLE  " + immutableDataTableFullName +
                "  (a_string varchar not null, col1 integer" +
                "  CONSTRAINT pk PRIMARY KEY (a_string)) IMMUTABLE_ROWS=true";
        stmt.execute(ddl);
        
        // create table with immutable table property set to false
        ddl = "CREATE TABLE  " + mutableDataTableFullName +
                "  (a_string varchar not null, col1 integer" +
                "  CONSTRAINT pk PRIMARY KEY (a_string))  IMMUTABLE_ROWS=false";
        stmt.execute(ddl);
        
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        PTable immutableTable = phxConn.getTable(new PTableKey(null, immutableDataTableFullName));
        assertTrue("IMMUTABLE_ROWS should be set to true", immutableTable.isImmutableRows());
        PTable mutableTable = phxConn.getTable(new PTableKey(null, mutableDataTableFullName));
        assertFalse("IMMUTABLE_ROWS should be set to false", mutableTable.isImmutableRows());
    } 
}
 
Example #10
Source File: TestUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static Collection<GuidePostsInfo> getGuidePostsList(Connection conn, String tableName, String pkCol,
        byte[] lowerRange, byte[] upperRange, String whereClauseSuffix) throws SQLException {
    String whereClauseStart = (lowerRange == null && upperRange == null ? ""
            : " WHERE "
                    + ((lowerRange != null ? (pkCol + " >= ? " + (upperRange != null ? " AND " : "")) : "") + (upperRange != null ? (pkCol + " < ?")
                            : "")));
    String whereClause = whereClauseSuffix == null ? whereClauseStart
            : whereClauseStart.length() == 0 ? (" WHERE " + whereClauseSuffix) : (" AND " + whereClauseSuffix);
    String query = "SELECT /*+ NO_INDEX */ COUNT(*) FROM " + tableName + whereClause;
    PhoenixPreparedStatement pstmt = conn.prepareStatement(query).unwrap(PhoenixPreparedStatement.class);
    if (lowerRange != null) {
        pstmt.setBytes(1, lowerRange);
    }
    if (upperRange != null) {
        pstmt.setBytes(lowerRange != null ? 2 : 1, upperRange);
    }
    pstmt.execute();
    TableRef tableRef = pstmt.getQueryPlan().getTableRef();
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    PTable table = tableRef.getTable();
    GuidePostsInfo info = pconn.getQueryServices().getTableStats(new GuidePostsKey(table.getName().getBytes(), SchemaUtil.getEmptyColumnFamily(table)));
    return Collections.singletonList(info);
}
 
Example #11
Source File: ParallelIteratorsSplitTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSplitsWithSkipScanFilter() throws Exception {
    byte[][] splits = new byte[][] {Ka1A, Ka1B, Ka1E, Ka1G, Ka1I, Ka2A};
    createTestTable(getUrl(),DDL,splits, null);
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    
    PTable table = pconn.getMetaDataCache().getTable(new PTableKey(pconn.getTenantId(), TABLE_NAME));
    TableRef tableRef = new TableRef(table);
    List<HRegionLocation> regions = pconn.getQueryServices().getAllTableRegions(tableRef.getTable().getPhysicalName().getBytes());
    List<KeyRange> ranges = getSplits(tableRef, scan, regions, scanRanges);
    assertEquals("Unexpected number of splits: " + ranges.size(), expectedSplits.size(), ranges.size());
    for (int i=0; i<expectedSplits.size(); i++) {
        assertEquals(expectedSplits.get(i), ranges.get(i));
    }
}
 
Example #12
Source File: FromCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static ColumnResolver getResolverForCompiledDerivedTable(PhoenixConnection connection, TableRef tableRef, RowProjector projector)
        throws SQLException {
    List<PColumn> projectedColumns = new ArrayList<PColumn>();
    PTable table = tableRef.getTable();
    for (PColumn column : table.getColumns()) {
        Expression sourceExpression = projector.getColumnProjector(column.getPosition()).getExpression();
        PColumnImpl projectedColumn = new PColumnImpl(column.getName(), column.getFamilyName(),
                sourceExpression.getDataType(), sourceExpression.getMaxLength(), sourceExpression.getScale(), sourceExpression.isNullable(),
                column.getPosition(), sourceExpression.getSortOrder(), column.getArraySize(), column.getViewConstant(), column.isViewReferenced(), column.getExpressionStr(), column.isRowTimestamp(), column.isDynamic(), column.getColumnQualifierBytes(),
            column.getTimestamp());
        projectedColumns.add(projectedColumn);
    }
    PTable t = PTableImpl.builderWithColumns(table, projectedColumns)
            .build();
    return new SingleTableColumnResolver(connection, new TableRef(tableRef.getTableAlias(), t, tableRef.getLowerBoundTimeStamp(), tableRef.hasDynamicCols()));
}
 
Example #13
Source File: ExpressionUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Infer OrderBys from the rowkey columns of {@link PTable}.
 * The second part of the return pair is the rowkey column offset we must skip when we create OrderBys, because for table with salted/multiTenant/viewIndexId,
 * some leading rowkey columns should be skipped.
 * @param table
 * @param phoenixConnection
 * @param orderByReverse
 * @return
 */
public static Pair<OrderBy,Integer> getOrderByFromTableByRowKeyColumn(
        PTable table,
        PhoenixConnection phoenixConnection,
        boolean orderByReverse) {
    Pair<List<RowKeyColumnExpression>,Integer> rowKeyColumnExpressionsAndRowKeyColumnOffset =
            ExpressionUtil.getRowKeyColumnExpressionsFromTable(table, phoenixConnection);
    List<RowKeyColumnExpression> rowKeyColumnExpressions = rowKeyColumnExpressionsAndRowKeyColumnOffset.getFirst();
    int rowKeyColumnOffset = rowKeyColumnExpressionsAndRowKeyColumnOffset.getSecond();
    if(rowKeyColumnExpressions.isEmpty()) {
        return new Pair<OrderBy,Integer>(OrderBy.EMPTY_ORDER_BY,0);
    }
    return new Pair<OrderBy,Integer>(
            convertRowKeyColumnExpressionsToOrderBy(rowKeyColumnExpressions, orderByReverse),
            rowKeyColumnOffset);
}
 
Example #14
Source File: Task.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static void deleteTask(PhoenixConnection conn, PTable.TaskType taskType, Timestamp ts, String tenantId,
        String schemaName, String tableName, boolean accessCheckEnabled) throws IOException {
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("DELETE FROM " +
                PhoenixDatabaseMetaData.SYSTEM_TASK_NAME +
                " WHERE " + PhoenixDatabaseMetaData.TASK_TYPE + " = ? AND " +
                PhoenixDatabaseMetaData.TASK_TS + " = ? AND " +
                PhoenixDatabaseMetaData.TENANT_ID + (tenantId == null ? " IS NULL " : " = '" + tenantId + "'") + " AND " +
                PhoenixDatabaseMetaData.TABLE_SCHEM + (schemaName == null ? " IS NULL " : " = '" + schemaName + "'") + " AND " +
                PhoenixDatabaseMetaData.TABLE_NAME + " = ?");
        stmt.setByte(1, taskType.getSerializedValue());
        stmt.setTimestamp(2, ts);
        stmt.setString(3, tableName);
    } catch (SQLException e) {
        throw new IOException(e);
    }
    mutateSystemTaskTable(conn, stmt, accessCheckEnabled);
}
 
Example #15
Source File: ConnectionlessQueryServicesImpl.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public MetaDataMutationResult updateIndexState(List<Mutation> tableMetadata, String parentTableName) throws SQLException {
    byte[][] rowKeyMetadata = new byte[3][];
    SchemaUtil.getVarChars(tableMetadata.get(0).getRow(), rowKeyMetadata);
    Mutation m = MetaDataUtil.getTableHeaderRow(tableMetadata);
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    if (!MetaDataUtil.getMutationValue(m, INDEX_STATE_BYTES, kvBuilder, ptr)) {
        throw new IllegalStateException();
    }
    PIndexState newState =  PIndexState.fromSerializedValue(ptr.get()[ptr.getOffset()]);
    byte[] tenantIdBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
    String schemaName = Bytes.toString(rowKeyMetadata[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX]);
    String indexName = Bytes.toString(rowKeyMetadata[PhoenixDatabaseMetaData.TABLE_NAME_INDEX]);
    String indexTableName = SchemaUtil.getTableName(schemaName, indexName);
    PName tenantId = tenantIdBytes.length == 0 ? null : PNameFactory.newName(tenantIdBytes);
    PTable index = metaData.getTableRef(new PTableKey(tenantId, indexTableName)).getTable();
    index = PTableImpl.builderWithColumns(index, getColumnsToClone(index))
            .setState(newState == PIndexState.USABLE ? PIndexState.ACTIVE :
                    newState == PIndexState.UNUSABLE ? PIndexState.INACTIVE : newState)
            .build();
    return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS, 0, index);
}
 
Example #16
Source File: WhereCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
protected ColumnRef resolveColumn(ColumnParseNode node) throws SQLException {
    ColumnRef ref = super.resolveColumn(node);
    PTable table = ref.getTable();
    // if current table in the context is local index and table in column reference is global means
    // the column is not present in the local index. If where condition contains the column 
    // not present in the index then we need to go through main table for each row in index and get the
    // missing column which is like full scan of index table and data table. Which is
    // inefficient. Then we can skip this plan.
    if (context.getCurrentTable().getTable().getIndexType() == IndexType.LOCAL
            && (table.getIndexType() == null || table.getIndexType() == IndexType.GLOBAL)) {
        throw new ColumnNotFoundException(ref.getColumn().getName().getString());
    }
    // Track if we need to compare KeyValue during filter evaluation
    // using column family. If the column qualifier is enough, we
    // just use that.
    try {
        if (!SchemaUtil.isPKColumn(ref.getColumn())) {
            table.getColumn(ref.getColumn().getName().getString());
        }
    } catch (AmbiguousColumnException e) {
        disambiguateWithFamily = true;
    }
    return ref;
 }
 
Example #17
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static PColumn getColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    // normalize and remove quotes from family and column names before looking up.
    familyName = SchemaUtil.normalizeIdentifier(familyName);
    columnName = SchemaUtil.normalizeIdentifier(columnName);
    // Column names are always for the data table, so we must translate them if
    // we're dealing with an index table.
    if (table.getType() == PTableType.INDEX) {
        columnName = IndexUtil.getIndexColumnName(familyName, columnName);
    }
    PColumn pColumn = null;
    if (familyName != null) {
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(columnName);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return pColumn;
}
 
Example #18
Source File: AbstractBulkLoadTool.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Get the index tables of current data table
 * @throws java.sql.SQLException
 */
private List<TargetTableRef> getIndexTables(Connection conn, String qualifiedTableName)
        throws SQLException {
    PTable table = PhoenixRuntime.getTable(conn, qualifiedTableName);
    List<TargetTableRef> indexTables = new ArrayList<TargetTableRef>();
    for(PTable indexTable : table.getIndexes()){
        indexTables.add(new TargetTableRef(indexTable.getName().getString(), indexTable
                .getPhysicalName().getString()));
    }
    return indexTables;
}
 
Example #19
Source File: AlterTableIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisableWAL1() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);

    try {
        conn.createStatement()
                .execute(
                        "CREATE TABLE " + dataTableFullName
                                + "  (a_string varchar not null, col1 integer, cf1.col2 integer, col3 integer , cf2.col4 integer "
                                + "  CONSTRAINT pk PRIMARY KEY (a_string)) "
                                + generateDDLOptions("immutable_rows=true, disable_wal=true"
                                + (!columnEncoded ? ",IMMUTABLE_STORAGE_SCHEME=" + PTable.ImmutableStorageScheme.ONE_CELL_PER_COLUMN : "")));

        Connection conn2 = DriverManager.getConnection(getUrl(), props);
        String query = "SELECT * FROM " + dataTableFullName;
        ResultSet rs = conn2.createStatement().executeQuery(query);
        assertFalse(rs.next());
        asssertIsWALDisabled(conn2,dataTableFullName, true);
        conn2.close();
        asssertIsWALDisabled(conn,dataTableFullName, true);

        conn.createStatement().execute("CREATE INDEX " + indexTableName + " ON " + dataTableFullName + " (col1) include (cf1.col2) SALT_BUCKETS=4");
        conn2 = DriverManager.getConnection(getUrl(), props);
        query = "SELECT * FROM " + indexTableFullName;
        rs = conn2.createStatement().executeQuery(query);
        asssertIsWALDisabled(conn2,indexTableFullName, false);
        assertFalse(rs.next());
        conn2.close();
        asssertIsWALDisabled(conn,indexTableFullName, false);
    } finally {
        conn.close();
    }
}
 
Example #20
Source File: IndexExpressionParseNodeRewriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public IndexExpressionParseNodeRewriter(PTable index, PhoenixConnection connection) throws SQLException {
      indexedParseNodeToColumnParseNodeMap = Maps.newHashMapWithExpectedSize(index.getColumns().size());
      NamedTableNode tableNode = NamedTableNode.create(null,
              TableName.create(index.getParentSchemaName().getString(), index.getParentTableName().getString()),
              Collections.<ColumnDef> emptyList());
      ColumnResolver dataResolver = FromCompiler.getResolver(tableNode, connection);
      StatementContext context = new StatementContext(new PhoenixStatement(connection), dataResolver);
      IndexStatementRewriter rewriter = new IndexStatementRewriter(dataResolver, null);
      ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
      ColumnParseNodeVisitor columnParseNodeVisitor = new ColumnParseNodeVisitor();
      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);
          columnParseNodeVisitor.reset();
          expressionParseNode.accept(columnParseNodeVisitor);
          String colName = column.getName().getString();
          if (columnParseNodeVisitor.isParseNodeCaseSensitive()) {
              // force column name to be case sensitive name by surround with double quotes
              colName = "\"" + colName + "\"";
          }
          
          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(null, colName, null);
          if ( indexColType != expressionDataType) {
              columnParseNode = NODE_FACTORY.cast(columnParseNode, expressionDataType, null, null);
          }
          indexedParseNodeToColumnParseNodeMap.put(indexedParseNode, columnParseNode);
      }
  }
 
Example #21
Source File: SystemCatalogWALEntryFilterIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public Get getGet(PTable catalogTable, byte[] tenantId, String viewName) {
  byte[][] tenantKeyParts = new byte[5][];
  tenantKeyParts[0] = tenantId;
  tenantKeyParts[1] = Bytes.toBytes(SCHEMA_NAME.toUpperCase());
  tenantKeyParts[2] = Bytes.toBytes(viewName.toUpperCase());
  tenantKeyParts[3] = Bytes.toBytes(VIEW_COLUMN_NAME);
  tenantKeyParts[4] = VIEW_COLUMN_FAMILY_BYTES;
  ImmutableBytesWritable key = new ImmutableBytesWritable();
  catalogTable.newKey(key, tenantKeyParts);
  //the backing byte array of key might have extra space at the end.
  // need to just slice "the good parts" which we do by calling copyBytes
  return new Get(key.copyBytes());
}
 
Example #22
Source File: OmidTransactionContext.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public Table getTransactionalTableWriter(PhoenixConnection connection, PTable table, Table htable, boolean isIndex) throws SQLException {
    // When we're getting a table for writing, if the table being written to is an index,
    // write the shadow cells immediately since the only time we write to an index is
    // when we initially populate it synchronously.
    return new OmidTransactionTable(this, htable, table.isImmutableRows() || isIndex, isIndex);
}
 
Example #23
Source File: GlobalCache.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public Cache<ImmutableBytesPtr,PTable> getMetaDataCache() {
    // Lazy initialize QueryServices so that we only attempt to create an HBase Configuration
    // object upon the first attempt to connect to any cluster. Otherwise, an attempt will be
    // made at driver initialization time which is too early for some systems.
    Cache<ImmutableBytesPtr,PTable> result = metaDataCache;
    if (result == null) {
        synchronized(this) {
            result = metaDataCache;
            if(result == null) {
                long maxTTL = Math.min(config.getLong(
                        QueryServices.MAX_SERVER_METADATA_CACHE_TIME_TO_LIVE_MS_ATTRIB,
                        QueryServicesOptions.DEFAULT_MAX_SERVER_METADATA_CACHE_TIME_TO_LIVE_MS), config.getLong(
                        QueryServices.STATS_UPDATE_FREQ_MS_ATTRIB,
                        QueryServicesOptions.DEFAULT_STATS_UPDATE_FREQ_MS));
                long maxSize = config.getLong(QueryServices.MAX_SERVER_METADATA_CACHE_SIZE_ATTRIB,
                        QueryServicesOptions.DEFAULT_MAX_SERVER_METADATA_CACHE_SIZE);
                metaDataCache = result = CacheBuilder.newBuilder()
                        .maximumWeight(maxSize)
                        .expireAfterAccess(maxTTL, TimeUnit.MILLISECONDS)
                        .weigher(new Weigher<ImmutableBytesPtr, PTable>() {
                            @Override
                            public int weigh(ImmutableBytesPtr key, PTable table) {
                                return SizedUtil.IMMUTABLE_BYTES_PTR_SIZE + key.getLength() + table.getEstimatedSize();
                            }
                        })
                        .build();
            }
        }
    }
    return result;
}
 
Example #24
Source File: TupleProjector.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public TupleProjector(PTable projectedTable) throws SQLException {
    Preconditions.checkArgument(projectedTable.getType() == PTableType.PROJECTED);
	List<PColumn> columns = projectedTable.getColumns();
	this.expressions = new Expression[columns.size() - projectedTable.getPKColumns().size()];
	KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
	int i = 0;
    for (PColumn column : columns) {
    	if (!SchemaUtil.isPKColumn(column)) {
    		builder.addField(column);
    		expressions[i++] = ((ProjectedColumn) column).getSourceColumnRef().newColumnExpression();
    	}
    }
    schema = builder.build();
    valueSet = ValueBitSet.newInstance(schema);
}
 
Example #25
Source File: AlterMultiTenantTableWithViewsIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private boolean checkColumnPartOfPk(PhoenixConnection conn, String columnName, String tableName) throws SQLException {
    String normalizedTableName = SchemaUtil.normalizeIdentifier(tableName);
    PTable table = conn.getTable(new PTableKey(conn.getTenantId(), normalizedTableName));
    List<PColumn> pkCols = table.getPKColumns();
    String normalizedColumnName = SchemaUtil.normalizeIdentifier(columnName);
    for (PColumn pkCol : pkCols) {
        if (pkCol.getName().getString().equals(normalizedColumnName)) {
            return true;
        }
    }
    return false;
}
 
Example #26
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 #27
Source File: PhoenixRuntimeTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void getTableTester(String normalizedName, String sqlStatementName) throws SQLException {
    Connection conn = DriverManager.getConnection(getUrl());
    try {
        conn.createStatement().execute("CREATE TABLE " + sqlStatementName + " (k VARCHAR PRIMARY KEY)");
        PTable aTable = PhoenixRuntime.getTable(conn, normalizedName);
        assertNotNull(aTable);
    } finally {
        if (null != conn) {
            conn.createStatement().execute("DROP TABLE IF EXISTS " + sqlStatementName);
        }
    }
}
 
Example #28
Source File: AsyncIndexDisabledIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncIndexRegularBuild() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.setAutoCommit(true);
        Statement stmt = conn.createStatement();
        String tableName = "TBL_" + generateUniqueName();
        String indexName = "IND_" + generateUniqueName();
        
        String ddl = "CREATE TABLE " + tableName + " (pk INTEGER NOT NULL PRIMARY KEY, val VARCHAR)";
        stmt.execute(ddl);
        stmt.execute("UPSERT INTO " + tableName + " values(1, 'y')");
        // create the async index
        stmt.execute("CREATE INDEX " + indexName + " ON " + tableName + "(val) ASYNC");

        // it should be built as a regular index
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        PTable table = phxConn.getTable(new PTableKey(null, tableName));
        assertEquals("Index not built", 1, table.getIndexes().size());
        assertEquals("Wrong index created", indexName, table.getIndexes().get(0).getName().getString());
        
        ResultSet rs = stmt.executeQuery("select /*+ INDEX(" + indexName + ")*/ pk, val from " + tableName);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals("y", rs.getString(2));
        assertFalse(rs.next());
    }
}
 
Example #29
Source File: BaseQueryPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void serializeIndexMaintainerIntoScan(Scan scan, PTable dataTable) {
    PName name = context.getCurrentTable().getTable().getName();
    List<PTable> indexes = Lists.newArrayListWithExpectedSize(1);
    for (PTable index : dataTable.getIndexes()) {
        if (index.getName().equals(name) && index.getIndexType() == IndexType.LOCAL) {
            indexes.add(index);
            break;
        }
    }
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    IndexMaintainer.serialize(dataTable, ptr, indexes, context.getConnection());
    scan.setAttribute(BaseScannerRegionObserver.LOCAL_INDEX_BUILD, ByteUtil.copyKeyBytesIfNecessary(ptr));
}
 
Example #30
Source File: ViewMetadataIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void validateCols(PTable table) {
    final String prefix = table.getType() == PTableType.INDEX ? "0:" : "";
    Predicate<PColumn> predicate = new Predicate<PColumn>() {
        @Override
        public boolean apply(PColumn col) {
            return col.getName().getString().equals(prefix + "V3")
                    || col.getName().getString().equals(prefix + "V2");
        }
    };
    List<PColumn> colList = table.getColumns();
    Collection<PColumn> filteredCols = Collections2.filter(colList, predicate);
    assertEquals(1, filteredCols.size());
    assertEquals(prefix + "V3", filteredCols.iterator().next().getName().getString());
}