Github user twdsilva commented on a diff in the pull request:
https://github.com/apache/phoenix/pull/355#discussion_r224635422
--- Diff:
phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java ---
@@ -1298,7 +1284,322 @@ public void testAddingColumnsToTablesAndViews()
throws Exception {
assertSequenceNumber(schemaName, viewName,
PTable.INITIAL_SEQ_NUM + 1);
}
}
-
+
+
+ @Test
+ public void testModifyingRowTimestampColumnNotAllowedViaAlterTable()
throws Exception {
+ try (Connection conn = DriverManager.getConnection(getUrl())) {
+ conn.createStatement().execute("CREATE TABLE " +
dataTableFullName +
+ " (PK1 DATE NOT NULL, PK2 VARCHAR NOT NULL, KV1
VARCHAR CONSTRAINT PK PRIMARY KEY(PK1 ROW_TIMESTAMP, PK2)) " + tableDDLOptions);
+ try {
+ conn.createStatement().execute("ALTER TABLE " +
dataTableFullName + " modify PK1 BIGINT");
+ fail("Altering table to modify a row_timestamp column
should fail");
+ } catch (SQLException e) {
+
assertEquals(SQLExceptionCode.DISALLOW_MODIFY_TIMESTAMP_OR_PK_COLUMN.getErrorCode(),
e.getErrorCode());
+ }
+ conn.close();
+ }
+ }
+
+ @Test
+ public void testModifyingPKColumnNotAllowedViaAlterTable() throws
Exception {
+ try (Connection conn = DriverManager.getConnection(getUrl())) {
+ conn.createStatement().execute("CREATE TABLE " +
dataTableFullName +
+ " (PK1 DATE NOT NULL PRIMARY KEY, PK2 VARCHAR , KV1
VARCHAR) " + tableDDLOptions);
+ try {
+ conn.createStatement().execute("ALTER TABLE " +
dataTableFullName + " modify PK1 BIGINT");
+ fail("Altering table to modify a PK column should fail");
+ } catch (SQLException e) {
+
assertEquals(SQLExceptionCode.DISALLOW_MODIFY_TIMESTAMP_OR_PK_COLUMN.getErrorCode(),
e.getErrorCode());
+ }
+ conn.close();
+ }
+ }
+
+ @Test
+ public void testQueryAfterModifiedColumn() throws Exception {
+ Properties props = new Properties();
+ String tableName = generateUniqueName();
+ String indexName = "IDX_" + tableName;
+ try (Connection conn = DriverManager.getConnection(getUrl(),
props)) {
+ conn.setAutoCommit(true);
+ conn.createStatement().execute("CREATE TABLE " + tableName
+ + " (a VARCHAR(5), b VARCHAR(5), CONSTRAINT PK PRIMARY
KEY (a))");
+ conn.createStatement().execute("CREATE INDEX " + indexName + "
ON " + tableName + " (b)");
+ conn.createStatement().execute("UPSERT INTO " + tableName + "
Values('a','12345')");
+ conn.createStatement().execute("UPSERT INTO " + tableName + "
Values('b','13555')");
+ conn.createStatement().execute("UPSERT INTO " + tableName + "
Values('c','13666')");
+ conn.commit();
+ conn.createStatement().execute("ALTER TABLE " + tableName + "
modify b VARCHAR(2)");
+ conn.createStatement().execute("UPSERT INTO " + tableName + "
Values('d','13')");
+
+ {
+ ResultSet rs = conn.createStatement().executeQuery("SELECT
/*+ NO_INDEX*/ b from " + tableName + " WHERE b='13'");
+ assertTrue(rs.next());
+ assertEquals("13", rs.getString(1));
+ }
+
+ {
+ ResultSet rs = conn.createStatement().executeQuery("SELECT
/*+ NO_INDEX*/ b from " + tableName + " WHERE b='13555'");
+ assertTrue(rs.next());
+ assertEquals("13555", rs.getString(1));
+ }
+
+ {
+ ResultSet rs = conn.createStatement().executeQuery("SELECT
\"0:B\" from " + indexName + " WHERE \"0:B\"='13555'");
+ assertTrue(rs.next());
+ assertEquals("13555", rs.getString(1));
+ }
+
+ {
+ ResultSet rs = conn.createStatement().executeQuery("SELECT
\"0:B\" from " + indexName + " WHERE \"0:B\"='13'");
+ assertTrue(rs.next());
+ assertEquals("13", rs.getString(1));
+ }
+ }
+ }
+
+ @Test
+ public void testModifiedCharType() throws Exception {
+ Properties props = new Properties();
+ String tableName = generateUniqueName();
+ try (Connection conn = DriverManager.getConnection(getUrl(),
props)) {
+ conn.setAutoCommit(true);
+ conn.createStatement().execute("CREATE TABLE " + tableName + "
(a char(5), b char(5), CONSTRAINT PK PRIMARY KEY (a))");
+ conn.createStatement().execute("ALTER TABLE " + tableName + "
modify b char(2)");
+ } catch (SQLException e) {
+
assertEquals(SQLExceptionCode.DISALLOW_DECREASE_CHAR_LENGTH.getErrorCode(),
e.getErrorCode());
+ }
+ }
+
+
+ @Test
+ public void testModifyColumnWithIndexTables() throws Exception {
+ String schemaName = generateUniqueName();
+ String baseTableName = generateUniqueName();
+ String tableName = schemaName + "." + baseTableName;
+ Properties props = new Properties();
+ props.put(QueryServices.IS_NAMESPACE_MAPPING_ENABLED,
Boolean.toString(true));
+ try (Connection conn = DriverManager.getConnection(getUrl(),
props)) {
+ conn.setAutoCommit(true);
+ conn.createStatement().execute("CREATE SCHEMA " + schemaName);
+ conn.createStatement().execute("CREATE TABLE " + tableName +
+ " (ID CHAR(3) NOT NULL, COL1 VARCHAR(5), COL2
VARCHAR(3), COL3 CHAR(4), COL4 CHAR(4)"
+ + " CONSTRAINT PKVIEW PRIMARY KEY(ID, COL1)) " +
tableDDLOptions);
+ conn.createStatement().execute("CREATE INDEX IDX_COL2 ON "+
tableName + "(COL2) INCLUDE(COL3, COL4)");
+
+ conn.createStatement().executeUpdate("UPSERT INTO " +
tableName + " VALUES('123','12345','123','1234','1444')");
+
+ //Increasing char length
+ conn.createStatement().execute("ALTER TABLE " + tableName + "
modify COL3 CHAR(6)");
+ //Increasing char length of the indexed column
+ //Indexed columns will convert fixed length columns to
variable length while creating the index,
+ //but length/scale of field still had been reserved old values.
+ conn.createStatement().execute("ALTER TABLE " + tableName + "
modify COL2 CHAR(5)");
+
+ conn.createStatement().executeUpdate("UPSERT INTO " +
tableName + " VALUES('124','12345','12345','123456','12')");
+
+ PhoenixConnection phxConn =
conn.unwrap(PhoenixConnection.class);
+ PTable table = phxConn.getTable(new
PTableKey(phxConn.getTenantId(), tableName));
+ assertColumnModify("COL3", table.getSchemaName().getString(),
+ table.getTableName().getString(), 6);
+ assertColumnModify(QueryConstants.DEFAULT_COLUMN_FAMILY + ":"
+ "COL3", table.getSchemaName().getString(),
+ "IDX_COL2", 6);
+ //char length of the indexed column will not change
+ assertColumnModify(QueryConstants.DEFAULT_COLUMN_FAMILY + ":"
+ "COL2", table.getSchemaName().getString(),
+ "IDX_COL2", 3);
+
+ {
+
assertEquals(QueryUtil.getExplainPlan(conn.createStatement().executeQuery("
explain select * from " + tableName)),
+ "CLIENT PARALLEL 1-WAY FULL SCAN OVER " +
schemaName + ":IDX_COL2");
+
+ // Getting query results by index table
+ ResultSet rs = conn.createStatement().executeQuery("select
* from " + tableName);
+ assertTrue(rs.next());
+ assertEquals("123", rs.getString(1));
+ assertEquals("12345", rs.getString(2));
+ // The length of column col2 added 2,
+ assertEquals("123 ", rs.getString(3));
+ // The length of column col3 added 2
+ assertEquals("1234 ", rs.getString(4));
+ assertEquals("1444", rs.getString(5));
+ assertTrue(rs.next());
+ assertEquals("124", rs.getString(1));
+ assertEquals("12345", rs.getString(2));
+ assertEquals("12345", rs.getString(3));
+ assertEquals("123456", rs.getString(4));
+ assertEquals("12 ", rs.getString(5));
+ assertFalse(rs.next());
+ rs.close();
+ }
+
+ {
+ ResultSet rs = conn.createStatement().executeQuery("select
* from " + schemaName + ".IDX_COL2");
+ assertTrue(rs.next());
+ // Col2 as a index column that length of type was changed,
+ // but the result length will not change.
+ assertEquals("123", rs.getString(1));
+ assertEquals("123", rs.getString(2));
+ assertEquals("12345", rs.getString(3));
+ // Col3 as a covered column which the length of type was
increased,
+ // the result length has increased too.
+ assertEquals("1234 ", rs.getString(4));
+ assertEquals("1444", rs.getString(5));
+ assertTrue(rs.next());
+ assertEquals("12345", rs.getString(1));
+ assertEquals("124", rs.getString(2));
+ assertEquals("12345", rs.getString(3));
+ assertEquals("123456", rs.getString(4));
+ assertEquals("12 ", rs.getString(5));
+ assertFalse(rs.next());
+ rs.close();
+ }
+
+ conn.close();
+ }
--- End diff --
Can you also test a point look-up to verify it works "SELECT ... WHERE
COL3='...' "
---