From 73c3bccee70a58d80ec2a327397af4993e2d39c7 Mon Sep 17 00:00:00 2001 From: MrPresent-Han Date: Mon, 11 Nov 2024 23:19:18 -0500 Subject: [PATCH] added calculation for sum --- internal/core/CMakeLists.txt | 1 + internal/core/src/common/Types.h | 2 +- internal/core/src/common/Utils.h | 10 ++++++ .../src/exec/operator/query-agg/Aggregate.h | 9 +++-- .../query-agg/SimpleNumericAggregate.h | 10 ++++++ .../operator/query-agg/SumAggregateBase.h | 34 +++++++++++++++++++ 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/internal/core/CMakeLists.txt b/internal/core/CMakeLists.txt index 2b33959183843..3de2f4cfb9c0c 100644 --- a/internal/core/CMakeLists.txt +++ b/internal/core/CMakeLists.txt @@ -253,6 +253,7 @@ endif () config_summary() # **************************** Source files **************************** +set(BUILD_UNIT_TEST "ON") if ( BUILD_UNIT_TEST STREQUAL "ON" AND BUILD_COVERAGE STREQUAL "ON" ) append_flags( CMAKE_CXX_FLAGS FLAGS diff --git a/internal/core/src/common/Types.h b/internal/core/src/common/Types.h index 590bd9bc62603..d89c5d93a7e0f 100644 --- a/internal/core/src/common/Types.h +++ b/internal/core/src/common/Types.h @@ -697,7 +697,7 @@ class RowType final { AssertInfo(names_.size() == columns_types_.size(), "Name count:{} and column count:{} must be the same", names_.size(), - columns_types_.size()) + columns_types_.size()); }; static const std::shared_ptr None; diff --git a/internal/core/src/common/Utils.h b/internal/core/src/common/Utils.h index 430f70f8670bc..da530c94ba778 100644 --- a/internal/core/src/common/Utils.h +++ b/internal/core/src/common/Utils.h @@ -327,5 +327,15 @@ inline std::string lowerString(const std::string& str) { return ret; } +template +T checkPlus(const T& a, const T& b, const char* typeName = "integer"){ + T result; + bool overflow = __builtin_add_overflow(a, b, &result); + if (UNLIKELY(overflow)) { + VELOX_ARITHMETIC_ERROR("{} overflow: {} + {}", typeName, a, b); + } + return result; +} + } // namespace milvus diff --git a/internal/core/src/exec/operator/query-agg/Aggregate.h b/internal/core/src/exec/operator/query-agg/Aggregate.h index aca4b75488c55..0a735b3d092c0 100644 --- a/internal/core/src/exec/operator/query-agg/Aggregate.h +++ b/internal/core/src/exec/operator/query-agg/Aggregate.h @@ -43,11 +43,6 @@ class Aggregate { // so larger values need not be represented. int32_t rowSizeOffset_ = 0; - // Number of null accumulators in the current state of the aggregation - // operator for this aggregate. If 0, clearing the null as part of update - // is not needed. - uint64_t numNulls_ = 0; - public: DataType resultType() const { return result_type_; @@ -128,6 +123,10 @@ class Aggregate { int32_t rowSizeOffset); virtual void initializeNewGroupsInternal(char** groups, folly::Range indices) = 0; + // Number of null accumulators in the current state of the aggregation + // operator for this aggregate. If 0, clearing the null as part of update + // is not needed. + uint64_t numNulls_ = 0; }; using AggregateFunctionFactory = std::function(plan::AggregationNode::Step step, diff --git a/internal/core/src/exec/operator/query-agg/SimpleNumericAggregate.h b/internal/core/src/exec/operator/query-agg/SimpleNumericAggregate.h index c514043c99c8e..ac0ce098dea73 100644 --- a/internal/core/src/exec/operator/query-agg/SimpleNumericAggregate.h +++ b/internal/core/src/exec/operator/query-agg/SimpleNumericAggregate.h @@ -41,6 +41,16 @@ class SimpleNumericAggregate : public exec::Aggregate { } } } + + template + void updateGroups(char** groups, + const TargetBitmapView& rows, + const VectorPtr& vector, + UpdateSingleValue updateSingleValue, + bool mayPushdown){ + + } }; } diff --git a/internal/core/src/exec/operator/query-agg/SumAggregateBase.h b/internal/core/src/exec/operator/query-agg/SumAggregateBase.h index 6d1e82a9cc727..afbd6d0b2f5fd 100644 --- a/internal/core/src/exec/operator/query-agg/SumAggregateBase.h +++ b/internal/core/src/exec/operator/query-agg/SumAggregateBase.h @@ -10,6 +10,7 @@ // or implied. See the License for the specific language governing permissions and limitations under the License #pragma once #include "SimpleNumericAggregate.h" +#include "common/Utils.h" namespace milvus{ namespace exec { @@ -38,9 +39,42 @@ class SumAggregateBase: public SimpleNumericAggregate& input, bool mayPushDown) override { + + } + void initializeNewGroupsInternal(char** groups, folly::Range indices) override { } + +protected: + template + void updateInternal(char** groups, const TargetBitmapView& activeRows, + const std::vector& input, bool mayPushDown) { + const auto& input_column = input[0]; + if (Aggregate::numNulls_) { + BaseAggregate::template updateGroups(groups, activeRows, input_column, + &updateSingleValue, false); + } else { + BaseAggregate::template updateGroups(groups, activeRows, input_column, + &updateSingleValue, false); + } + } + +private: + template +#if defined(FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER) + FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER("signed-integer-overflow") +#endif + static void updateSingleValue(TData& result, TData value) { + if constexpr (std::is_same_v || std::is_same_v|| + std::is_same_v && Overflow) { + result += value; + } else { + result = checkPlus(result, value); + } + } }; } }