// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include "function_test_util.h" #include "vec/columns/column.h" #include "vec/columns/column_const.h" #include "vec/columns/column_decimal.h" #include "vec/columns/columns_number.h" #include "vec/common/assert_cast.h" #include "vec/core/column_numbers.h" #include "vec/core/types.h" #include "vec/data_types/data_type_decimal.h" #include "vec/data_types/data_type_number.h" #include "vec/functions/function_truncate.h" namespace doris::vectorized { // {precision, scale} -> {input, scale_arg, expectation} using TestDataSet = std::map, std::vector>>; const static TestDataSet truncate_decimal32_cases = { {{1, 0}, { {1, -10, 0}, {1, -9, 0}, {1, -8, 0}, {1, -7, 0}, {1, -6, 0}, {1, -5, 0}, {1, -4, 0}, {1, -3, 0}, {1, -2, 0}, {1, -1, 0}, {1, 0, 1}, {1, 1, 1}, {1, 2, 1}, {1, 3, 1}, {1, 4, 1}, {1, 5, 1}, {1, 6, 1}, {1, 7, 1}, {1, 8, 1}, {1, 9, 1}, {1, 10, 1}, }}, {{1, 1}, { {1, -10, 0}, {1, -9, 0}, {1, -8, 0}, {1, -7, 0}, {1, -6, 0}, {1, -5, 0}, {1, -4, 0}, {1, -3, 0}, {1, -2, 0}, {1, -1, 0}, {1, 0, 0}, {1, 1, 1}, {1, 2, 1}, {1, 3, 1}, {1, 4, 1}, {1, 5, 1}, {1, 6, 1}, {1, 7, 1}, {1, 8, 1}, {1, 9, 1}, {1, 10, 1}, }}, {{2, 0}, { {12, -4, 0}, {12, -3, 0}, {12, -2, 0}, {12, -1, 10}, {12, 0, 12}, {12, 1, 12}, {12, 2, 12}, {12, 3, 12}, {12, 4, 12}, }}, {{2, 1}, { {12, -4, 0}, {12, -3, 0}, {12, -2, 0}, {12, -1, 0}, {12, 0, 10}, {12, 1, 12}, {12, 2, 12}, {12, 3, 12}, {12, 4, 12}, }}, {{2, 2}, { {12, -4, 0}, {12, -3, 0}, {12, -2, 0}, {12, -1, 0}, {12, 0, 0}, {12, 1, 10}, {12, 2, 12}, {12, 3, 12}, {12, 4, 12}, }}, {{9, 0}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 100000000}, {123456789, -7, 120000000}, {123456789, -6, 123000000}, {123456789, -5, 123400000}, {123456789, -4, 123450000}, {123456789, -3, 123456000}, {123456789, -2, 123456700}, {123456789, -1, 123456780}, {123456789, 0, 123456789}, {123456789, 1, 123456789}, {123456789, 2, 123456789}, {123456789, 3, 123456789}, {123456789, 4, 123456789}, {123456789, 5, 123456789}, {123456789, 6, 123456789}, {123456789, 7, 123456789}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 1}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 100000000}, {123456789, -6, 120000000}, {123456789, -5, 123000000}, {123456789, -4, 123400000}, {123456789, -3, 123450000}, {123456789, -2, 123456000}, {123456789, -1, 123456700}, {123456789, 0, 123456780}, {123456789, 1, 123456789}, {123456789, 2, 123456789}, {123456789, 3, 123456789}, {123456789, 4, 123456789}, {123456789, 5, 123456789}, {123456789, 6, 123456789}, {123456789, 7, 123456789}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 2}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 0}, {123456789, -6, 100000000}, {123456789, -5, 120000000}, {123456789, -4, 123000000}, {123456789, -3, 123400000}, {123456789, -2, 123450000}, {123456789, -1, 123456000}, {123456789, 0, 123456700}, {123456789, 1, 123456780}, {123456789, 2, 123456789}, {123456789, 3, 123456789}, {123456789, 4, 123456789}, {123456789, 5, 123456789}, {123456789, 6, 123456789}, {123456789, 7, 123456789}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 3}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 0}, {123456789, -6, 0}, {123456789, -5, 100000000}, {123456789, -4, 120000000}, {123456789, -3, 123000000}, {123456789, -2, 123400000}, {123456789, -1, 123450000}, {123456789, 0, 123456000}, {123456789, 1, 123456700}, {123456789, 2, 123456780}, {123456789, 3, 123456789}, {123456789, 4, 123456789}, {123456789, 5, 123456789}, {123456789, 6, 123456789}, {123456789, 7, 123456789}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 4}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 0}, {123456789, -6, 0}, {123456789, -5, 0}, {123456789, -4, 100000000}, {123456789, -3, 120000000}, {123456789, -2, 123000000}, {123456789, -1, 123400000}, {123456789, 0, 123450000}, {123456789, 1, 123456000}, {123456789, 2, 123456700}, {123456789, 3, 123456780}, {123456789, 4, 123456789}, {123456789, 5, 123456789}, {123456789, 6, 123456789}, {123456789, 7, 123456789}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 5}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 0}, {123456789, -6, 0}, {123456789, -5, 0}, {123456789, -4, 0}, {123456789, -3, 100000000}, {123456789, -2, 120000000}, {123456789, -1, 123000000}, {123456789, 0, 123400000}, {123456789, 1, 123450000}, {123456789, 2, 123456000}, {123456789, 3, 123456700}, {123456789, 4, 123456780}, {123456789, 5, 123456789}, {123456789, 6, 123456789}, {123456789, 7, 123456789}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 6}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 0}, {123456789, -6, 0}, {123456789, -5, 0}, {123456789, -4, 0}, {123456789, -3, 0}, {123456789, -2, 100000000}, {123456789, -1, 120000000}, {123456789, 0, 123000000}, {123456789, 1, 123400000}, {123456789, 2, 123450000}, {123456789, 3, 123456000}, {123456789, 4, 123456700}, {123456789, 5, 123456780}, {123456789, 6, 123456789}, {123456789, 7, 123456789}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 7}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 0}, {123456789, -6, 0}, {123456789, -5, 0}, {123456789, -4, 0}, {123456789, -3, 0}, {123456789, -2, 0}, {123456789, -1, 100000000}, {123456789, 0, 120000000}, {123456789, 1, 123000000}, {123456789, 2, 123400000}, {123456789, 3, 123450000}, {123456789, 4, 123456000}, {123456789, 5, 123456700}, {123456789, 6, 123456780}, {123456789, 7, 123456789}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 8}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 0}, {123456789, -6, 0}, {123456789, -5, 0}, {123456789, -4, 0}, {123456789, -3, 0}, {123456789, -2, 0}, {123456789, -1, 0}, {123456789, 0, 100000000}, {123456789, 1, 120000000}, {123456789, 2, 123000000}, {123456789, 3, 123400000}, {123456789, 4, 123450000}, {123456789, 5, 123456000}, {123456789, 6, 123456700}, {123456789, 7, 123456780}, {123456789, 8, 123456789}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}, {{9, 9}, { {123456789, -10, 0}, {123456789, -9, 0}, {123456789, -8, 0}, {123456789, -7, 0}, {123456789, -6, 0}, {123456789, -5, 0}, {123456789, -4, 0}, {123456789, -3, 0}, {123456789, -2, 0}, {123456789, -1, 0}, {123456789, 0, 0}, {123456789, 1, 100000000}, {123456789, 2, 120000000}, {123456789, 3, 123000000}, {123456789, 4, 123400000}, {123456789, 5, 123450000}, {123456789, 6, 123456000}, {123456789, 7, 123456700}, {123456789, 8, 123456780}, {123456789, 9, 123456789}, {123456789, 10, 123456789}, }}}; const static TestDataSet truncate_decimal64_cases = { {{10, 0}, {{1234567891, -11, 0}, {1234567891, -10, 0}, {1234567891, -9, 1000000000}, {1234567891, -8, 1200000000}, {1234567891, -7, 1230000000}, {1234567891, -6, 1234000000}, {1234567891, -5, 1234500000}, {1234567891, -4, 1234560000}, {1234567891, -3, 1234567000}, {1234567891, -2, 1234567800}, {1234567891, -1, 1234567890}, {1234567891, 0, 1234567891}, {1234567891, 1, 1234567891}, {1234567891, 2, 1234567891}, {1234567891, 3, 1234567891}, {1234567891, 4, 1234567891}, {1234567891, 5, 1234567891}, {1234567891, 6, 1234567891}, {1234567891, 7, 1234567891}, {1234567891, 8, 1234567891}, {1234567891, 9, 1234567891}, {1234567891, 10, 1234567891}, {1234567891, 11, 1234567891}}}, {{10, 1}, {{1234567891, -11, 0}, {1234567891, -10, 0}, {1234567891, -9, 0}, {1234567891, -8, 1000000000}, {1234567891, -7, 1200000000}, {1234567891, -6, 1230000000}, {1234567891, -5, 1234000000}, {1234567891, -4, 1234500000}, {1234567891, -3, 1234560000}, {1234567891, -2, 1234567000}, {1234567891, -1, 1234567800}, {1234567891, 0, 1234567890}, {1234567891, 1, 1234567891}, {1234567891, 2, 1234567891}, {1234567891, 3, 1234567891}, {1234567891, 4, 1234567891}, {1234567891, 5, 1234567891}, {1234567891, 6, 1234567891}, {1234567891, 7, 1234567891}, {1234567891, 8, 1234567891}, {1234567891, 9, 1234567891}, {1234567891, 10, 1234567891}, {1234567891, 11, 1234567891} }}, {{10, 2}, {{1234567891, -11, 0}, {1234567891, -10, 0}, {1234567891, -9, 0}, {1234567891, -8, 0}, {1234567891, -7, 1000000000}, {1234567891, -6, 1200000000}, {1234567891, -5, 1230000000}, {1234567891, -4, 1234000000}, {1234567891, -3, 1234500000}, {1234567891, -2, 1234560000}, {1234567891, -1, 1234567000}, {1234567891, 0, 1234567800}, {1234567891, 1, 1234567890}, {1234567891, 2, 1234567891}, {1234567891, 3, 1234567891}, {1234567891, 4, 1234567891}, {1234567891, 5, 1234567891}, {1234567891, 6, 1234567891}, {1234567891, 7, 1234567891}, {1234567891, 8, 1234567891}, {1234567891, 9, 1234567891}, {1234567891, 10, 1234567891}, {1234567891, 11, 1234567891}}}, {{10, 9}, {{1234567891, -11, 0}, {1234567891, -10, 0}, {1234567891, -9, 0}, {1234567891, -8, 0}, {1234567891, -7, 0}, {1234567891, -6, 0}, {1234567891, -5, 0}, {1234567891, -4, 0}, {1234567891, -3, 0}, {1234567891, -2, 0}, {1234567891, -1, 0}, {1234567891, 0, 1000000000}, {1234567891, 1, 1200000000}, {1234567891, 2, 1230000000}, {1234567891, 3, 1234000000}, {1234567891, 4, 1234500000}, {1234567891, 5, 1234560000}, {1234567891, 6, 1234567000}, {1234567891, 7, 1234567800}, {1234567891, 8, 1234567890}, {1234567891, 9, 1234567891}, {1234567891, 10, 1234567891}, {1234567891, 11, 1234567891}}}, {{18, 0}, {{123456789123456789, -19, 0}, {123456789123456789, -18, 0}, {123456789123456789, -17, 100000000000000000}, {123456789123456789, -16, 120000000000000000}, {123456789123456789, -15, 123000000000000000}, {123456789123456789, -14, 123400000000000000}, {123456789123456789, -13, 123450000000000000}, {123456789123456789, -12, 123456000000000000}, {123456789123456789, -11, 123456700000000000}, {123456789123456789, -10, 123456780000000000}, {123456789123456789, -9, 123456789000000000}, {123456789123456789, -8, 123456789100000000}, {123456789123456789, -7, 123456789120000000}, {123456789123456789, -6, 123456789123000000}, {123456789123456789, -5, 123456789123400000}, {123456789123456789, -4, 123456789123450000}, {123456789123456789, -3, 123456789123456000}, {123456789123456789, -2, 123456789123456700}, {123456789123456789, -1, 123456789123456780}, {123456789123456789, 0, 123456789123456789}, {123456789123456789, 1, 123456789123456789}, {123456789123456789, 2, 123456789123456789}, {123456789123456789, 3, 123456789123456789}, {123456789123456789, 4, 123456789123456789}, {123456789123456789, 5, 123456789123456789}, {123456789123456789, 6, 123456789123456789}, {123456789123456789, 7, 123456789123456789}, {123456789123456789, 8, 123456789123456789}, {123456789123456789, 18, 123456789123456789}}}, {{18, 18}, {{123456789123456789, -1, 0}, {123456789123456789, 0, 0}, {123456789123456789, 1, 100000000000000000}, {123456789123456789, 2, 120000000000000000}, {123456789123456789, 3, 123000000000000000}, {123456789123456789, 4, 123400000000000000}, {123456789123456789, 5, 123450000000000000}, {123456789123456789, 6, 123456000000000000}, {123456789123456789, 7, 123456700000000000}, {123456789123456789, 8, 123456780000000000}, {123456789123456789, 9, 123456789000000000}, {123456789123456789, 10, 123456789100000000}, {123456789123456789, 11, 123456789120000000}, {123456789123456789, 12, 123456789123000000}, {123456789123456789, 13, 123456789123400000}, {123456789123456789, 14, 123456789123450000}, {123456789123456789, 15, 123456789123456000}, {123456789123456789, 16, 123456789123456700}, {123456789123456789, 17, 123456789123456780}, {123456789123456789, 18, 123456789123456789}, {123456789123456789, 19, 123456789123456789}, {123456789123456789, 20, 123456789123456789}, {123456789123456789, 21, 123456789123456789}, {123456789123456789, 22, 123456789123456789}, {123456789123456789, 23, 123456789123456789}, {123456789123456789, 24, 123456789123456789}, {123456789123456789, 25, 123456789123456789}, {123456789123456789, 26, 123456789123456789}}}}; template static void checker(const TestDataSet& truncate_test_cases, bool decimal_col_is_const) { static_assert(IsDecimalNumber); auto func = std::dynamic_pointer_cast(FuncType::create()); FunctionContext* context = nullptr; for (const auto& test_case : truncate_test_cases) { Block block; size_t res_idx = 2; ColumnNumbers arguments = {0, 1, 2}; const int precision = test_case.first.first; const int scale = test_case.first.second; const size_t input_rows_count = test_case.second.size(); auto col_general = ColumnDecimal::create(input_rows_count, scale); auto col_scale = ColumnInt32::create(); auto col_res_expected = ColumnDecimal::create(input_rows_count, scale); size_t rid = 0; for (const auto& test_date : test_case.second) { auto input = std::get<0>(test_date); auto scale_arg = std::get<1>(test_date); auto expectation = std::get<2>(test_date); col_general->get_element(rid) = DecimalType(input); col_scale->insert(scale_arg); col_res_expected->get_element(rid) = DecimalType(expectation); rid++; } if (decimal_col_is_const) { block.insert({ColumnConst::create(col_general->clone_resized(1), 1), std::make_shared>(precision, scale), "col_general_const"}); } else { block.insert({col_general->clone(), std::make_shared>(precision, scale), "col_general"}); } block.insert({col_scale->clone(), std::make_shared(), "col_scale"}); block.insert({nullptr, std::make_shared>(precision, scale), "col_res"}); auto status = func->execute_impl(context, block, arguments, res_idx, input_rows_count); auto col_res = assert_cast&>( *(block.get_by_position(res_idx).column)); EXPECT_TRUE(status.ok()); for (size_t i = 0; i < input_rows_count; ++i) { auto res = col_res.get_element(i); auto res_expected = col_res_expected->get_element(i); EXPECT_EQ(res, res_expected) << "precision " << precision << " input_scale " << scale << " input " << col_general->get_element(i) << " scale_arg " << col_scale->get_element(i) << " res " << res << " res_expected " << res_expected; } } } TEST(TruncateFunctionTest, normal_decimal) { checker, Decimal32>(truncate_decimal32_cases, false); checker, Decimal64>(truncate_decimal64_cases, false); } TEST(TruncateFunctionTest, normal_decimal_const) { checker, Decimal32>(truncate_decimal32_cases, true); checker, Decimal64>(truncate_decimal64_cases, true); } } // namespace doris::vectorized