use of java.sql.SQLException in project hive by apache.
the class TestJdbcDriver2 method doTestErrorCase.
private void doTestErrorCase(String sql, String expectedMessage, String expectedSQLState, int expectedErrorCode) throws SQLException {
Statement stmt = con.createStatement();
boolean exceptionFound = false;
try {
stmt.execute(sql);
} catch (SQLException e) {
assertTrue("Adequate error messaging not found for '" + sql + "': " + e.getMessage(), e.getMessage().contains(expectedMessage));
assertEquals("Expected SQLState not found for '" + sql + "'", expectedSQLState, e.getSQLState());
assertEquals("Expected error code not found for '" + sql + "'", expectedErrorCode, e.getErrorCode());
exceptionFound = true;
}
assertNotNull("Exception should have been thrown for query: " + sql, exceptionFound);
stmt.close();
}
use of java.sql.SQLException in project hive by apache.
the class TestJdbcDriver2 method testQueryCancel.
/**
* Test the cancellation of a query that is running.
* We spawn 2 threads - one running the query and
* the other attempting to cancel.
* We're using a dummy udf to simulate a query,
* that runs for a sufficiently long time.
* @throws Exception
*/
@Test
public void testQueryCancel() throws Exception {
String udfName = SleepMsUDF.class.getName();
Statement stmt1 = con.createStatement();
stmt1.execute("create temporary function sleepMsUDF as '" + udfName + "'");
stmt1.close();
final Statement stmt = con.createStatement();
// Thread executing the query
Thread tExecute = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Executing query: ");
// The test table has 500 rows, so total query time should be ~ 500*500ms
stmt.executeQuery("select sleepMsUDF(t1.under_col, 1) 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 SQLException");
} catch (SQLException e) {
// This thread should throw an exception
assertNotNull(e);
System.out.println(e.toString());
}
}
});
// Thread cancelling the query
Thread tCancel = new Thread(new Runnable() {
@Override
public void run() {
try {
// Sleep for 100ms
Thread.sleep(100);
System.out.println("Cancelling query: ");
stmt.cancel();
} catch (Exception e) {
// No-op
}
}
});
tExecute.start();
tCancel.start();
tExecute.join();
tCancel.join();
stmt.close();
}
use of java.sql.SQLException in project hive by apache.
the class TestJdbcDriver2 method doTestSelectAll.
private void doTestSelectAll(String tableName, int maxRows, int fetchSize) throws Exception {
boolean isPartitionTable = tableName.equals(partitionedTableName);
Statement stmt = con.createStatement();
if (maxRows >= 0) {
stmt.setMaxRows(maxRows);
}
if (fetchSize > 0) {
stmt.setFetchSize(fetchSize);
assertEquals(fetchSize, stmt.getFetchSize());
}
// JDBC says that 0 means return all, which is the default
int expectedMaxRows = maxRows < 1 ? 0 : maxRows;
assertNotNull("Statement is null", stmt);
assertEquals("Statement max rows not as expected", expectedMaxRows, stmt.getMaxRows());
assertFalse("Statement should not be closed", stmt.isClosed());
ResultSet res;
// run some queries
res = stmt.executeQuery("select * from " + tableName);
assertNotNull("ResultSet is null", res);
assertTrue("getResultSet() not returning expected ResultSet", res == stmt.getResultSet());
assertEquals("get update count not as expected", -1, stmt.getUpdateCount());
int i = 0;
ResultSetMetaData meta = res.getMetaData();
int expectedColCount = isPartitionTable ? 3 : 2;
assertEquals("Unexpected column count", expectedColCount, meta.getColumnCount());
boolean moreRow = res.next();
while (moreRow) {
try {
i++;
assertEquals(res.getInt(1), res.getInt(tableName + ".under_col"));
assertEquals(res.getInt(1), res.getInt("under_col"));
assertEquals(res.getString(1), res.getString(tableName + ".under_col"));
assertEquals(res.getString(1), res.getString("under_col"));
assertEquals(res.getString(2), res.getString(tableName + ".value"));
assertEquals(res.getString(2), res.getString("value"));
if (isPartitionTable) {
assertEquals(res.getString(3), partitionedColumnValue);
assertEquals(res.getString(3), res.getString(partitionedColumnName));
assertEquals(res.getString(3), res.getString(tableName + "." + partitionedColumnName));
}
assertFalse("Last result value was not null", res.wasNull());
assertNull("No warnings should be found on ResultSet", res.getWarnings());
// verifying that method is supported
res.clearWarnings();
// System.out.println(res.getString(1) + " " + res.getString(2));
assertEquals("getInt and getString don't align for the same result value", String.valueOf(res.getInt(1)), res.getString(1));
assertEquals("Unexpected result found", "val_" + res.getString(1), res.getString(2));
moreRow = res.next();
} catch (SQLException e) {
System.out.println(e.toString());
e.printStackTrace();
throw new Exception(e.toString());
}
}
// supposed to get 500 rows if maxRows isn't set
int expectedRowCount = maxRows > 0 ? maxRows : 500;
assertEquals("Incorrect number of rows returned", expectedRowCount, i);
// should have no more rows
assertEquals(false, moreRow);
assertNull("No warnings should be found on statement", stmt.getWarnings());
// verifying that method is supported
stmt.clearWarnings();
assertNull("No warnings should be found on connection", con.getWarnings());
// verifying that method is supported
con.clearWarnings();
stmt.close();
assertTrue("Statement should be closed", stmt.isClosed());
}
use of java.sql.SQLException in project hive by apache.
the class TestJdbcDriver2 method testCancelQueryErrored.
@Test
public void testCancelQueryErrored() throws Exception {
final Statement stmt = con.createStatement();
try {
System.out.println("Executing query: ");
stmt.executeQuery("list dbs");
fail("Expecting SQLException");
} catch (SQLException e) {
// No-op
}
// Cancel the query
System.out.println("Cancel the Statement ...");
stmt.cancel();
stmt.close();
}
use of java.sql.SQLException in project hive by apache.
the class Function method execMinMaxPart.
/**
* Execute MIN or MAX partition function
*/
public void execMinMaxPart(HplsqlParser.Expr_spec_funcContext ctx, Var.Type type, boolean max) {
String tabname = evalPop(ctx.expr(0)).toString();
String sql = "SHOW PARTITIONS " + tabname;
String colname = null;
int colnum = -1;
int exprnum = ctx.expr().size();
// Column name
if (ctx.expr(1) != null) {
colname = evalPop(ctx.expr(1)).toString();
} else {
colnum = 0;
}
// Partition filter
if (exprnum >= 4) {
sql += " PARTITION (";
int i = 2;
while (i + 1 < exprnum) {
String fcol = evalPop(ctx.expr(i)).toString();
String fval = evalPop(ctx.expr(i + 1)).toSqlString();
if (i > 2) {
sql += ", ";
}
sql += fcol + "=" + fval;
i += 2;
}
sql += ")";
}
if (trace) {
trace(ctx, "Query: " + sql);
}
if (exec.getOffline()) {
evalNull();
return;
}
Query query = exec.executeQuery(ctx, sql, exec.conf.defaultConnection);
if (query.error()) {
evalNullClose(query, exec.conf.defaultConnection);
return;
}
ResultSet rs = query.getResultSet();
try {
String resultString = null;
Long resultInt = null;
Date resultDate = null;
while (rs.next()) {
String[] parts = rs.getString(1).split("/");
// Find partition column by name
if (colnum == -1) {
for (int i = 0; i < parts.length; i++) {
String[] name = parts[i].split("=");
if (name[0].equalsIgnoreCase(colname)) {
colnum = i;
break;
}
}
// No partition column with the specified name exists
if (colnum == -1) {
evalNullClose(query, exec.conf.defaultConnection);
return;
}
}
String[] pair = parts[colnum].split("=");
if (type == Var.Type.STRING) {
resultString = Utils.minMaxString(resultString, pair[1], max);
} else if (type == Var.Type.BIGINT) {
resultInt = Utils.minMaxInt(resultInt, pair[1], max);
} else if (type == Var.Type.DATE) {
resultDate = Utils.minMaxDate(resultDate, pair[1], max);
}
}
if (resultString != null) {
evalString(resultString);
} else if (resultInt != null) {
evalInt(resultInt);
} else if (resultDate != null) {
evalDate(resultDate);
} else {
evalNull();
}
} catch (SQLException e) {
}
exec.closeQuery(query, exec.conf.defaultConnection);
}
Aggregations