Repository: hive
Updated Branches:
  refs/heads/master 3464df457 -> c34938b9d


HIVE-18614 : Fix sys db creation in Hive (Miklos Gergely via Ashutosh Chauhan)

Signed-off-by: Ashutosh Chauhan <hashut...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/c34938b9
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/c34938b9
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/c34938b9

Branch: refs/heads/master
Commit: c34938b9dfef8557b8329cad1340a15b7b757bd2
Parents: 3464df4
Author: Miklos Gergely <mgerg...@hortonworks.com>
Authored: Thu Feb 8 23:25:24 2018 -0800
Committer: Ashutosh Chauhan <hashut...@apache.org>
Committed: Thu Feb 8 23:25:24 2018 -0800

----------------------------------------------------------------------
 .../org/apache/hive/beeline/HiveSchemaTool.java | 23 ++++--------
 .../org/apache/hive/beeline/TestSchemaTool.java |  8 ++--
 .../upgrade/hive/hive-schema-3.0.0.hive.sql     |  4 +-
 .../hive/metastore/MetaStoreSchemaInfo.java     | 11 +++---
 .../hive/metastore/tools/HiveSchemaHelper.java  | 39 ++++++++++++--------
 5 files changed, 42 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/c34938b9/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java
----------------------------------------------------------------------
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 class HiveSchemaTool {
     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 @@ public class HiveSchemaTool {
     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 class HiveSchemaTool {
 
   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 class HiveSchemaTool {
     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 @@ public class HiveSchemaTool {
     }
   }
 
-  boolean validateSchemaVersions(Connection conn) throws HiveMetaException {
+  boolean validateSchemaVersions() throws HiveMetaException {
     System.out.println("Validating schema version");
     try {
       String newSchemaVersion = 
metaStoreSchemaInfo.getMetaStoreSchemaVersion(getConnectionInfo(false));

http://git-wip-us.apache.org/repos/asf/hive/blob/c34938b9/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java
----------------------------------------------------------------------
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 class TestSchemaTool extends TestCase {
   */
  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 class TestSchemaTool extends TestCase {
    };
    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 class TestSchemaTool extends TestCase {
    };
    scriptFile = generateTestScript(scripts);
    schemaTool.runBeeLine(scriptFile.getPath());
-   isValid = schemaTool.validateSchemaVersions(conn);
+   isValid = schemaTool.validateSchemaVersions();
    assertFalse(isValid);
  }
 

http://git-wip-us.apache.org/repos/asf/hive/blob/c34938b9/metastore/scripts/upgrade/hive/hive-schema-3.0.0.hive.sql
----------------------------------------------------------------------
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;

http://git-wip-us.apache.org/repos/asf/hive/blob/c34938b9/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreSchemaInfo.java
----------------------------------------------------------------------
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 class MetaStoreSchemaInfo implements 
IMetaStoreSchemaInfo {
   }
 
   @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.");

http://git-wip-us.apache.org/repos/asf/hive/blob/c34938b9/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/tools/HiveSchemaHelper.java
----------------------------------------------------------------------
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 @@ public class HiveSchemaHelper {
   /***
    * 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 class HiveSchemaHelper {
       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 class HiveSchemaHelper {
     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 class HiveSchemaHelper {
       this.printInfo = printInfo;
       this.conf = conf;
       this.dbType = dbType;
+      this.metaDbType = metaDbType;
     }
 
     public String getPassword() {
@@ -637,5 +640,9 @@ public class HiveSchemaHelper {
     public String getDbType() {
       return dbType;
     }
+
+    public String getMetaDbType() {
+      return metaDbType;
+    }
   }
 }

Reply via email to