Search in sources :

Example 26 with SQLNotNullConstraint

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

the class TestNotNullConstraint method createTableWithConstraintsPkInOtherCatalog.

@Test
public void createTableWithConstraintsPkInOtherCatalog() throws TException {
    Table table = new TableBuilder().setTableName("table_in_other_catalog_with_constraints").inDb(inOtherCatalog).addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
    List<SQLNotNullConstraint> nn = new SQLNotNullConstraintBuilder().onTable(table).addColumn("col1").build(metaStore.getConf());
    client.createTableWithConstraints(table, null, null, null, nn, null, null);
    NotNullConstraintsRequest rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLNotNullConstraint> fetched = client.getNotNullConstraints(rqst);
    nn.get(0).setNn_name(fetched.get(0).getNn_name());
    Assert.assertEquals(nn, fetched);
    client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), nn.get(0).getNn_name());
    rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    fetched = client.getNotNullConstraints(rqst);
    Assert.assertTrue(fetched.isEmpty());
}
Also used : SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) SQLNotNullConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) NotNullConstraintsRequest(org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 27 with SQLNotNullConstraint

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

the class TestNotNullConstraint method createGetDrop.

@Test
public void createGetDrop() throws TException {
    Table table = testTables[0];
    // Make sure get on a table with no key returns empty list
    NotNullConstraintsRequest rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLNotNullConstraint> fetched = client.getNotNullConstraints(rqst);
    Assert.assertTrue(fetched.isEmpty());
    // Single column unnamed primary key in default catalog and database
    List<SQLNotNullConstraint> nn = new SQLNotNullConstraintBuilder().onTable(table).addColumn("col1").build(metaStore.getConf());
    client.addNotNullConstraint(nn);
    rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    fetched = client.getNotNullConstraints(rqst);
    nn.get(0).setNn_name(fetched.get(0).getNn_name());
    Assert.assertEquals(nn, fetched);
    // Drop a primary key
    client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), nn.get(0).getNn_name());
    rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    fetched = client.getNotNullConstraints(rqst);
    Assert.assertTrue(fetched.isEmpty());
    // Make sure I can add it back
    client.addNotNullConstraint(nn);
}
Also used : SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) SQLNotNullConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) NotNullConstraintsRequest(org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 28 with SQLNotNullConstraint

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

the class ObjectStore method getNotNullConstraintsViaJdo.

private List<SQLNotNullConstraint> getNotNullConstraintsViaJdo(String catName, String dbName, String tblName) {
    boolean commited = false;
    List<SQLNotNullConstraint> notNullConstraints = null;
    Query query = null;
    try {
        openTransaction();
        query = pm.newQuery(MConstraint.class, "parentTable.tableName == tbl_name && parentTable.database.name == db_name &&" + " parentTable.database.catalogName == catName && constraintType == MConstraint.NOT_NULL_CONSTRAINT");
        query.declareParameters("java.lang.String tbl_name, java.lang.String db_name, java.lang.String catName");
        Collection<?> constraints = (Collection<?>) query.execute(tblName, dbName, catName);
        pm.retrieveAll(constraints);
        notNullConstraints = new ArrayList<>();
        for (Iterator<?> i = constraints.iterator(); i.hasNext(); ) {
            MConstraint currConstraint = (MConstraint) i.next();
            List<MFieldSchema> cols = currConstraint.getParentColumn() != null ? currConstraint.getParentColumn().getCols() : currConstraint.getParentTable().getPartitionKeys();
            int enableValidateRely = currConstraint.getEnableValidateRely();
            boolean enable = (enableValidateRely & 4) != 0;
            boolean validate = (enableValidateRely & 2) != 0;
            boolean rely = (enableValidateRely & 1) != 0;
            notNullConstraints.add(new SQLNotNullConstraint(catName, dbName, tblName, cols.get(currConstraint.getParentIntegerIndex()).getName(), currConstraint.getConstraintName(), enable, validate, rely));
        }
        commited = commitTransaction();
    } finally {
        rollbackAndCleanup(commited, query);
    }
    return notNullConstraints;
}
Also used : SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) ScheduledQuery(org.apache.hadoop.hive.metastore.api.ScheduledQuery) Query(javax.jdo.Query) MScheduledQuery(org.apache.hadoop.hive.metastore.model.MScheduledQuery) MFieldSchema(org.apache.hadoop.hive.metastore.model.MFieldSchema) MConstraint(org.apache.hadoop.hive.metastore.model.MConstraint) Collection(java.util.Collection) MConstraint(org.apache.hadoop.hive.metastore.model.MConstraint) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)

Aggregations

SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)28 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)14 Test (org.junit.Test)14 SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)13 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)12 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)11 NotNullConstraintsRequest (org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest)11 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)9 SQLNotNullConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder)9 ArrayList (java.util.ArrayList)8 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)8 Table (org.apache.hadoop.hive.metastore.api.Table)7 TableName (org.apache.hadoop.hive.common.TableName)3 AllTableConstraintsRequest (org.apache.hadoop.hive.metastore.api.AllTableConstraintsRequest)3 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)3 SQLDefaultConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Tree (org.antlr.runtime.tree.Tree)2 Path (org.apache.hadoop.fs.Path)2