Java Code Examples for org.apache.phoenix.util.SchemaUtil

The following examples show how to use org.apache.phoenix.util.SchemaUtil. These examples are extracted from open source projects. 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
static long getPendingDisableCount(PhoenixConnection conn, String indexTableName) {
    byte[] indexTableKey = SchemaUtil.getTableKeyFromFullName(indexTableName);
    Get get = new Get(indexTableKey);
    get.addColumn(TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.PENDING_DISABLE_COUNT_BYTES);
    get.addColumn(TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.INDEX_STATE_BYTES);

    try {
        pendingDisableCountResult =
                conn.getQueryServices()
                        .getTable(SchemaUtil.getPhysicalTableName(
                            PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME,
                            conn.getQueryServices().getProps()).getName())
                        .get(get);
        return Bytes.toLong(pendingDisableCountResult.getValue(TABLE_FAMILY_BYTES,
            PhoenixDatabaseMetaData.PENDING_DISABLE_COUNT_BYTES));
    } catch (Exception e) {
        LOGGER.error("Exception in getPendingDisableCount: " + e);
        return 0;
    }
}
 
Example 2
Source Project: phoenix   Source File: ConnectionQueryServicesImpl.java    License: Apache License 2.0 6 votes vote down vote up
private HColumnDescriptor generateColumnFamilyDescriptor(Pair<byte[],Map<String,Object>> family, PTableType tableType) throws SQLException {
    HColumnDescriptor columnDesc = new HColumnDescriptor(family.getFirst());
    if (tableType != PTableType.VIEW) {
        if(props.get(QueryServices.DEFAULT_KEEP_DELETED_CELLS_ATTRIB) != null){
            columnDesc.setKeepDeletedCells(props.getBoolean(
                QueryServices.DEFAULT_KEEP_DELETED_CELLS_ATTRIB, QueryServicesOptions.DEFAULT_KEEP_DELETED_CELLS));
        }
        columnDesc.setDataBlockEncoding(SchemaUtil.DEFAULT_DATA_BLOCK_ENCODING);
        for (Entry<String,Object> entry : family.getSecond().entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            columnDesc.setValue(key, value == null ? null : value.toString());
        }
    }
    return columnDesc;
}
 
Example 3
Source Project: phoenix   Source File: RowProjector.java    License: Apache License 2.0 6 votes vote down vote up
public int getColumnIndex(String name) throws SQLException {
    if (!someCaseSensitive) {
        name = SchemaUtil.normalizeIdentifier(name);
    }
    List<Integer> index = reverseIndex.get(name);
    if (index.isEmpty()) {
        if (!allCaseSensitive && someCaseSensitive) {
            name = SchemaUtil.normalizeIdentifier(name);
            index = reverseIndex.get(name);
        }
        if (index.isEmpty()) {
            throw new ColumnNotFoundException(name);
        }
    }
    
    return index.get(0);
}
 
Example 4
Source Project: phoenix   Source File: ConnectionQueryServicesImpl.java    License: Apache License 2.0 6 votes vote down vote up
private void ensureViewIndexTableCreated(PName tenantId, byte[] physicalIndexTableName, long timestamp) throws SQLException {
    PTable table;
    String name = Bytes.toString(
            physicalIndexTableName,
            MetaDataUtil.VIEW_INDEX_TABLE_PREFIX_BYTES.length,
            physicalIndexTableName.length-MetaDataUtil.VIEW_INDEX_TABLE_PREFIX_BYTES.length);
    try {
        PMetaData metadata = latestMetaData;
        if (metadata == null) {
            throwConnectionClosedException();
        }
        table = metadata.getTable(new PTableKey(tenantId, name));
        if (table.getTimeStamp() >= timestamp) { // Table in cache is newer than client timestamp which shouldn't be the case
            throw new TableNotFoundException(table.getSchemaName().getString(), table.getTableName().getString());
        }
    } catch (TableNotFoundException e) {
        byte[] schemaName = Bytes.toBytes(SchemaUtil.getSchemaNameFromFullName(name));
        byte[] tableName = Bytes.toBytes(SchemaUtil.getTableNameFromFullName(name));
        MetaDataMutationResult result = this.getTable(null, schemaName, tableName, HConstants.LATEST_TIMESTAMP, timestamp);
        table = result.getTable();
        if (table == null) {
            throw e;
        }
    }
    ensureViewIndexTableCreated(table, timestamp);
}
 
Example 5
Source Project: phoenix   Source File: ProjectionCompiler.java    License: Apache License 2.0 6 votes vote down vote up
private static void projectTableColumnFamily(StatementContext context, String cfName, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
    PTable table = tableRef.getTable();
    PColumnFamily pfamily = table.getColumnFamily(cfName);
    for (PColumn column : pfamily.getColumns()) {
        ColumnRef ref = new ColumnRef(tableRef, column.getPosition());
        if (resolveColumn) {
            ref = context.getResolver().resolveColumn(table.getTableName().getString(), cfName, column.getName().getString());
        }
        Expression expression = ref.newColumnExpression();
        projectedExpressions.add(expression);
        String colName = column.getName().toString();
        boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
        projectedColumns.add(new ExpressionProjector(colName, tableRef.getTableAlias() == null ? 
                table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
    }
}
 
Example 6
Source Project: phoenix   Source File: SystemCatalogIT.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that we cannot add a column to a base table if QueryServices.BLOCK_METADATA_CHANGES_REQUIRE_PROPAGATION
 * is true
 */
@Test
public void testAddingColumnFails() throws Exception {
    try (Connection conn = DriverManager.getConnection(getJdbcUrl())) {
        String fullTableName = SchemaUtil.getTableName(generateUniqueName(), generateUniqueName());
        String fullViewName = SchemaUtil.getTableName(generateUniqueName(), generateUniqueName());
        String ddl = "CREATE TABLE " + fullTableName + " (k1 INTEGER NOT NULL, v1 INTEGER " +
                "CONSTRAINT pk PRIMARY KEY (k1))";
        conn.createStatement().execute(ddl);

        ddl = "CREATE VIEW " + fullViewName + " AS SELECT * FROM " + fullTableName;
        conn.createStatement().execute(ddl);

        try {
            ddl = "ALTER TABLE " + fullTableName + " ADD v2 INTEGER";
            conn.createStatement().execute(ddl);
            fail();
        }
        catch (SQLException e) {
            assertEquals(SQLExceptionCode.CANNOT_MUTATE_TABLE.getErrorCode(), e.getErrorCode());
        }
    }
}
 
Example 7
Source Project: phoenix   Source File: BaseStatsCollectorIT.java    License: Apache License 2.0 6 votes vote down vote up
private void verifyGuidePostGenerated(ConnectionQueryServices queryServices,
        String tableName, String[] familyNames,
        long guidePostWidth, boolean emptyGuidePostExpected) throws Exception {
    try (Table statsHTable =
            queryServices.getTable(
                    SchemaUtil.getPhysicalName(PhoenixDatabaseMetaData.SYSTEM_STATS_NAME_BYTES,
                            queryServices.getProps()).getName())) {
        for (String familyName : familyNames) {
            GuidePostsInfo gps =
                    StatisticsUtil.readStatistics(statsHTable,
                            new GuidePostsKey(Bytes.toBytes(tableName), Bytes.toBytes(familyName)),
                            HConstants.LATEST_TIMESTAMP);
            assertTrue(emptyGuidePostExpected ? gps.isEmptyGuidePost() : !gps.isEmptyGuidePost());
            assertTrue(gps.getByteCounts()[0] >= guidePostWidth);
            assertTrue(gps.getGuidePostTimestamps()[0] > 0);
        }
    }
}
 
Example 8
Source Project: phoenix   Source File: IndexScrutinyMapperTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSourceTableName_index() {
    String fullTableName = SchemaUtil.getQualifiedTableName(schema, indexName);
    PName sourcePhysicalName = SchemaUtil.getPhysicalHBaseTableName(schema, indexName,
            isNamespaceEnabled);
    String expectedName = SchemaUtil.getPhysicalTableName(Bytes.toBytes(fullTableName),
            isNamespaceEnabled).toString();

    //setup
    Mockito.when(inputTable.getType()).thenReturn(PTableType.INDEX);
    Mockito.when(inputTable.getPhysicalName()).thenReturn(sourcePhysicalName);
    Mockito.when(inputTable.getTableName()).thenReturn(PNameFactory.newName(indexName));
    Mockito.when(inputTable.getSchemaName()).thenReturn(PNameFactory.newName(schema));

    //test
    String output = IndexScrutinyMapper.getSourceTableName(inputTable, isNamespaceEnabled);
    //assert
    Assert.assertEquals(expectedName, output);
}
 
Example 9
Source Project: phoenix   Source File: PTableImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<Mutation> toRowMutations() {
    List<Mutation> mutations = new ArrayList<Mutation>(3);
    if (deleteRow != null) {
        // Include only deleteRow mutation if present because it takes precedence over all others
        mutations.add(deleteRow);
    } else {
        // Because we cannot enforce a not null constraint on a KV column (since we don't know if the row exists when
        // we upsert it), se instead add a KV that is always emtpy. This allows us to imitate SQL semantics given the
        // way HBase works.
        addQuietly(setValues, kvBuilder, kvBuilder.buildPut(keyPtr,
            SchemaUtil.getEmptyColumnFamilyPtr(PTableImpl.this),
            QueryConstants.EMPTY_COLUMN_BYTES_PTR, ts, ByteUtil.EMPTY_BYTE_ARRAY_PTR));
        mutations.add(setValues);
        if (!unsetValues.isEmpty()) {
            mutations.add(unsetValues);
        }
    }
    return mutations;
}
 
Example 10
Source Project: phoenix   Source File: BaseIndexIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGroupByCount() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String tableName = "TBL_" + generateUniqueName();
    String indexName = "IND_" + generateUniqueName();
    String fullTableName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName);
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.setAutoCommit(false);
        String ddl ="CREATE TABLE " + fullTableName + TestUtil.TEST_TABLE_SCHEMA + tableDDLOptions;
        Statement stmt = conn.createStatement();
        stmt.execute(ddl);
        BaseTest.populateTestTable(fullTableName);
        ddl = "CREATE " + (localIndex ? "LOCAL" : "") + " INDEX " + indexName + " ON " + fullTableName + " (int_col2)";
        stmt.execute(ddl);
        ResultSet rs;
        rs = conn.createStatement().executeQuery("SELECT int_col2, COUNT(*) FROM " + fullTableName + " GROUP BY int_col2");
        assertTrue(rs.next());
        assertEquals(1,rs.getInt(2));
    }
}
 
Example 11
@BeforeClass
public static synchronized void doSetup() throws Exception {
    Map<String, String> serverProps = getServerProps();
    serverProps.put(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.TRUE.toString());
    Map<String, String> clientProps = Maps.newHashMapWithExpectedSize(3);
    clientProps.put(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.TRUE.toString());
    clientProps.put(HConstants.HBASE_CLIENT_RETRIES_NUMBER, "2");
    clientProps.put(QueryServices.INDEX_REGION_OBSERVER_ENABLED_ATTRIB, Boolean.FALSE.toString());
    NUM_SLAVES_BASE = 4;
    setUpTestDriver(new ReadOnlyProps(serverProps.entrySet().iterator()), new ReadOnlyProps(clientProps.entrySet().iterator()));
    TableName systemTable = SchemaUtil.getPhysicalTableName(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES,
            true);
    indexRebuildTaskRegionEnvironment = getUtility()
            .getRSForFirstRegionInTable(systemTable).getRegions(systemTable).get(0).getCoprocessorHost()
            .findCoprocessorEnvironment(MetaDataRegionObserver.class.getName());
    MetaDataRegionObserver.initRebuildIndexConnectionProps(indexRebuildTaskRegionEnvironment.getConfiguration());
}
 
Example 12
Source Project: phoenix   Source File: ProductMetricsIT.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test to repro ArrayIndexOutOfBoundException that happens during filtering in BinarySubsetComparator
 * only after a flush is performed
 * @throws Exception
 */
@Test
public void testFilterOnTrailingKeyColumn() throws Exception {
	long ts = nextTimestamp();
    String tenantId = getOrganizationId();
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts+1));
    Connection conn = DriverManager.getConnection(getUrl(), props);

    HBaseAdmin admin = null;
    try {
        initTableValues(tenantId, getSplits(tenantId), ts);
        admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
        admin.flush(SchemaUtil.getTableNameAsBytes(PRODUCT_METRICS_SCHEMA_NAME,PRODUCT_METRICS_NAME));
        String query = "SELECT SUM(TRANSACTIONS) FROM " + PRODUCT_METRICS_NAME + " WHERE FEATURE=?";
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, F1);
        ResultSet rs = statement.executeQuery();
        assertTrue(rs.next());
        assertEquals(1200, rs.getInt(1));
    } finally {
        if (admin != null) admin.close();
        conn.close();
    }	
}
 
Example 13
Source Project: phoenix   Source File: MutableIndexFailureIT.java    License: Apache License 2.0 6 votes vote down vote up
public MutableIndexFailureIT(String transactionProvider, boolean localIndex, boolean isNamespaceMapped, Boolean disableIndexOnWriteFailure, boolean failRebuildTask, Boolean throwIndexWriteFailure) {
    this.transactional = transactionProvider != null;
    this.transactionProvider = transactionProvider == null ? null :
        TransactionFactory.getTransactionProvider(TransactionFactory.Provider.valueOf(transactionProvider));
    this.localIndex = localIndex;
    this.tableDDLOptions = " SALT_BUCKETS=2, COLUMN_ENCODED_BYTES=NONE" + (transactional ? (",TRANSACTIONAL=true,TRANSACTION_PROVIDER='"+transactionProvider+"'") : "") 
            + (disableIndexOnWriteFailure == null ? "" : (", " + PhoenixIndexFailurePolicy.DISABLE_INDEX_ON_WRITE_FAILURE + "=" + disableIndexOnWriteFailure))
            + (throwIndexWriteFailure == null ? "" : (", " + PhoenixIndexFailurePolicy.THROW_INDEX_WRITE_FAILURE + "=" + throwIndexWriteFailure));
    this.tableName = FailingRegionObserver.FAIL_TABLE_NAME;
    this.indexName = "A_" + FailingRegionObserver.FAIL_INDEX_NAME;
    fullTableName = SchemaUtil.getTableName(schema, tableName);
    this.fullIndexName = SchemaUtil.getTableName(schema, indexName);
    this.isNamespaceMapped = isNamespaceMapped;
    this.leaveIndexActiveOnFailure = ! (disableIndexOnWriteFailure == null ? QueryServicesOptions.DEFAULT_INDEX_FAILURE_DISABLE_INDEX : disableIndexOnWriteFailure);
    this.failRebuildTask = failRebuildTask;
    this.throwIndexWriteFailure = ! Boolean.FALSE.equals(throwIndexWriteFailure);
}
 
Example 14
Source Project: phoenix   Source File: TableRef.java    License: Apache License 2.0 6 votes vote down vote up
public String getColumnDisplayName(ColumnRef ref, boolean cfCaseSensitive, boolean cqCaseSensitive) {
    String cf = null;
    String cq = null;       
    PColumn column = ref.getColumn();
    String name = column.getName().getString();
    boolean isIndex = IndexUtil.isIndexColumn(name);
    if ((table.getType() == PTableType.PROJECTED && TupleProjectionCompiler.PROJECTED_TABLE_SCHEMA.equals(table.getSchemaName()))
            || table.getType() == PTableType.SUBQUERY) {
        cq = name;
    }
    else if (SchemaUtil.isPKColumn(column)) {
        cq = isIndex ? IndexUtil.getDataColumnName(name) : name;
    }
    else {
        String defaultFamilyName = table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY : table.getDefaultFamilyName().getString();
        // Translate to the data table column name
        String dataFamilyName = isIndex ? IndexUtil.getDataColumnFamilyName(name) : column.getFamilyName().getString() ;
        cf = (table.getIndexType()==IndexType.LOCAL? IndexUtil.getActualColumnFamilyName(defaultFamilyName):defaultFamilyName).equals(dataFamilyName) ? null : dataFamilyName;
        cq = isIndex ? IndexUtil.getDataColumnName(name) : name;
    }
    
    cf = (cf!=null && cfCaseSensitive) ? "\"" + cf + "\"" : cf;
    cq = cqCaseSensitive ? "\"" + cq + "\"" : cq;
    return SchemaUtil.getColumnDisplayName(cf, cq);
}
 
Example 15
Source Project: phoenix   Source File: IndexCoprocIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAlterDoesntChangeCoprocs() throws Exception {
    String schemaName = "S" + generateUniqueName();
    String tableName = "T_" + generateUniqueName();
    String indexName = "I_" + generateUniqueName();
    String physicalTableName = SchemaUtil.getPhysicalHBaseTableName(schemaName, tableName,
        isNamespaceMapped).getString();
    String physicalIndexName = SchemaUtil.getPhysicalHBaseTableName(schemaName,
        indexName, isNamespaceMapped).getString();
    Admin admin = ((PhoenixConnection) getConnection()).getQueryServices().getAdmin();

    createBaseTable(schemaName, tableName, isMultiTenant, 0, null);
    createIndexTable(schemaName, tableName, indexName);
    TableDescriptor baseDescriptor = admin.getDescriptor(TableName.valueOf(physicalTableName));
    TableDescriptor indexDescriptor = admin.getDescriptor(TableName.valueOf(physicalIndexName));

    assertCoprocsContains(IndexRegionObserver.class, baseDescriptor);
    assertCoprocsContains(GlobalIndexChecker.class, indexDescriptor);
    String columnName = "foo";
    addColumnToBaseTable(schemaName, tableName, columnName);
    assertCoprocsContains(IndexRegionObserver.class, baseDescriptor);
    assertCoprocsContains(GlobalIndexChecker.class, indexDescriptor);
    dropColumnToBaseTable(schemaName, tableName, columnName);
    assertCoprocsContains(IndexRegionObserver.class, baseDescriptor);
    assertCoprocsContains(GlobalIndexChecker.class, indexDescriptor);
}
 
Example 16
Source Project: phoenix   Source File: WhereCompiler.java    License: 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();
    // 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.getColumnForColumnName(ref.getColumn().getName().getString());
        }
    } catch (AmbiguousColumnException e) {
        disambiguateWithFamily = true;
    }
    return ref;
 }
 
Example 17
Source Project: phoenix   Source File: VerifySingleIndexRowTest.java    License: Apache License 2.0 6 votes vote down vote up
public void createDBObject() throws SQLException {
    try(Connection conn = DriverManager.getConnection(getUrl(), new Properties())) {
        schema = generateUniqueName();
        table = generateUniqueName();
        index = generateUniqueName();
        dataTableFullName = SchemaUtil.getQualifiedTableName(schema, table);
        indexTableFullName = SchemaUtil.getQualifiedTableName(schema, index);

        conn.createStatement().execute(String.format(CREATE_TABLE_DDL, dataTableFullName));
        conn.createStatement().execute(String.format(CREATE_INDEX_DDL, index, dataTableFullName));
        conn.commit();

        pconn = conn.unwrap(PhoenixConnection.class);
        pIndexTable = pconn.getTable(new PTableKey(pconn.getTenantId(), indexTableFullName));
        pDataTable = pconn.getTable(new PTableKey(pconn.getTenantId(), dataTableFullName));
    }
}
 
Example 18
Source Project: phoenix   Source File: GlobalConnectionTenantTableIT.java    License: Apache License 2.0 6 votes vote down vote up
private static void createBaseTable(String schemaName, String tableName, boolean multiTenant,
        Integer saltBuckets, String splits) throws SQLException {
    Connection conn = getConnection();
    String ddl =
            "CREATE TABLE " + SchemaUtil.getTableName(schemaName, tableName)
                    + " (t_id VARCHAR NOT NULL,\n" + "k1 VARCHAR NOT NULL,\n"
                    + "k2 INTEGER NOT NULL,\n" + "v1 VARCHAR,\n" + VIEW_INDEX_COL
                    + " INTEGER,\n" + "CONSTRAINT pk PRIMARY KEY (t_id, k1, k2))\n";
    String ddlOptions = multiTenant ? "MULTI_TENANT=true" : "";
    if (saltBuckets != null) {
        ddlOptions =
                ddlOptions + (ddlOptions.isEmpty() ? "" : ",") + "salt_buckets=" + saltBuckets;
    }
    if (splits != null) {
        ddlOptions = ddlOptions + (ddlOptions.isEmpty() ? "" : ",") + "splits=" + splits;
    }
    conn.createStatement().execute(ddl + ddlOptions);
    conn.close();
}
 
Example 19
Source Project: phoenix   Source File: ScanRanges.java    License: Apache License 2.0 6 votes vote down vote up
private static List<byte[]> getPointKeys(List<List<KeyRange>> ranges, int[] slotSpan, RowKeySchema schema, Integer bucketNum) {
    if (ranges == null || ranges.isEmpty()) {
        return Collections.emptyList();
    }
    boolean isSalted = bucketNum != null;
    int count = 1;
    int offset = isSalted ? 1 : 0;
    // Skip salt byte range in the first position if salted
    for (int i = offset; i < ranges.size(); i++) {
        count *= ranges.get(i).size();
    }
    List<byte[]> keys = Lists.newArrayListWithExpectedSize(count);
    int[] position = new int[ranges.size()];
    int maxKeyLength = SchemaUtil.getMaxKeyLength(schema, ranges);
    int length;
    byte[] key = new byte[maxKeyLength];
    do {
        length = ScanUtil.setKey(schema, ranges, slotSpan, position, Bound.LOWER, key, offset, offset, ranges.size(), offset);
        if (isSalted) {
            key[0] = SaltingUtil.getSaltingByte(key, offset, length, bucketNum);
        }
        keys.add(Arrays.copyOf(key, length + offset));
    } while (incrementKey(ranges, position));
    return keys;
}
 
Example 20
Source Project: phoenix   Source File: UpsertValuesIT.java    License: Apache License 2.0 5 votes vote down vote up
public void testColumnQualifierForUpsertedValues() throws Exception {
    String schemaName = "A";
    String tableName = "TEST";
    String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    String ddl = "create table " + fullTableName 
            + " (" 
            + " K varchar primary key,"
            + " CF1.V1 varchar, CF2.V2 VARCHAR, CF2.V3 VARCHAR)";
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.createStatement().execute(ddl);
    }
    String dml = "UPSERT INTO " + fullTableName + " VALUES (?, ?, ?, ?)";
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PreparedStatement stmt = conn.prepareStatement(dml);
        stmt.setString(1, "KEY1");
        stmt.setString(2, "VALUE1");
        stmt.setString(3, "VALUE2");
        stmt.setString(4, "VALUE3");
        stmt.executeUpdate();
        conn.commit();
    }
    // Issue a raw hbase scan and assert that key values have the expected column qualifiers.
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        Table table = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(fullTableName));
        ResultScanner scanner = table.getScanner(new Scan());
        Result next = scanner.next();
        assertTrue(next.containsColumn(Bytes.toBytes("CF1"), PInteger.INSTANCE.toBytes(1)));
        assertTrue(next.containsColumn(Bytes.toBytes("CF2"), PInteger.INSTANCE.toBytes(2)));
        assertTrue(next.containsColumn(Bytes.toBytes("CF2"), PInteger.INSTANCE.toBytes(3)));
    }
}
 
Example 21
Source Project: phoenix   Source File: AlterTableWithViewsIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testAddNewColumnsToBaseTableWithViews() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl());
            Connection viewConn = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : conn ) {       
        String tableName = SchemaUtil.getTableName(SCHEMA1, generateUniqueName());
        String viewOfTable = SchemaUtil.getTableName(SCHEMA2, generateUniqueName());

        String ddlFormat = "CREATE TABLE IF NOT EXISTS " + tableName + " ("
                        + " %s ID char(1) NOT NULL,"
                        + " COL1 integer NOT NULL,"
                        + " COL2 bigint NOT NULL,"
                        + " CONSTRAINT NAME_PK PRIMARY KEY (%s ID, COL1, COL2)"
                        + " ) %s";
        conn.createStatement().execute(generateDDL(ddlFormat));
        assertTableDefinition(conn, tableName, PTableType.TABLE, null, 0, 3, QueryConstants.BASE_TABLE_BASE_COLUMN_COUNT, true, "ID", "COL1", "COL2");
        
        viewConn.createStatement().execute("CREATE VIEW " + viewOfTable + " ( VIEW_COL1 DECIMAL(10,2), VIEW_COL2 VARCHAR ) AS SELECT * FROM " + tableName);
        assertTableDefinition(viewConn, viewOfTable, PTableType.VIEW, tableName, 0, 5, 3, true, "ID", "COL1", "COL2", "VIEW_COL1", "VIEW_COL2");
        
        // adding a new pk column and a new regular column
        conn.createStatement().execute("ALTER TABLE " + tableName + " ADD COL3 varchar(10) PRIMARY KEY, COL4 integer");
        assertTableDefinition(conn, tableName, PTableType.TABLE, null, columnEncoded ? 2 : 1, 5, QueryConstants.BASE_TABLE_BASE_COLUMN_COUNT, false, "ID", "COL1", "COL2", "COL3", "COL4");
        // TODO PHOENIX-4766 add/drop column to a base table are no longer propagated to child views
        // assertTableDefinition(viewConn, viewOfTable, PTableType.VIEW, tableName, 0, 5, 3, "ID", "COL1", "COL2", "VIEW_COL1", "VIEW_COL2");
        assertTableDefinition(viewConn, viewOfTable, PTableType.VIEW, tableName, 0, 5, 3, true, "ID", "COL1", "COL2", "COL3", "COL4", "VIEW_COL1", "VIEW_COL2");
    } 
}
 
Example 22
Source Project: phoenix   Source File: FromCompiler.java    License: 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 23
Source Project: phoenix   Source File: CorrelatePlan.java    License: Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
    KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
    if (table != null) {
        for (PColumn column : table.getColumns()) {
            if (!SchemaUtil.isPKColumn(column)) {
                builder.addField(column);
            }
        }
    }
    return builder.build();
}
 
Example 24
Source Project: phoenix   Source File: UngroupedAggregateRegionObserver.java    License: Apache License 2.0 5 votes vote down vote up
private void checkForLocalIndexColumnFamilies(Region region,
        List<IndexMaintainer> indexMaintainers) throws IOException {
    TableDescriptor tableDesc = region.getTableDescriptor();
    String schemaName =
            tableDesc.getTableName().getNamespaceAsString()
                    .equals(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR) ? SchemaUtil
                    .getSchemaNameFromFullName(tableDesc.getTableName().getNameAsString())
                    : tableDesc.getTableName().getNamespaceAsString();
    String tableName = SchemaUtil.getTableNameFromFullName(tableDesc.getTableName().getNameAsString());
    for (IndexMaintainer indexMaintainer : indexMaintainers) {
        Set<ColumnReference> coveredColumns = indexMaintainer.getCoveredColumns();
        if(coveredColumns.isEmpty()) {
            byte[] localIndexCf = indexMaintainer.getEmptyKeyValueFamily().get();
            // When covered columns empty we store index data in default column family so check for it.
            if (tableDesc.getColumnFamily(localIndexCf) == null) {
                ServerUtil.throwIOException("Column Family Not Found",
                    new ColumnFamilyNotFoundException(schemaName, tableName, Bytes
                            .toString(localIndexCf)));
            }
        }
        for (ColumnReference reference : coveredColumns) {
            byte[] cf = IndexUtil.getLocalIndexColumnFamily(reference.getFamily());
            ColumnFamilyDescriptor family = region.getTableDescriptor().getColumnFamily(cf);
            if (family == null) {
                ServerUtil.throwIOException("Column Family Not Found",
                    new ColumnFamilyNotFoundException(schemaName, tableName, Bytes.toString(cf)));
            }
        }
    }
}
 
Example 25
Source Project: phoenix   Source File: OrphanViewToolIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteGrandchildViewRows() throws Exception {
    String baseTableName = generateUniqueName();
    String baseTableFullName = SchemaUtil.getTableName(SCHEMA1, baseTableName);
    try (Connection connection = DriverManager.getConnection(getUrl());
         Connection viewConnection =
                 isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL) : connection) {
        createBaseTableIndexAndViews(connection, baseTableFullName, viewConnection, SCHEMA2, SCHEMA3, null);
        // Delete the grand child view rows from the system catalog
        executeDeleteQuery(connection, deleteViewRows, SCHEMA3);
        // Verify that grand grand child views are still in the system catalog table
        verifyCountQuery(connection, countViewsQuery, null, grandGrandChildCount);
        // Run the orphan view tool to clean up orphan views
        runOrphanViewTool(true, false, true, false);
        // Verify that the orphan views and links have been removed
        verifyOrphanFileLineCounts(grandGrandChildCount,
                grandChildCount + grandGrandChildCount,
                grandChildCount,
                grandChildCount + grandGrandChildCount);
        // Verify that all views and links records for grand and grand grand child views are removed
        // from the system catalog table
        verifyNoViewNoLinkInSystemCatalog(connection, SCHEMA3);
        verifyNoViewNoLinkInSystemCatalog(connection, null);
        // Verify the child links are also removed
        verifyNoChildLink(connection, SCHEMA2);
        verifyNoChildLink(connection, SCHEMA3);
        deleteAllRows(connection, SCHEMA1, SCHEMA2, SCHEMA3, null);
    }
}
 
Example 26
@Test
public void testTableSentWhenIndexStateChanges() throws Throwable {
    // Create connections 1 and 2
    Properties longRunningProps = new Properties(); // Must update config before starting server
    longRunningProps.put(QueryServices.EXTRA_JDBC_ARGUMENTS_ATTRIB,
        QueryServicesOptions.DEFAULT_EXTRA_JDBC_ARGUMENTS);
    longRunningProps.put(QueryServices.DROP_METADATA_ATTRIB, Boolean.TRUE.toString());
    Connection conn1 = DriverManager.getConnection(url, longRunningProps);
    String url2 = url + PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + "LongRunningQueries";
    Connection conn2 = DriverManager.getConnection(url2, longRunningProps);
    conn1.setAutoCommit(true);
    conn2.setAutoCommit(true);
    try {
        String schemaName = generateUniqueName();
        String tableName = generateUniqueName();
        String indexName = generateUniqueName();
        final String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
        String fullIndexName = SchemaUtil.getTableName(schemaName, indexName);
        conn1.createStatement().execute("CREATE TABLE " + fullTableName + "(k INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER) COLUMN_ENCODED_BYTES = 0, STORE_NULLS=true");
        conn1.createStatement().execute("CREATE INDEX " + indexName + " ON " + fullTableName + " (v1) INCLUDE (v2)");
        Table metaTable = conn2.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
        IndexUtil.updateIndexState(fullIndexName, 0, metaTable, PIndexState.DISABLE);
        conn2.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES(1,2,3)");
        conn2.commit();
        conn1.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES(4,5,6)");
        conn1.commit();
        PTableKey key = new PTableKey(null,fullTableName);
        PMetaData metaCache = conn1.unwrap(PhoenixConnection.class).getMetaDataCache();
        PTable table = metaCache.getTableRef(key).getTable();
        for (PTable index : table.getIndexes()) {
            assertEquals(PIndexState.DISABLE, index.getIndexState());
        }
    } finally {
        conn1.close();
        conn2.close();
    }
}
 
Example 27
Source Project: phoenix   Source File: PartialIndexRebuilderIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteAfterFailure() throws Throwable {
    String schemaName = generateUniqueName();
    String tableName = generateUniqueName();
    String indexName = generateUniqueName();
    String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    String fullIndexName = SchemaUtil.getTableName(schemaName, indexName);
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.createStatement().execute("CREATE TABLE " + fullTableName + "(k VARCHAR PRIMARY KEY, v VARCHAR) COLUMN_ENCODED_BYTES = 0, STORE_NULLS=true");
        conn.createStatement().execute("CREATE INDEX " + indexName + " ON " + fullTableName + " (v)");
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('a',null)");
        conn.commit();
        long disableTS = EnvironmentEdgeManager.currentTimeMillis();
        Table metaTable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
        IndexUtil.updateIndexState(fullIndexName, disableTS, metaTable, PIndexState.DISABLE);
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('a','b')");
        conn.commit();
        conn.createStatement().execute("DELETE FROM " + fullTableName);
        conn.commit();
        runIndexRebuilder(fullTableName);
        Thread.sleep(WAIT_AFTER_DISABLED);
        runIndexRebuilder(fullTableName);
        assertTrue(TestUtil.checkIndexState(conn, fullIndexName, PIndexState.ACTIVE, 0L));

        IndexScrutiny.scrutinizeIndex(conn, fullTableName, fullIndexName);
   }
}
 
Example 28
Source Project: phoenix   Source File: ViewMetadataIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDropTableCascadeWithChildViewWithIndex() throws SQLException {
    String tableName = generateUniqueName();
    String fullTableName = SchemaUtil.getTableName(SCHEMA1, tableName);
    String fullViewName = SchemaUtil.getTableName(SCHEMA2, generateUniqueName());
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        createTableViewAndDropCascade(conn, fullTableName, fullViewName, true);
        validateViewDoesNotExist(conn, fullViewName);
        validateSystemTaskContainsCompletedDropChildViewsTasks(conn, SCHEMA1, tableName, 1);
    }
}
 
Example 29
Source Project: phoenix   Source File: FromCompiler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TableRef resolveTable(String schemaName, String tableName) throws SQLException {
    String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    List<TableRef> tableRefs = tableMap.get(fullTableName);
    if (tableRefs.size() == 0) {
        throw new TableNotFoundException(fullTableName);
    } else if (tableRefs.size() > 1) {
        throw new AmbiguousTableException(tableName);
    } else {
        return tableRefs.get(0);
    }
}
 
Example 30
Source Project: phoenix   Source File: MutableIndexIT.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * PHOENIX-4988
 * Test updating only a non-indexed column after two successive deletes to an indexed row
 */
@Test
public void testUpdateNonIndexedColumn() throws Exception {
    String tableName = "TBL_" + generateUniqueName();
    String indexName = "IDX_" + generateUniqueName();
    String fullTableName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName);
    String fullIndexName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, indexName);
    try (Connection conn = getConnection()) {
        conn.setAutoCommit(false);
        conn.createStatement().execute("CREATE TABLE " + fullTableName + " (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) " + tableDDLOptions);
        conn.createStatement().execute("CREATE " + (localIndex ? " LOCAL " : "") + " INDEX " + indexName + " ON " + fullTableName + " (v2)");
        conn.createStatement().executeUpdate("UPSERT INTO " + fullTableName + "(k,v1,v2) VALUES ('testKey','v1_1','v2_1')");
        conn.commit();
        conn.createStatement().executeUpdate("DELETE FROM " + fullTableName);
        conn.commit();
        conn.createStatement().executeUpdate("UPSERT INTO " + fullTableName + "(k,v1,v2) VALUES ('testKey','v1_2','v2_2')");
        conn.commit();
        conn.createStatement().executeUpdate("DELETE FROM " + fullTableName);
        conn.commit();
        conn.createStatement().executeUpdate("UPSERT INTO " + fullTableName + "(k,v1) VALUES ('testKey','v1_3')");
        conn.commit();
        IndexScrutiny.scrutinizeIndex(conn, fullTableName, fullIndexName);
        // PHOENIX-4980
        // When there is a flush after a data table update of non-indexed columns, the
        // index gets out of sync on the next write
        getUtility().getHBaseAdmin().flush(TableName.valueOf(fullTableName));
        conn.createStatement().executeUpdate("UPSERT INTO " + fullTableName + "(k,v1,v2) VALUES ('testKey','v1_4','v2_3')");
        conn.commit();
        IndexScrutiny.scrutinizeIndex(conn, fullTableName, fullIndexName);
    }
}