Skip to content

Commit 8d63caf

Browse files
authored
HIVE-27193: Database names starting with @ cause error during ALTER/DROP table (#6371)
1 parent 8fdd6c8 commit 8d63caf

2 files changed

Lines changed: 66 additions & 17 deletions

File tree

standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/MetaStoreUtils.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* to you under the Apache License, Version 2.0 (the
77
* "License"); you may not use this file except in compliance
88
* with the License. You may obtain a copy of the License at
9-
* <p>
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
* <p>
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
1212
* Unless required by applicable law or agreed to in writing, software
1313
* distributed under the License is distributed on an "AS IS" BASIS,
1414
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -1027,7 +1027,8 @@ public static WMPoolSchedulingPolicy parseSchedulingPolicy(String schedulingPoli
10271027

10281028
private static boolean hasCatalogName(String dbName) {
10291029
return dbName != null && dbName.length() > 0 &&
1030-
dbName.charAt(0) == CATALOG_DB_THRIFT_NAME_MARKER;
1030+
dbName.charAt(0) == CATALOG_DB_THRIFT_NAME_MARKER &&
1031+
dbName.contains(CATALOG_DB_SEPARATOR);
10311032
}
10321033

10331034
/**
@@ -1092,25 +1093,20 @@ public static String prependCatalogToDbName(String dbName, Configuration conf) {
10921093
* in the database name.
10931094
* @return an array of two elements, the first being the catalog name, the second the database
10941095
* name.
1095-
* @throws MetaException if the name is not either just a database name or a catalog plus
1096-
* database name with the proper delimiters.
10971096
*/
1098-
public static String[] parseDbName(String dbName, Configuration conf) throws MetaException {
1097+
public static String[] parseDbName(String dbName, Configuration conf) {
10991098
if (dbName == null) {
11001099
return Arrays.copyOf(nullCatalogAndDatabase, nullCatalogAndDatabase.length);
11011100
}
11021101
if (hasCatalogName(dbName)) {
1103-
if (dbName.endsWith(CATALOG_DB_SEPARATOR)) {
1104-
// This means the DB name is null
1105-
return new String[] {dbName.substring(1, dbName.length() - 1), null};
1106-
} else if (dbName.endsWith(DB_EMPTY_MARKER)) {
1107-
// This means the DB name is empty
1108-
return new String[] {dbName.substring(1, dbName.length() - DB_EMPTY_MARKER.length() - 1), ""};
1109-
}
11101102
String[] names = dbName.substring(1).split(CATALOG_DB_SEPARATOR, 2);
1111-
if (names.length != 2) {
1112-
throw new MetaException(dbName + " is prepended with the catalog marker but does not " +
1113-
"appear to have a catalog name in it");
1103+
if (names.length == 1) {
1104+
return new String[] {names[0], null};
1105+
}
1106+
if (names[1].isEmpty()) {
1107+
names[1] = null;
1108+
} else if (names[1].equals(DB_EMPTY_MARKER)) {
1109+
names[1] = "";
11141110
}
11151111
return names;
11161112
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hive.metastore.utils;
19+
20+
import org.apache.hadoop.conf.Configuration;
21+
import org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest;
22+
import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
23+
import org.junit.Test;
24+
import org.junit.experimental.categories.Category;
25+
26+
import static org.apache.hadoop.hive.metastore.utils.MetaStoreUtils.parseDbName;
27+
import static org.junit.Assert.assertArrayEquals;
28+
29+
@Category(MetastoreUnitTest.class)
30+
public class TestMetastoreUtilsParseDbName {
31+
32+
@Test
33+
public void testParseDbNameEdgeCases() {
34+
Configuration conf = MetastoreConf.newMetastoreConf();
35+
MetastoreConf.setVar(conf, MetastoreConf.ConfVars.CATALOG_DEFAULT, "hive");
36+
37+
assertArrayEquals(new String[]{"hive", "@"}, parseDbName("@", conf));
38+
assertArrayEquals(new String[]{"hive", "@!"}, parseDbName("@!", conf));
39+
assertArrayEquals(new String[]{"", null}, parseDbName("@#", conf));
40+
assertArrayEquals(new String[]{"", "db1"}, parseDbName("@#db1", conf));
41+
assertArrayEquals(new String[]{"hive", "@cat1"}, parseDbName("@cat1", conf));
42+
assertArrayEquals(new String[]{"cat1", null}, parseDbName("@cat1#", conf));
43+
assertArrayEquals(new String[]{"cat1", ""}, parseDbName("@cat1#!", conf));
44+
assertArrayEquals(new String[]{"cat1", "@db1"}, parseDbName("@cat1#@db1", conf));
45+
assertArrayEquals(new String[]{"cat1", "#db1"}, parseDbName("@cat1##db1", conf));
46+
assertArrayEquals(new String[]{"cat1", "db1"}, parseDbName("@cat1#db1", conf));
47+
assertArrayEquals(new String[]{"cat1", "db1!"}, parseDbName("@cat1#db1!", conf));
48+
assertArrayEquals(new String[]{"hive", "@cat1!"}, parseDbName("@cat1!", conf));
49+
assertArrayEquals(new String[]{"hive", "#db1"}, parseDbName("#db1", conf));
50+
assertArrayEquals(new String[]{"hive", "#!"}, parseDbName("#!", conf));
51+
assertArrayEquals(new String[]{"hive", "#"}, parseDbName("#", conf));
52+
}
53+
}

0 commit comments

Comments
 (0)