use of java.sql.SQLException in project hive by apache.
the class TestJdbcWithLocalClusterSpark method testTempTable.
@Test
public void testTempTable() throws Exception {
// Create temp table with current connection
String tempTableName = "tmp1";
stmt.execute("CREATE TEMPORARY TABLE " + tempTableName + " (key string, value string)");
stmt.execute("load data local inpath '" + dataFilePath.toString() + "' into table " + tempTableName);
String resultVal = "val_238";
String queryStr = "SELECT * FROM " + tempTableName + " where value = '" + resultVal + "'";
verifyResult(queryStr, resultVal, 2);
// A second connection should not be able to see the table
Connection conn2 = DriverManager.getConnection(miniHS2.getJdbcURL(dbName), System.getProperty("user.name"), "bar");
Statement stmt2 = conn2.createStatement();
stmt2.execute("USE " + dbName);
boolean gotException = false;
try {
stmt2.executeQuery(queryStr);
} catch (SQLException err) {
// This is expected to fail.
assertTrue("Expecting table not found error, instead got: " + err, err.getMessage().contains("Table not found"));
gotException = true;
}
assertTrue("Exception while querying non-existing temp table", gotException);
}
use of java.sql.SQLException in project hive by apache.
the class TestJdbcWithMiniHS2 method testUdfWhiteBlackList.
/**
* Test UDF whitelist
* - verify default value
* - verify udf allowed with default whitelist
* - verify udf allowed with specific whitelist
* - verify udf disallowed when not in whitelist
* @throws Exception
*/
@Test
public void testUdfWhiteBlackList() throws Exception {
HiveConf testConf = new HiveConf();
assertTrue(testConf.getVar(ConfVars.HIVE_SERVER2_BUILTIN_UDF_WHITELIST).isEmpty());
// verify that udf in default whitelist can be executed
Statement stmt = conDefault.createStatement();
stmt.executeQuery("SELECT substr('foobar', 4) ");
stmt.close();
// setup whitelist
stopMiniHS2();
Set<String> funcNames = FunctionRegistry.getFunctionNames();
funcNames.remove("reflect");
String funcNameStr = "";
for (String funcName : funcNames) {
funcNameStr += "," + funcName;
}
// remove ',' at begining
funcNameStr = funcNameStr.substring(1);
testConf.setVar(ConfVars.HIVE_SERVER2_BUILTIN_UDF_WHITELIST, funcNameStr);
startMiniHS2(testConf);
Connection conn = getConnection(miniHS2.getJdbcURL(testDbName), System.getProperty("user.name"), "bar");
stmt = conn.createStatement();
// verify that udf in whitelist can be executed
stmt.executeQuery("SELECT substr('foobar', 3) ");
// verify that udf not in whitelist fails
try {
stmt.executeQuery("SELECT reflect('java.lang.String', 'valueOf', 1) ");
fail("reflect() udf invocation should fail");
} catch (SQLException e) {
// expected
}
conn.close();
// Restore original state
restoreMiniHS2AndConnections();
}
use of java.sql.SQLException in project hive by apache.
the class TestJdbcDriver2 method testQueryTimeout.
@Test
public void testQueryTimeout() throws Exception {
String udfName = SleepMsUDF.class.getName();
Statement stmt1 = con.createStatement();
stmt1.execute("create temporary function sleepMsUDF as '" + udfName + "'");
stmt1.close();
Statement stmt = con.createStatement();
// Test a query where timeout kicks in
// Set query timeout to 1 second
stmt.setQueryTimeout(1);
System.err.println("Executing query: ");
try {
// The test table has 500 rows, so total query time should be ~ 2500ms
stmt.executeQuery("select sleepMsUDF(t1.under_col, 5) as u0, t1.under_col as u1, " + "t2.under_col as u2 from " + tableName + " t1 join " + tableName + " t2 on t1.under_col = t2.under_col");
fail("Expecting SQLTimeoutException");
} catch (SQLTimeoutException e) {
assertNotNull(e);
System.err.println(e.toString());
} catch (SQLException e) {
fail("Expecting SQLTimeoutException, but got SQLException: " + e);
e.printStackTrace();
}
// Test a query where timeout does not kick in. Set it to 5s;
// show tables should be faster than that
stmt.setQueryTimeout(5);
try {
stmt.executeQuery("show tables");
} catch (SQLException e) {
fail("Unexpected SQLException: " + e);
e.printStackTrace();
}
stmt.close();
}
use of java.sql.SQLException in project hive by apache.
the class TestJdbcDriver2 method testResultSetColumnNameCaseInsensitive.
@Test
public void testResultSetColumnNameCaseInsensitive() throws SQLException {
Statement stmt = con.createStatement();
ResultSet res;
res = stmt.executeQuery("select c1 from " + dataTypeTableName + " limit 1");
try {
int count = 0;
while (res.next()) {
res.findColumn("c1");
res.findColumn("C1");
count++;
}
assertEquals(count, 1);
} catch (Exception e) {
String msg = "Unexpected exception: " + e;
LOG.info(msg, e);
fail(msg);
}
res = stmt.executeQuery("select c1 C1 from " + dataTypeTableName + " limit 1");
try {
int count = 0;
while (res.next()) {
res.findColumn("c1");
res.findColumn("C1");
count++;
}
assertEquals(count, 1);
} catch (Exception e) {
String msg = "Unexpected exception: " + e;
LOG.info(msg, e);
fail(msg);
}
stmt.close();
}
use of java.sql.SQLException in project hive by apache.
the class TestJdbcDriver2 method testGetQueryLog.
/**
* Test getting query log method in Jdbc
* @throws Exception
*/
@Test
public void testGetQueryLog() throws Exception {
// Prepare
String[] expectedLogs = { "Compiling command", "Completed compiling command", "Starting Semantic Analysis", "Semantic Analysis Completed", "Executing command", "Completed executing command" };
String sql = "select count(*) from " + tableName;
// Verify the fetched log (from the beginning of log file)
HiveStatement stmt = (HiveStatement) con.createStatement();
assertNotNull("Statement is null", stmt);
stmt.executeQuery(sql);
List<String> logs = stmt.getQueryLog(false, 10000);
stmt.close();
verifyFetchedLog(logs, expectedLogs);
// Verify the fetched log (incrementally)
final HiveStatement statement = (HiveStatement) con.createStatement();
assertNotNull("Statement is null", statement);
statement.setFetchSize(10000);
final List<String> incrementalLogs = new ArrayList<String>();
Runnable logThread = new Runnable() {
@Override
public void run() {
while (statement.hasMoreLogs()) {
try {
incrementalLogs.addAll(statement.getQueryLog());
Thread.sleep(500);
} catch (SQLException e) {
LOG.error("Failed getQueryLog. Error message: " + e.getMessage());
fail("error in getting log thread");
} catch (InterruptedException e) {
LOG.error("Getting log thread is interrupted. Error message: " + e.getMessage());
fail("error in getting log thread");
}
}
}
};
Thread thread = new Thread(logThread);
thread.setDaemon(true);
thread.start();
statement.executeQuery(sql);
thread.interrupt();
thread.join(10000);
// fetch remaining logs
List<String> remainingLogs;
do {
remainingLogs = statement.getQueryLog();
incrementalLogs.addAll(remainingLogs);
} while (remainingLogs.size() > 0);
statement.close();
verifyFetchedLog(incrementalLogs, expectedLogs);
}
Aggregations