Java Code Examples for org.apache.phoenix.schema.PTable#getSequenceNumber()
The following examples show how to use
org.apache.phoenix.schema.PTable#getSequenceNumber() .
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: AlterMultiTenantTableWithViewsIT.java From phoenix with Apache License 2.0 | 4 votes |
private static long getTableSequenceNumber(PhoenixConnection conn, String tableName) throws SQLException { PTable table = conn.getTable(new PTableKey(conn.getTenantId(), SchemaUtil.normalizeIdentifier(tableName))); return table.getSequenceNumber(); }
Example 2
Source File: AlterTableIT.java From phoenix with Apache License 2.0 | 4 votes |
@Test public void testMetadataForImmutableTable() throws Exception { String schemaName = "XYZ"; String baseTableName = generateUniqueName(); String viewName = generateUniqueName(); String fullTableName = schemaName + "." + baseTableName; String fullViewName = schemaName + "." + viewName; try (Connection conn = DriverManager.getConnection(getUrl())) { PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class); conn.createStatement().execute("CREATE TABLE IF NOT EXISTS " + fullTableName + " (" + " ID char(1) NOT NULL," + " COL1 integer NOT NULL," + " COL2 bigint NOT NULL," + " KV1 VARCHAR" + " CONSTRAINT NAME_PK PRIMARY KEY (ID, COL1, COL2)" + " ) " + generateDDLOptions("IMMUTABLE_ROWS = true" + (!columnEncoded ? ",IMMUTABLE_STORAGE_SCHEME="+ PTable.ImmutableStorageScheme.ONE_CELL_PER_COLUMN : ""))); PTable baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName)); long initBaseTableSeqNumber = baseTable.getSequenceNumber(); // assert that the client side cache is updated. EncodedCQCounter cqCounter = baseTable.getEncodedCQCounter(); assertEquals( columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 1) : null, cqCounter.getNextQualifier(QueryConstants.DEFAULT_COLUMN_FAMILY)); // assert that the server side metadata is updated correctly. assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "KV1", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE); assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber); // now create a view and validate client and server side metadata String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName; conn.createStatement().execute(viewDDL); baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName)); PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName)); // verify that the client side cache is updated. Base table's cq counters should be updated. assertEquals( columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 2) : null, baseTable.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY)); assertEquals( columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 1) : null, baseTable.getEncodedCQCounter().getNextQualifier("A")); assertNull("A view should always have the null cq counter", 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 + 2); assertEncodedCQCounter("A", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE); assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + (columnEncoded ? 1 : 0)); assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM); } }
Example 3
Source File: AlterTableIT.java From phoenix with Apache License 2.0 | 4 votes |
@Test public void testMetadataForMutableTable() throws Exception { String schemaName = "XYZ"; String baseTableName = generateUniqueName(); String viewName = generateUniqueName(); String fullTableName = schemaName + "." + baseTableName; String fullViewName = schemaName + "." + viewName; try (Connection conn = DriverManager.getConnection(getUrl())) { PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class); conn.createStatement().execute("CREATE TABLE IF NOT EXISTS " + fullTableName + " (" + " ID char(1) NOT NULL," + " COL1 integer NOT NULL," + " COL2 bigint NOT NULL," + " KV1 VARCHAR" + " CONSTRAINT NAME_PK PRIMARY KEY (ID, COL1, COL2)" + " ) " + tableDDLOptions); PTable baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName)); long initBaseTableSeqNumber = baseTable.getSequenceNumber(); // assert that the client side cache is updated. EncodedCQCounter cqCounter = baseTable.getEncodedCQCounter(); assertEquals( columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 1) : null, cqCounter.getNextQualifier(QueryConstants.DEFAULT_COLUMN_FAMILY)); // assert that the server side metadata is updated correctly. assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "KV1", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE); assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber); // now create a view and validate client and server side metadata String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName; conn.createStatement().execute(viewDDL); baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName)); PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName)); // verify that the client side cache is updated. Base table's cq counters should be updated. assertEquals(columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 3) : null, baseTable.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY)); assertNull("A view should always have the null cq counter", 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 + 3); assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2); assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + (columnEncoded ? 1 : 0)); assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM); } }
Example 4
Source File: AlterTableIT.java From phoenix with Apache License 2.0 | 4 votes |
@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, baseTableName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 8)); assertEncodedCQValue("A", "COL11", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 9); assertSequenceNumber(schemaName, baseTableName, columnEncoded ? initBaseTableSeqNumber + 4 : initBaseTableSeqNumber + 2 ); // view sequence number does not change as base table column changes are not propagated to views assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM + 1); } }