Return-Path: X-Original-To: apmail-hive-commits-archive@www.apache.org Delivered-To: apmail-hive-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3DB1B174C2 for ; Fri, 25 Sep 2015 01:39:45 +0000 (UTC) Received: (qmail 31259 invoked by uid 500); 25 Sep 2015 01:39:42 -0000 Delivered-To: apmail-hive-commits-archive@hive.apache.org Received: (qmail 31155 invoked by uid 500); 25 Sep 2015 01:39:42 -0000 Mailing-List: contact commits-help@hive.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: hive-dev@hive.apache.org Delivered-To: mailing list commits@hive.apache.org Received: (qmail 28572 invoked by uid 99); 25 Sep 2015 01:39:41 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Sep 2015 01:39:41 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A9CCBE0A1A; Fri, 25 Sep 2015 01:39:40 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: xuf@apache.org To: commits@hive.apache.org Date: Fri, 25 Sep 2015 01:40:18 -0000 Message-Id: In-Reply-To: <0d6526eecc944404bd1fd32595ca2990@git.apache.org> References: <0d6526eecc944404bd1fd32595ca2990@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [40/50] [abbrv] hive git commit: HIVE-11748: HivePreparedStatement's setTimestamp() does not quote value as required (Angus Smithson, reviewed by Sergio Pena) HIVE-11748: HivePreparedStatement's setTimestamp() does not quote value as required (Angus Smithson, reviewed by Sergio Pena) Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/cdc65dc7 Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/cdc65dc7 Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/cdc65dc7 Branch: refs/heads/beeline-cli Commit: cdc65dc7c6a0c725054839269a9c04ba02da0f5f Parents: b98a60d Author: Sergio Pena Authored: Wed Sep 23 14:18:16 2015 -0500 Committer: Sergio Pena Committed: Wed Sep 23 14:18:16 2015 -0500 ---------------------------------------------------------------------- NOTICE | 3 + .../org/apache/hive/jdbc/TestJdbcDriver2.java | 80 +++++++++++--------- .../apache/hive/jdbc/HivePreparedStatement.java | 4 +- 3 files changed, 48 insertions(+), 39 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/cdc65dc7/NOTICE ---------------------------------------------------------------------- diff --git a/NOTICE b/NOTICE index 32d89b1..5c862df 100644 --- a/NOTICE +++ b/NOTICE @@ -9,3 +9,6 @@ Copyright (c) 2010-2014 Oracle and/or its affiliates. This project includes software copyrighted by Microsoft Corporation and licensed under the Apache License, Version 2.0. + +This project includes software copyrighted by Dell SecureWorks and +licensed under the Apache License, Version 2.0. http://git-wip-us.apache.org/repos/asf/hive/blob/cdc65dc7/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java ---------------------------------------------------------------------- diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index b2dd2ab..3aa6bce 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -37,7 +37,9 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; import java.sql.Types; +import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -412,29 +414,28 @@ public class TestJdbcDriver2 { @Test public void testPrepareStatement() { - - String sql = "from (select count(1) from " + String sql = "FROM (SELECT 1 FROM " + tableName + " where 'not?param?not?param' <> 'not_param??not_param' and ?=? " + " and 1=? and 2=? and 3.0=? and 4.0=? and 'test\\'string\"'=? and 5=? and ?=? " + " and date '2012-01-01' = date ?" - + " ) t select '2011-03-25' ddate,'China',true bv, 10 num limit 10"; + + " and timestamp '2012-04-22 09:00:00.123456789' = timestamp ?" + + " ) t SELECT '2011-03-25' ddate,'China',true bv, 10 num LIMIT 1"; /////////////////////////////////////////////// //////////////////// correct testcase //////////////////// executed twice: once with the typed ps setters, once with the generic setObject ////////////////////////////////////////////// try { - PreparedStatement ps = createPreapredStatementUsingSetXXX(sql); - ResultSet res = ps.executeQuery(); - assertPreparedStatementResultAsExpected(res); - ps.close(); - - ps = createPreapredStatementUsingSetObject(sql); - res = ps.executeQuery(); - assertPreparedStatementResultAsExpected(res); - ps.close(); + try (PreparedStatement ps = createPreapredStatementUsingSetXXX(sql); + ResultSet res = ps.executeQuery()) { + assertPreparedStatementResultAsExpected(res); + } + try (PreparedStatement ps = createPreapredStatementUsingSetObject(sql); + ResultSet res = ps.executeQuery()) { + assertPreparedStatementResultAsExpected(res); + } } catch (Exception e) { e.printStackTrace(); fail(e.toString()); @@ -445,9 +446,8 @@ public class TestJdbcDriver2 { ////////////////////////////////////////////// // set nothing for prepared sql Exception expectedException = null; - try { - PreparedStatement ps = con.prepareStatement(sql); - ps.executeQuery(); + try (PreparedStatement ps = con.prepareStatement(sql); + ResultSet ignored = ps.executeQuery()) { } catch (Exception e) { expectedException = e; } @@ -457,11 +457,10 @@ public class TestJdbcDriver2 { // set some of parameters for prepared sql, not all of them. expectedException = null; - try { - PreparedStatement ps = con.prepareStatement(sql); + try (PreparedStatement ps = con.prepareStatement(sql)) { ps.setBoolean(1, true); ps.setBoolean(2, true); - ps.executeQuery(); + try (ResultSet ignored = ps.executeQuery()) {} } catch (Exception e) { expectedException = e; } @@ -471,16 +470,11 @@ public class TestJdbcDriver2 { // set the wrong type parameters for prepared sql. expectedException = null; - try { - PreparedStatement ps = con.prepareStatement(sql); - + try (PreparedStatement ps = con.prepareStatement(sql)) { // wrong type here ps.setString(1, "wrong"); - - assertTrue(true); - ResultSet res = ps.executeQuery(); - if (!res.next()) { - throw new Exception("there must be a empty result set"); + try (ResultSet res = ps.executeQuery()) { + assertFalse("ResultSet was not empty", res.next()); } } catch (Exception e) { expectedException = e; @@ -491,17 +485,15 @@ public class TestJdbcDriver2 { // setObject to the yet unknown type java.util.Date expectedException = null; - try { - PreparedStatement ps = con.prepareStatement(sql); + try (PreparedStatement ps = con.prepareStatement(sql)) { ps.setObject(1, new Date()); - ps.executeQuery(); + try (ResultSet ignored = ps.executeQuery()) {} } catch (Exception e) { expectedException = e; } assertNotNull( "Setting to an unknown type should throw an exception", expectedException); - } private PreparedStatement createPreapredStatementUsingSetObject(String sql) throws SQLException { @@ -509,7 +501,6 @@ public class TestJdbcDriver2 { ps.setObject(1, true); //setBoolean ps.setObject(2, true); //setBoolean - ps.setObject(3, Short.valueOf("1")); //setShort ps.setObject(4, 2); //setInt ps.setObject(5, 3f); //setFloat @@ -519,6 +510,7 @@ public class TestJdbcDriver2 { ps.setObject(9, (byte) 1); //setByte ps.setObject(10, (byte) 1); //setByte ps.setString(11, "2012-01-01"); //setString + ps.setObject(12, Timestamp.valueOf("2012-04-22 09:00:00.123456789")); //setTimestamp ps.setMaxRows(2); return ps; @@ -529,7 +521,6 @@ public class TestJdbcDriver2 { ps.setBoolean(1, true); //setBoolean ps.setBoolean(2, true); //setBoolean - ps.setShort(3, Short.valueOf("1")); //setShort ps.setInt(4, 2); //setInt ps.setFloat(5, 3f); //setFloat @@ -539,15 +530,17 @@ public class TestJdbcDriver2 { ps.setByte(9, (byte) 1); //setByte ps.setByte(10, (byte) 1); //setByte ps.setString(11, "2012-01-01"); //setString + ps.setTimestamp(12, Timestamp.valueOf("2012-04-22 09:00:00.123456789")); //setTimestamp ps.setMaxRows(2); return ps; } - private void assertPreparedStatementResultAsExpected(ResultSet res ) throws SQLException { + private void assertPreparedStatementResultAsExpected(ResultSet res) throws SQLException { assertNotNull(res); + assertTrue("ResultSet contained no rows", res.next()); - while (res.next()) { + do { assertEquals("2011-03-25", res.getString("ddate")); assertEquals("10", res.getString("num")); assertEquals((byte) 10, res.getByte("num")); @@ -561,9 +554,7 @@ public class TestJdbcDriver2 { assertNotNull(o); o = res.getObject("num"); assertNotNull(o); - } - res.close(); - assertTrue(true); + } while (res.next()); } /** @@ -2382,4 +2373,19 @@ public void testParseUrlHttpMode() throws SQLException, JdbcUriParseException, fail(e.toString()); } } + + @Test + public void testPrepareSetTimestamp() throws SQLException, ParseException { + String sql = String.format("SELECT * FROM %s WHERE c17 = ?", dataTypeTableName); + try (PreparedStatement ps = con.prepareStatement(sql)) { + Timestamp timestamp = Timestamp.valueOf("2012-04-22 09:00:00.123456789"); + ps.setTimestamp(1, timestamp); + // Ensure we find the single row which matches our timestamp (where field 1 has value 1) + try (ResultSet resultSet = ps.executeQuery()) { + assertTrue(resultSet.next()); + assertEquals(1, resultSet.getInt(1)); + assertFalse(resultSet.next()); + } + } + } } http://git-wip-us.apache.org/repos/asf/hive/blob/cdc65dc7/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java ---------------------------------------------------------------------- diff --git a/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java b/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java index 7687537..c28b7d6 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java @@ -607,7 +607,7 @@ public class HivePreparedStatement extends HiveStatement implements PreparedStat } else if (x instanceof Character) { setString(parameterIndex, x.toString()); } else if (x instanceof Timestamp) { - setString(parameterIndex, x.toString()); + setTimestamp(parameterIndex, (Timestamp) x); } else if (x instanceof BigDecimal) { setString(parameterIndex, x.toString()); } else { @@ -728,7 +728,7 @@ public class HivePreparedStatement extends HiveStatement implements PreparedStat */ public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { - this.parameters.put(parameterIndex, x.toString()); + this.parameters.put(parameterIndex, "'" + x.toString() + "'"); } /*