quickstep-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shix...@apache.org
Subject incubator-quickstep git commit: NULL value will be ignored unless all values in the window are NULL
Date Wed, 20 Jul 2016 19:32:54 GMT
Repository: incubator-quickstep
Updated Branches:
  refs/heads/SQL-window-aggregation 06d229a14 -> b80f5e9cf


NULL value will be ignored unless all values in the window are NULL


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

Branch: refs/heads/SQL-window-aggregation
Commit: b80f5e9cf36bfbe18ae4143d130f268646a40823
Parents: 06d229a
Author: shixuan <shixuan@apache.org>
Authored: Wed Jul 20 14:32:40 2016 -0500
Committer: shixuan <shixuan@apache.org>
Committed: Wed Jul 20 14:32:40 2016 -0500

----------------------------------------------------------------------
 .../WindowAggregationHandleAvg.cpp              | 46 +++++++++----------
 .../WindowAggregationHandleAvg_unittest.cpp     | 47 ++++++++------------
 .../tests/execution_generator/Select.test       | 26 +++++------
 3 files changed, 52 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/b80f5e9c/expressions/window_aggregation/WindowAggregationHandleAvg.cpp
----------------------------------------------------------------------
diff --git a/expressions/window_aggregation/WindowAggregationHandleAvg.cpp b/expressions/window_aggregation/WindowAggregationHandleAvg.cpp
index 698e0a6..a6a10d4 100644
--- a/expressions/window_aggregation/WindowAggregationHandleAvg.cpp
+++ b/expressions/window_aggregation/WindowAggregationHandleAvg.cpp
@@ -125,18 +125,16 @@ TypedValue WindowAggregationHandleAvg::calculateOneWindow(
     const bool is_row,
     const std::int64_t num_preceding,
     const std::int64_t num_following) const {
-  // If current value is null, return null.
-  if (argument_accessor->getTypedValue(0).isNull()) {
-    return TypedValue(result_type_->getTypeID());
-  }
-
   // Initialize.
   TypedValue sum = sum_type_->makeZeroValue();
   TypedValue current_value = argument_accessor->getTypedValue(0);
+  std::uint64_t count = 0;
 
-  sum = fast_add_operator_->
-      applyToTypedValues(sum, current_value);
-  std::uint64_t count = 1;
+  // Ignore the value if null.
+  if (!current_value.isNull()) {
+    sum = fast_add_operator_->applyToTypedValues(sum, current_value);
+    count++;
+  }
 
   // Get the partition key for the current row.
   std::vector<TypedValue> current_row_partition_key;
@@ -173,16 +171,13 @@ TypedValue WindowAggregationHandleAvg::calculateOneWindow(
     TypedValue preceding_value =
         argument_accessor->getTypedValueAtAbsolutePosition(0, preceding_tuple_id);
 
-    // If a null value is in the window, return a null value.
-    if (preceding_value.isNull()) {
-      return TypedValue(result_type_->getTypeID());
+    // Ignore the value if null.
+    if (!preceding_value.isNull()) {
+      sum = fast_add_operator_->applyToTypedValues(sum, preceding_value);
+      count++;
     }
-
-    sum = fast_add_operator_->applyToTypedValues(sum, preceding_value);
   }
 
-  count += count_preceding;
-
   // Find following tuples.
   int count_following = 0;
   tuple_id following_tuple_id = current_tuple_id;
@@ -208,19 +203,20 @@ TypedValue WindowAggregationHandleAvg::calculateOneWindow(
     TypedValue following_value =
         argument_accessor->getTypedValueAtAbsolutePosition(0, following_tuple_id);
 
-    // If a null value is in the window, return a null value.
-    if (following_value.isNull()) {
-      return TypedValue(result_type_->getTypeID());
+    // Ignore the value if null.
+    if (!following_value.isNull()) {
+      sum = fast_add_operator_->applyToTypedValues(sum, following_value);
+      count++;
     }
-
-    sum = fast_add_operator_->applyToTypedValues(sum, following_value);
   }
 
-  count += count_following;
-
-
-  return divide_operator_->applyToTypedValues(sum,
-                                              TypedValue(static_cast<double>(count)));
+  // If all values are NULLs, return NULL; Otherwise, return the quotient.
+  if (count == 0) {
+    return result_type_->makeNullValue();
+  } else {
+    return divide_operator_->applyToTypedValues(sum,
+                                                TypedValue(static_cast<double>(count)));
+  }
 }
 
 bool WindowAggregationHandleAvg::samePartition(

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/b80f5e9c/expressions/window_aggregation/tests/WindowAggregationHandleAvg_unittest.cpp
----------------------------------------------------------------------
diff --git a/expressions/window_aggregation/tests/WindowAggregationHandleAvg_unittest.cpp
b/expressions/window_aggregation/tests/WindowAggregationHandleAvg_unittest.cpp
index ae28781..c044a98 100644
--- a/expressions/window_aggregation/tests/WindowAggregationHandleAvg_unittest.cpp
+++ b/expressions/window_aggregation/tests/WindowAggregationHandleAvg_unittest.cpp
@@ -62,7 +62,7 @@ namespace {
 }  // namespace
 
 // Attribute value could be null if set true.
-class WindowAggregationHandleAvgTest : public::testing::TestWithParam<bool> {
+class WindowAggregationHandleAvgTest : public::testing::Test {
  protected:
   // Handle initialization.
   void initializeHandle(const Type &argument_type) {
@@ -195,27 +195,24 @@ class WindowAggregationHandleAvgTest : public::testing::TestWithParam<bool>
{
 
     // Get the cpptype result.
     std::vector<typename OutputType::cpptype*> result_cpp_vector;
-    bool is_null;
     typename GenericType::cpptype sum;
     int count;
     for (std::size_t i = 0; i < argument_cpp_vector.size(); ++i) {
       // Start of new partition
       if (i % kNumTuplesPerPartition == 0) {
-        is_null = false;
         SetDataType(0, &sum);
         count = 0;
       }
 
       typename GenericType::cpptype *value = argument_cpp_vector[i];
-      if (value == nullptr) {
-        is_null = true;
+      if (value != nullptr) {
+        sum += *value;
+        count++;
       }
 
-      if (is_null) {
+      if (count == 0) {
         result_cpp_vector.push_back(nullptr);
       } else {
-        sum += *value;
-        count++;
         typename OutputType::cpptype *result_cpp_value =
             new typename OutputType::cpptype;
         *result_cpp_value = static_cast<typename OutputType::cpptype>(sum) / count;
@@ -248,16 +245,14 @@ class WindowAggregationHandleAvgTest : public::testing::TestWithParam<bool>
{
     std::vector<typename OutputType::cpptype*> result_cpp_vector;
 
     for (std::size_t i = 0; i < argument_cpp_vector.size(); ++i) {
-      if (argument_cpp_vector[i] == nullptr) {
-        result_cpp_vector.push_back(nullptr);
-        continue;
-      }
-
       typename GenericType::cpptype sum;
       SetDataType(0, &sum);
-      sum += *argument_cpp_vector[i];
-      int count = 1;
-      bool is_null = false;
+      int count = 0;
+
+      if (argument_cpp_vector[i] != nullptr) {
+        sum += *argument_cpp_vector[i];
+        count++;
+      }
 
       for (std::size_t precede = 1; precede <= kNumPreceding; ++precede) {
         // Not the same partition.
@@ -266,13 +261,10 @@ class WindowAggregationHandleAvgTest : public::testing::TestWithParam<bool>
{
           break;
         }
 
-        if (argument_cpp_vector[i - precede] == nullptr) {
-          is_null = true;
-          break;
+        if (argument_cpp_vector[i - precede] != nullptr) {
+          sum += *argument_cpp_vector[i - precede];
+          count++;
         }
-
-        sum += *argument_cpp_vector[i - precede];
-        count++;
       }
 
       for (int follow = 1; follow <= kNumPreceding; ++follow) {
@@ -282,16 +274,13 @@ class WindowAggregationHandleAvgTest : public::testing::TestWithParam<bool>
{
           break;
         }
 
-        if (argument_cpp_vector[i + follow] == nullptr) {
-          is_null = true;
-          break;
+        if (argument_cpp_vector[i + follow] != nullptr) {
+          sum += *argument_cpp_vector[i + follow];
+          count++;
         }
-
-        sum += *argument_cpp_vector[i + follow];
-        count++;
       }
 
-      if (is_null) {
+      if (count == 0) {
         result_cpp_vector.push_back(nullptr);
       } else {
         typename OutputType::cpptype *result_cpp_value =

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/b80f5e9c/query_optimizer/tests/execution_generator/Select.test
----------------------------------------------------------------------
diff --git a/query_optimizer/tests/execution_generator/Select.test b/query_optimizer/tests/execution_generator/Select.test
index f290059..30a3c39 100644
--- a/query_optimizer/tests/execution_generator/Select.test
+++ b/query_optimizer/tests/execution_generator/Select.test
@@ -999,29 +999,29 @@ WINDOW w AS
 +--------------------+-----------+------------------------+
 |                 576|         24|       7.666666666666667|
 |                 529|        -23|                     0.5|
-|                 484|         22|                    NULL|
-|                 441|        -21|                    NULL|
-|                 400|       NULL|                    NULL|
-|                 361|        -19|                    NULL|
-|                 324|         18|                    NULL|
+|                 484|         22|                     0.5|
+|                 441|        -21|                  -10.25|
+|                 400|       NULL|                       0|
+|                 361|        -19|                   -9.75|
+|                 324|         18|                    -0.5|
 |                 289|        -17|     -3.3999999999999999|
 |                 256|         16|      3.2000000000000002|
 |                 225|        -15|                      -3|
 |                 196|         14|      2.7999999999999998|
 |                 169|        -13|     -2.6000000000000001|
-|                 144|         12|                    NULL|
-|                 121|        -11|                    NULL|
-|                 100|       NULL|                    NULL|
-|                  81|         -9|                    NULL|
-|                  64|          8|                    NULL|
+|                 144|         12|                     0.5|
+|                 121|        -11|                   -5.25|
+|                 100|       NULL|                       0|
+|                  81|         -9|                   -4.75|
+|                  64|          8|                    -0.5|
 |                  49|         -7|     -1.3999999999999999|
 |                  36|          6|                     1.2|
 |                  25|         -5|                      -1|
 |                  16|          4|     0.80000000000000004|
 |                   9|         -3|    -0.59999999999999998|
-|                   4|          2|                    NULL|
-|                   1|         -1|                    NULL|
-|                   0|       NULL|                    NULL|
+|                   4|          2|                     0.5|
+|                   1|         -1|    -0.66666666666666663|
+|                   0|       NULL|                     0.5|
 +--------------------+-----------+------------------------+
 ==
 


Mime
View raw message