|
| 1 | +// Copyright 2023 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <AggregateFunctions/AggregateFunctionNull.h> |
| 16 | +#include <AggregateFunctions/AggregateFunctionSum.h> |
| 17 | +#include <Core/Types.h> |
| 18 | +#include <DataTypes/DataTypesNumber.h> |
| 19 | +#include <Flash/Coprocessor/AggregationInterpreterHelper.h> |
| 20 | +#include <Flash/Coprocessor/DAGCodec.h> |
| 21 | +#include <Flash/Coprocessor/DAGUtils.h> |
| 22 | +#include <IO/Buffer/WriteBufferFromString.h> |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +namespace DB |
| 26 | +{ |
| 27 | +namespace tests |
| 28 | +{ |
| 29 | +namespace |
| 30 | +{ |
| 31 | +template <typename T> |
| 32 | +using AggregateFunctionSumSimple = AggregateFunctionSum< |
| 33 | + T, |
| 34 | + typename NearestFieldType<T>::Type, |
| 35 | + AggregateFunctionSumData<typename NearestFieldType<T>::Type>>; |
| 36 | + |
| 37 | +template <typename TInput, typename TExpectedOutput> |
| 38 | +void checkSumIntReturnType(const String & expected_output, const String & expected_output_nullable) |
| 39 | +{ |
| 40 | + using NestedFunc = AggregateFunctionSumSimple<TInput>; |
| 41 | + auto nested = std::make_shared<NestedFunc>(); |
| 42 | + ASSERT_EQ(nested->getReturnType()->getName(), expected_output); |
| 43 | + |
| 44 | + auto wrapped |
| 45 | + = std::make_shared<AggregateFunctionNullUnary</*result_is_nullable*/ true, /*input_is_nullable*/ true>>(nested); |
| 46 | + ASSERT_EQ(wrapped->getReturnType()->getName(), expected_output_nullable); |
| 47 | +} |
| 48 | +} // namespace |
| 49 | + |
| 50 | +TEST(SumIntAggFuncTest, DagUtilsMappedToSum) |
| 51 | +{ |
| 52 | + tipb::Expr expr; |
| 53 | + expr.set_tp(tipb::ExprType::SumInt); |
| 54 | + |
| 55 | + ASSERT_TRUE(isAggFunctionExpr(expr)); |
| 56 | + ASSERT_EQ(getAggFunctionName(expr), "sum"); |
| 57 | + ASSERT_EQ(getFunctionName(expr), "sum"); |
| 58 | +} |
| 59 | + |
| 60 | +TEST(SumIntAggFuncTest, ExprToStringIsSum) |
| 61 | +{ |
| 62 | + std::vector<NameAndTypePair> input_cols; |
| 63 | + input_cols.emplace_back("col0", std::make_shared<DataTypeInt32>()); |
| 64 | + |
| 65 | + tipb::Expr col_ref; |
| 66 | + col_ref.set_tp(tipb::ExprType::ColumnRef); |
| 67 | + WriteBufferFromOwnString ss; |
| 68 | + encodeDAGInt64(0, ss); |
| 69 | + col_ref.set_val(ss.releaseStr()); |
| 70 | + |
| 71 | + tipb::Expr sum_int; |
| 72 | + sum_int.set_tp(tipb::ExprType::SumInt); |
| 73 | + *sum_int.add_children() = col_ref; |
| 74 | + |
| 75 | + ASSERT_EQ(exprToString(sum_int, input_cols), "sum(col0)"); |
| 76 | +} |
| 77 | + |
| 78 | +TEST(SumIntAggFuncTest, PartialStageIsTreatedAsSum) |
| 79 | +{ |
| 80 | + tipb::Expr expr; |
| 81 | + expr.set_tp(tipb::ExprType::SumInt); |
| 82 | + |
| 83 | + ASSERT_FALSE(AggregationInterpreterHelper::isSumOnPartialResults(expr)); |
| 84 | + |
| 85 | + expr.set_aggfuncmode(tipb::AggFunctionMode::Partial1Mode); |
| 86 | + ASSERT_FALSE(AggregationInterpreterHelper::isSumOnPartialResults(expr)); |
| 87 | + |
| 88 | + expr.set_aggfuncmode(tipb::AggFunctionMode::Partial2Mode); |
| 89 | + ASSERT_TRUE(AggregationInterpreterHelper::isSumOnPartialResults(expr)); |
| 90 | + |
| 91 | + expr.set_aggfuncmode(tipb::AggFunctionMode::FinalMode); |
| 92 | + ASSERT_TRUE(AggregationInterpreterHelper::isSumOnPartialResults(expr)); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(SumIntAggFuncTest, ReturnTypeForIntegerInputs) |
| 96 | +{ |
| 97 | + checkSumIntReturnType<Int8, Int64>("Int64", "Nullable(Int64)"); |
| 98 | + checkSumIntReturnType<Int16, Int64>("Int64", "Nullable(Int64)"); |
| 99 | + checkSumIntReturnType<Int32, Int64>("Int64", "Nullable(Int64)"); |
| 100 | + checkSumIntReturnType<Int64, Int64>("Int64", "Nullable(Int64)"); |
| 101 | + |
| 102 | + checkSumIntReturnType<UInt8, UInt64>("UInt64", "Nullable(UInt64)"); |
| 103 | + checkSumIntReturnType<UInt16, UInt64>("UInt64", "Nullable(UInt64)"); |
| 104 | + checkSumIntReturnType<UInt32, UInt64>("UInt64", "Nullable(UInt64)"); |
| 105 | + checkSumIntReturnType<UInt64, UInt64>("UInt64", "Nullable(UInt64)"); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace tests |
| 109 | +} // namespace DB |
0 commit comments