diff --git jdbc/pom.xml jdbc/pom.xml index 8cf5210..61de8f6 100644 --- jdbc/pom.xml +++ jdbc/pom.xml @@ -103,6 +103,13 @@ curator-framework ${curator.version} + + + junit + junit + ${junit.version} + test + @@ -139,6 +146,7 @@ ${basedir}/src/java + ${basedir}/src/test ${basedir}/src/resources diff --git jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java index d4041bb..49cc748 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java @@ -54,12 +54,13 @@ */ public class HiveStatement implements java.sql.Statement { public static final Log LOG = LogFactory.getLog(HiveStatement.class.getName()); + private static final int DEFAULT_FETCH_SIZE = 1000; private final HiveConnection connection; private TCLIService.Iface client; private TOperationHandle stmtHandle = null; private final TSessionHandle sessHandle; Map sessConf = new HashMap(); - private int fetchSize = 1000; + private int fetchSize = DEFAULT_FETCH_SIZE; private boolean isScrollableResultset = false; /** * We need to keep a reference to the result set to support the following: @@ -674,7 +675,16 @@ public void setFetchDirection(int direction) throws SQLException { @Override public void setFetchSize(int rows) throws SQLException { checkConnection("setFetchSize"); - fetchSize = rows; + if (rows > 0) { + fetchSize = rows; + } else if (rows == 0) { + // Javadoc for Statement interface states that if the value is zero + // then "fetch size" hint is ignored. + // In this case it means reverting it to the default value. + fetchSize = DEFAULT_FETCH_SIZE; + } else { + throw new SQLException("Fetch size must be greater or equal to 0"); + } } /* diff --git jdbc/src/test/org/apache/hive/jdbc/HiveStatementTest.java jdbc/src/test/org/apache/hive/jdbc/HiveStatementTest.java new file mode 100644 index 0000000..be23b10 --- /dev/null +++ jdbc/src/test/org/apache/hive/jdbc/HiveStatementTest.java @@ -0,0 +1,31 @@ +package org.apache.hive.jdbc; + +import org.junit.Test; + +import java.sql.SQLException; + +import static org.junit.Assert.assertEquals; + +public class HiveStatementTest { + + @Test + public void testSetFetchSize1() throws SQLException { + HiveStatement stmt = new HiveStatement(null, null, null); + stmt.setFetchSize(123); + assertEquals(123, stmt.getFetchSize()); + } + + @Test + public void testSetFetchSize2() throws SQLException { + HiveStatement stmt = new HiveStatement(null, null, null); + int initial = stmt.getFetchSize(); + stmt.setFetchSize(0); + assertEquals(initial, stmt.getFetchSize()); + } + + @Test(expected = SQLException.class) + public void testSetFetchSize3() throws SQLException { + HiveStatement stmt = new HiveStatement(null, null, null); + stmt.setFetchSize(-1); + } +}