diff --git data/files/datatypes.txt data/files/datatypes.txt index 40e43ff..9663bf4 100644 --- data/files/datatypes.txt +++ data/files/datatypes.txt @@ -1,3 +1,3 @@ \N\N\N\N\N\N\N\N\N\N\N\N\N\N\N\N\N\N\N --1false-1.1\N\N\N-1-1-1.0-1\N\N\N\N\N +-1false-1.1\N\N\N-1-1-1.0-1\N\N2012-04-22 09:00:00\N\N 1true1.11121x2ykva92.2111.01abcd1111213142212212x1abcd22012-04-22 09:00:00.123456789123456789.0123456YWJjZA== diff --git jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java index 775ebee..56d26f5 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java @@ -42,6 +42,7 @@ import java.util.List; import java.util.Map; +import org.apache.hadoop.hive.serde2.io.TimestampWritable; import org.apache.hive.service.cli.TableSchema; import org.apache.hive.service.cli.Type; import org.apache.hive.service.cli.thrift.TBoolValue; @@ -596,13 +597,17 @@ public Statement getStatement() throws SQLException { */ public String getString(int columnIndex) throws SQLException { Object value = getColumnValue(columnIndex); - if (wasNull) { - return null; - } - if (value instanceof byte[]){ - return new String((byte[])value); + String result = null; + if (!wasNull) { // SQL null is always returned as Java null + if (value instanceof byte[]) { // byte arrays are interpreted as strings + result = new String((byte[])value); + } else if (value instanceof Timestamp) { // avoid trailing ".0" with TimestampWritable + result = new TimestampWritable((Timestamp)value).toString(); + } else { // all other cases just use "toString" + result = value.toString(); + } } - return value.toString(); + return result; } public String getString(String columnName) throws SQLException { diff --git jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java index b1bacd9..7b72496 100644 --- jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java +++ jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -431,8 +431,11 @@ public void testDataTypes() throws Exception { assertEquals("{}", res.getString(14)); assertEquals("[null, null]", res.getString(15)); assertEquals("[]", res.getString(16)); - assertEquals(null, res.getString(17)); - assertEquals(null, res.getTimestamp(17)); + + //format without trailing 0 is compatible with hive cli + // and also compatible with Timestamp.valueOf(String) + assertEquals("2012-04-22 09:00:00", res.getString(17)); + assertEquals("2012-04-22 09:00:00.0", res.getTimestamp(17).toString()); assertEquals(null, res.getBigDecimal(18)); assertEquals(null, res.getString(19));