quickstep-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hakanmemiso...@apache.org
Subject [14/17] incubator-quickstep git commit: QUICKSTEP-6: New fixed precision number type: Decimal.
Date Thu, 23 Jun 2016 20:16:40 GMT
http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/operations/unary_operations/NumericCastOperation.hpp
----------------------------------------------------------------------
diff --git a/types/operations/unary_operations/NumericCastOperation.hpp b/types/operations/unary_operations/NumericCastOperation.hpp
index 250df6d..9eb50c2 100644
--- a/types/operations/unary_operations/NumericCastOperation.hpp
+++ b/types/operations/unary_operations/NumericCastOperation.hpp
@@ -29,6 +29,7 @@
 #include "catalog/CatalogTypedefs.hpp"
 #include "storage/ValueAccessor.hpp"
 #include "storage/ValueAccessorUtil.hpp"
+#include "types/DecimalType.hpp"
 #include "types/DoubleType.hpp"
 #include "types/FloatType.hpp"
 #include "types/IntType.hpp"
@@ -126,7 +127,7 @@ class UncheckedNumericCastOperator : public UncheckedUnaryOperator {
           result->appendNullValue();
         } else {
           *static_cast<typename TargetType::cpptype*>(result->getPtrForDirectWrite())
-              = static_cast<typename SourceType::cpptype>(*scalar_arg);
+              = static_cast<typename TargetType::cpptype>(*scalar_arg);
         }
       }
       return result;
@@ -285,6 +286,8 @@ class NumericCastOperation : public UnaryOperation {
         return makeUncheckedUnaryOperatorHelperForTargetNullability<SourceType, source_nullability,
FloatType>();
       case kDouble:
         return makeUncheckedUnaryOperatorHelperForTargetNullability<SourceType, source_nullability,
DoubleType>();
+      case kDecimal:
+        return makeUncheckedUnaryOperatorHelperForTargetNullability<SourceType, source_nullability,
DecimalType>();
       default:
         FATAL_ERROR("Unhandled type " << kTypeNames[target_type_.getTypeID()]);
     }

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/tests/DecimalType_unittest.cpp
----------------------------------------------------------------------
diff --git a/types/tests/DecimalType_unittest.cpp b/types/tests/DecimalType_unittest.cpp
new file mode 100644
index 0000000..e0e76af
--- /dev/null
+++ b/types/tests/DecimalType_unittest.cpp
@@ -0,0 +1,82 @@
+/**
+ *   Copyright 2016, Quickstep Research Group, Computer Sciences Department,
+ *     University of Wisconsin—Madison.
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ **/
+
+#include <string>
+
+#include "types/DecimalLit.hpp"
+#include "types/DecimalType.hpp"
+#include "types/Type.hpp"
+#include "types/TypeFactory.hpp"
+#include "types/TypedValue.hpp"
+
+#include "gtest/gtest.h"
+
+namespace quickstep {
+
+TEST(DecimalTypeTest, PrintValueTest) {
+  const Type &decimal_type = TypeFactory::GetType(kDecimal);
+
+  // Try integer version.
+  DecimalLit integer_decimal = DecimalLit(36509);
+  TypedValue value_for_integer_decimal(integer_decimal);
+  EXPECT_EQ(std::string("36509.00"),
+            decimal_type.printValueToString(value_for_integer_decimal));
+
+  // Try double version.
+  DecimalLit double_decimal = DecimalLit(36509.65);
+  TypedValue value_for_double_decimal(double_decimal);
+  EXPECT_EQ(std::string("36509.65"),
+            decimal_type.printValueToString(value_for_double_decimal));
+
+  // Try truncation of double version.
+  DecimalLit double_decimal_truncated = DecimalLit(36509.6526762);
+  TypedValue value_for_double_decimal_truncated(double_decimal_truncated);
+  EXPECT_EQ(std::string("36509.65"),
+            decimal_type.printValueToString(
+                value_for_double_decimal_truncated));
+
+  // Test that number is truncated, not rounded.
+  double_decimal_truncated = DecimalLit(36509.6599999);
+  TypedValue value_for_double_decimal_truncated_other(double_decimal_truncated);
+  EXPECT_EQ(std::string("36509.65"),
+            decimal_type.printValueToString(
+                value_for_double_decimal_truncated));
+}
+
+TEST(DecimalTypeTest, DecimalLitOperationsTest) {
+  const Type &decimal_type = TypeFactory::GetType(kDecimal);
+
+  DecimalLit decimal_a(560.35);
+  DecimalLit decimal_b(439.65);
+
+  EXPECT_EQ(std::string("1000.00"),
+            decimal_type.printValueToString(TypedValue(decimal_a + decimal_b)));
+
+  EXPECT_EQ(std::string("120.70"),
+            decimal_type.printValueToString(TypedValue(decimal_a - decimal_b)));
+
+  EXPECT_EQ(std::string("246357.87"),
+            decimal_type.printValueToString(TypedValue(decimal_a * decimal_b)));
+
+  EXPECT_EQ(std::string("1.27"),
+            decimal_type.printValueToString(TypedValue(decimal_a / decimal_b)));
+
+  EXPECT_EQ(std::string("120.70"),
+            decimal_type.printValueToString(TypedValue(decimal_a % decimal_b)));
+}
+
+}  // namespace quickstep


Mime
View raw message