You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@quickstep.apache.org by ji...@apache.org on 2018/05/16 19:44:35 UTC

[3/3] incubator-quickstep git commit: Updates for aggregation result type

Updates for aggregation result type


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

Branch: refs/heads/trace
Commit: f477c5d319e8dc6e501c9ce1a366d3552ec907e0
Parents: a0024a2
Author: Jianqiao Zhu <ji...@cs.wisc.edu>
Authored: Wed May 16 14:42:54 2018 -0500
Committer: Jianqiao Zhu <ji...@cs.wisc.edu>
Committed: Wed May 16 14:42:54 2018 -0500

----------------------------------------------------------------------
 expressions/aggregation/AggregateFunction.hpp   |  3 ++-
 .../aggregation/AggregateFunctionAvg.cpp        | 19 ++++++++++++-------
 .../aggregation/AggregateFunctionAvg.hpp        |  3 ++-
 .../aggregation/AggregateFunctionCount.cpp      |  3 ++-
 .../aggregation/AggregateFunctionCount.hpp      |  3 ++-
 .../aggregation/AggregateFunctionMax.cpp        | 10 ++++++----
 .../aggregation/AggregateFunctionMax.hpp        |  3 ++-
 .../aggregation/AggregateFunctionMin.cpp        | 10 ++++++----
 .../aggregation/AggregateFunctionMin.hpp        |  3 ++-
 .../aggregation/AggregateFunctionSum.cpp        | 20 ++++++++++++--------
 .../aggregation/AggregateFunctionSum.hpp        |  3 ++-
 .../tests/AggregationHandleAvg_unittest.cpp     |  4 ++--
 .../tests/AggregationHandleCount_unittest.cpp   |  4 ++--
 .../tests/AggregationHandleMax_unittest.cpp     |  4 ++--
 .../tests/AggregationHandleMin_unittest.cpp     |  4 ++--
 .../tests/AggregationHandleSum_unittest.cpp     |  4 ++--
 .../expressions/AggregateFunction.cpp           |  6 ++++--
 types/Type.cpp                                  |  4 ++++
 types/Type.hpp                                  |  2 ++
 19 files changed, 70 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunction.hpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunction.hpp b/expressions/aggregation/AggregateFunction.hpp
index cccc699..5f290d1 100644
--- a/expressions/aggregation/AggregateFunction.hpp
+++ b/expressions/aggregation/AggregateFunction.hpp
@@ -112,7 +112,8 @@ class AggregateFunction {
    *         applicable to the specified Type(s).
    **/
   virtual const Type* resultTypeForArgumentTypes(
-      const std::vector<const Type*> &argument_types) const = 0;
+      const std::vector<const Type*> &argument_types,
+      const bool is_vector_aggregate) const = 0;
 
   /**
    * @brief Create an AggregationHandle to compute aggregates.

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionAvg.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionAvg.cpp b/expressions/aggregation/AggregateFunctionAvg.cpp
index 040d7d9..e966d4a 100644
--- a/expressions/aggregation/AggregateFunctionAvg.cpp
+++ b/expressions/aggregation/AggregateFunctionAvg.cpp
@@ -48,22 +48,27 @@ bool AggregateFunctionAvg::canApplyToTypes(
 }
 
 const Type* AggregateFunctionAvg::resultTypeForArgumentTypes(
-    const std::vector<const Type*> &argument_types) const {
+    const std::vector<const Type*> &argument_types,
+    const bool is_vector_aggregate) const {
   if (!canApplyToTypes(argument_types)) {
     return nullptr;
   }
 
-  // The type used to sum values is nullable, and we automatically widen int to
-  // long and float to double to have more headroom when adding up many values.
-  const Type *sum_type = &(argument_types.front()->getNullableVersion());
-  switch (sum_type->getTypeID()) {
+  // We automatically widen int to long and float to double to have more
+  // headroom when adding up many values.
+  const Type *argument_type = argument_types.front();
+  const bool nullable = argument_type->isNullable() || !is_vector_aggregate;
+
+  const Type *sum_type;
+  switch (argument_type->getTypeID()) {
     case kInt:
-      sum_type = &TypeFactory::GetType(kLong, true);
+      sum_type = &TypeFactory::GetType(kLong, nullable);
       break;
     case kFloat:
-      sum_type = &TypeFactory::GetType(kDouble, true);
+      sum_type = &TypeFactory::GetType(kDouble, nullable);
       break;
     default:
+      sum_type = &TypeFactory::GetType(argument_type->getTypeID(), nullable);
       break;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionAvg.hpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionAvg.hpp b/expressions/aggregation/AggregateFunctionAvg.hpp
index 9d08d1c..d040607 100644
--- a/expressions/aggregation/AggregateFunctionAvg.hpp
+++ b/expressions/aggregation/AggregateFunctionAvg.hpp
@@ -54,7 +54,8 @@ class AggregateFunctionAvg : public AggregateFunction {
       const std::vector<const Type*> &argument_types) const override;
 
   const Type* resultTypeForArgumentTypes(
-      const std::vector<const Type*> &argument_types) const override;
+      const std::vector<const Type*> &argument_types,
+      const bool is_vector_aggregate) const override;
 
   AggregationHandle* createHandle(
       const std::vector<const Type*> &argument_types) const override;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionCount.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionCount.cpp b/expressions/aggregation/AggregateFunctionCount.cpp
index 9795b4a..968b26e 100644
--- a/expressions/aggregation/AggregateFunctionCount.cpp
+++ b/expressions/aggregation/AggregateFunctionCount.cpp
@@ -37,7 +37,8 @@ bool AggregateFunctionCount::canApplyToTypes(
 }
 
 const Type* AggregateFunctionCount::resultTypeForArgumentTypes(
-    const std::vector<const Type*> &argument_types) const {
+    const std::vector<const Type*> &argument_types,
+    const bool is_vector_aggregate) const {
   if (!canApplyToTypes(argument_types)) {
     return nullptr;
   }

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionCount.hpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionCount.hpp b/expressions/aggregation/AggregateFunctionCount.hpp
index ee34de4..61dd367 100644
--- a/expressions/aggregation/AggregateFunctionCount.hpp
+++ b/expressions/aggregation/AggregateFunctionCount.hpp
@@ -54,7 +54,8 @@ class AggregateFunctionCount : public AggregateFunction {
       const std::vector<const Type*> &argument_types) const override;
 
   const Type* resultTypeForArgumentTypes(
-      const std::vector<const Type*> &argument_types) const override;
+      const std::vector<const Type*> &argument_types,
+      const bool is_vector_aggregate) const override;
 
   AggregationHandle* createHandle(
       const std::vector<const Type*> &argument_types) const override;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionMax.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionMax.cpp b/expressions/aggregation/AggregateFunctionMax.cpp
index 6ceacc9..1d999c9 100644
--- a/expressions/aggregation/AggregateFunctionMax.cpp
+++ b/expressions/aggregation/AggregateFunctionMax.cpp
@@ -45,14 +45,16 @@ bool AggregateFunctionMax::canApplyToTypes(
 }
 
 const Type* AggregateFunctionMax::resultTypeForArgumentTypes(
-    const std::vector<const Type*> &argument_types) const {
+    const std::vector<const Type*> &argument_types,
+    const bool is_vector_aggregate) const {
   if (!canApplyToTypes(argument_types)) {
     return nullptr;
   }
 
-  // FIXME(jianqiao): The result type can be nullable when it is NOT a group-by
-  // aggregation.
-  return argument_types.front();
+  const Type *argument_type = argument_types.front();
+  const bool nullable = argument_type->isNullable() || !is_vector_aggregate;
+
+  return &argument_type->getInstance(nullable);
 }
 
 AggregationHandle* AggregateFunctionMax::createHandle(

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionMax.hpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionMax.hpp b/expressions/aggregation/AggregateFunctionMax.hpp
index 4eadaa2..c144220 100644
--- a/expressions/aggregation/AggregateFunctionMax.hpp
+++ b/expressions/aggregation/AggregateFunctionMax.hpp
@@ -54,7 +54,8 @@ class AggregateFunctionMax : public AggregateFunction {
       const std::vector<const Type*> &argument_types) const override;
 
   const Type* resultTypeForArgumentTypes(
-      const std::vector<const Type*> &argument_types) const override;
+      const std::vector<const Type*> &argument_types,
+      const bool is_vector_aggregate) const override;
 
   AggregationHandle* createHandle(
       const std::vector<const Type*> &argument_types) const override;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionMin.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionMin.cpp b/expressions/aggregation/AggregateFunctionMin.cpp
index d88169f..f66b055 100644
--- a/expressions/aggregation/AggregateFunctionMin.cpp
+++ b/expressions/aggregation/AggregateFunctionMin.cpp
@@ -45,14 +45,16 @@ bool AggregateFunctionMin::canApplyToTypes(
 }
 
 const Type* AggregateFunctionMin::resultTypeForArgumentTypes(
-    const std::vector<const Type*> &argument_types) const {
+    const std::vector<const Type*> &argument_types,
+    const bool is_vector_aggregate) const {
   if (!canApplyToTypes(argument_types)) {
     return nullptr;
   }
 
-  // FIXME(jianqiao): The result type can be nullable when it is NOT a group-by
-  // aggregation.
-  return argument_types.front();
+  const Type *argument_type = argument_types.front();
+  const bool nullable = argument_type->isNullable() || !is_vector_aggregate;
+
+  return &argument_type->getInstance(nullable);
 }
 
 AggregationHandle* AggregateFunctionMin::createHandle(

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionMin.hpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionMin.hpp b/expressions/aggregation/AggregateFunctionMin.hpp
index 90d1762..1bafbbd 100644
--- a/expressions/aggregation/AggregateFunctionMin.hpp
+++ b/expressions/aggregation/AggregateFunctionMin.hpp
@@ -54,7 +54,8 @@ class AggregateFunctionMin : public AggregateFunction {
       const std::vector<const Type*> &argument_types) const override;
 
   const Type* resultTypeForArgumentTypes(
-      const std::vector<const Type*> &argument_types) const override;
+      const std::vector<const Type*> &argument_types,
+      const bool is_vector_aggregate) const override;
 
   AggregationHandle* createHandle(
       const std::vector<const Type*> &argument_types) const override;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionSum.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionSum.cpp b/expressions/aggregation/AggregateFunctionSum.cpp
index b62660f..f123c0c 100644
--- a/expressions/aggregation/AggregateFunctionSum.cpp
+++ b/expressions/aggregation/AggregateFunctionSum.cpp
@@ -46,23 +46,27 @@ bool AggregateFunctionSum::canApplyToTypes(
 }
 
 const Type* AggregateFunctionSum::resultTypeForArgumentTypes(
-    const std::vector<const Type*> &argument_types) const {
+    const std::vector<const Type*> &argument_types,
+    const bool is_vector_aggregate) const {
   if (!canApplyToTypes(argument_types)) {
     return nullptr;
   }
 
-  // SUM may return NULL if there are no input rows, and we automatically widen
-  // int to long and float to double to have more headroom when adding up many
-  // values.
-  const Type *sum_type = &(argument_types.front()->getNullableVersion());
-  switch (sum_type->getTypeID()) {
+  // We automatically widen int to long and float to double to have more
+  // headroom when adding up many values.
+  const Type *argument_type = argument_types.front();
+  const bool nullable = argument_type->isNullable() || !is_vector_aggregate;
+
+  const Type *sum_type;
+  switch (argument_type->getTypeID()) {
     case kInt:
-      sum_type = &TypeFactory::GetType(kLong, true);
+      sum_type = &TypeFactory::GetType(kLong, nullable);
       break;
     case kFloat:
-      sum_type = &TypeFactory::GetType(kDouble, true);
+      sum_type = &TypeFactory::GetType(kDouble, nullable);
       break;
     default:
+      sum_type = &TypeFactory::GetType(argument_type->getTypeID(), nullable);
       break;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/AggregateFunctionSum.hpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/AggregateFunctionSum.hpp b/expressions/aggregation/AggregateFunctionSum.hpp
index a4086c3..6ee2fd2 100644
--- a/expressions/aggregation/AggregateFunctionSum.hpp
+++ b/expressions/aggregation/AggregateFunctionSum.hpp
@@ -54,7 +54,8 @@ class AggregateFunctionSum : public AggregateFunction {
       const std::vector<const Type*> &argument_types) const override;
 
   const Type* resultTypeForArgumentTypes(
-      const std::vector<const Type*> &argument_types) const override;
+      const std::vector<const Type*> &argument_types,
+      const bool is_vector_aggregate) const override;
 
   AggregationHandle* createHandle(
       const std::vector<const Type*> &argument_types) const override;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/tests/AggregationHandleAvg_unittest.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/tests/AggregationHandleAvg_unittest.cpp b/expressions/aggregation/tests/AggregationHandleAvg_unittest.cpp
index 0ad50d5..6f91eae 100644
--- a/expressions/aggregation/tests/AggregationHandleAvg_unittest.cpp
+++ b/expressions/aggregation/tests/AggregationHandleAvg_unittest.cpp
@@ -86,8 +86,8 @@ class AggregationHandleAvgTest : public ::testing::Test {
                                             TypeID output_type_id) {
     const Type *result_type =
         AggregateFunctionFactory::Get(AggregationID::kAvg)
-            .resultTypeForArgumentTypes(std::vector<const Type *>(
-                1, &TypeFactory::GetType(input_type_id)));
+            .resultTypeForArgumentTypes({&TypeFactory::GetType(input_type_id)},
+                                        true /* is_vector_aggregate */);
     return (result_type->getTypeID() == output_type_id);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/tests/AggregationHandleCount_unittest.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/tests/AggregationHandleCount_unittest.cpp b/expressions/aggregation/tests/AggregationHandleCount_unittest.cpp
index 86a014b..2f14c6e 100644
--- a/expressions/aggregation/tests/AggregationHandleCount_unittest.cpp
+++ b/expressions/aggregation/tests/AggregationHandleCount_unittest.cpp
@@ -96,8 +96,8 @@ class AggregationHandleCountTest : public ::testing::Test {
                                             TypeID output_type_id) {
     const Type *result_type =
         AggregateFunctionFactory::Get(AggregationID::kCount)
-            .resultTypeForArgumentTypes(std::vector<const Type *>(
-                1, &TypeFactory::GetType(input_type_id)));
+            .resultTypeForArgumentTypes({&TypeFactory::GetType(input_type_id)},
+                                        true /* is_vector_aggregate */);
     return (result_type->getTypeID() == output_type_id);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/tests/AggregationHandleMax_unittest.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/tests/AggregationHandleMax_unittest.cpp b/expressions/aggregation/tests/AggregationHandleMax_unittest.cpp
index d5e8d18..dbc8bff 100644
--- a/expressions/aggregation/tests/AggregationHandleMax_unittest.cpp
+++ b/expressions/aggregation/tests/AggregationHandleMax_unittest.cpp
@@ -96,8 +96,8 @@ class AggregationHandleMaxTest : public ::testing::Test {
                                             TypeID output_type_id) {
     const Type *result_type =
         AggregateFunctionFactory::Get(AggregationID::kMax)
-            .resultTypeForArgumentTypes(std::vector<const Type *>(
-                1, &TypeFactory::GetType(input_type_id)));
+            .resultTypeForArgumentTypes({&TypeFactory::GetType(input_type_id)},
+                                        true /* is_vector_aggregate */);
     return (result_type->getTypeID() == output_type_id);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/tests/AggregationHandleMin_unittest.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/tests/AggregationHandleMin_unittest.cpp b/expressions/aggregation/tests/AggregationHandleMin_unittest.cpp
index 798ba76..f71a3e4 100644
--- a/expressions/aggregation/tests/AggregationHandleMin_unittest.cpp
+++ b/expressions/aggregation/tests/AggregationHandleMin_unittest.cpp
@@ -95,8 +95,8 @@ class AggregationHandleMinTest : public ::testing::Test {
                                             TypeID output_type_id) {
     const Type *result_type =
         AggregateFunctionFactory::Get(AggregationID::kMin)
-            .resultTypeForArgumentTypes(std::vector<const Type *>(
-                1, &TypeFactory::GetType(input_type_id)));
+            .resultTypeForArgumentTypes({&TypeFactory::GetType(input_type_id)},
+                                        true /* is_vector_aggregate */);
     return (result_type->getTypeID() == output_type_id);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/expressions/aggregation/tests/AggregationHandleSum_unittest.cpp
----------------------------------------------------------------------
diff --git a/expressions/aggregation/tests/AggregationHandleSum_unittest.cpp b/expressions/aggregation/tests/AggregationHandleSum_unittest.cpp
index 31a35a0..5d2e665 100644
--- a/expressions/aggregation/tests/AggregationHandleSum_unittest.cpp
+++ b/expressions/aggregation/tests/AggregationHandleSum_unittest.cpp
@@ -85,8 +85,8 @@ class AggregationHandleSumTest : public ::testing::Test {
                                             TypeID output_type_id) {
     const Type *result_type =
         AggregateFunctionFactory::Get(AggregationID::kSum)
-            .resultTypeForArgumentTypes(std::vector<const Type *>(
-                1, &TypeFactory::GetType(input_type_id)));
+            .resultTypeForArgumentTypes({&TypeFactory::GetType(input_type_id)},
+                                         true /* is_vector_aggregate */);
     return (result_type->getTypeID() == output_type_id);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/query_optimizer/expressions/AggregateFunction.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/expressions/AggregateFunction.cpp b/query_optimizer/expressions/AggregateFunction.cpp
index cf2cb00..a34d34d 100644
--- a/query_optimizer/expressions/AggregateFunction.cpp
+++ b/query_optimizer/expressions/AggregateFunction.cpp
@@ -43,7 +43,8 @@ bool AggregateFunction::isNullable() const {
     argument_types.emplace_back(&argument->getValueType());
   }
 
-  const Type *return_type = aggregate_.resultTypeForArgumentTypes(argument_types);
+  const Type *return_type =
+      aggregate_.resultTypeForArgumentTypes(argument_types, is_vector_aggregate_);
   DCHECK(return_type != nullptr);
   return return_type->isNullable();
 }
@@ -54,7 +55,8 @@ const Type& AggregateFunction::getValueType() const {
     argument_types.emplace_back(&argument->getValueType());
   }
 
-  const Type *return_type = aggregate_.resultTypeForArgumentTypes(argument_types);
+  const Type *return_type =
+      aggregate_.resultTypeForArgumentTypes(argument_types, is_vector_aggregate_);
   DCHECK(return_type != nullptr);
   return *return_type;
 }

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/types/Type.cpp
----------------------------------------------------------------------
diff --git a/types/Type.cpp b/types/Type.cpp
index f3d3f1b..50b45cc 100644
--- a/types/Type.cpp
+++ b/types/Type.cpp
@@ -67,6 +67,10 @@ serialization::Type Type::getProto() const {
   return proto;
 }
 
+const Type& Type::getInstance(const bool nullable) const {
+  return nullable ? getNullableVersion() : getNonNullableVersion();
+}
+
 TypedValue Type::coerceValue(const TypedValue &original_value,
                              const Type &original_type) const {
   DCHECK(isCoercibleFrom(original_type))

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f477c5d3/types/Type.hpp
----------------------------------------------------------------------
diff --git a/types/Type.hpp b/types/Type.hpp
index 0e8c4e5..ae39fc3 100644
--- a/types/Type.hpp
+++ b/types/Type.hpp
@@ -183,6 +183,8 @@ class Type {
    **/
   virtual const Type& getNonNullableVersion() const = 0;
 
+  virtual const Type& getInstance(const bool nullable) const;
+
   /**
    * @brief Determine whether data items of this type have variable
    *        byte-length.