Search in sources :

Example 81 with SQLException

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();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement)

Example 82 with SQLException

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();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) String(java.lang.String) SQLTimeoutException(java.sql.SQLTimeoutException) ParseException(java.text.ParseException) Exception(java.lang.Exception) SQLException(java.sql.SQLException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 83 with SQLException

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());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) ParseException(java.text.ParseException) Exception(java.lang.Exception) SQLException(java.sql.SQLException) ExpectedException(org.junit.rules.ExpectedException)

Example 84 with SQLException

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();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Test(org.junit.Test)

Example 85 with SQLException

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);
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Date(java.sql.Date)

Aggregations

SQLException (java.sql.SQLException)6792 PreparedStatement (java.sql.PreparedStatement)3048 ResultSet (java.sql.ResultSet)2426 Connection (java.sql.Connection)1871 ArrayList (java.util.ArrayList)972 Test (org.junit.Test)873 Statement (java.sql.Statement)779 IOException (java.io.IOException)341 List (java.util.List)335 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)298 Properties (java.util.Properties)255 DatabaseException (net.jforum.exceptions.DatabaseException)249 HashMap (java.util.HashMap)232 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)184 Timestamp (java.sql.Timestamp)171 CallableStatement (java.sql.CallableStatement)165 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)160 DalHints (com.ctrip.platform.dal.dao.DalHints)159 Map (java.util.Map)125 Date (java.util.Date)123