You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2021/10/24 20:53:26 UTC

[GitHub] [hive] zabetak opened a new pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

zabetak opened a new pull request #2742:
URL: https://github.com/apache/hive/pull/2742


   ### What changes were proposed in this pull request?
   1. Refactor `AbstractExternalDB` and subclasses: (i) turn setters to getters; (ii) inline methods; (iii) reduce visibility when possible; (iv) drop redundant comments; (v) drop unnecessary logging and try/catch blocks.
   2. Use provided user/password/dbname when starting Postgres instead of default.
   3. Use correct driver and docker image for MySQL database.
   4. Support additional databases (MariaDB, Oracle, MSSQLServer) for tests.
   5. Introduce `QTestDatabaseHandler` class for handling completely setup, init and cleanup of dockerized DBMS.
   6. Drop `TestMiniLlapExtDBCliDriver`, with associated config, properties etc. 
   
   ### Why are the changes needed?
   It is now possible to:
   1. initialize a dockerized DBMS in any qtest without the need for specific CLI driver or configuration;
   2. use multiple DBMS of different types in the same qtest;
   3. use different initialization scripts per test/database;
   4. use additional databases (MariaDB, Oracle, MSSQLServer) in tests.
   
   Misc:
   5. Improve readability and encapsulation of `AbstractExternalDB` and friends.
   6. Small bugs in Postgres and MySQL.
   
   ### Does this PR introduce _any_ user-facing change?
   No, it only impacts developers.
   
   ### How was this patch tested?
   `mvn test -Dtest=TestMiniLlapLocalCliDriver -Dqfile_regex="qt_database.*" -Dtest.output.overwrite`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] asolimando commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
asolimando commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737501273



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -193,96 +147,62 @@ public final String getContainerHostAddress() {
         }
     }
 
-    public abstract void setJdbcUrl(String hostAddress);
-
-    public abstract void setJdbcDriver();
-
-    public abstract String getDockerImageName();
-
-    public abstract String[] getDockerAdditionalArgs();
-
-    public abstract boolean isContainerReady(ProcessResults pr);
+    /**
+     * Return the name of the root user.
+     * 
+     * Override the method if the name of the root user must be different than the default.
+     */
+    protected String getRootUser() {
+        return "qtestuser";
+    }
 
-    protected String[] buildArray(String... strs) {
-        return strs;
+    /**
+     * Return the password of the root user.
+     * 
+     * Override the method if the password must be different than the default.
+     */
+    protected String getRootPassword() {
+        return  "qtestpassword";
     }
+    
+    protected abstract String getJdbcUrl();
 
-    public Connection getConnectionToExternalDB() throws SQLException, ClassNotFoundException {
-        try {
-            LOG.info("external database connection URL:\t " + url);
-            LOG.info("JDBC Driver :\t " + driver);
-            LOG.info("external database connection User:\t " + userName);
-            LOG.info("external database connection Password:\t " + password);
+    protected abstract String getJdbcDriver();
 
-            // load required JDBC driver
-            Class.forName(driver);
+    protected abstract String getDockerImageName();
 
-            // Connect using the JDBC URL and user/password
-            Connection conn = DriverManager.getConnection(url, userName, password);
-            return conn;
-        } catch (SQLException e) {
-            LOG.error("Failed to connect to external databse", e);
-            throw new SQLException(e);
-        } catch (ClassNotFoundException e) {
-            LOG.error("Unable to find driver class", e);
-            throw new ClassNotFoundException("Unable to find driver class");
-        }
-    }
+    protected abstract String[] getDockerAdditionalArgs();
 
-    public void testConnectionToExternalDB() throws SQLException, ClassNotFoundException {
-        Connection conn = getConnectionToExternalDB();
-        try {
-            conn.close();
-        } catch (SQLException e) {
-            LOG.error("Failed to close external database connection", e);
-        }
-    }
+    protected abstract boolean isContainerReady(ProcessResults pr);
 
-    protected String[] SQLLineCmdBuild(String sqlScriptFile) throws IOException {
-        return new String[] {"-u", url,
-                            "-d", driver,
-                            "-n", userName,
-                            "-p", password,
+    private String[] SQLLineCmdBuild(String sqlScriptFile) {
+        return new String[] {"-u", getJdbcUrl(),
+                            "-d", getJdbcDriver(),
+                            "-n", getRootUser(),
+                            "-p", getRootPassword(),
                             "--isolation=TRANSACTION_READ_COMMITTED",
                             "-f", sqlScriptFile};
 
     }
 
-    protected void execSql(String sqlScriptFile) throws IOException {
-        // run the script using SqlLine
-        SqlLine sqlLine = new SqlLine();
-        ByteArrayOutputStream outputForLog = null;
-        OutputStream out;
-        if (LOG.isDebugEnabled()) {
-            out = outputForLog = new ByteArrayOutputStream();
-        } else {
-            out = new NullOutputStream();
+    public void execute(String script) throws IOException, SQLException, ClassNotFoundException {
+        // Test we can connect to database
+        Class.forName(getJdbcDriver());
+        try (Connection ignored = DriverManager.getConnection(getJdbcUrl(), getRootUser(), getRootPassword())) {
+            LOG.info("Successfully connected to {} with user {} and password {}", getJdbcUrl(), getRootUser(), getRootPassword());
         }
+        LOG.info("Starting {} initialization", getClass().getSimpleName());
+        SqlLine sqlLine = new SqlLine();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
         sqlLine.setOutputStream(new PrintStream(out));
+        sqlLine.setErrorStream(new PrintStream(out));
         System.setProperty("sqlline.silent", "true");
-
-        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(sqlScriptFile), null, false);
-        if (LOG.isDebugEnabled() && outputForLog != null) {
-            LOG.debug("Received following output from Sqlline:");
-            LOG.debug(outputForLog.toString("UTF-8"));
-        }
+        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(script), null, false);
+        LOG.debug("Printing output from SQLLine:");
+        LOG.debug(out.toString());
         if (status != SqlLine.Status.OK) {
-            throw new IOException("external database script failed, errorcode " + status);
+            throw new RuntimeException("Database script " + script + " failed with status " + status);
         }
-    }
-
-    public void execute(String script) throws IOException, SQLException, ClassNotFoundException {
-        testConnectionToExternalDB();
-        LOG.info("Starting external database initialization to " + this.externalDBType);
-
-        try {
-            LOG.info("Initialization script " + script);
-            execSql(script);
-            LOG.info("Initialization script completed in external database");
-
-        } catch (IOException e) {
-            throw new IOException("initialization in external database FAILED!");
-        }
-
+        LOG.info("Completed {} initialization", getClass().getSimpleName());
     }
 }

Review comment:
       missing newline




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737527955



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -193,96 +147,62 @@ public final String getContainerHostAddress() {
         }
     }
 
-    public abstract void setJdbcUrl(String hostAddress);
-
-    public abstract void setJdbcDriver();
-
-    public abstract String getDockerImageName();
-
-    public abstract String[] getDockerAdditionalArgs();
-
-    public abstract boolean isContainerReady(ProcessResults pr);
+    /**
+     * Return the name of the root user.
+     * 
+     * Override the method if the name of the root user must be different than the default.
+     */
+    protected String getRootUser() {
+        return "qtestuser";
+    }
 
-    protected String[] buildArray(String... strs) {
-        return strs;
+    /**
+     * Return the password of the root user.
+     * 
+     * Override the method if the password must be different than the default.
+     */
+    protected String getRootPassword() {
+        return  "qtestpassword";
     }
+    
+    protected abstract String getJdbcUrl();
 
-    public Connection getConnectionToExternalDB() throws SQLException, ClassNotFoundException {
-        try {
-            LOG.info("external database connection URL:\t " + url);
-            LOG.info("JDBC Driver :\t " + driver);
-            LOG.info("external database connection User:\t " + userName);
-            LOG.info("external database connection Password:\t " + password);
+    protected abstract String getJdbcDriver();
 
-            // load required JDBC driver
-            Class.forName(driver);
+    protected abstract String getDockerImageName();
 
-            // Connect using the JDBC URL and user/password
-            Connection conn = DriverManager.getConnection(url, userName, password);
-            return conn;
-        } catch (SQLException e) {
-            LOG.error("Failed to connect to external databse", e);
-            throw new SQLException(e);
-        } catch (ClassNotFoundException e) {
-            LOG.error("Unable to find driver class", e);
-            throw new ClassNotFoundException("Unable to find driver class");
-        }
-    }
+    protected abstract String[] getDockerAdditionalArgs();
 
-    public void testConnectionToExternalDB() throws SQLException, ClassNotFoundException {
-        Connection conn = getConnectionToExternalDB();
-        try {
-            conn.close();
-        } catch (SQLException e) {
-            LOG.error("Failed to close external database connection", e);
-        }
-    }
+    protected abstract boolean isContainerReady(ProcessResults pr);
 
-    protected String[] SQLLineCmdBuild(String sqlScriptFile) throws IOException {
-        return new String[] {"-u", url,
-                            "-d", driver,
-                            "-n", userName,
-                            "-p", password,
+    private String[] SQLLineCmdBuild(String sqlScriptFile) {
+        return new String[] {"-u", getJdbcUrl(),
+                            "-d", getJdbcDriver(),
+                            "-n", getRootUser(),
+                            "-p", getRootPassword(),
                             "--isolation=TRANSACTION_READ_COMMITTED",
                             "-f", sqlScriptFile};
 
     }
 
-    protected void execSql(String sqlScriptFile) throws IOException {
-        // run the script using SqlLine
-        SqlLine sqlLine = new SqlLine();
-        ByteArrayOutputStream outputForLog = null;
-        OutputStream out;
-        if (LOG.isDebugEnabled()) {
-            out = outputForLog = new ByteArrayOutputStream();
-        } else {
-            out = new NullOutputStream();
+    public void execute(String script) throws IOException, SQLException, ClassNotFoundException {
+        // Test we can connect to database
+        Class.forName(getJdbcDriver());
+        try (Connection ignored = DriverManager.getConnection(getJdbcUrl(), getRootUser(), getRootPassword())) {
+            LOG.info("Successfully connected to {} with user {} and password {}", getJdbcUrl(), getRootUser(), getRootPassword());
         }
+        LOG.info("Starting {} initialization", getClass().getSimpleName());
+        SqlLine sqlLine = new SqlLine();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
         sqlLine.setOutputStream(new PrintStream(out));
+        sqlLine.setErrorStream(new PrintStream(out));
         System.setProperty("sqlline.silent", "true");
-
-        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(sqlScriptFile), null, false);
-        if (LOG.isDebugEnabled() && outputForLog != null) {
-            LOG.debug("Received following output from Sqlline:");
-            LOG.debug(outputForLog.toString("UTF-8"));
-        }
+        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(script), null, false);
+        LOG.debug("Printing output from SQLLine:");
+        LOG.debug(out.toString());
         if (status != SqlLine.Status.OK) {
-            throw new IOException("external database script failed, errorcode " + status);
+            throw new RuntimeException("Database script " + script + " failed with status " + status);
         }
-    }
-
-    public void execute(String script) throws IOException, SQLException, ClassNotFoundException {
-        testConnectionToExternalDB();
-        LOG.info("Starting external database initialization to " + this.externalDBType);
-
-        try {
-            LOG.info("Initialization script " + script);
-            execSql(script);
-            LOG.info("Initialization script completed in external database");
-
-        } catch (IOException e) {
-            throw new IOException("initialization in external database FAILED!");
-        }
-
+        LOG.info("Completed {} initialization", getClass().getSimpleName());
     }
 }

Review comment:
       Fixed in https://github.com/apache/hive/pull/2742/commits/7c051086f3a603c305c4dcaf789599d051cd893e




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737525094



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       There are indeed many similarities with the classes in `DatabaseRule` although the latter contain additional code related to setting up and using a Hive metastore. I do agree that a refactoring needs to be done but maybe it would be better to leave it out of this change and treat it in a follow-up. What do you think?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak closed pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak closed pull request #2742:
URL: https://github.com/apache/hive/pull/2742


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak closed pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak closed pull request #2742:
URL: https://github.com/apache/hive/pull/2742


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r741839431



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       Logged https://issues.apache.org/jira/browse/HIVE-25667 for tracking this further.

##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       This suggestion would be definitely very useful and necessary if we want to support multiple databases of the same type running side by side or enabling database reuse among qfiles. Leaving this for https://issues.apache.org/jira/browse/HIVE-25668 where we are going to have more concrete requirements.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737532309



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       Didn't get that, can you elaborate a bit more?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kgyrtkirk commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r740914957



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       sure; can be covered in a followup as well

##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       something like:
   * use some real class insted of the enum
   * retain an object for the lifetime of the backing docker container to have an object represent it in the code
   * give the instructions to that object
   
   but this is not that important...we can have it this way as well




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kgyrtkirk commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737503807



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       how much these differ from (`
   standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/dbinstall/rules/DatabaseRule.java
   `)?
   can't we reuse those classes?
   

##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/MariaDB.java
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.externalDB;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class MariaDB extends AbstractExternalDB {

Review comment:
       ./standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/dbinstall/rules/Mysql.java ?

##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       this #.create() works; but wouldn't it better to create the object in the intialization which represents the container in the code?
   
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r741854852



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       This suggestion would be definitely very useful and necessary if we want to support multiple databases of the same type running side by side or enabling database reuse among qfiles. Leaving this for https://issues.apache.org/jira/browse/HIVE-25668 where we are going to have more concrete requirements.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737529431



##########
File path: data/scripts/q_test_state_table.sql
##########
@@ -0,0 +1,18 @@
+create table state
+(
+    name    varchar(255) not null,
+    country int          not null
+);
+
+insert into state (name, country)
+values ('Maharashtra', 1);
+insert into state (name, country)
+values ('Madhya Pradesh', 1);
+insert into state (name, country)
+values ('Moscow', 3);
+insert into state (name, country)
+values ('Something', 4);
+insert into state (name, country)
+values ('Florida', 4);
+insert into state (name, country)
+values ('Texas', 4);

Review comment:
       Fixed in https://github.com/apache/hive/pull/2742/commits/f2ec2c66740b380ba3af67512dd6343fe6c7323e




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r741839431



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       Logged https://issues.apache.org/jira/browse/HIVE-25667 for tracking this further.

##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       This suggestion would be definitely very useful and necessary if we want to support multiple databases of the same type running side by side or enabling database reuse among qfiles. Leaving this for https://issues.apache.org/jira/browse/HIVE-25668 where we are going to have more concrete requirements.

##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       Logged https://issues.apache.org/jira/browse/HIVE-25667 for tracking this further.

##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       This suggestion would be definitely very useful and necessary if we want to support multiple databases of the same type running side by side or enabling database reuse among qfiles. Leaving this for https://issues.apache.org/jira/browse/HIVE-25668 where we are going to have more concrete requirements.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak closed pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak closed pull request #2742:
URL: https://github.com/apache/hive/pull/2742


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak closed pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak closed pull request #2742:
URL: https://github.com/apache/hive/pull/2742


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kgyrtkirk commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r740914957



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       sure; can be covered in a followup as well




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] asolimando commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
asolimando commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737500744



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -193,96 +147,62 @@ public final String getContainerHostAddress() {
         }
     }
 
-    public abstract void setJdbcUrl(String hostAddress);
-
-    public abstract void setJdbcDriver();
-
-    public abstract String getDockerImageName();
-
-    public abstract String[] getDockerAdditionalArgs();
-
-    public abstract boolean isContainerReady(ProcessResults pr);
+    /**
+     * Return the name of the root user.
+     * 
+     * Override the method if the name of the root user must be different than the default.
+     */
+    protected String getRootUser() {
+        return "qtestuser";
+    }
 
-    protected String[] buildArray(String... strs) {
-        return strs;
+    /**
+     * Return the password of the root user.
+     * 
+     * Override the method if the password must be different than the default.
+     */
+    protected String getRootPassword() {
+        return  "qtestpassword";
     }
+    
+    protected abstract String getJdbcUrl();
 
-    public Connection getConnectionToExternalDB() throws SQLException, ClassNotFoundException {
-        try {
-            LOG.info("external database connection URL:\t " + url);
-            LOG.info("JDBC Driver :\t " + driver);
-            LOG.info("external database connection User:\t " + userName);
-            LOG.info("external database connection Password:\t " + password);
+    protected abstract String getJdbcDriver();
 
-            // load required JDBC driver
-            Class.forName(driver);
+    protected abstract String getDockerImageName();
 
-            // Connect using the JDBC URL and user/password
-            Connection conn = DriverManager.getConnection(url, userName, password);
-            return conn;
-        } catch (SQLException e) {
-            LOG.error("Failed to connect to external databse", e);
-            throw new SQLException(e);
-        } catch (ClassNotFoundException e) {
-            LOG.error("Unable to find driver class", e);
-            throw new ClassNotFoundException("Unable to find driver class");
-        }
-    }
+    protected abstract String[] getDockerAdditionalArgs();
 
-    public void testConnectionToExternalDB() throws SQLException, ClassNotFoundException {
-        Connection conn = getConnectionToExternalDB();
-        try {
-            conn.close();
-        } catch (SQLException e) {
-            LOG.error("Failed to close external database connection", e);
-        }
-    }
+    protected abstract boolean isContainerReady(ProcessResults pr);
 
-    protected String[] SQLLineCmdBuild(String sqlScriptFile) throws IOException {
-        return new String[] {"-u", url,
-                            "-d", driver,
-                            "-n", userName,
-                            "-p", password,
+    private String[] SQLLineCmdBuild(String sqlScriptFile) {
+        return new String[] {"-u", getJdbcUrl(),
+                            "-d", getJdbcDriver(),
+                            "-n", getRootUser(),
+                            "-p", getRootPassword(),
                             "--isolation=TRANSACTION_READ_COMMITTED",
                             "-f", sqlScriptFile};
 
     }
 
-    protected void execSql(String sqlScriptFile) throws IOException {
-        // run the script using SqlLine
-        SqlLine sqlLine = new SqlLine();
-        ByteArrayOutputStream outputForLog = null;
-        OutputStream out;
-        if (LOG.isDebugEnabled()) {
-            out = outputForLog = new ByteArrayOutputStream();
-        } else {
-            out = new NullOutputStream();
+    public void execute(String script) throws IOException, SQLException, ClassNotFoundException {
+        // Test we can connect to database
+        Class.forName(getJdbcDriver());
+        try (Connection ignored = DriverManager.getConnection(getJdbcUrl(), getRootUser(), getRootPassword())) {
+            LOG.info("Successfully connected to {} with user {} and password {}", getJdbcUrl(), getRootUser(), getRootPassword());
         }
+        LOG.info("Starting {} initialization", getClass().getSimpleName());
+        SqlLine sqlLine = new SqlLine();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
         sqlLine.setOutputStream(new PrintStream(out));
+        sqlLine.setErrorStream(new PrintStream(out));
         System.setProperty("sqlline.silent", "true");
-
-        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(sqlScriptFile), null, false);
-        if (LOG.isDebugEnabled() && outputForLog != null) {
-            LOG.debug("Received following output from Sqlline:");
-            LOG.debug(outputForLog.toString("UTF-8"));
-        }
+        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(script), null, false);
+        LOG.debug("Printing output from SQLLine:");

Review comment:
       I don't know how heavy `out.toString()` can be, maybe activate conditionally with `if(LOG.isDebugEnabled())`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] asolimando commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
asolimando commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737494195



##########
File path: data/scripts/q_test_state_table.sql
##########
@@ -0,0 +1,18 @@
+create table state
+(
+    name    varchar(255) not null,
+    country int          not null
+);
+
+insert into state (name, country)
+values ('Maharashtra', 1);
+insert into state (name, country)
+values ('Madhya Pradesh', 1);
+insert into state (name, country)
+values ('Moscow', 3);
+insert into state (name, country)
+values ('Something', 4);
+insert into state (name, country)
+values ('Florida', 4);
+insert into state (name, country)
+values ('Texas', 4);

Review comment:
       Newline is missing




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737519278



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -193,96 +147,62 @@ public final String getContainerHostAddress() {
         }
     }
 
-    public abstract void setJdbcUrl(String hostAddress);
-
-    public abstract void setJdbcDriver();
-
-    public abstract String getDockerImageName();
-
-    public abstract String[] getDockerAdditionalArgs();
-
-    public abstract boolean isContainerReady(ProcessResults pr);
+    /**
+     * Return the name of the root user.
+     * 
+     * Override the method if the name of the root user must be different than the default.
+     */
+    protected String getRootUser() {
+        return "qtestuser";
+    }
 
-    protected String[] buildArray(String... strs) {
-        return strs;
+    /**
+     * Return the password of the root user.
+     * 
+     * Override the method if the password must be different than the default.
+     */
+    protected String getRootPassword() {
+        return  "qtestpassword";
     }
+    
+    protected abstract String getJdbcUrl();
 
-    public Connection getConnectionToExternalDB() throws SQLException, ClassNotFoundException {
-        try {
-            LOG.info("external database connection URL:\t " + url);
-            LOG.info("JDBC Driver :\t " + driver);
-            LOG.info("external database connection User:\t " + userName);
-            LOG.info("external database connection Password:\t " + password);
+    protected abstract String getJdbcDriver();
 
-            // load required JDBC driver
-            Class.forName(driver);
+    protected abstract String getDockerImageName();
 
-            // Connect using the JDBC URL and user/password
-            Connection conn = DriverManager.getConnection(url, userName, password);
-            return conn;
-        } catch (SQLException e) {
-            LOG.error("Failed to connect to external databse", e);
-            throw new SQLException(e);
-        } catch (ClassNotFoundException e) {
-            LOG.error("Unable to find driver class", e);
-            throw new ClassNotFoundException("Unable to find driver class");
-        }
-    }
+    protected abstract String[] getDockerAdditionalArgs();
 
-    public void testConnectionToExternalDB() throws SQLException, ClassNotFoundException {
-        Connection conn = getConnectionToExternalDB();
-        try {
-            conn.close();
-        } catch (SQLException e) {
-            LOG.error("Failed to close external database connection", e);
-        }
-    }
+    protected abstract boolean isContainerReady(ProcessResults pr);
 
-    protected String[] SQLLineCmdBuild(String sqlScriptFile) throws IOException {
-        return new String[] {"-u", url,
-                            "-d", driver,
-                            "-n", userName,
-                            "-p", password,
+    private String[] SQLLineCmdBuild(String sqlScriptFile) {
+        return new String[] {"-u", getJdbcUrl(),
+                            "-d", getJdbcDriver(),
+                            "-n", getRootUser(),
+                            "-p", getRootPassword(),
                             "--isolation=TRANSACTION_READ_COMMITTED",
                             "-f", sqlScriptFile};
 
     }
 
-    protected void execSql(String sqlScriptFile) throws IOException {
-        // run the script using SqlLine
-        SqlLine sqlLine = new SqlLine();
-        ByteArrayOutputStream outputForLog = null;
-        OutputStream out;
-        if (LOG.isDebugEnabled()) {
-            out = outputForLog = new ByteArrayOutputStream();
-        } else {
-            out = new NullOutputStream();
+    public void execute(String script) throws IOException, SQLException, ClassNotFoundException {
+        // Test we can connect to database
+        Class.forName(getJdbcDriver());
+        try (Connection ignored = DriverManager.getConnection(getJdbcUrl(), getRootUser(), getRootPassword())) {
+            LOG.info("Successfully connected to {} with user {} and password {}", getJdbcUrl(), getRootUser(), getRootPassword());
         }
+        LOG.info("Starting {} initialization", getClass().getSimpleName());
+        SqlLine sqlLine = new SqlLine();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
         sqlLine.setOutputStream(new PrintStream(out));
+        sqlLine.setErrorStream(new PrintStream(out));
         System.setProperty("sqlline.silent", "true");
-
-        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(sqlScriptFile), null, false);
-        if (LOG.isDebugEnabled() && outputForLog != null) {
-            LOG.debug("Received following output from Sqlline:");
-            LOG.debug(outputForLog.toString("UTF-8"));
-        }
+        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(script), null, false);
+        LOG.debug("Printing output from SQLLine:");

Review comment:
       Please check comment https://github.com/apache/hive/pull/2742/commits/10a2f90dafd040c5fdb59080a88c0c3866745006.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kgyrtkirk commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r740914957



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       sure; can be covered in a followup as well

##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       something like:
   * use some real class insted of the enum
   * retain an object for the lifetime of the backing docker container to have an object represent it in the code
   * give the instructions to that object
   
   but this is not that important...we can have it this way as well




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kgyrtkirk commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r740918261



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestDatabaseHandler.java
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql.qoption;
+
+import org.apache.hadoop.hive.ql.QTestUtil;
+import org.apache.hadoop.hive.ql.externalDB.AbstractExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.MSSQLServer;
+import org.apache.hadoop.hive.ql.externalDB.MariaDB;
+import org.apache.hadoop.hive.ql.externalDB.MySQLExternalDB;
+import org.apache.hadoop.hive.ql.externalDB.Oracle;
+import org.apache.hadoop.hive.ql.externalDB.PostgresExternalDB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * An option handler for spinning (resp. stopping) databases before (resp. after) running a test.
+ *
+ * Syntax: qt:database:DatabaseType:path_to_init_script
+ *
+ * The database type ({@link DatabaseType}) is obligatory argument. The initialization script can be omitted.
+ *
+ * Current limitations:
+ * <ol>
+ *   <li>Only one test/file per run</li>
+ *   <li>Does not support parallel execution</li>
+ *   <li>Cannot instantiate more than one database of the same type per test</li>
+ * </ol>
+ */
+public class QTestDatabaseHandler implements QTestOptionHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(QTestDatabaseHandler.class);
+
+  private enum DatabaseType {
+    POSTGRES {
+      @Override
+      AbstractExternalDB create() {
+        return new PostgresExternalDB();
+      }
+    }, MYSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MySQLExternalDB();
+      }
+    }, MARIADB {
+      @Override
+      AbstractExternalDB create() {
+        return new MariaDB();
+      }
+    }, MSSQL {
+      @Override
+      AbstractExternalDB create() {
+        return new MSSQLServer();
+      }
+    }, ORACLE {
+      @Override
+      AbstractExternalDB create() {
+        return new Oracle();
+      }
+    };
+
+    abstract AbstractExternalDB create();
+  }
+
+  private final Map<DatabaseType, String> databaseToScript = new EnumMap<>(DatabaseType.class);
+
+  @Override
+  public void processArguments(String arguments) {
+    String[] args = arguments.split(":");
+    if (args.length == 0) {
+      throw new IllegalArgumentException("No arguments provided");
+    }
+    if (args.length > 2) {
+      throw new IllegalArgumentException(
+          "Too many arguments. Expected {dbtype:script}. Found: " + Arrays.toString(args));
+    }
+    DatabaseType dbType = DatabaseType.valueOf(args[0].toUpperCase());
+    String initScript = args.length == 2 ? args[1] : "";
+    if (databaseToScript.containsKey(dbType)) {
+      throw new IllegalArgumentException(dbType + " database is already defined in this file.");
+    }
+    databaseToScript.put(dbType, initScript);
+  }
+
+  @Override
+  public void beforeTest(QTestUtil qt) throws Exception {
+    if (databaseToScript.isEmpty()) {
+      return;
+    }
+    for (Map.Entry<DatabaseType, String> dbEntry : databaseToScript.entrySet()) {
+      String scriptsDir = QTestUtil.getScriptsDir(qt.getConf());
+      Path dbScript = Paths.get(scriptsDir, dbEntry.getValue());
+      AbstractExternalDB db = dbEntry.getKey().create();

Review comment:
       something like:
   * use some real class insted of the enum
   * retain an object for the lifetime of the backing docker container to have an object represent it in the code
   * give the instructions to that object
   
   but this is not that important...we can have it this way as well




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #2742: HIVE-25594: Setup JDBC databases in tests via QT options

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r741839431



##########
File path: itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -71,27 +59,8 @@ public ProcessResults(String stdout, String stderr, int rc) {
         }
     }
 
-    public static AbstractExternalDB initalizeExternalDB(String externalDBType) throws IOException {
-        AbstractExternalDB abstractExternalDB;
-        switch (externalDBType) {
-            case "mysql":
-                abstractExternalDB = new MySQLExternalDB();
-                break;
-            case "postgres":
-                abstractExternalDB = new PostgresExternalDB();
-                break;
-            default:
-                throw new IOException("unsupported external database type " + externalDBType);
-        }
-        return abstractExternalDB;
-    }
-
-    public AbstractExternalDB(String externalDBType) {
-        this.externalDBType = externalDBType;
-    }
-
-    protected String getDockerContainerName() {
-        return String.format("qtestExternalDB-%s", externalDBType);
+    private final String getDockerContainerName() {

Review comment:
       Logged https://issues.apache.org/jira/browse/HIVE-25667 for tracking this further.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org