diff --git a/jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/JdbcSerDe.java b/jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/JdbcSerDe.java index 87ba682..0499299 100644 --- a/jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/JdbcSerDe.java +++ b/jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/JdbcSerDe.java @@ -46,6 +46,7 @@ import org.apache.hive.storage.jdbc.conf.JdbcStorageConfig; import java.math.BigDecimal; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -59,7 +60,7 @@ private PrimitiveTypeInfo[] hiveColumnTypes; private ObjectInspector inspector; private List row; - + private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); /* * This method gets called multiple times by Hive. On some invocations, the properties will be empty. @@ -197,7 +198,11 @@ public Object deserialize(Writable blob) throws SerDeException { case CHAR: case VARCHAR: case STRING: - rowVal = rowVal.toString(); + if (rowVal instanceof java.sql.Date) { + rowVal = df.format((java.sql.Date)rowVal); + } else { + rowVal = rowVal.toString(); + } break; case DATE: if (rowVal instanceof java.sql.Date) { diff --git a/ql/src/test/queries/clientpositive/external_jdbc_table.q b/ql/src/test/queries/clientpositive/external_jdbc_table.q index 3e629d2..36ed93a 100644 --- a/ql/src/test/queries/clientpositive/external_jdbc_table.q +++ b/ql/src/test/queries/clientpositive/external_jdbc_table.q @@ -34,19 +34,19 @@ FROM src SELECT dboutput ( 'jdbc:derby:;databaseName=${system:test.tmp.dir}/test_derby_as_external_table_db;create=true','','', -'CREATE TABLE SIMPLE_DERBY_TABLE2 ("ikey" INTEGER, "bkey" BIGINT, "fkey" REAL, "dkey" DOUBLE )' ), +'CREATE TABLE SIMPLE_DERBY_TABLE2 ("ikey" INTEGER, "bkey" BIGINT, "fkey" REAL, "dkey" DOUBLE, "datekey" DATE)' ), dboutput('jdbc:derby:;databaseName=${system:test.tmp.dir}/test_derby_as_external_table_db;create=true','','', -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','20','20','20.0','20.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','20','20','20.0','20.0','1999-02-22'), dboutput('jdbc:derby:;databaseName=${system:test.tmp.dir}/test_derby_as_external_table_db;create=true','','', -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','-20','8','9.0','11.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','-20','8','9.0','11.0','2000-03-15'), dboutput('jdbc:derby:;databaseName=${system:test.tmp.dir}/test_derby_as_external_table_db;create=true','','', -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','101','-16','66.0','-75.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','101','-16','66.0','-75.0','2010-04-01'), dboutput('jdbc:derby:;databaseName=${system:test.tmp.dir}/test_derby_as_external_table_db;create=true','','', -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','40','50','-455.4543','330.767') +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','40','50','-455.4543','330.767','2010-04-02') limit 1; @@ -75,7 +75,8 @@ CREATE EXTERNAL TABLE ext_simple_derby_table2 ikey int, bkey bigint, fkey float, - dkey double + dkey double, + datekey string ) STORED BY 'org.apache.hive.storage.jdbc.JdbcStorageHandler' TBLPROPERTIES ( @@ -96,7 +97,7 @@ select dkey,fkey,bkey,ikey from ext_simple_derby_table1; select bkey+ikey,fkey+dkey from ext_simple_derby_table1; select abs(dkey),abs(ikey),abs(fkey),abs(bkey) from ext_simple_derby_table1; - +select datekey from ext_simple_derby_table2; --Test aggregation select count(*) from ext_simple_derby_table1; @@ -184,4 +185,4 @@ SELECT bkey FROM ext_simple_derby_table2; ---select dkey from ext_simple_derby_table1 order by dkey limit 10 offset 60; \ No newline at end of file +--select dkey from ext_simple_derby_table1 order by dkey limit 10 offset 60; diff --git a/ql/src/test/results/clientpositive/llap/external_jdbc_table.q.out b/ql/src/test/results/clientpositive/llap/external_jdbc_table.q.out index 2fa37f5..a3b0ac4 100644 --- a/ql/src/test/results/clientpositive/llap/external_jdbc_table.q.out +++ b/ql/src/test/results/clientpositive/llap/external_jdbc_table.q.out @@ -64,19 +64,19 @@ PREHOOK: query: FROM src SELECT #### A masked pattern was here #### -'CREATE TABLE SIMPLE_DERBY_TABLE2 ("ikey" INTEGER, "bkey" BIGINT, "fkey" REAL, "dkey" DOUBLE )' ), +'CREATE TABLE SIMPLE_DERBY_TABLE2 ("ikey" INTEGER, "bkey" BIGINT, "fkey" REAL, "dkey" DOUBLE, "datekey" DATE)' ), #### A masked pattern was here #### -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','20','20','20.0','20.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','20','20','20.0','20.0','1999-02-22'), #### A masked pattern was here #### -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','-20','8','9.0','11.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','-20','8','9.0','11.0','2000-03-15'), #### A masked pattern was here #### -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','101','-16','66.0','-75.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','101','-16','66.0','-75.0','2010-04-01'), #### A masked pattern was here #### -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','40','50','-455.4543','330.767') +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','40','50','-455.4543','330.767','2010-04-02') limit 1 PREHOOK: type: QUERY @@ -87,19 +87,19 @@ POSTHOOK: query: FROM src SELECT #### A masked pattern was here #### -'CREATE TABLE SIMPLE_DERBY_TABLE2 ("ikey" INTEGER, "bkey" BIGINT, "fkey" REAL, "dkey" DOUBLE )' ), +'CREATE TABLE SIMPLE_DERBY_TABLE2 ("ikey" INTEGER, "bkey" BIGINT, "fkey" REAL, "dkey" DOUBLE, "datekey" DATE)' ), #### A masked pattern was here #### -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','20','20','20.0','20.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','20','20','20.0','20.0','1999-02-22'), #### A masked pattern was here #### -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','-20','8','9.0','11.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','-20','8','9.0','11.0','2000-03-15'), #### A masked pattern was here #### -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','101','-16','66.0','-75.0'), +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','101','-16','66.0','-75.0','2010-04-01'), #### A masked pattern was here #### -'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey") VALUES (?,?,?,?)','40','50','-455.4543','330.767') +'INSERT INTO SIMPLE_DERBY_TABLE2 ("ikey","bkey","fkey","dkey","datekey") VALUES (?,?,?,?,?)','40','50','-455.4543','330.767','2010-04-02') limit 1 POSTHOOK: type: QUERY @@ -151,7 +151,8 @@ PREHOOK: query: CREATE EXTERNAL TABLE ext_simple_derby_table2 ikey int, bkey bigint, fkey float, - dkey double + dkey double, + datekey string ) STORED BY 'org.apache.hive.storage.jdbc.JdbcStorageHandler' TBLPROPERTIES ( @@ -171,7 +172,8 @@ POSTHOOK: query: CREATE EXTERNAL TABLE ext_simple_derby_table2 ikey int, bkey bigint, fkey float, - dkey double + dkey double, + datekey string ) STORED BY 'org.apache.hive.storage.jdbc.JdbcStorageHandler' TBLPROPERTIES ( @@ -234,6 +236,18 @@ POSTHOOK: Input: default@ext_simple_derby_table1 20.0 20 20.0 20 74.0 100 65.0 15 330.76 44 455.4540100097656 53 +PREHOOK: query: select datekey from ext_simple_derby_table2 +PREHOOK: type: QUERY +PREHOOK: Input: default@ext_simple_derby_table2 +#### A masked pattern was here #### +POSTHOOK: query: select datekey from ext_simple_derby_table2 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@ext_simple_derby_table2 +#### A masked pattern was here #### +1999-02-22 +2000-03-15 +2010-04-01 +2010-04-02 PREHOOK: query: select count(*) from ext_simple_derby_table1 PREHOOK: type: QUERY PREHOOK: Input: default@ext_simple_derby_table1