Search in sources :

Example 1 with NotNullConstraintsRequest

use of org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest in project hive by apache.

the class TestReplicationScenarios method testConstraints.

@Test
public void testConstraints() throws IOException {
    String testName = "constraints";
    LOG.info("Testing " + testName);
    String dbName = testName + "_" + tid;
    run("CREATE DATABASE " + dbName, driver);
    run("CREATE TABLE " + dbName + ".tbl1(a string, b string, primary key (a, b) disable novalidate rely)", driver);
    run("CREATE TABLE " + dbName + ".tbl2(a string, b string, foreign key (a, b) references " + dbName + ".tbl1(a, b) disable novalidate)", driver);
    run("CREATE TABLE " + dbName + ".tbl3(a string, b string not null disable, unique (a) disable)", driver);
    advanceDumpDir();
    run("REPL DUMP " + dbName, driver);
    String replDumpLocn = getResult(0, 0, driver);
    String replDumpId = getResult(0, 1, true, driver);
    LOG.info("Dumped to {} with id {}", replDumpLocn, replDumpId);
    run("REPL LOAD " + dbName + "_dupe FROM '" + replDumpLocn + "'", driverMirror);
    try {
        List<SQLPrimaryKey> pks = metaStoreClientMirror.getPrimaryKeys(new PrimaryKeysRequest(dbName + "_dupe", "tbl1"));
        assertEquals(pks.size(), 2);
        List<SQLUniqueConstraint> uks = metaStoreClientMirror.getUniqueConstraints(new UniqueConstraintsRequest(dbName + "_dupe", "tbl3"));
        assertEquals(uks.size(), 1);
        List<SQLForeignKey> fks = metaStoreClientMirror.getForeignKeys(new ForeignKeysRequest(null, null, dbName + "_dupe", "tbl2"));
        assertEquals(fks.size(), 2);
        List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(dbName + "_dupe", "tbl3"));
        assertEquals(nns.size(), 1);
    } catch (TException te) {
        assertNull(te);
    }
    run("CREATE TABLE " + dbName + ".tbl4(a string, b string, primary key (a, b) disable novalidate rely)", driver);
    run("CREATE TABLE " + dbName + ".tbl5(a string, b string, foreign key (a, b) references " + dbName + ".tbl4(a, b) disable novalidate)", driver);
    run("CREATE TABLE " + dbName + ".tbl6(a string, b string not null disable, unique (a) disable)", driver);
    advanceDumpDir();
    run("REPL DUMP " + dbName + " FROM " + replDumpId, driver);
    String incrementalDumpLocn = getResult(0, 0, driver);
    String incrementalDumpId = getResult(0, 1, true, driver);
    LOG.info("Dumped to {} with id {}", incrementalDumpLocn, incrementalDumpId);
    run("REPL LOAD " + dbName + "_dupe FROM '" + incrementalDumpLocn + "'", driverMirror);
    String pkName = null;
    String ukName = null;
    String fkName = null;
    String nnName = null;
    try {
        List<SQLPrimaryKey> pks = metaStoreClientMirror.getPrimaryKeys(new PrimaryKeysRequest(dbName + "_dupe", "tbl4"));
        assertEquals(pks.size(), 2);
        pkName = pks.get(0).getPk_name();
        List<SQLUniqueConstraint> uks = metaStoreClientMirror.getUniqueConstraints(new UniqueConstraintsRequest(dbName + "_dupe", "tbl6"));
        assertEquals(uks.size(), 1);
        ukName = uks.get(0).getUk_name();
        List<SQLForeignKey> fks = metaStoreClientMirror.getForeignKeys(new ForeignKeysRequest(null, null, dbName + "_dupe", "tbl5"));
        assertEquals(fks.size(), 2);
        fkName = fks.get(0).getFk_name();
        List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(dbName + "_dupe", "tbl6"));
        assertEquals(nns.size(), 1);
        nnName = nns.get(0).getNn_name();
    } catch (TException te) {
        assertNull(te);
    }
    run("ALTER TABLE " + dbName + ".tbl4 DROP CONSTRAINT `" + pkName + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl4 DROP CONSTRAINT `" + ukName + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl5 DROP CONSTRAINT `" + fkName + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl6 DROP CONSTRAINT `" + nnName + "`", driver);
    advanceDumpDir();
    run("REPL DUMP " + dbName + " FROM " + incrementalDumpId, driver);
    incrementalDumpLocn = getResult(0, 0, driver);
    incrementalDumpId = getResult(0, 1, true, driver);
    LOG.info("Dumped to {} with id {}", incrementalDumpLocn, incrementalDumpId);
    run("REPL LOAD " + dbName + "_dupe FROM '" + incrementalDumpLocn + "'", driverMirror);
    try {
        List<SQLPrimaryKey> pks = metaStoreClientMirror.getPrimaryKeys(new PrimaryKeysRequest(dbName + "_dupe", "tbl4"));
        assertTrue(pks.isEmpty());
        List<SQLUniqueConstraint> uks = metaStoreClientMirror.getUniqueConstraints(new UniqueConstraintsRequest(dbName + "_dupe", "tbl4"));
        assertTrue(uks.isEmpty());
        List<SQLForeignKey> fks = metaStoreClientMirror.getForeignKeys(new ForeignKeysRequest(null, null, dbName + "_dupe", "tbl5"));
        assertTrue(fks.isEmpty());
        List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(dbName + "_dupe", "tbl6"));
        assertTrue(nns.isEmpty());
    } catch (TException te) {
        assertNull(te);
    }
}
Also used : SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) UniqueConstraintsRequest(org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest) TException(org.apache.thrift.TException) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) NotNullConstraintsRequest(org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) ForeignKeysRequest(org.apache.hadoop.hive.metastore.api.ForeignKeysRequest) Test(org.junit.Test)

Aggregations

ForeignKeysRequest (org.apache.hadoop.hive.metastore.api.ForeignKeysRequest)1 NotNullConstraintsRequest (org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest)1 PrimaryKeysRequest (org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest)1 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)1 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)1 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)1 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)1 UniqueConstraintsRequest (org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest)1 TException (org.apache.thrift.TException)1 Test (org.junit.Test)1