use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class AlterMultiTenantTableWithViewsIT method getIndexOfPkColumn.
private int getIndexOfPkColumn(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);
int i = 0;
for (PColumn pkCol : pkCols) {
if (pkCol.getName().getString().equals(normalizedColumnName)) {
return i;
}
i++;
}
return -1;
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class AlterTableIT method testAddingColumnsToTablesAndViews.
@Test
public void testAddingColumnsToTablesAndViews() throws Exception {
String schemaName = generateUniqueName();
String baseTableName = generateUniqueName();
String viewName = generateUniqueName();
String fullTableName = schemaName + "." + baseTableName;
String fullViewName = schemaName + "." + viewName;
Properties props = new Properties();
props.put(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.toString(true));
try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
conn.createStatement().execute("CREATE SCHEMA " + schemaName);
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
conn.createStatement().execute("CREATE TABLE " + fullTableName + " (" + " ID char(1) NOT NULL," + " COL1 integer NOT NULL," + " COL2 bigint NOT NULL," + " CONSTRAINT NAME_PK PRIMARY KEY (ID, COL1, COL2)" + " ) " + tableDDLOptions);
PTable baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
long initBaseTableSeqNumber = baseTable.getSequenceNumber();
// Add a column to the base table and see if the client and server metadata is updated correctly
String alterDDL = "ALTER TABLE " + fullTableName + " ADD COL3 VARCHAR PRIMARY KEY, COL4 INTEGER, COL5 VARCHAR, B.COL6 DECIMAL (10, 2)";
conn.createStatement().execute(alterDDL);
// assert that the client side cache is updated.
baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
EncodedCQCounter encodedCqCounter = baseTable.getEncodedCQCounter();
assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 3) : null, encodedCqCounter.getNextQualifier(DEFAULT_COLUMN_FAMILY));
// assert that the server side metadata is updated correctly.
assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
// assert that the server side metadata for columns is updated correctly.
assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL4", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE);
assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL5", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
assertEncodedCQValue("B", "COL6", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2);
long baseTableSeqNumBeforeAddingChildCols = initBaseTableSeqNumber + 1;
assertSequenceNumber(schemaName, baseTableName, baseTableSeqNumBeforeAddingChildCols);
// Create a view
String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName;
conn.createStatement().execute(viewDDL);
// assert that the server side metadata is updated correctly.
assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 5);
// assert that the server side metadata for columns is updated correctly.
assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 4);
// for encoded columns creating a view that adds its own columns should increment the base table's sequence number too.
assertSequenceNumber(schemaName, baseTableName, columnEncoded ? initBaseTableSeqNumber + 2 : baseTableSeqNumBeforeAddingChildCols);
// Add column to the view
viewDDL = "ALTER VIEW " + fullViewName + " ADD VIEW_COL3 DECIMAL(10, 2), A.VIEW_COL4 VARCHAR, B.VIEW_COL5 INTEGER";
conn.createStatement().execute(viewDDL);
// assert that the client cache for the base table is updated
baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
encodedCqCounter = baseTable.getEncodedCQCounter();
assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 8) : null, encodedCqCounter.getNextQualifier(DEFAULT_COLUMN_FAMILY));
// assert client cache for view
PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName));
encodedCqCounter = view.getEncodedCQCounter();
assertNull("A view should always have the column qualifier counter as null", view.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
// assert that the server side metadata for the base table and the view is also updated correctly.
assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 8);
assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 4);
assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL3", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 5);
assertEncodedCQValue("A", "VIEW_COL4", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 6);
assertEncodedCQValue("B", "VIEW_COL5", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 7);
// adding a column to the should increment the base table's sequence number too since we update the cq counters for column families.
assertSequenceNumber(schemaName, baseTableName, columnEncoded ? initBaseTableSeqNumber + 3 : baseTableSeqNumBeforeAddingChildCols);
assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM + 1);
// Add column to the base table which doesn't already exist in the view.
alterDDL = "ALTER TABLE " + fullTableName + " ADD COL10 VARCHAR, A.COL11 INTEGER";
conn.createStatement().execute(alterDDL);
baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
// assert that the client cache for the base table is updated
encodedCqCounter = baseTable.getEncodedCQCounter();
assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 10) : null, encodedCqCounter.getNextQualifier(DEFAULT_COLUMN_FAMILY));
// assert client cache for view
view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName));
encodedCqCounter = view.getEncodedCQCounter();
assertNull("A view should always have the column qualifier counter as null", view.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
// assert that the server side metadata for the base table and the view is also updated correctly.
assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 10));
assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL10", schemaName, viewName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 8));
assertEncodedCQValue("A", "COL11", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 9);
assertSequenceNumber(schemaName, baseTableName, columnEncoded ? initBaseTableSeqNumber + 4 : initBaseTableSeqNumber + 2);
assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM + 2);
}
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class StatsCollectorIT method invalidateStats.
private static void invalidateStats(Connection conn, String tableName) throws SQLException {
PTable ptable = conn.unwrap(PhoenixConnection.class).getMetaDataCache().getTableRef(new PTableKey(null, tableName)).getTable();
byte[] name = ptable.getPhysicalName().getBytes();
conn.unwrap(PhoenixConnection.class).getQueryServices().invalidateStats(new GuidePostsKey(name, SchemaUtil.getEmptyColumnFamily(ptable)));
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class PhoenixRuntimeTest method getTableTester.
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);
}
}
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class ConnectionQueryServicesImpl method addColumnQualifierColumn.
// Special method for adding the column qualifier column for 4.10.
private PhoenixConnection addColumnQualifierColumn(PhoenixConnection oldMetaConnection, Long timestamp) throws SQLException {
Properties props = PropertiesUtil.deepCopy(oldMetaConnection.getClientInfo());
props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(timestamp));
// Cannot go through DriverManager or you end up in an infinite loop because it'll call init again
PhoenixConnection metaConnection = new PhoenixConnection(oldMetaConnection, this, props);
PTable sysCatalogPTable = metaConnection.getTable(new PTableKey(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME));
int numColumns = sysCatalogPTable.getColumns().size();
try (PreparedStatement mutateTable = metaConnection.prepareStatement(MetaDataClient.MUTATE_TABLE)) {
mutateTable.setString(1, null);
mutateTable.setString(2, SYSTEM_CATALOG_SCHEMA);
mutateTable.setString(3, SYSTEM_CATALOG_TABLE);
mutateTable.setString(4, PTableType.SYSTEM.getSerializedValue());
mutateTable.setLong(5, sysCatalogPTable.getSequenceNumber() + 1);
mutateTable.setInt(6, numColumns + 1);
mutateTable.execute();
}
List<Mutation> tableMetadata = new ArrayList<>();
tableMetadata.addAll(metaConnection.getMutationState().toMutations(metaConnection.getSCN()).next().getSecond());
metaConnection.rollback();
PColumn column = new PColumnImpl(PNameFactory.newName("COLUMN_QUALIFIER"), PNameFactory.newName(DEFAULT_COLUMN_FAMILY_NAME), PVarbinary.INSTANCE, null, null, true, numColumns, SortOrder.ASC, null, null, false, null, false, false, Bytes.toBytes("COLUMN_QUALIFIER"));
String upsertColumnMetadata = "UPSERT INTO " + SYSTEM_CATALOG_SCHEMA + ".\"" + SYSTEM_CATALOG_TABLE + "\"( " + TENANT_ID + "," + TABLE_SCHEM + "," + TABLE_NAME + "," + COLUMN_NAME + "," + COLUMN_FAMILY + "," + DATA_TYPE + "," + NULLABLE + "," + COLUMN_SIZE + "," + DECIMAL_DIGITS + "," + ORDINAL_POSITION + "," + SORT_ORDER + "," + DATA_TABLE_NAME + "," + ARRAY_SIZE + "," + VIEW_CONSTANT + "," + IS_VIEW_REFERENCED + "," + PK_NAME + "," + KEY_SEQ + "," + COLUMN_DEF + "," + IS_ROW_TIMESTAMP + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement colUpsert = metaConnection.prepareStatement(upsertColumnMetadata)) {
colUpsert.setString(1, null);
colUpsert.setString(2, SYSTEM_CATALOG_SCHEMA);
colUpsert.setString(3, SYSTEM_CATALOG_TABLE);
colUpsert.setString(4, "COLUMN_QUALIFIER");
colUpsert.setString(5, DEFAULT_COLUMN_FAMILY);
colUpsert.setInt(6, column.getDataType().getSqlType());
colUpsert.setInt(7, ResultSetMetaData.columnNullable);
colUpsert.setNull(8, Types.INTEGER);
colUpsert.setNull(9, Types.INTEGER);
colUpsert.setInt(10, sysCatalogPTable.getBucketNum() != null ? numColumns : (numColumns + 1));
colUpsert.setInt(11, SortOrder.ASC.getSystemValue());
colUpsert.setString(12, null);
colUpsert.setNull(13, Types.INTEGER);
colUpsert.setBytes(14, null);
colUpsert.setBoolean(15, false);
colUpsert.setString(16, sysCatalogPTable.getPKName() == null ? null : sysCatalogPTable.getPKName().getString());
colUpsert.setNull(17, Types.SMALLINT);
colUpsert.setNull(18, Types.VARCHAR);
colUpsert.setBoolean(19, false);
colUpsert.execute();
}
tableMetadata.addAll(metaConnection.getMutationState().toMutations(metaConnection.getSCN()).next().getSecond());
metaConnection.rollback();
metaConnection.getQueryServices().addColumn(tableMetadata, sysCatalogPTable, Collections.<String, List<Pair<String, Object>>>emptyMap(), Collections.<String>emptySet(), Lists.newArrayList(column));
metaConnection.removeTable(null, SYSTEM_CATALOG_NAME, null, timestamp);
ConnectionQueryServicesImpl.this.removeTable(null, SYSTEM_CATALOG_NAME, null, timestamp);
clearCache();
return metaConnection;
}
Aggregations