diff --git a/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java b/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java index fc0887b..0dd594e 100644 --- a/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java +++ b/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java @@ -99,7 +99,7 @@ public HiveSchemaTool(String hiveHome, HiveConf hiveConf, String dbType, String this.hiveConf = hiveConf; this.dbType = dbType; this.metaDbType = metaDbType; - this.needsQuotedIdentifier = getDbCommandParser(dbType).needsQuotedIdentifier(); + this.needsQuotedIdentifier = getDbCommandParser(dbType, metaDbType).needsQuotedIdentifier(); this.metaStoreSchemaInfo = MetaStoreSchemaInfoFactory.get(hiveConf, hiveHome, dbType); } @@ -151,20 +151,13 @@ private static void printAndExit(Options cmdLineOptions) { System.exit(1); } - Connection getConnectionToMetastore(boolean printInfo) - throws HiveMetaException { - return HiveSchemaHelper.getConnectionToMetastore(userName, - passWord, url, driver, printInfo, hiveConf); + Connection getConnectionToMetastore(boolean printInfo) throws HiveMetaException { + return HiveSchemaHelper.getConnectionToMetastore(userName, passWord, url, driver, printInfo, hiveConf, + null); } private NestedScriptParser getDbCommandParser(String dbType, String metaDbType) { - return HiveSchemaHelper.getDbCommandParser(dbType, dbOpts, userName, - passWord, hiveConf, metaDbType); - } - - private NestedScriptParser getDbCommandParser(String dbType) { - return HiveSchemaHelper.getDbCommandParser(dbType, dbOpts, userName, - passWord, hiveConf, null); + return HiveSchemaHelper.getDbCommandParser(dbType, dbOpts, userName, passWord, hiveConf, metaDbType); } /*** @@ -511,7 +504,7 @@ public void doUpgrade() throws HiveMetaException { private MetaStoreConnectionInfo getConnectionInfo(boolean printInfo) { return new MetaStoreConnectionInfo(userName, passWord, url, driver, printInfo, hiveConf, - dbType); + dbType, metaDbType); } /** * Perform metastore schema upgrade @@ -593,7 +586,7 @@ public void doValidate() throws HiveMetaException { Connection conn = getConnectionToMetastore(false); boolean success = true; try { - if (validateSchemaVersions(conn)) { + if (validateSchemaVersions()) { System.out.println("[SUCCESS]\n"); } else { success = false; @@ -701,7 +694,7 @@ boolean validateSequences(Connection conn) throws HiveMetaException { } } - boolean validateSchemaVersions(Connection conn) throws HiveMetaException { + boolean validateSchemaVersions() throws HiveMetaException { System.out.println("Validating schema version"); try { String newSchemaVersion = metaStoreSchemaInfo.getMetaStoreSchemaVersion(getConnectionInfo(false)); diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java index 25bd71a..100ec91 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java @@ -233,14 +233,14 @@ public void testSchemaInit() throws Exception { */ public void testValidateSchemaVersions() throws Exception { schemaTool.doInit(); - boolean isValid = schemaTool.validateSchemaVersions(conn); + boolean isValid = schemaTool.validateSchemaVersions(); // Test an invalid case with multiple versions String[] scripts = new String[] { "insert into VERSION values(100, '2.2.0', 'Hive release version 2.2.0')" }; File scriptFile = generateTestScript(scripts); schemaTool.runBeeLine(scriptFile.getPath()); - isValid = schemaTool.validateSchemaVersions(conn); + isValid = schemaTool.validateSchemaVersions(); assertFalse(isValid); scripts = new String[] { @@ -248,7 +248,7 @@ public void testValidateSchemaVersions() throws Exception { }; scriptFile = generateTestScript(scripts); schemaTool.runBeeLine(scriptFile.getPath()); - isValid = schemaTool.validateSchemaVersions(conn); + isValid = schemaTool.validateSchemaVersions(); assertTrue(isValid); // Test an invalid case without version @@ -257,7 +257,7 @@ public void testValidateSchemaVersions() throws Exception { }; scriptFile = generateTestScript(scripts); schemaTool.runBeeLine(scriptFile.getPath()); - isValid = schemaTool.validateSchemaVersions(conn); + isValid = schemaTool.validateSchemaVersions(); assertFalse(isValid); } diff --git a/metastore/scripts/upgrade/hive/hive-schema-3.0.0.hive.sql b/metastore/scripts/upgrade/hive/hive-schema-3.0.0.hive.sql index d6e0c5c..84d523e 100644 --- a/metastore/scripts/upgrade/hive/hive-schema-3.0.0.hive.sql +++ b/metastore/scripts/upgrade/hive/hive-schema-3.0.0.hive.sql @@ -1,6 +1,6 @@ -- HIVE system db -DROP DATABASE IF EXISTS SYS; +DROP DATABASE IF EXISTS SYS CASCADE; CREATE DATABASE SYS; USE SYS; @@ -1098,7 +1098,7 @@ LEFT OUTER JOIN WM_POOL ON WM_POOL.POOL_ID = WM_MAPPING.POOL_ID " ); -DROP DATABASE IF EXISTS INFORMATION_SCHEMA; +DROP DATABASE IF EXISTS INFORMATION_SCHEMA CASCADE; CREATE DATABASE INFORMATION_SCHEMA; USE INFORMATION_SCHEMA; diff --git a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreSchemaInfo.java b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreSchemaInfo.java index 0c36855..9b014da 100644 --- a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreSchemaInfo.java +++ b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreSchemaInfo.java @@ -205,19 +205,18 @@ public boolean isVersionCompatible(String hiveVersion, String dbVersion) { } @Override - public String getMetaStoreSchemaVersion(MetaStoreConnectionInfo connectionInfo) - throws HiveMetaException { + public String getMetaStoreSchemaVersion(MetaStoreConnectionInfo connectionInfo) throws HiveMetaException { String versionQuery; boolean needsQuotedIdentifier = - HiveSchemaHelper.getDbCommandParser(connectionInfo.getDbType()).needsQuotedIdentifier(); + HiveSchemaHelper.getDbCommandParser(connectionInfo.getDbType(), connectionInfo.getMetaDbType()).needsQuotedIdentifier(); if (needsQuotedIdentifier) { versionQuery = "select t.\"SCHEMA_VERSION\" from \"VERSION\" t"; } else { versionQuery = "select t.SCHEMA_VERSION from VERSION t"; } - try (Connection metastoreDbConnection = - HiveSchemaHelper.getConnectionToMetastore(connectionInfo); Statement stmt = - metastoreDbConnection.createStatement()) { + String schema = ( HiveSchemaHelper.DB_HIVE.equals(connectionInfo.getDbType()) ? "SYS" : null ); + try (Connection metastoreDbConnection = HiveSchemaHelper.getConnectionToMetastore(connectionInfo, schema); + Statement stmt = metastoreDbConnection.createStatement()) { ResultSet res = stmt.executeQuery(versionQuery); if (!res.next()) { throw new HiveMetaException("Could not find version info in metastore VERSION table."); diff --git a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/tools/HiveSchemaHelper.java b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/tools/HiveSchemaHelper.java index 08a3af5..70007cd 100644 --- a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/tools/HiveSchemaHelper.java +++ b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/tools/HiveSchemaHelper.java @@ -44,22 +44,20 @@ /*** * Get JDBC connection to metastore db * + * @param dbType the type of the meteastore database * @param userName metastore connection username * @param password metastore connection password * @param printInfo print connection parameters * @param conf hive config object + * @param schema the schema to create the connection for * @return metastore connection object * @throws org.apache.hadoop.hive.metastore.HiveMetaException */ - public static Connection getConnectionToMetastore(String userName, - String password, String url, String driver, boolean printInfo, - Configuration conf) - throws HiveMetaException { + public static Connection getConnectionToMetastore(String userName, String password, String url, + String driver, boolean printInfo, Configuration conf, String schema) throws HiveMetaException { try { - url = url == null ? getValidConfVar( - MetastoreConf.ConfVars.CONNECTURLKEY, conf) : url; - driver = driver == null ? getValidConfVar( - MetastoreConf.ConfVars.CONNECTION_DRIVER, conf) : driver; + url = url == null ? getValidConfVar(MetastoreConf.ConfVars.CONNECTURLKEY, conf) : url; + driver = driver == null ? getValidConfVar(MetastoreConf.ConfVars.CONNECTION_DRIVER, conf) : driver; if (printInfo) { System.out.println("Metastore connection URL:\t " + url); System.out.println("Metastore Connection Driver :\t " + driver); @@ -73,19 +71,22 @@ public static Connection getConnectionToMetastore(String userName, Class.forName(driver); // Connect using the JDBC URL and user/pass from conf - return DriverManager.getConnection(url, userName, password); - } catch (IOException e) { - throw new HiveMetaException("Failed to get schema version.", e); - } catch (SQLException e) { + Connection conn = DriverManager.getConnection(url, userName, password); + if (schema != null) { + conn.setSchema(schema); + } + return conn; + } catch (IOException | SQLException e) { throw new HiveMetaException("Failed to get schema version.", e); } catch (ClassNotFoundException e) { throw new HiveMetaException("Failed to load driver", e); } } - public static Connection getConnectionToMetastore(MetaStoreConnectionInfo info) throws HiveMetaException { - return getConnectionToMetastore(info.getUsername(), info.getPassword(), info.getUrl(), - info.getDriver(), info.getPrintInfo(), info.getConf()); + public static Connection getConnectionToMetastore(MetaStoreConnectionInfo info, String schema) + throws HiveMetaException { + return getConnectionToMetastore(info.getUsername(), info.getPassword(), info.getUrl(), info.getDriver(), + info.getPrintInfo(), info.getConf(), schema); } public static String getValidConfVar(MetastoreConf.ConfVars confVar, Configuration conf) @@ -593,9 +594,10 @@ public static NestedScriptParser getDbCommandParser(String dbName, private final boolean printInfo; private final Configuration conf; private final String dbType; + private final String metaDbType; public MetaStoreConnectionInfo(String userName, String password, String url, String driver, - boolean printInfo, Configuration conf, String dbType) { + boolean printInfo, Configuration conf, String dbType, String metaDbType) { super(); this.userName = userName; this.password = password; @@ -604,6 +606,7 @@ public MetaStoreConnectionInfo(String userName, String password, String url, Str this.printInfo = printInfo; this.conf = conf; this.dbType = dbType; + this.metaDbType = metaDbType; } public String getPassword() { @@ -637,5 +640,9 @@ public boolean getPrintInfo() { public String getDbType() { return dbType; } + + public String getMetaDbType() { + return metaDbType; + } } }