DRILL-3566: Fix: PreparedStatement.executeQuery() got ClassCastException. Main: Restored DrillResultSetImpl(...)'s statement parameter from overly restrictive DrillStatementImpl to AvaticaStatement and removed caller cast that was throwing. (Relatedly, adjusted getStatement() and moved internal casting from statement to connection.)
Added basic test of querying via PreparedStatement. [PreparedStatementTest] Added some case test of statement-creation methods. [ConnectionTest] Removed now-unneeded DrillPrepareResult cast. Eliminated redundant getConnection() and getClient calls, intermediate variables, etc. Fixed comment error. this closes #143 Project: http://git-wip-us.apache.org/repos/asf/drill/repo Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/6db7c05a Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/6db7c05a Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/6db7c05a Branch: refs/heads/master Commit: 6db7c05a95950ccb91dddb4e6097da8f72c4dd35 Parents: 7a864a4 Author: dbarclay <[email protected]> Authored: Mon Jul 27 19:27:50 2015 -0700 Committer: adeneche <[email protected]> Committed: Tue Sep 8 15:35:24 2015 -0700 ---------------------------------------------------------------------- .../drill/jdbc/impl/DrillJdbc41Factory.java | 4 +- .../jdbc/impl/DrillPreparedStatementImpl.java | 3 +- .../drill/jdbc/impl/DrillResultSetImpl.java | 28 ++- .../org/apache/drill/jdbc/ConnectionTest.java | 203 ++++++++++++++++++- .../org/apache/drill/jdbc/JdbcTestBase.java | 3 +- .../drill/jdbc/PreparedStatementTest.java | 34 +++- 6 files changed, 248 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/drill/blob/6db7c05a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillJdbc41Factory.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillJdbc41Factory.java b/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillJdbc41Factory.java index 11191ae..9723358 100644 --- a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillJdbc41Factory.java +++ b/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillJdbc41Factory.java @@ -103,9 +103,7 @@ public class DrillJdbc41Factory extends DrillFactory { TimeZone timeZone) { final ResultSetMetaData metaData = newResultSetMetaData(statement, prepareResult.getColumnList()); - return new DrillResultSetImpl( (DrillStatementImpl) statement, - (DrillPrepareResult) prepareResult, - metaData, timeZone); + return new DrillResultSetImpl(statement, prepareResult, metaData, timeZone); } @Override http://git-wip-us.apache.org/repos/asf/drill/blob/6db7c05a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillPreparedStatementImpl.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillPreparedStatementImpl.java b/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillPreparedStatementImpl.java index 86683cb..871298f 100644 --- a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillPreparedStatementImpl.java +++ b/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillPreparedStatementImpl.java @@ -30,7 +30,8 @@ import net.hydromatic.avatica.AvaticaPreparedStatement; * Implementation of {@link java.sql.PreparedStatement} for Drill. * * <p> - * This class has sub-classes which implement JDBC 3.0 and JDBC 4.0 APIs; it is instantiated using + * This class has sub-classes which implement JDBC 3.0 and JDBC 4.0 APIs; it is + * instantiated using * {@link net.hydromatic.avatica.AvaticaFactory#newPreparedStatement}. * </p> */ http://git-wip-us.apache.org/repos/asf/drill/blob/6db7c05a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillResultSetImpl.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillResultSetImpl.java b/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillResultSetImpl.java index 95fee00..039d133 100644 --- a/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillResultSetImpl.java +++ b/exec/jdbc/src/main/java/org/apache/drill/jdbc/impl/DrillResultSetImpl.java @@ -43,6 +43,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import net.hydromatic.avatica.AvaticaPrepareResult; import net.hydromatic.avatica.AvaticaResultSet; +import net.hydromatic.avatica.AvaticaStatement; import org.apache.drill.common.exceptions.UserException; import org.apache.drill.exec.ExecConstants; @@ -75,7 +76,7 @@ class DrillResultSetImpl extends AvaticaResultSet implements DrillResultSet { private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillResultSetImpl.class); - private final DrillStatementImpl statement; + private final DrillConnectionImpl connection; SchemaChangeListener changeListener; final ResultsListener resultsListener; @@ -87,18 +88,16 @@ class DrillResultSetImpl extends AvaticaResultSet implements DrillResultSet { boolean hasPendingCancelationNotification; - DrillResultSetImpl(DrillStatementImpl statement, AvaticaPrepareResult prepareResult, + DrillResultSetImpl(AvaticaStatement statement, AvaticaPrepareResult prepareResult, ResultSetMetaData resultSetMetaData, TimeZone timeZone) { super(statement, prepareResult, resultSetMetaData, timeZone); - this.statement = statement; + connection = (DrillConnectionImpl) statement.getConnection(); + client = connection.getClient(); final int batchQueueThrottlingThreshold = - this.getStatement().getConnection().getClient().getConfig().getInt( + client.getConfig().getInt( ExecConstants.JDBC_BATCH_QUEUE_THROTTLING_THRESHOLD ); - resultsListener = new ResultsListener( batchQueueThrottlingThreshold ); - DrillConnection c = (DrillConnection) statement.getConnection(); - DrillClient client = c.getClient(); + resultsListener = new ResultsListener(batchQueueThrottlingThreshold); batchLoader = new RecordBatchLoader(client.getAllocator()); - this.client = client; cursor = new DrillCursor(this); } @@ -107,7 +106,7 @@ class DrillResultSetImpl extends AvaticaResultSet implements DrillResultSet { * ResultSet is closed. * * @throws ExecutionCanceledSqlException if ResultSet is closed because of - * cancelation and no QueryCanceledSqlException had been thrown yet + * cancelation and no QueryCanceledSqlException has been thrown yet * for this ResultSet * @throws AlreadyClosedSqlException if ResultSet is closed * @throws SQLException if error in calling {@link #isClosed()} @@ -850,9 +849,9 @@ class DrillResultSetImpl extends AvaticaResultSet implements DrillResultSet { } @Override - public DrillStatementImpl getStatement() { - // Note: No already-closed exception for close(). - return statement; + public AvaticaStatement getStatement() { + // Note: No already-closed exception for getStatement(). + return super.getStatement(); } @Override @@ -1334,10 +1333,7 @@ class DrillResultSetImpl extends AvaticaResultSet implements DrillResultSet { @Override protected DrillResultSetImpl execute() throws SQLException{ - DrillConnectionImpl connection = (DrillConnectionImpl) statement.getConnection(); - - connection.getClient().runQuery(QueryType.SQL, this.prepareResult.getSql(), - resultsListener); + client.runQuery(QueryType.SQL, this.prepareResult.getSql(), resultsListener); connection.getDriver().handler.onStatementExecute(statement, null); super.execute(); http://git-wip-us.apache.org/repos/asf/drill/blob/6db7c05a/exec/jdbc/src/test/java/org/apache/drill/jdbc/ConnectionTest.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/ConnectionTest.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/ConnectionTest.java index 8735146..198cf4c 100644 --- a/exec/jdbc/src/test/java/org/apache/drill/jdbc/ConnectionTest.java +++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/ConnectionTest.java @@ -21,15 +21,20 @@ import org.apache.drill.jdbc.Driver; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertTrue; + import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLFeatureNotSupportedException; import java.sql.SQLException; +import java.sql.Statement; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -44,6 +49,7 @@ public class ConnectionTest extends JdbcTestBase { private static ExecutorService executor; + @BeforeClass public static void setUpConnection() throws SQLException { // (Note: Can't use JdbcTest's connect(...) because JdbcTest closes @@ -60,6 +66,201 @@ public class ConnectionTest extends JdbcTestBase { executor.shutdown(); } + private static void emitSupportExceptionWarning() { + System.err.println( + "Note: Still throwing older-Avatica UnsupportedOperationException" + + " instead of less-noncompliant SQLFeatureNotSupportedException" ); + } + + + //////////////////////////////////////// + // Basic tests of statement creation methods (not necessarily executing + // statements): + + ////////// + // Simplest cases of createStatement, prepareStatement, prepareCall: + + @Test + public void testCreateStatementBasicCaseWorks() throws SQLException { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery( "VALUES 1" ); + assertTrue( rs.next() ); + } + + @Test + public void testPrepareStatementBasicCaseWorks() throws SQLException { + PreparedStatement stmt = connection.prepareStatement( "VALUES 1" ); + ResultSet rs = stmt.executeQuery(); + assertTrue( rs.next() ); + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + public void testPrepareCallThrows() throws SQLException { + try { + connection.prepareCall( "VALUES 1" ); + } + catch ( UnsupportedOperationException e) { + // TODO(DRILL-2769): Purge this mapping when right exception is thrown. + emitSupportExceptionWarning(); + throw new SQLFeatureNotSupportedException( + "Note: Still throwing UnsupportedOperationException ", e ); + } + } + + ////////// + // createStatement(int, int): + + @Test + public void testCreateStatement_overload2_supportedCase_returns() throws SQLException { + connection.createStatement( ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_READ_ONLY ); + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + @Ignore( "until unsupported characteristics are rejected" ) + public void testCreateStatement_overload2_unsupportedType1_throws() throws SQLException { + connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_READ_ONLY ); + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + @Ignore( "until unsupported characteristics are rejected" ) + public void testCreateStatement_overload2_unsupportedType2_throws() throws SQLException { + connection.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, + ResultSet.CONCUR_READ_ONLY); + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + @Ignore( "until unsupported characteristics are rejected" ) + public void testCreateStatement_overload2_unsupportedConcurrency_throws() throws SQLException { + connection.createStatement( ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_UPDATABLE ); + } + + + ////////// + // prepareStatement(String, int, int, int): + + @Test + public void testPrepareStatement_overload2_supportedCase_returns() throws SQLException { + connection.prepareStatement( "VALUES 1", + ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_READ_ONLY ); + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + @Ignore( "until unsupported characteristics are rejected" ) + public void testPrepareStatement_overload2_unsupportedType1_throws() throws SQLException { + connection.prepareStatement( "VALUES 1", + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_READ_ONLY ); + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + @Ignore( "until unsupported characteristics are rejected" ) + public void testPrepareStatement_overload2_unsupportedType2_throws() throws SQLException { + connection.prepareStatement( "VALUES 1", + ResultSet.TYPE_SCROLL_SENSITIVE, + ResultSet.CONCUR_READ_ONLY ); + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + @Ignore( "until unsupported characteristics are rejected" ) + public void testPrepareStatement_overload2_unsupportedConcurrency_throws() throws SQLException { + connection.prepareStatement( "VALUES 1", + ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_UPDATABLE ); + } + + + ////////// + // createStatement(int, int, int) (case not covered with + // createStatement(int, int)): + + + @Test( expected = SQLFeatureNotSupportedException.class ) + @Ignore( "until unsupported characteristics are rejected" ) + public void testCreateStatement_overload3_unsupportedHoldability_throws() throws SQLException { + connection.createStatement( ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_READ_ONLY, + ResultSet.CLOSE_CURSORS_AT_COMMIT); + } + + + ////////// + // prepareStatement(int, int, int) (case not covered with + // prepareStatement(int, int)): + + @Test( expected = SQLFeatureNotSupportedException.class ) + @Ignore( "until unsupported characteristics are rejected" ) + public void testPrepareStatement_overload3_unsupportedHoldability_throws() throws SQLException { + connection.prepareStatement( "VALUES 1", + ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_READ_ONLY, + ResultSet.CLOSE_CURSORS_AT_COMMIT ); + } + + ////////// + // prepareCall(String, int, int, int): + + @Test( expected = SQLFeatureNotSupportedException.class ) + public void testCreateCall_overload3_throws() throws SQLException { + try { + connection.prepareCall( "VALUES 1", + ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_READ_ONLY, + ResultSet.HOLD_CURSORS_OVER_COMMIT ); + } + catch ( UnsupportedOperationException e) { + // TODO(DRILL-2769): Purge this mapping when right exception is thrown. + emitSupportExceptionWarning(); + throw new SQLFeatureNotSupportedException( + "Note: Still throwing UnsupportedOperationException ", e ); + } + } + + ////////// + // remaining prepareStatement(...): + + @Test( expected = SQLFeatureNotSupportedException.class ) + public void testPrepareStatement_overload4_throws() throws SQLException { + try { + connection.prepareStatement( "VALUES 1", Statement.RETURN_GENERATED_KEYS ); + } + catch ( UnsupportedOperationException e) { + // TODO(DRILL-2769): Purge this mapping when right exception is thrown. + emitSupportExceptionWarning(); + throw new SQLFeatureNotSupportedException( + "Note: Still throwing UnsupportedOperationException ", e ); + } + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + public void testPrepareStatement_overload5_throws() throws SQLException { + try { + connection.prepareStatement( "VALUES 1", new int[] { 1 } ); + } + catch ( UnsupportedOperationException e) { + // TODO(DRILL-2769): Purge this mapping when right exception is thrown. + emitSupportExceptionWarning(); + throw new SQLFeatureNotSupportedException( + "Note: Still throwing UnsupportedOperationException ", e ); + } + } + + @Test( expected = SQLFeatureNotSupportedException.class ) + public void testPrepareStatement_overload6_throws() throws SQLException { + try { + connection.prepareStatement( "VALUES 1 AS colA", new String[] { "colA" } ); + } + catch ( UnsupportedOperationException e) { + // TODO(DRILL-2769): Purge this mapping when right exception is thrown. + emitSupportExceptionWarning(); + throw new SQLFeatureNotSupportedException( + "Note: Still throwing UnsupportedOperationException ", e ); + } + } + //////////////////////////////////////// // Network timeout methods: http://git-wip-us.apache.org/repos/asf/drill/blob/6db7c05a/exec/jdbc/src/test/java/org/apache/drill/jdbc/JdbcTestBase.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/JdbcTestBase.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/JdbcTestBase.java index af49f7d..7fb601d 100644 --- a/exec/jdbc/src/test/java/org/apache/drill/jdbc/JdbcTestBase.java +++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/JdbcTestBase.java @@ -43,7 +43,8 @@ import org.junit.runner.Description; // (e.g., the reusing of connections, the automatic interception of test // failures and resetting of connections, etc.). public class JdbcTestBase extends ExecTest { - static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(JdbcTestBase.class); + @SuppressWarnings("unused") + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(JdbcTestBase.class); @Rule public final TestRule watcher = new TestWatcher() { http://git-wip-us.apache.org/repos/asf/drill/blob/6db7c05a/exec/jdbc/src/test/java/org/apache/drill/jdbc/PreparedStatementTest.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/PreparedStatementTest.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/PreparedStatementTest.java index ad3538d..bea309f 100644 --- a/exec/jdbc/src/test/java/org/apache/drill/jdbc/PreparedStatementTest.java +++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/PreparedStatementTest.java @@ -17,7 +17,9 @@ */ package org.apache.drill.jdbc; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.hamcrest.CoreMatchers.*; import org.hamcrest.Matcher; @@ -30,10 +32,14 @@ import java.sql.Clob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +/** + * Test for Drill's implementation of PreparedStatement's methods. + */ public class PreparedStatementTest extends JdbcTestBase { /** Fuzzy matcher for parameters-not-supported message assertions. (Based on @@ -57,12 +63,30 @@ public class PreparedStatementTest extends JdbcTestBase { connection.close(); } + + ////////// + // Basic querying-works test: + + /** Tests that basic executeQuery() (with query statement) works. */ + @Test + public void testExecuteQueryBasicCaseWorks() throws SQLException { + PreparedStatement stmt = connection.prepareStatement( "VALUES 11" ); + ResultSet rs = stmt.executeQuery(); + assertThat( "Unexpected column count", + rs.getMetaData().getColumnCount(), equalTo( 1 ) ); + assertTrue( "No expected first row", rs.next() ); + assertThat( rs.getInt( 1 ), equalTo( 11 ) ); + assertFalse( "Unexpected second row", rs.next() ); + } + + + ////////// // Parameters-not-implemented tests: /** Tests that basic case of trying to set parameter says not supported. */ @Test( expected = SQLFeatureNotSupportedException.class ) public void testParamSettingSaysUnsupported() throws SQLException { - PreparedStatement prepStmt = connection.prepareStatement( "SELECT ?, ?" ); + PreparedStatement prepStmt = connection.prepareStatement( "VALUES ?, ?" ); try { prepStmt.setInt( 0, 123456789 ); } @@ -77,7 +101,7 @@ public class PreparedStatementTest extends JdbcTestBase { /** Tests that "not supported" has priority over "bad index" check. */ @Test( expected = SQLFeatureNotSupportedException.class ) public void testParamSettingWithImpossibleIndexSaysUnsupported() throws SQLException { - PreparedStatement prepStmt = connection.prepareStatement( "SELECT ?, ?" ); + PreparedStatement prepStmt = connection.prepareStatement( "VALUES ?, ?" ); try { prepStmt.setString( -1, "some value" ); } @@ -92,7 +116,7 @@ public class PreparedStatementTest extends JdbcTestBase { /** Tests that "not supported" has priority over "bad index" check. */ @Test( expected = SQLFeatureNotSupportedException.class ) public void testParamSettingWithInconsistentIndexSaysUnsupported() throws SQLException { - PreparedStatement prepStmt = connection.prepareStatement( "SELECT ?, ?" ); + PreparedStatement prepStmt = connection.prepareStatement( "VALUES ?, ?" ); try { prepStmt.setBytes( 4, null ); } @@ -108,7 +132,7 @@ public class PreparedStatementTest extends JdbcTestBase { * check. */ @Test( expected = SQLFeatureNotSupportedException.class ) public void testParamSettingWhenNoParametersIndexSaysUnsupported() throws SQLException { - PreparedStatement prepStmt = connection.prepareStatement( "SELECT 1" ); + PreparedStatement prepStmt = connection.prepareStatement( "VALUES 1" ); try { prepStmt.setBytes( 4, null ); } @@ -124,7 +148,7 @@ public class PreparedStatementTest extends JdbcTestBase { * check. */ @Test( expected = SQLFeatureNotSupportedException.class ) public void testParamSettingWhenUnsupportedTypeSaysUnsupported() throws SQLException { - PreparedStatement prepStmt = connection.prepareStatement( "SELECT 1" ); + PreparedStatement prepStmt = connection.prepareStatement( "VALUES 1" ); try { prepStmt.setClob( 2, (Clob) null ); }
