org.apache.phoenix.util.SchemaUtil Java Examples

The following examples show how to use org.apache.phoenix.util.SchemaUtil. 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: ProjectionCompiler.java    From phoenix with 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 #2
Source File: ConnectionQueryServicesImpl.java    From phoenix with 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 File: RowProjector.java    From phoenix with 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 File: ScanRanges.java    From phoenix with 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 #5
Source File: IndexRebuildIncrementDisableCountIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
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 #6
Source File: ConnectionQueryServicesImpl.java    From phoenix with 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 #7
Source File: GlobalConnectionTenantTableIT.java    From phoenix with 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 #8
Source File: VerifySingleIndexRowTest.java    From phoenix with 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 #9
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();
    // 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 #10
Source File: IndexCoprocIT.java    From phoenix with 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 #11
Source File: TableRef.java    From phoenix with 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 #12
Source File: SystemCatalogIT.java    From phoenix with 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 #13
Source File: BaseStatsCollectorIT.java    From phoenix with 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 #14
Source File: IndexScrutinyMapperTest.java    From phoenix with 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 #15
Source File: PTableImpl.java    From phoenix with 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 #16
Source File: BaseIndexIT.java    From phoenix with 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 #17
Source File: MutableIndexFailureWithNamespaceIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@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 #18
Source File: ProductMetricsIT.java    From phoenix with 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 #19
Source File: MutableIndexFailureIT.java    From phoenix with 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 #20
Source File: SplitSystemCatalogIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
protected static void splitSystemCatalog() throws SQLException, Exception {
    try (Connection conn = DriverManager.getConnection(getUrl())) {
    }
    String tableName = "TABLE";
    String fullTableName1 = SchemaUtil.getTableName(SCHEMA1, tableName);
    String fullTableName2 = SchemaUtil.getTableName(SCHEMA2, tableName);
    String fullTableName3 = SchemaUtil.getTableName(SCHEMA3, tableName);
    String fullTableName4 = SchemaUtil.getTableName(SCHEMA4, tableName);
    ArrayList<String> tableList = Lists.newArrayList(fullTableName1, fullTableName2, fullTableName3);
    Map<String, List<String>> tenantToTableMap = Maps.newHashMap();
    tenantToTableMap.put(null, tableList);
    tenantToTableMap.put(TENANT1, Lists.newArrayList(fullTableName2, fullTableName3));
    tenantToTableMap.put(TENANT2, Lists.newArrayList(fullTableName4));
    splitSystemCatalog(tenantToTableMap);
}
 
Example #21
Source File: PartialIndexRebuilderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteAndUpsertValuesAtSameTS1() throws Throwable {
    String schemaName = generateUniqueName();
    String tableName = generateUniqueName();
    String indexName = generateUniqueName();
    final String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    final String fullIndexName = SchemaUtil.getTableName(schemaName, indexName);
    final MyClock clock = new MyClock(1000);
    EnvironmentEdgeManager.injectEdge(clock);
    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','a')");
        conn.commit();
        Table metaTable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
        IndexUtil.updateIndexState(fullIndexName, clock.currentTime(), metaTable, PIndexState.DISABLE);
        clock.setAdvance(false);
        conn.createStatement().execute("DELETE FROM " + fullTableName + " WHERE k='a'");
        conn.commit();
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('a','ccc')");
        conn.commit();
        clock.setAdvance(true);
        waitForIndexState(conn, fullTableName, fullIndexName, PIndexState.INACTIVE);
        clock.time += WAIT_AFTER_DISABLED;
        runIndexRebuilder(fullTableName);
        assertTrue(TestUtil.checkIndexState(conn, fullIndexName, PIndexState.ACTIVE, 0L));
        IndexScrutiny.scrutinizeIndex(conn, fullTableName, fullIndexName);
    } finally {
        EnvironmentEdgeManager.injectEdge(null);
    }
}
 
Example #22
Source File: ParseNodeFactory.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public FunctionParseNode functionDistinct(String name, List<ParseNode> args) {
    if (CountAggregateFunction.NAME.equals(SchemaUtil.normalizeIdentifier(name))) {
        BuiltInFunctionInfo info = getInfo(
                SchemaUtil.normalizeIdentifier(DistinctCountAggregateFunction.NAME), args);
        return new DistinctCountParseNode(DistinctCountAggregateFunction.NAME, args, info);
    } else {
        throw new UnsupportedOperationException("DISTINCT not supported with " + name);
    }
}
 
Example #23
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 #24
Source File: AlterMultiTenantTableWithViewsIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddColumnsToSaltedBaseTableWithViews() throws Exception {
    String baseTable = SchemaUtil.getTableName(SCHEMA1, generateUniqueName());
    String view1 = SchemaUtil.getTableName(SCHEMA2, generateUniqueName());
    String tenant = TENANT1;
    try (Connection conn = DriverManager.getConnection(getUrl());
            Connection tenant1Conn = getTenantConnection(tenant)) {
        String baseTableDDL =
                "CREATE TABLE " + baseTable
                        + " (TENANT_ID VARCHAR NOT NULL, PK1 VARCHAR NOT NULL, V1 VARCHAR, "
                        + "V2 VARCHAR, V3 VARCHAR CONSTRAINT NAME_PK PRIMARY KEY(TENANT_ID, PK1))"
                        + " MULTI_TENANT = true, SALT_BUCKETS = 4";
        conn.createStatement().execute(baseTableDDL);

        String view1DDL = "CREATE VIEW " + view1 + " ( VIEW_COL1 DECIMAL(10,2), VIEW_COL2 CHAR(256)) AS SELECT * FROM " + baseTable;
        tenant1Conn.createStatement().execute(view1DDL);

        assertTableDefinition(conn, baseTable, PTableType.TABLE, null, 1, 6, BASE_TABLE_BASE_COLUMN_COUNT, "TENANT_ID", "PK1", "V1", "V2", "V3");
        assertTableDefinition(tenant1Conn, view1, PTableType.VIEW, baseTable, 0, 8, 6,  "PK1", "V1", "V2", "V3", "VIEW_COL1", "VIEW_COL2");

        String alterBaseTable = "ALTER TABLE " + baseTable + " ADD KV VARCHAR, PK2 VARCHAR PRIMARY KEY";
        conn.createStatement().execute(alterBaseTable);

        assertTableDefinition(conn, baseTable, PTableType.TABLE, null, 2, 7, BASE_TABLE_BASE_COLUMN_COUNT, "TENANT_ID", "PK1", "V1", "V2", "V3", "KV", "PK2");
        assertTableDefinition(tenant1Conn, view1, PTableType.VIEW, baseTable, 0, 8, 6,  "PK1", "V1", "V2", "V3", "KV", "PK2", "VIEW_COL1", "VIEW_COL2");

        // verify that the both columns were added to view1
        tenant1Conn.createStatement().execute("SELECT KV from " + view1);
        tenant1Conn.createStatement().execute("SELECT PK2 from " + view1);
    }
}
 
Example #25
Source File: ConnectionlessQueryServicesImpl.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public MetaDataMutationResult getTable(PName tenantId, byte[] schemaBytes, byte[] tableBytes, long tableTimestamp, long clientTimestamp) throws SQLException {
    // Return result that will cause client to use it's own metadata instead of needing
    // to get anything from the server (since we don't have a connection)
    try {
        String fullTableName = SchemaUtil.getTableName(schemaBytes, tableBytes);
        PTable table = metaData.getTableRef(new PTableKey(tenantId, fullTableName)).getTable();
        return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS, 0, table);
    } catch (TableNotFoundException e) {
        return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, 0, null);
    }
}
 
Example #26
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 #27
Source File: WhereCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
     public Expression visit(ColumnParseNode node) throws SQLException {
         ColumnRef ref = resolveColumn(node);
         TableRef tableRef = ref.getTableRef();
         Expression newColumnExpression = ref.newColumnExpression(node.isTableNameCaseSensitive(), node.isCaseSensitive());
         if (tableRef.equals(context.getCurrentTable()) && !SchemaUtil.isPKColumn(ref.getColumn())) {
             byte[] cq = tableRef.getTable().getImmutableStorageScheme() == ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS 
             		? QueryConstants.SINGLE_KEYVALUE_COLUMN_QUALIFIER_BYTES : ref.getColumn().getColumnQualifierBytes();
             // track the where condition columns. Later we need to ensure the Scan in HRS scans these column CFs
             context.addWhereConditionColumn(ref.getColumn().getFamilyName().getBytes(), cq);
         }
return newColumnExpression;
     }
 
Example #28
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testSecondPkColInListFilter() throws SQLException {
    String tenantId = "000000000000001";
    String entityId1 = "00000000000000X";
    String entityId2 = "00000000000000Y";
    String query = String.format("select * from %s where organization_id='%s' AND entity_id IN ('%s','%s')",
            ATABLE_NAME, tenantId, entityId1, entityId2);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId + entityId1);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = PVarchar.INSTANCE.toBytes(tenantId + entityId2);
    assertArrayEquals(ByteUtil.concat(stopRow, QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow());

    Filter filter = scan.getFilter();

    assertEquals(
        new SkipScanFilter(
            ImmutableList.of(
                Arrays.asList(
                    pointRange(tenantId,entityId1),
                    pointRange(tenantId,entityId2))),
            SchemaUtil.VAR_BINARY_SCHEMA),
        filter);
}
 
Example #29
Source File: StatisticsWriter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * @param tableName
 *            TODO
 * @param clientTimeStamp
 *            TODO
 * @return the {@link StatisticsWriter} for the given primary table.
 * @throws IOException
 *             if the table cannot be created due to an underlying HTable creation error
 */
public static StatisticsWriter newWriter(RegionCoprocessorEnvironment env, String tableName, long clientTimeStamp)
        throws IOException {
    Configuration configuration = env.getConfiguration();
    long newClientTimeStamp = determineClientTimeStamp(configuration, clientTimeStamp);
    Table statsWriterTable = ConnectionFactory.getConnection(ConnectionType.DEFAULT_SERVER_CONNECTION, env).getTable(
            SchemaUtil.getPhysicalTableName(PhoenixDatabaseMetaData.SYSTEM_STATS_NAME_BYTES, env.getConfiguration()));
    Table statsReaderTable = ServerUtil.getHTableForCoprocessorScan(env, statsWriterTable);
    StatisticsWriter statsTable = new StatisticsWriter(statsReaderTable, statsWriterTable, tableName,
            newClientTimeStamp);
    return statsTable;
}
 
Example #30
Source File: ColumnParseNode.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void toSQL(ColumnResolver resolver, StringBuilder buf) {
    // If resolver is not null, then resolve to get fully qualified name
    String tableName = null;
    if (resolver == null) {
        if (this.tableName != null) {
            tableName = this.tableName.getTableName();
        }
    } else {
        try {
            ColumnRef ref = resolver.resolveColumn(this.getSchemaName(), this.getTableName(), this.getName());
            PColumn column = ref.getColumn();
            if (!SchemaUtil.isPKColumn(column)) {
                PTable table = ref.getTable();
                String defaultFamilyName = table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY : table.getDefaultFamilyName().getString();
                // Translate to the data table column name
                String dataFamilyName = column.getFamilyName().getString() ;
                tableName = defaultFamilyName.equals(dataFamilyName) ? null : dataFamilyName;
            }
            
        } catch (SQLException e) {
            throw new RuntimeException(e); // Already resolved, so not possible
        }
    }
    if (tableName != null) {
        if (isTableNameCaseSensitive()) {
            buf.append('"');
            buf.append(tableName);
            buf.append('"');
        } else {
            buf.append(tableName);
        }
        buf.append('.');
    }
    toSQL(buf);
}