Skip to content

Commit

Permalink
HIVE-26062: Make sure that running 4.0.0-alpha-2 above 4.0.0-alpha-1 …
Browse files Browse the repository at this point in the history
…HMS DB reports the correct error (Zhihua Deng reviewed by Peter Vary) (apache#3138)
  • Loading branch information
dengzhhu653 committed Apr 2, 2022
1 parent 4641f6e commit 8d0365f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.google.common.collect.ImmutableMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hive.metastore.tools.schematool.HiveSchemaHelper;
import org.apache.hadoop.hive.metastore.tools.schematool.HiveSchemaHelper.MetaStoreConnectionInfo;
import org.apache.hadoop.hive.metastore.utils.MetastoreVersionInfo;
Expand Down Expand Up @@ -205,19 +206,30 @@ public boolean isVersionCompatible(String hiveVersion, String dbVersion) {
return false;
}

for (int i = 0; i < dbVerParts.length; i++) {
int dbVerPart = Integer.parseInt(dbVerParts[i]);
int hiveVerPart = Integer.parseInt(hiveVerParts[i]);
if (dbVerPart > hiveVerPart) {
return true;
} else if (dbVerPart < hiveVerPart) {
return false;
} else {
continue; // compare next part
hiveVerParts = hiveVersion.split("\\.|-");
dbVerParts = dbVersion.split("\\.|-");
for (int i = 0; i < Math.min(hiveVerParts.length, dbVerParts.length); i++) {
int compare = compareVersion(dbVerParts[i], hiveVerParts[i]);
if (compare != 0) {
return compare > 0;
}
}
return hiveVerParts.length > dbVerParts.length;
}

return true;
private int compareVersion(String dbVerPart, String hiveVerPart) {
if (dbVerPart.equals(hiveVerPart)) {
return 0;
}
boolean isDbVerNum = StringUtils.isNumeric(dbVerPart);
boolean isHiveVerNum = StringUtils.isNumeric(hiveVerPart);
if (isDbVerNum && isHiveVerNum) {
return Integer.parseInt(dbVerPart) - Integer.parseInt(hiveVerPart);
} else if (!isDbVerNum && !isHiveVerNum) {
return dbVerPart.compareTo(hiveVerPart);
}
// return -1 for one is a number but the other is a string
return -1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,24 @@ public void testIsVersionCompatible() throws Exception {
Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("1.0.2", "2.0.1"));
Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("0.0.9", "9.0.0"));

Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("4.0.0-alpha-1", "4.0.0-alpha-2"));
Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("4.0.0-alpha-1", "4.0.0-alpha"));
Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("4.0.0-alpha-1", "4.0.0"));
Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("4.0.0-alpha-1", "4.0.1"));
Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("4.0.0-alpha-1", "4.0.0-beta"));

// check equivalent versions, should be compatible
Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("0.13.0", "0.13.1"));
Assert.assertTrue(metastoreSchemaInfo.isVersionCompatible("0.13.1", "0.13.0"));

// check incompatible versions
Assert.assertFalse(metastoreSchemaInfo.isVersionCompatible("0.1.1", "0.1.0"));
Assert.assertFalse(metastoreSchemaInfo.isVersionCompatible("4.0.1", "0.1.0"));

Assert.assertFalse(metastoreSchemaInfo.isVersionCompatible("4.0.1", "4.0.0-alpha-1"));
Assert.assertFalse(metastoreSchemaInfo.isVersionCompatible("4.0.0", "4.0.0-alpha-1"));
Assert.assertFalse(metastoreSchemaInfo.isVersionCompatible("4.0.0-alpha-2", "4.0.0-alpha-1"));
Assert.assertFalse(metastoreSchemaInfo.isVersionCompatible("4.0.0-alpha", "4.0.0-alpha-1"));
Assert.assertFalse(metastoreSchemaInfo.isVersionCompatible("4.0.0-beta", "4.0.0-alpha-1"));
}

}

0 comments on commit 8d0365f

Please sign in to comment.