You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@quickstep.apache.org by ha...@apache.org on 2016/09/03 01:43:59 UTC

incubator-quickstep git commit: New representation and comparison operators for DateLit. [Forced Update!]

Repository: incubator-quickstep
Updated Branches:
  refs/heads/date-representation 998994eee -> 8b450062a (forced update)


New representation and comparison operators for DateLit.


Project: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/commit/8b450062
Tree: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/tree/8b450062
Diff: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/diff/8b450062

Branch: refs/heads/date-representation
Commit: 8b450062ad294f0362f20623825b2c49b1ecfe21
Parents: 3c8708d
Author: Hakan Memisoglu <ha...@gmail.com>
Authored: Fri Sep 2 14:53:47 2016 -0500
Committer: Hakan Memisoglu <ha...@gmail.com>
Committed: Fri Sep 2 20:43:16 2016 -0500

----------------------------------------------------------------------
 types/DateOperatorOverloads.hpp                 | 12 ++---
 types/DateType.cpp                              |  8 ++--
 types/DatetimeLit.hpp                           | 49 +++++++++++---------
 types/TypedValue.cpp                            |  6 +--
 .../unary_operations/DateExtractOperation.cpp   |  2 +-
 5 files changed, 41 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8b450062/types/DateOperatorOverloads.hpp
----------------------------------------------------------------------
diff --git a/types/DateOperatorOverloads.hpp b/types/DateOperatorOverloads.hpp
index b04f44e..e2e5f6a 100644
--- a/types/DateOperatorOverloads.hpp
+++ b/types/DateOperatorOverloads.hpp
@@ -122,8 +122,8 @@ inline DatetimeLit operator+(const YearMonthIntervalLit &lhs, const DatetimeLit
 }
 
 inline DateLit operator+(const DateLit &lhs, const YearMonthIntervalLit &rhs) {
-  std::int32_t result_year = lhs.year + (rhs.months / 12);
-  std::uint8_t result_month = lhs.month + (rhs.months % 12);
+  std::int32_t result_year = lhs.yearField() + (rhs.months / 12);
+  std::uint8_t result_month = static_cast<std::uint8_t>(lhs.monthField()) + (rhs.months % 12);
 
   if (result_month > 11) {
     result_month -= 12;
@@ -131,7 +131,7 @@ inline DateLit operator+(const DateLit &lhs, const YearMonthIntervalLit &rhs) {
   }
 
   const std::uint8_t result_day = static_cast<std::uint8_t>(
-      ClampDayOfMonth(result_year, result_month, lhs.day));
+      ClampDayOfMonth(result_year, result_month, lhs.dayField()));
 
   return DateLit::Create(result_year, result_month, result_day);
 }
@@ -187,8 +187,8 @@ inline DatetimeLit operator-(const DatetimeLit &lhs, const YearMonthIntervalLit
 }
 
 inline DateLit operator-(const DateLit &lhs, const YearMonthIntervalLit &rhs) {
-  std::int32_t result_year = lhs.year - (rhs.months / 12);
-  std::int8_t result_month = lhs.month - (rhs.months % 12);
+  std::int32_t result_year = lhs.yearField() - (rhs.months / 12);
+  std::int8_t result_month = static_cast<std::int8_t>(lhs.monthField()) - (rhs.months % 12);
 
   if (result_month < 0) {
     --result_year;
@@ -196,7 +196,7 @@ inline DateLit operator-(const DateLit &lhs, const YearMonthIntervalLit &rhs) {
   }
 
   const std::uint8_t result_day = static_cast<std::uint8_t>(
-      ClampDayOfMonth(result_year, result_month, lhs.day));
+      ClampDayOfMonth(result_year, result_month, lhs.dayField()));
 
   return DateLit::Create(result_year, result_month, result_day);
 }

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8b450062/types/DateType.cpp
----------------------------------------------------------------------
diff --git a/types/DateType.cpp b/types/DateType.cpp
index 5bb982c..388898e 100644
--- a/types/DateType.cpp
+++ b/types/DateType.cpp
@@ -60,7 +60,7 @@ std::string DateType::printValueToString(const TypedValue &value) const {
   DCHECK(!value.isNull());
 
   const DateLit literal = value.getLiteral<DateLit>();
-  const std::int32_t year = literal.year;
+  const std::int32_t year = literal.yearField();
 
   char datebuf[DateLit::kIsoChars + 1];
   std::size_t chars_written = 0;
@@ -78,9 +78,9 @@ std::string DateType::printValueToString(const TypedValue &value) const {
   // All the rest of the ISO 8601 date/time parts.
   snprintf_result = snprintf(datebuf + chars_written,
                              sizeof(datebuf) - chars_written,
-                             "%02d-%02d",
-                             literal.month,
-                             literal.day);
+                             "%02u-%02u",
+                             literal.monthField(),
+                             literal.dayField());
   CheckSnprintf(snprintf_result, sizeof(datebuf), &chars_written);
 
   return std::string(datebuf);

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8b450062/types/DatetimeLit.hpp
----------------------------------------------------------------------
diff --git a/types/DatetimeLit.hpp b/types/DatetimeLit.hpp
index 58c852f..f766b59 100644
--- a/types/DatetimeLit.hpp
+++ b/types/DatetimeLit.hpp
@@ -36,10 +36,13 @@ namespace quickstep {
  * @brief A literal representing the date.
  **/
 struct DateLit {
-  // Note that although there is no year 0 in the Gregorian calendar, ISO 8601
-  // has year 0 equivalent to 1 BCE, year -1 equivalent to 2 BCE, and so on.
-  std::int32_t year;
-  std::uint8_t month, day;
+  // ----------------------------------------------------
+  // |  23 bits                       | 4 bits | 5 bits |
+  // ----------------------------------------------------
+  // |       year                     |  month |   day  |
+  // | (unsigned)  (18 bits used)     |  (unsigned)     |
+  // ----------------------------------------------------
+  std::uint32_t year_month_day;
 
   // The maximum number of characters needed to represent any date in ISO 8601
   // notation.
@@ -55,49 +58,51 @@ struct DateLit {
                         const std::uint8_t _month,
                         const std::uint8_t _day) {
     DateLit date;
-    date.year = _year;
-    date.month = _month;
-    date.day = _day;
-
+    std::uint32_t representation
+        = (static_cast<std::uint32_t>(_year + 99999) << 9u)
+          | (_month << 5u)
+          | _day;
+    date.year_month_day = representation;
     return date;
   }
 
   inline bool operator< (const DateLit& rhs) const {
-    return (year != rhs.year)
-        ? (year < rhs.year)
-        : ((month != rhs.month) ? (month < rhs.month) : (day < rhs.day));
+    return year_month_day < rhs.year_month_day;
   }
 
   inline bool operator> (const DateLit& rhs) const {
-    return (year != rhs.year)
-        ? (year > rhs.year)
-        : ((month != rhs.month) ? (month > rhs.month) : (day > rhs.day));
+    return year_month_day < rhs.year_month_day;
   }
 
   inline bool operator<=(const DateLit& rhs) const {
-    return !(*this > rhs);
+    return year_month_day <= rhs.year_month_day;
   }
 
   inline bool operator>=(const DateLit& rhs) const {
-    return !(*this < rhs);
+    return year_month_day >= rhs.year_month_day;
   }
 
   inline bool operator==(const DateLit& rhs) const {
-    return (year == rhs.year) &&
-           (month == rhs.month) &&
-           (day == rhs.day);
+    return year_month_day == rhs.year_month_day;
   }
 
   inline bool operator!=(const DateLit& rhs) const {
-    return !(*this == rhs);
+    return year_month_day != rhs.year_month_day;
   }
 
   inline std::int32_t yearField() const {
+    const std::int32_t year = static_cast<std::int32_t>(year_month_day >> 9u) - 99999;
     return year;
   }
 
-  inline std::int32_t monthField() const {
-    return static_cast<std::int32_t>(month);
+  inline std::uint32_t monthField() const {
+    const std::uint32_t mask = 0x1E0u;  // 0b111100000
+    return (year_month_day & mask) >> 5;
+  }
+
+  inline std::uint32_t dayField() const {
+    const std::uint32_t mask = 0x1Fu;  // 0b11111
+    return year_month_day & mask;
   }
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8b450062/types/TypedValue.cpp
----------------------------------------------------------------------
diff --git a/types/TypedValue.cpp b/types/TypedValue.cpp
index 8dd8b60..d7b4956 100644
--- a/types/TypedValue.cpp
+++ b/types/TypedValue.cpp
@@ -111,9 +111,9 @@ serialization::TypedValue TypedValue::getProto() const {
       proto.set_type_id(serialization::Type::DATE);
       if (!isNull()) {
         serialization::TypedValue::DateLit *literal_date_proto = proto.mutable_date_value();
-        literal_date_proto->set_year(value_union_.date_value.year);
-        literal_date_proto->set_month(value_union_.date_value.month);
-        literal_date_proto->set_day(value_union_.date_value.day);
+        literal_date_proto->set_year(value_union_.date_value.yearField());
+        literal_date_proto->set_month(value_union_.date_value.monthField());
+        literal_date_proto->set_day(value_union_.date_value.dayField());
       }
       break;
     case kDatetime:

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8b450062/types/operations/unary_operations/DateExtractOperation.cpp
----------------------------------------------------------------------
diff --git a/types/operations/unary_operations/DateExtractOperation.cpp b/types/operations/unary_operations/DateExtractOperation.cpp
index c99e403..585fb85 100644
--- a/types/operations/unary_operations/DateExtractOperation.cpp
+++ b/types/operations/unary_operations/DateExtractOperation.cpp
@@ -444,7 +444,7 @@ TypedValue DateExtractOperation::applyToChecked(const TypedValue &argument,
       } else {
         // argument type is kDate.
         DCHECK_EQ(TypeID::kDate, argument.getTypeID());
-        return TypedValue(argument.getLiteral<DateLit>().monthField());
+        return TypedValue(static_cast<std::int32_t>(argument.getLiteral<DateLit>().monthField()));
       }
     }
     case DateExtractUnit::kDay: