diff --git beeline/src/java/org/apache/hive/beeline/BeeLine.java beeline/src/java/org/apache/hive/beeline/BeeLine.java index dc79712054..8d10c81423 100644 --- beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -175,6 +175,7 @@ private static final String HIVE_CONF_PREFIX = "--hiveconf"; private static final String PROP_FILE_PREFIX = "--property-file"; public static final String PASSWD_MASK = "[passwd stripped]"; + public int fetchSize = 0; private final Map formats = map(new Object[] { "vertical", new VerticalOutputFormat(this), @@ -434,6 +435,13 @@ static Manifest getManifest() throws IOException { return null; } + public void setFetchSize(int fetchRows) { + if (fetchRows > 0) { + this.fetchSize = fetchRows; + } else { // if set to 0 or less, use default + this.fetchSize = 0; + } + } String getManifestAttribute(String name) { try { @@ -2347,6 +2355,7 @@ Statement createStatement() throws SQLException { if (signalHandler != null) { signalHandler.setStatement(stmnt); } + stmnt.setFetchSize(fetchSize); return stmnt; } diff --git beeline/src/java/org/apache/hive/beeline/Commands.java beeline/src/java/org/apache/hive/beeline/Commands.java index 338b105a2d..e288426f8e 100644 --- beeline/src/java/org/apache/hive/beeline/Commands.java +++ beeline/src/java/org/apache/hive/beeline/Commands.java @@ -52,6 +52,8 @@ import java.util.Properties; import java.util.Set; import java.util.TreeSet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -1053,6 +1055,17 @@ private boolean executeInternal(String sql, boolean call) { true, beeLine.getErrorStream()); } } + // anytime this property is set, reset the local value so it can be set on new Statement + Pattern setPattern = Pattern.compile("set\\s+(\\S+)\\s*=\\s*(-?\\d+)\\s*;*"); + Matcher matcher = setPattern.matcher(sql.toLowerCase()); + + if (matcher.find() && matcher.group(1).equals("hive.server2.thrift.resultset.default.fetch.size")) { + try { + beeLine.setFetchSize(Integer.parseInt(matcher.group(2))); + } catch (NumberFormatException nfe) { + // ignore + } + } } finally { if (logThread != null) { if (!logThread.isInterrupted()) { diff --git jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index d797d40642..0f7f3f65b6 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -143,11 +143,12 @@ private final List supportedProtocols = new LinkedList(); private int loginTimeout = 0; private TProtocolVersion protocol; - private int fetchSize = HiveStatement.DEFAULT_FETCH_SIZE; + private int fetchSize = 0; // default to server value private String initFile = null; private String wmPool = null, wmApp = null; private Properties clientInfo; private Subject loggedInSubject; + private int serverDefaultFetchSize = -1; /** * Get all direct HiveServer2 URLs from a ZooKeeper based HiveServer2 URL @@ -868,10 +869,12 @@ private void openSession() throws SQLException { sessHandle = openResp.getSessionHandle(); // Update fetchSize if modified by server - String serverFetchSize = + String serverFetchSizeString = openResp.getConfiguration().get("hive.server2.thrift.resultset.default.fetch.size"); - if (serverFetchSize != null) { - fetchSize = Integer.parseInt(serverFetchSize); + if (serverFetchSizeString != null) { + serverDefaultFetchSize = Integer.parseInt(serverFetchSizeString); + if (fetchSize <= 0) // use server-side value when either set to 0 or not set at all (-1) + fetchSize = serverDefaultFetchSize; } } catch (TException e) { LOG.error("Error opening session", e); diff --git jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java index 693203fab3..0f64db9743 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java @@ -815,8 +815,8 @@ public void setFetchSize(int rows) throws SQLException { } 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; + // In this case it means reverting it to the default value set on the connection. + // would be either the value set in URL or server side default } else { throw new SQLException("Fetch size must be greater or equal to 0"); } diff --git service/src/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java service/src/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java index 8f13fb3cca..93b86541b1 100644 --- service/src/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java +++ service/src/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java @@ -324,13 +324,11 @@ public TOpenSessionResp OpenSession(TOpenSessionReq req) throws TException { SessionHandle sessionHandle = getSessionHandle(req, resp); resp.setSessionHandle(sessionHandle.toTSessionHandle()); Map configurationMap = new HashMap(); - // Set the updated fetch size from the server into the configuration map for the client + // client sends its value to be stored in the sessionConf, server sends its value for the client to default to. HiveConf sessionConf = cliService.getSessionConf(sessionHandle); configurationMap.put( HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_DEFAULT_FETCH_SIZE.varname, - Integer.toString(sessionConf != null ? - sessionConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_DEFAULT_FETCH_SIZE) : - hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_DEFAULT_FETCH_SIZE))); + Integer.toString(hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_DEFAULT_FETCH_SIZE))); resp.setConfiguration(configurationMap); resp.setStatus(OK_STATUS); ThriftCLIServerContext context =