Search in sources :

Example 1 with ColumnNotFoundException

use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.

the class DropColumnIT method testDropCol.

@Test
public void testDropCol() throws Exception {
    String indexTableName = generateUniqueName();
    String dataTableName = generateUniqueName();
    String localIndexTableName = "LOCAL_" + indexTableName;
    try (Connection conn = getConnection()) {
        conn.setAutoCommit(false);
        conn.createStatement().execute("CREATE TABLE " + dataTableName + " (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR, v3 VARCHAR) " + tableDDLOptions);
        // create one global and one local index
        conn.createStatement().execute("CREATE INDEX " + indexTableName + " ON " + dataTableName + " (v1) INCLUDE (v2, v3)");
        conn.createStatement().execute("CREATE LOCAL INDEX " + localIndexTableName + " ON " + dataTableName + " (v1) INCLUDE (v2, v3)");
        // upsert a single row
        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + dataTableName + " VALUES(?,?,?,?)");
        stmt.setString(1, "a");
        stmt.setString(2, "x");
        stmt.setString(3, "1");
        stmt.setString(4, "2");
        stmt.execute();
        conn.commit();
        // verify v2 exists in the data table
        PTable dataTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, dataTableName));
        PColumn dataColumn = dataTable.getColumnForColumnName("V2");
        byte[] dataCq = dataColumn.getColumnQualifierBytes();
        // verify v2 exists in the global index table
        PTable globalIndexTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexTableName));
        PColumn glovalIndexCol = globalIndexTable.getColumnForColumnName("0:V2");
        byte[] globalIndexCq = glovalIndexCol.getColumnQualifierBytes();
        // verify v2 exists in the global index table
        PTable localIndexTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, localIndexTableName));
        PColumn localIndexCol = localIndexTable.getColumnForColumnName("0:V2");
        byte[] localIndexCq = localIndexCol.getColumnQualifierBytes();
        verifyColValue(indexTableName, dataTableName, conn, dataTable, dataColumn, dataCq, globalIndexTable, glovalIndexCol, globalIndexCq, localIndexTable, localIndexCol, localIndexCq);
        // drop v2 column
        conn.createStatement().execute("ALTER TABLE " + dataTableName + " DROP COLUMN v2 ");
        conn.createStatement().execute("SELECT * FROM " + dataTableName);
        // verify that the column was dropped from the data table
        dataTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, dataTableName));
        try {
            dataTable.getColumnForColumnName("V2");
            fail("Column V2 should have been dropped from data table");
        } catch (ColumnNotFoundException e) {
        }
        // verify that the column was dropped from the global index table
        globalIndexTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexTableName));
        try {
            globalIndexTable.getColumnForColumnName("V2");
            fail("Column V2 should have been dropped from global index table");
        } catch (ColumnNotFoundException e) {
        }
        // verify that the column was dropped from the local index table
        localIndexTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexTableName));
        try {
            localIndexTable.getColumnForColumnName("V2");
            fail("Column V2 should have been dropped from global index table");
        } catch (ColumnNotFoundException e) {
        }
        if (mutable || !columnEncoded) {
            byte[] key = Bytes.toBytes("a");
            Scan scan = new Scan();
            scan.setRaw(true);
            scan.setStartRow(key);
            scan.setStopRow(key);
            HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(dataTableName.getBytes());
            ResultScanner results = table.getScanner(scan);
            Result result = results.next();
            assertNotNull(result);
            assertEquals("data table column value should have been deleted", KeyValue.Type.DeleteColumn.getCode(), result.getColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, dataCq).get(0).getTypeByte());
            assertNull(results.next());
            // key value for v2 should have been deleted from the global index table
            scan = new Scan();
            scan.setRaw(true);
            table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(indexTableName.getBytes());
            results = table.getScanner(scan);
            result = results.next();
            assertNotNull(result);
            assertEquals("data table column value should have been deleted", KeyValue.Type.DeleteColumn.getCode(), result.getColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, globalIndexCq).get(0).getTypeByte());
            assertNull(results.next());
            // key value for v2 should have been deleted from the local index table
            scan = new Scan();
            scan.setRaw(true);
            scan.addFamily(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES);
            table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(dataTableName.getBytes());
            results = table.getScanner(scan);
            result = results.next();
            assertNotNull(result);
            assertEquals("data table col" + "umn value should have been deleted", KeyValue.Type.DeleteColumn.getCode(), result.getColumn(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES, localIndexCq).get(0).getTypeByte());
            assertNull(results.next());
        } else {
            // verify we don't issue deletes when we drop a column from an immutable encoded table
            verifyColValue(indexTableName, dataTableName, conn, dataTable, dataColumn, dataCq, globalIndexTable, glovalIndexCol, globalIndexCq, localIndexTable, localIndexCol, localIndexCq);
        }
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PreparedStatement(java.sql.PreparedStatement) HTable(org.apache.hadoop.hbase.client.HTable) PTable(org.apache.phoenix.schema.PTable) Result(org.apache.hadoop.hbase.client.Result) PColumn(org.apache.phoenix.schema.PColumn) ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) Scan(org.apache.hadoop.hbase.client.Scan) PTableKey(org.apache.phoenix.schema.PTableKey) Test(org.junit.Test)

Example 2 with ColumnNotFoundException

use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.

the class QueryCompilerTest method testFailNoFromClauseSelect.

@Test
public void testFailNoFromClauseSelect() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    try {
        try {
            conn.createStatement().executeQuery("SELECT foo, bar");
            fail("Should have got ColumnNotFoundException");
        } catch (ColumnNotFoundException e) {
        }
        try {
            conn.createStatement().executeQuery("SELECT *");
            fail("Should have got SQLException");
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.NO_TABLE_SPECIFIED_FOR_WILDCARD_SELECT.getErrorCode(), e.getErrorCode());
        }
        try {
            conn.createStatement().executeQuery("SELECT A.*");
            fail("Should have got SQLException");
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.NO_TABLE_SPECIFIED_FOR_WILDCARD_SELECT.getErrorCode(), e.getErrorCode());
        }
    } finally {
        conn.close();
    }
}
Also used : ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 3 with ColumnNotFoundException

use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.

the class AlterMultiTenantTableWithViewsIT method testAddDropColumnToBaseTablePropagatesToEntireViewHierarchy.

@Test
public void testAddDropColumnToBaseTablePropagatesToEntireViewHierarchy() throws Exception {
    String baseTable = "testViewHierarchy";
    String baseViewName = generateUniqueName();
    String view1 = baseViewName + "_VIEW1";
    String view2 = baseViewName + "_VIEW2";
    String view3 = baseViewName + "_VIEW3";
    String view4 = baseViewName + "_VIEW4";
    /*                                     baseTable
                                 /                  |               \ 
                         view1(tenant1)    view3(tenant2)          view4(global)
                          /
                        view2(tenant1)  
        */
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        String baseTableDDL = "CREATE TABLE " + baseTable + " (TENANT_ID VARCHAR NOT NULL, PK1 VARCHAR NOT NULL, V1 VARCHAR, V2 VARCHAR CONSTRAINT NAME_PK PRIMARY KEY(TENANT_ID, PK1)) MULTI_TENANT = true ";
        conn.createStatement().execute(baseTableDDL);
        try (Connection tenant1Conn = getTenantConnection("tenant1")) {
            String view1DDL = "CREATE VIEW " + view1 + " AS SELECT * FROM " + baseTable;
            tenant1Conn.createStatement().execute(view1DDL);
            String view2DDL = "CREATE VIEW " + view2 + " AS SELECT * FROM " + view1;
            tenant1Conn.createStatement().execute(view2DDL);
        }
        try (Connection tenant2Conn = getTenantConnection("tenant2")) {
            String view3DDL = "CREATE VIEW " + view3 + " AS SELECT * FROM " + baseTable;
            tenant2Conn.createStatement().execute(view3DDL);
        }
        String view4DDL = "CREATE VIEW " + view4 + " AS SELECT * FROM " + baseTable;
        conn.createStatement().execute(view4DDL);
        String alterBaseTable = "ALTER TABLE " + baseTable + " ADD V3 VARCHAR";
        conn.createStatement().execute(alterBaseTable);
        // verify that the column is visible to view4
        conn.createStatement().execute("SELECT V3 FROM " + view4);
        // verify that the column is visible to view1 and view2
        try (Connection tenant1Conn = getTenantConnection("tenant1")) {
            tenant1Conn.createStatement().execute("SELECT V3 from " + view1);
            tenant1Conn.createStatement().execute("SELECT V3 from " + view2);
        }
        // verify that the column is visible to view3
        try (Connection tenant2Conn = getTenantConnection("tenant2")) {
            tenant2Conn.createStatement().execute("SELECT V3 from " + view3);
        }
        alterBaseTable = "ALTER TABLE " + baseTable + " DROP COLUMN V1";
        conn.createStatement().execute(alterBaseTable);
        // verify that the column is not visible to view4
        try {
            conn.createStatement().execute("SELECT V1 FROM " + view4);
            fail();
        } catch (ColumnNotFoundException e) {
        }
        // verify that the column is not visible to view1 and view2
        try (Connection tenant1Conn = getTenantConnection("tenant1")) {
            try {
                tenant1Conn.createStatement().execute("SELECT V1 from " + view1);
                fail();
            } catch (ColumnNotFoundException e) {
            }
            try {
                tenant1Conn.createStatement().execute("SELECT V1 from " + view2);
                fail();
            } catch (ColumnNotFoundException e) {
            }
        }
        // verify that the column is not visible to view3
        try (Connection tenant2Conn = getTenantConnection("tenant2")) {
            try {
                tenant2Conn.createStatement().execute("SELECT V1 from " + view3);
                fail();
            } catch (ColumnNotFoundException e) {
            }
        }
    }
}
Also used : ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Test(org.junit.Test)

Example 4 with ColumnNotFoundException

use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.

the class IndexUtil method getDataColumn.

public static PColumn getDataColumn(PTable dataTable, String indexColumnName) {
    int pos = indexColumnName.indexOf(INDEX_COLUMN_NAME_SEP);
    if (pos < 0) {
        throw new IllegalArgumentException("Could not find expected '" + INDEX_COLUMN_NAME_SEP + "' separator in index column name of \"" + indexColumnName + "\"");
    }
    if (pos == 0) {
        try {
            return dataTable.getPKColumn(indexColumnName.substring(1));
        } catch (ColumnNotFoundException e) {
            throw new IllegalArgumentException("Could not find PK column \"" + indexColumnName.substring(pos + 1) + "\" in index column name of \"" + indexColumnName + "\"", e);
        }
    }
    PColumnFamily family;
    try {
        family = dataTable.getColumnFamily(getDataColumnFamilyName(indexColumnName));
    } catch (ColumnFamilyNotFoundException e) {
        throw new IllegalArgumentException("Could not find column family \"" + indexColumnName.substring(0, pos) + "\" in index column name of \"" + indexColumnName + "\"", e);
    }
    try {
        return family.getPColumnForColumnName(indexColumnName.substring(pos + 1));
    } catch (ColumnNotFoundException e) {
        throw new IllegalArgumentException("Could not find column \"" + indexColumnName.substring(pos + 1) + "\" in index column name of \"" + indexColumnName + "\"", e);
    }
}
Also used : ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) PColumnFamily(org.apache.phoenix.schema.PColumnFamily) ColumnFamilyNotFoundException(org.apache.phoenix.schema.ColumnFamilyNotFoundException)

Example 5 with ColumnNotFoundException

use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.

the class ViewCompilerTest method testViewInvalidation.

@Test
public void testViewInvalidation() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    PhoenixConnection conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
    String ct = "CREATE TABLE s1.t (k1 INTEGER NOT NULL, k2 VARCHAR, v VARCHAR, CONSTRAINT pk PRIMARY KEY (k1,k2))";
    conn.createStatement().execute(ct);
    conn.createStatement().execute("CREATE VIEW s2.v3 AS SELECT * FROM s1.t WHERE v = 'bar'");
    // TODO: should it be an error to remove columns from a VIEW that we're defined there?
    conn.createStatement().execute("ALTER VIEW s2.v3 DROP COLUMN v");
    try {
        conn.createStatement().executeQuery("SELECT * FROM s2.v3");
        fail();
    } catch (ColumnNotFoundException e) {
    }
    // No error, as v still exists in t
    conn.createStatement().execute("CREATE VIEW s2.v4 AS SELECT * FROM s1.t WHERE v = 'bas'");
    // No error, even though view is invalid
    conn.createStatement().execute("DROP VIEW s2.v3");
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) Properties(java.util.Properties) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

ColumnNotFoundException (org.apache.phoenix.schema.ColumnNotFoundException)30 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)16 PTable (org.apache.phoenix.schema.PTable)14 Connection (java.sql.Connection)13 PColumn (org.apache.phoenix.schema.PColumn)13 ColumnFamilyNotFoundException (org.apache.phoenix.schema.ColumnFamilyNotFoundException)12 Test (org.junit.Test)11 ColumnRef (org.apache.phoenix.schema.ColumnRef)7 PColumnFamily (org.apache.phoenix.schema.PColumnFamily)7 ByteString (com.google.protobuf.ByteString)6 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 Properties (java.util.Properties)6 Mutation (org.apache.hadoop.hbase.client.Mutation)6 RowLock (org.apache.hadoop.hbase.regionserver.Region.RowLock)6 ResultSet (java.sql.ResultSet)5 List (java.util.List)5 Delete (org.apache.hadoop.hbase.client.Delete)4 Expression (org.apache.phoenix.expression.Expression)4 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)4