Return-Path: X-Original-To: apmail-tajo-commits-archive@minotaur.apache.org Delivered-To: apmail-tajo-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9C24F11E41 for ; Wed, 9 Jul 2014 04:10:19 +0000 (UTC) Received: (qmail 89027 invoked by uid 500); 9 Jul 2014 04:10:19 -0000 Delivered-To: apmail-tajo-commits-archive@tajo.apache.org Received: (qmail 88923 invoked by uid 500); 9 Jul 2014 04:10:19 -0000 Mailing-List: contact commits-help@tajo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tajo.apache.org Delivered-To: mailing list commits@tajo.apache.org Received: (qmail 88479 invoked by uid 99); 9 Jul 2014 04:10:19 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Jul 2014 04:10:19 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id DAFD89A8C43; Wed, 9 Jul 2014 04:10:18 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: hyunsik@apache.org To: commits@tajo.apache.org Date: Wed, 09 Jul 2014 04:10:30 -0000 Message-Id: <22a1bc7d250a4ba0be84aa8c89a93e54@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [13/50] [abbrv] git commit: TAJO-844: JDBC should be support getTime, getDate, and getTimestamp. (Hyoungjun Kim via hyunsik) TAJO-844: JDBC should be support getTime, getDate, and getTimestamp. (Hyoungjun Kim via hyunsik) Closes #19 Project: http://git-wip-us.apache.org/repos/asf/tajo/repo Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/06be0608 Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/06be0608 Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/06be0608 Branch: refs/heads/window_function Commit: 06be06085698e575414994a26956056f326f77dd Parents: 500c683 Author: Hyunsik Choi Authored: Tue Jun 10 19:03:29 2014 -0700 Committer: Hyunsik Choi Committed: Tue Jun 10 19:06:24 2014 -0700 ---------------------------------------------------------------------- CHANGES | 3 + .../org/apache/tajo/jdbc/TajoResultSetBase.java | 204 ++++++++++++------- .../apache/tajo/util/datetime/DateTimeUtil.java | 4 + .../apache/tajo/engine/query/TestSortQuery.java | 3 + .../org/apache/tajo/jdbc/TestResultSet.java | 92 ++++++++- 5 files changed, 230 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/06be0608/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 1e4fb8f..447e520 100644 --- a/CHANGES +++ b/CHANGES @@ -21,6 +21,9 @@ Release 0.9.0 - unreleased IMPROVEMENT + TAJO-844: JDBC should be support getTime, getDate, and getTimestamp. + (Hyoungjun Kim via hyunsik) + TAJO-843: implements COALESCE for BOOLEAN, DATE, TIME, TIMESTAMP. (Hyoungjun Kim via hyunsik) http://git-wip-us.apache.org/repos/asf/tajo/blob/06be0608/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java b/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java index bdcf216..d189c78 100644 --- a/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java +++ b/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java @@ -23,6 +23,8 @@ import org.apache.tajo.common.TajoDataTypes; import org.apache.tajo.conf.TajoConf; import org.apache.tajo.datum.*; import org.apache.tajo.storage.Tuple; +import org.apache.tajo.util.datetime.DateTimeUtil; +import org.apache.tajo.util.datetime.TimeMeta; import java.io.IOException; import java.io.InputStream; @@ -32,6 +34,7 @@ import java.net.URL; import java.sql.*; import java.util.Calendar; import java.util.Map; +import java.util.TimeZone; public abstract class TajoResultSetBase implements ResultSet { protected int curRow; @@ -218,16 +221,18 @@ public abstract class TajoResultSetBase implements ResultSet { case INT8: return d.asInt8(); case TEXT: case CHAR: - case DATE: case VARCHAR: return d.asChars(); case FLOAT4: return d.asFloat4(); case FLOAT8: return d.asFloat8(); case NUMERIC: return d.asFloat8(); + case DATE: { + return getDate((DateDatum)d, TajoConf.getCurrentTimeZone()); + } case TIME: { - return ((TimeDatum)d).asChars(TajoConf.getCurrentTimeZone(), false); + return getTime((TimeDatum)d, TajoConf.getCurrentTimeZone()); } case TIMESTAMP: { - return ((TimestampDatum)d).asChars(TajoConf.getCurrentTimeZone(), false); + return getTimestamp((TimestampDatum) d, TajoConf.getCurrentTimeZone()); } default: return d.asChars(); } @@ -235,7 +240,7 @@ public abstract class TajoResultSetBase implements ResultSet { @Override public Object getObject(String name) throws SQLException { - return getObject(findColumn(name)); + return getObject(findColumn(name) + 1); } @Override @@ -295,6 +300,127 @@ public abstract class TajoResultSetBase implements ResultSet { } @Override + public Date getDate(int fieldId) throws SQLException { + Datum datum = cur.get(fieldId - 1); + handleNull(datum); + if (wasNull) { + return null; + } + + return getDate((DateDatum)datum, TajoConf.getCurrentTimeZone()); + } + + @Override + public Date getDate(String name) throws SQLException { + return getDate(findColumn(name) + 1); + } + + @Override + public Date getDate(int fieldId, Calendar x) throws SQLException { + Datum datum = cur.get(fieldId - 1); + handleNull(datum); + if (wasNull) { + return null; + } + + return getDate((DateDatum)datum, x.getTimeZone()); + } + + @Override + public Date getDate(String name, Calendar x) throws SQLException { + return getDate(findColumn(name) + 1, x); + } + + private Date getDate(DateDatum datum, TimeZone tz) { + TimeMeta tm = datum.toTimeMeta(); + if (tz != null) { + DateTimeUtil.toUserTimezone(tm, tz); + } + return new Date(DateTimeUtil.julianTimeToJavaTime(DateTimeUtil.toJulianTimestamp(tm))); + } + + @Override + public Time getTime(int fieldId) throws SQLException { + Datum datum = cur.get(fieldId - 1); + handleNull(datum); + if (wasNull) { + return null; + } + + return getTime((TimeDatum)datum, TajoConf.getCurrentTimeZone()); + + } + + @Override + public Time getTime(String name) throws SQLException { + return getTime(findColumn(name) + 1); + } + + @Override + public Time getTime(int fieldId, Calendar x) throws SQLException { + Datum datum = cur.get(fieldId - 1); + handleNull(datum); + if (wasNull) { + return null; + } + + return getTime((TimeDatum)datum, x.getTimeZone()); + } + + @Override + public Time getTime(String name, Calendar x) throws SQLException { + return getTime(findColumn(name) + 1, x); + } + + private Time getTime(TimeDatum datum, TimeZone tz) { + TimeMeta tm = datum.toTimeMeta(); + if (tz != null) { + DateTimeUtil.toUserTimezone(tm, tz); + } + return new Time(DateTimeUtil.toJavaTime(tm.hours, tm.minutes, tm.secs, tm.fsecs)); + } + + @Override + public Timestamp getTimestamp(int fieldId) throws SQLException { + Datum datum = cur.get(fieldId - 1); + handleNull(datum); + if (wasNull) { + return null; + } + + return getTimestamp((TimestampDatum)datum, TajoConf.getCurrentTimeZone()); + } + + @Override + public Timestamp getTimestamp(String name) throws SQLException { + return getTimestamp(findColumn(name) + 1); + } + + @Override + public Timestamp getTimestamp(int fieldId, Calendar x) throws SQLException { + Datum datum = cur.get(fieldId - 1); + handleNull(datum); + if (wasNull) { + return null; + } + + return getTimestamp((TimestampDatum)datum, x.getTimeZone()); + } + + @Override + public Timestamp getTimestamp(String name, Calendar x) throws SQLException { + return getTimestamp(findColumn(name) + 1, x); + } + + private Timestamp getTimestamp(TimestampDatum datum, TimeZone tz) { + TimeMeta tm = datum.toTimeMeta(); + if (tz != null) { + DateTimeUtil.toUserTimezone(tm, tz); + } + return new Timestamp(DateTimeUtil.julianTimeToJavaTime(DateTimeUtil.toJulianTimestamp(tm))); + } + + @Override public boolean isWrapperFor(Class clazz) throws SQLException { throw new SQLFeatureNotSupportedException("isWrapperFor not supported"); } @@ -432,36 +558,6 @@ public abstract class TajoResultSetBase implements ResultSet { } @Override - public Date getDate(int index) throws SQLException { - Object obj = getObject(index); - if (obj == null) { - return null; - } - - try { - return Date.valueOf((String) obj); - } catch (Exception e) { - throw new SQLException("Cannot convert column " + index - + " to date: " + e.toString()); - } - } - - @Override - public Date getDate(String name) throws SQLException { - return getDate(findColumn(name)); - } - - @Override - public Date getDate(int index, Calendar x) throws SQLException { - throw new SQLFeatureNotSupportedException("getDate not supported"); - } - - @Override - public Date getDate(String name, Calendar x) throws SQLException { - throw new SQLFeatureNotSupportedException("getDate not supported"); - } - - @Override public int getFetchDirection() throws SQLException { return ResultSet.FETCH_FORWARD; } @@ -576,46 +672,6 @@ public abstract class TajoResultSetBase implements ResultSet { } @Override - public Time getTime(int index) throws SQLException { - throw new SQLFeatureNotSupportedException("getTime not supported"); - } - - @Override - public Time getTime(String name) throws SQLException { - throw new SQLFeatureNotSupportedException("getTime not supported"); - } - - @Override - public Time getTime(int index, Calendar x) throws SQLException { - throw new SQLFeatureNotSupportedException("getTime not supported"); - } - - @Override - public Time getTime(String name, Calendar x) throws SQLException { - throw new SQLFeatureNotSupportedException("getTime not supported"); - } - - @Override - public Timestamp getTimestamp(int index) throws SQLException { - throw new SQLFeatureNotSupportedException("getTimestamp not supported"); - } - - @Override - public Timestamp getTimestamp(String name) throws SQLException { - throw new SQLFeatureNotSupportedException("getTimestamp not supported"); - } - - @Override - public Timestamp getTimestamp(int index, Calendar x) throws SQLException { - throw new SQLFeatureNotSupportedException("getTimestamp not supported"); - } - - @Override - public Timestamp getTimestamp(String name, Calendar x) throws SQLException { - throw new SQLFeatureNotSupportedException("getTimestamp not supported"); - } - - @Override public int getType() throws SQLException { return ResultSet.TYPE_FORWARD_ONLY; } http://git-wip-us.apache.org/repos/asf/tajo/blob/06be0608/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java ---------------------------------------------------------------------- diff --git a/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java b/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java index 187e25d..327b423 100644 --- a/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java +++ b/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java @@ -346,6 +346,10 @@ public class DateTimeUtil { DateTimeConstants.USECS_PER_SEC) + fsec; } + public static long toJavaTime(int hour, int min, int sec, int fsec) { + return toTime(hour, min, sec, fsec)/DateTimeConstants.MSECS_PER_SEC; + } + /** * Calculate julian timestamp. * @param years http://git-wip-us.apache.org/repos/asf/tajo/blob/06be0608/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java index 415ed49..0b1831c 100644 --- a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java +++ b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java @@ -120,6 +120,8 @@ public class TestSortQuery extends QueryTestCaseBase { // skip this test if catalog uses HCatalogStore. // It is because HCatalogStore does not support Time data type. TimeZone oldTimeZone = TajoConf.setCurrentTimeZone(TimeZone.getTimeZone("UTC")); + TimeZone systemOldTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); try { if (!testingCluster.isHCatalogStoreRunning()) { // create external table table1 (col1 timestamp, col2 date, col3 time) ... @@ -131,6 +133,7 @@ public class TestSortQuery extends QueryTestCaseBase { } } finally { TajoConf.setCurrentTimeZone(oldTimeZone); + TimeZone.setDefault(systemOldTimeZone); } } http://git-wip-us.apache.org/repos/asf/tajo/blob/06be0608/tajo-core/src/test/java/org/apache/tajo/jdbc/TestResultSet.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/jdbc/TestResultSet.java b/tajo-core/src/test/java/org/apache/tajo/jdbc/TestResultSet.java index 4477fa5..fdbca61 100644 --- a/tajo-core/src/test/java/org/apache/tajo/jdbc/TestResultSet.java +++ b/tajo-core/src/test/java/org/apache/tajo/jdbc/TestResultSet.java @@ -36,14 +36,16 @@ import org.apache.tajo.common.TajoDataTypes.Type; import org.apache.tajo.conf.TajoConf; import org.apache.tajo.datum.DatumFactory; import org.apache.tajo.storage.*; +import org.apache.tajo.util.KeyValueSet; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; import java.io.IOException; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; +import java.sql.*; +import java.util.Calendar; +import java.util.TimeZone; import static org.junit.Assert.*; @@ -124,4 +126,90 @@ public class TestResultSet { assertEquals(10000, i); assertTrue(rs.isAfterLast()); } + + @Test + public void testDateTimeType() throws Exception { + TimeZone tajoCurrentTimeZone = TajoConf.getCurrentTimeZone(); + TajoConf.setCurrentTimeZone(TimeZone.getTimeZone("UTC")); + + TimeZone systemCurrentTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + ResultSet res = null; + try { + String tableName = "datetimetable"; + String query = "select col1, col2, col3 from " + tableName; + + String [] table = new String[] {tableName}; + Schema schema = new Schema(); + schema.addColumn("col1", Type.DATE); + schema.addColumn("col2", Type.TIME); + schema.addColumn("col3", Type.TIMESTAMP); + Schema [] schemas = new Schema[] {schema}; + String [] data = { + "2014-01-01|01:00:00|2014-01-01 01:00:00" + }; + KeyValueSet tableOptions = new KeyValueSet(); + tableOptions.put(StorageConstants.CSVFILE_DELIMITER, StorageConstants.DEFAULT_FIELD_DELIMITER); + + res = TajoTestingCluster + .run(table, schemas, tableOptions, new String[][]{data}, query); + + assertTrue(res.next()); + + Date date = res.getDate(1); + assertNotNull(date); + assertEquals(Date.valueOf("2014-01-01"), date); + + date = res.getDate("col1"); + assertNotNull(date); + assertEquals(Date.valueOf("2014-01-01"), date); + + Time time = res.getTime(2); + assertNotNull(time); + assertEquals(Time.valueOf("01:00:00"), time); + + time = res.getTime("col2"); + assertNotNull(time); + assertEquals(Time.valueOf("01:00:00"), time); + + Timestamp timestamp = res.getTimestamp(3); + assertNotNull(timestamp); + assertEquals(Timestamp.valueOf("2014-01-01 01:00:00"), timestamp); + + timestamp = res.getTimestamp("col3"); + assertNotNull(timestamp); + assertEquals(Timestamp.valueOf("2014-01-01 01:00:00"), timestamp); + + // assert with timezone + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+9")); + date = res.getDate(1, cal); + assertNotNull(date); + assertEquals("2014-01-01", date.toString()); + + date = res.getDate("col1", cal); + assertNotNull(date); + assertEquals("2014-01-01", date.toString()); + + time = res.getTime(2, cal); + assertNotNull(time); + assertEquals("10:00:00", time.toString()); + + time = res.getTime("col2", cal); + assertNotNull(time); + assertEquals("10:00:00", time.toString()); + + timestamp = res.getTimestamp(3, cal); + assertNotNull(timestamp); + assertEquals("2014-01-01 10:00:00.0", timestamp.toString()); + + timestamp = res.getTimestamp("col3", cal); + assertNotNull(timestamp); + assertEquals("2014-01-01 10:00:00.0", timestamp.toString()); + } finally { + TajoConf.setCurrentTimeZone(tajoCurrentTimeZone); + TimeZone.setDefault(systemCurrentTimeZone); + res.close(); + } + } }