Skip to content

Commit

Permalink
HIVE-19871 add_partitions, create constraint calls and create_table_w…
Browse files Browse the repository at this point in the history
…ith_constraints do not properly handle client being configured with a non-Hive catalog (Alan Gates reviewed by Daniel Dai)
  • Loading branch information
Alan Gates committed Jun 14, 2018
1 parent c4803e9 commit f41acae
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,25 @@ private void create_table_core(final RawStore ms, final Table tbl,
&& checkConstraints == null) {
ms.createTable(tbl);
} else {
// Check that constraints have catalog name properly set first
if (primaryKeys != null && !primaryKeys.isEmpty() && !primaryKeys.get(0).isSetCatName()) {
for (SQLPrimaryKey pkcol : primaryKeys) pkcol.setCatName(tbl.getCatName());
}
if (foreignKeys != null && !foreignKeys.isEmpty() && !foreignKeys.get(0).isSetCatName()) {
for (SQLForeignKey fkcol : foreignKeys) fkcol.setCatName(tbl.getCatName());
}
if (uniqueConstraints != null && !uniqueConstraints.isEmpty() && !uniqueConstraints.get(0).isSetCatName()) {
for (SQLUniqueConstraint uccol : uniqueConstraints) uccol.setCatName(tbl.getCatName());
}
if (notNullConstraints != null && !notNullConstraints.isEmpty() && !notNullConstraints.get(0).isSetCatName()) {
for (SQLNotNullConstraint nncol : notNullConstraints) nncol.setCatName(tbl.getCatName());
}
if (defaultConstraints != null && !defaultConstraints.isEmpty() && !defaultConstraints.get(0).isSetCatName()) {
for (SQLDefaultConstraint dccol : defaultConstraints) dccol.setCatName(tbl.getCatName());
}
if (checkConstraints != null && !checkConstraints.isEmpty() && !checkConstraints.get(0).isSetCatName()) {
for (SQLCheckConstraint cccol : checkConstraints) cccol.setCatName(tbl.getCatName());
}
// Set constraint name if null before sending to listener
List<String> constraintNames = ms.createTableWithConstraints(tbl, primaryKeys, foreignKeys,
uniqueConstraints, notNullConstraints, defaultConstraints, checkConstraints);
Expand All @@ -1882,6 +1901,7 @@ private void create_table_core(final RawStore ms, final Table tbl,
if (primaryKeys.get(i).getPk_name() == null) {
primaryKeys.get(i).setPk_name(constraintNames.get(i));
}
if (!primaryKeys.get(i).isSetCatName()) primaryKeys.get(i).setCatName(tbl.getCatName());
}
}
int foreignKeySize = 0;
Expand All @@ -1891,6 +1911,7 @@ private void create_table_core(final RawStore ms, final Table tbl,
if (foreignKeys.get(i).getFk_name() == null) {
foreignKeys.get(i).setFk_name(constraintNames.get(primaryKeySize + i));
}
if (!foreignKeys.get(i).isSetCatName()) foreignKeys.get(i).setCatName(tbl.getCatName());
}
}
int uniqueConstraintSize = 0;
Expand All @@ -1900,6 +1921,7 @@ private void create_table_core(final RawStore ms, final Table tbl,
if (uniqueConstraints.get(i).getUk_name() == null) {
uniqueConstraints.get(i).setUk_name(constraintNames.get(primaryKeySize + foreignKeySize + i));
}
if (!uniqueConstraints.get(i).isSetCatName()) uniqueConstraints.get(i).setCatName(tbl.getCatName());
}
}
int notNullConstraintSize = 0;
Expand All @@ -1908,6 +1930,7 @@ private void create_table_core(final RawStore ms, final Table tbl,
if (notNullConstraints.get(i).getNn_name() == null) {
notNullConstraints.get(i).setNn_name(constraintNames.get(primaryKeySize + foreignKeySize + uniqueConstraintSize + i));
}
if (!notNullConstraints.get(i).isSetCatName()) notNullConstraints.get(i).setCatName(tbl.getCatName());
}
}
int defaultConstraintSize = 0;
Expand All @@ -1917,6 +1940,7 @@ private void create_table_core(final RawStore ms, final Table tbl,
defaultConstraints.get(i).setDc_name(constraintNames.get(primaryKeySize + foreignKeySize
+ uniqueConstraintSize + notNullConstraintSize + i));
}
if (!defaultConstraints.get(i).isSetCatName()) defaultConstraints.get(i).setCatName(tbl.getCatName());
}
}
if (checkConstraints!= null) {
Expand All @@ -1927,6 +1951,7 @@ private void create_table_core(final RawStore ms, final Table tbl,
+ defaultConstraintSize
+ notNullConstraintSize + i));
}
if (!checkConstraints.get(i).isSetCatName()) checkConstraints.get(i).setCatName(tbl.getCatName());
}
}
}
Expand Down Expand Up @@ -2109,6 +2134,10 @@ public void add_primary_key(AddPrimaryKeyRequest req)
startFunction("add_primary_key", ": " + constraintName);
boolean success = false;
Exception ex = null;
if (!primaryKeyCols.isEmpty() && !primaryKeyCols.get(0).isSetCatName()) {
String defaultCat = getDefaultCatalog(conf);
primaryKeyCols.forEach(pk -> pk.setCatName(defaultCat));
}
RawStore ms = getMS();
try {
ms.openTransaction();
Expand Down Expand Up @@ -2161,6 +2190,10 @@ public void add_foreign_key(AddForeignKeyRequest req)
startFunction("add_foreign_key", ": " + constraintName);
boolean success = false;
Exception ex = null;
if (!foreignKeyCols.isEmpty() && !foreignKeyCols.get(0).isSetCatName()) {
String defaultCat = getDefaultCatalog(conf);
foreignKeyCols.forEach(pk -> pk.setCatName(defaultCat));
}
RawStore ms = getMS();
try {
ms.openTransaction();
Expand Down Expand Up @@ -2213,6 +2246,10 @@ public void add_unique_constraint(AddUniqueConstraintRequest req)
startFunction("add_unique_constraint", ": " + constraintName);
boolean success = false;
Exception ex = null;
if (!uniqueConstraintCols.isEmpty() && !uniqueConstraintCols.get(0).isSetCatName()) {
String defaultCat = getDefaultCatalog(conf);
uniqueConstraintCols.forEach(pk -> pk.setCatName(defaultCat));
}
RawStore ms = getMS();
try {
ms.openTransaction();
Expand Down Expand Up @@ -2265,6 +2302,10 @@ public void add_not_null_constraint(AddNotNullConstraintRequest req)
startFunction("add_not_null_constraint", ": " + constraintName);
boolean success = false;
Exception ex = null;
if (!notNullConstraintCols.isEmpty() && !notNullConstraintCols.get(0).isSetCatName()) {
String defaultCat = getDefaultCatalog(conf);
notNullConstraintCols.forEach(pk -> pk.setCatName(defaultCat));
}
RawStore ms = getMS();
try {
ms.openTransaction();
Expand Down Expand Up @@ -2317,6 +2358,10 @@ public void add_default_constraint(AddDefaultConstraintRequest req)
startFunction("add_default_constraint", ": " + constraintName);
boolean success = false;
Exception ex = null;
if (!defaultConstraintCols.isEmpty() && !defaultConstraintCols.get(0).isSetCatName()) {
String defaultCat = getDefaultCatalog(conf);
defaultConstraintCols.forEach(pk -> pk.setCatName(defaultCat));
}
RawStore ms = getMS();
try {
ms.openTransaction();
Expand Down Expand Up @@ -2370,6 +2415,10 @@ public void add_check_constraint(AddCheckConstraintRequest req)
startFunction("add_check_constraint", ": " + constraintName);
boolean success = false;
Exception ex = null;
if (!checkConstraintCols.isEmpty() && !checkConstraintCols.get(0).isSetCatName()) {
String defaultCat = getDefaultCatalog(conf);
checkConstraintCols.forEach(pk -> pk.setCatName(defaultCat));
}
RawStore ms = getMS();
try {
ms.openTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@ public List<Partition> add_partitions(
return needResults ? new ArrayList<>() : null;
}
Partition part = parts.get(0);
// Have to set it for each partition too
if (!part.isSetCatName()) {
final String defaultCat = getDefaultCatalog(conf);
parts.forEach(p -> p.setCatName(defaultCat));
}
AddPartitionsRequest req = new AddPartitionsRequest(
part.getDbName(), part.getTableName(), parts, ifNotExists);
req.setCatName(part.isSetCatName() ? part.getCatName() : getDefaultCatalog(conf));
Expand Down

0 comments on commit f41acae

Please sign in to comment.