From a6058011278c83991dc9eeccda34d3d6aa4e5e92 Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Thu, 3 Mar 2022 16:39:47 +0800 Subject: [PATCH] add more case in bench_function_cast.cpp (#4109) close pingcap/tics#4178 --- .../Functions/tests/bench_function_cast.cpp | 370 ++++++++++++++---- 1 file changed, 292 insertions(+), 78 deletions(-) diff --git a/dbms/src/Functions/tests/bench_function_cast.cpp b/dbms/src/Functions/tests/bench_function_cast.cpp index a5f9986d74c..a763a3f99ec 100644 --- a/dbms/src/Functions/tests/bench_function_cast.cpp +++ b/dbms/src/Functions/tests/bench_function_cast.cpp @@ -5,7 +5,13 @@ #include #include +#include +#include #include +#include + +using namespace boost::multiprecision; +using namespace boost::random; namespace DB { @@ -32,88 +38,168 @@ class CastToDecimalBench : public benchmark::Fixture DataTypePtr from_type_int16 = std::make_shared(); DataTypePtr from_type_int32 = std::make_shared(); DataTypePtr from_type_int64 = std::make_shared(); - DataTypePtr from_type_uint8 = std::make_shared(); - DataTypePtr from_type_uint16 = std::make_shared(); - DataTypePtr from_type_uint32 = std::make_shared(); - DataTypePtr from_type_uint64 = std::make_shared(); + DataTypePtr from_type_int64_small = std::make_shared(); + DataTypePtr from_type_uint8 = std::make_shared(); + DataTypePtr from_type_uint16 = std::make_shared(); + DataTypePtr from_type_uint32 = std::make_shared(); + DataTypePtr from_type_uint64 = std::make_shared(); DataTypePtr from_type_dec_2_1 = std::make_shared(2, 1); + DataTypePtr from_type_dec_2_1_small = std::make_shared(2, 1); + DataTypePtr from_type_dec_3_0 = std::make_shared(3, 0); DataTypePtr from_type_dec_10_3 = std::make_shared(10, 3); DataTypePtr from_type_dec_12_3 = std::make_shared(12, 3); DataTypePtr from_type_dec_25_3 = std::make_shared(25, 3); + DataTypePtr from_type_dec_35_0 = std::make_shared(35, 0); + DataTypePtr from_type_dec_40_0 = std::make_shared(40, 0); + DataTypePtr from_type_dec_40_5 = std::make_shared(40, 5); DataTypePtr from_type_dec_57_0 = std::make_shared(57, 0); DataTypePtr from_type_dec_60_0 = std::make_shared(60, 0); DataTypePtr from_type_dec_60_5 = std::make_shared(60, 5); + DataTypePtr from_type_date = std::make_shared(); + DataTypePtr from_type_datetime_fsp5 = std::make_shared(5); auto tmp_col_int8 = from_type_int8->createColumn(); auto tmp_col_int16 = from_type_int16->createColumn(); auto tmp_col_int32 = from_type_int32->createColumn(); auto tmp_col_int64 = from_type_int64->createColumn(); - auto tmp_col_uint8 = from_type_int8->createColumn(); - auto tmp_col_uint16 = from_type_int16->createColumn(); - auto tmp_col_uint32 = from_type_int32->createColumn(); - auto tmp_col_uint64 = from_type_int64->createColumn(); + auto tmp_col_int64_small = from_type_int64_small->createColumn(); + auto tmp_col_uint8 = from_type_uint8->createColumn(); + auto tmp_col_uint16 = from_type_uint16->createColumn(); + auto tmp_col_uint32 = from_type_uint32->createColumn(); + auto tmp_col_uint64 = from_type_uint64->createColumn(); auto tmp_col_dec_2_1 = from_type_dec_2_1->createColumn(); + auto tmp_col_dec_2_1_small = from_type_dec_2_1_small->createColumn(); + auto tmp_col_dec_3_0 = from_type_dec_3_0->createColumn(); auto tmp_col_dec_10_3 = from_type_dec_10_3->createColumn(); auto tmp_col_dec_12_3 = from_type_dec_12_3->createColumn(); auto tmp_col_dec_25_3 = from_type_dec_25_3->createColumn(); + auto tmp_col_dec_35_0 = from_type_dec_35_0->createColumn(); + auto tmp_col_dec_40_0 = from_type_dec_40_0->createColumn(); + auto tmp_col_dec_40_5 = from_type_dec_40_5->createColumn(); auto tmp_col_dec_57_0 = from_type_dec_57_0->createColumn(); auto tmp_col_dec_60_0 = from_type_dec_60_0->createColumn(); auto tmp_col_dec_60_5 = from_type_dec_60_5->createColumn(); + auto tmp_col_date = from_type_date->createColumn(); + auto tmp_col_datetime_fsp5 = from_type_date->createColumn(); + + std::uniform_int_distribution dist64(std::numeric_limits::min(), std::numeric_limits::max()); + + mt19937 mt; + uniform_int_distribution dist256(-(cpp_int(1) << 256), cpp_int(1) << 256); + const Int256 mod_prec_10 = getScaleMultiplier(10); + const Int256 mod_prec_12 = getScaleMultiplier(12); + const Int256 mod_prec_25 = getScaleMultiplier(25); + const Int256 mod_prec_35 = getScaleMultiplier(35); + const Int256 mod_prec_40 = getScaleMultiplier(40); + const Int256 mod_prec_57 = getScaleMultiplier(57); + const Int256 mod_prec_60 = getScaleMultiplier(60); - std::default_random_engine randomer; for (int i = 0; i < row_num; ++i) { - tmp_col_int8->insert(Field(static_cast(randomer()))); - tmp_col_int16->insert(Field(static_cast(randomer()))); - tmp_col_int32->insert(Field(static_cast(randomer()))); - tmp_col_int64->insert(Field(static_cast(randomer()))); - tmp_col_uint8->insert(Field(static_cast(randomer()))); - tmp_col_uint16->insert(Field(static_cast(randomer()))); - tmp_col_uint32->insert(Field(static_cast(randomer()))); - tmp_col_uint64->insert(Field(static_cast(randomer()))); - - tmp_col_dec_2_1->insert(DecimalField(Decimal(static_cast(randomer())), 0)); - tmp_col_dec_10_3->insert(DecimalField(Decimal(static_cast(randomer())), 0)); - tmp_col_dec_12_3->insert(DecimalField(Decimal(static_cast(randomer())), 0)); - tmp_col_dec_25_3->insert(DecimalField(Decimal(static_cast(randomer())), 0)); - tmp_col_dec_57_0->insert(DecimalField(Decimal(static_cast(randomer())), 0)); - tmp_col_dec_60_0->insert(DecimalField(Decimal(static_cast(randomer())), 0)); - tmp_col_dec_60_5->insert(DecimalField(Decimal(static_cast(randomer())), 0)); + tmp_col_int8->insert(Field(static_cast(static_cast(dist64(mt))))); + tmp_col_int16->insert(Field(static_cast(static_cast(dist64(mt))))); + tmp_col_int32->insert(Field(static_cast(static_cast(dist64(mt))))); + tmp_col_int64->insert(Field(static_cast(static_cast(dist64(mt))))); + tmp_col_int64_small->insert(Field(static_cast(static_cast(dist64(mt) % mod_prec_10)))); + + tmp_col_uint8->insert(Field(static_cast(static_cast(dist64(mt))))); + tmp_col_uint16->insert(Field(static_cast(static_cast(dist64(mt))))); + tmp_col_uint32->insert(Field(static_cast(static_cast(dist64(mt))))); + tmp_col_uint64->insert(Field(static_cast(static_cast(dist64(mt))))); + + tmp_col_dec_2_1->insert(DecimalField(Decimal(static_cast(dist64(mt) % 100)), 1)); + tmp_col_dec_2_1_small->insert(DecimalField(Decimal(static_cast(dist64(mt) % 10)), 1)); + tmp_col_dec_3_0->insert(DecimalField(Decimal(static_cast(dist64(mt) % 1000)), 0)); + tmp_col_dec_10_3->insert(DecimalField(Decimal(static_cast(dist64(mt) % mod_prec_10)), 3)); + tmp_col_dec_12_3->insert(DecimalField(Decimal(static_cast(dist64(mt) % mod_prec_12)), 3)); + tmp_col_dec_25_3->insert(DecimalField(Decimal(static_cast(dist256(mt) % mod_prec_25)), 3)); + tmp_col_dec_35_0->insert(DecimalField(Decimal(static_cast(dist256(mt) % mod_prec_35)), 0)); + tmp_col_dec_40_0->insert(DecimalField(Decimal(static_cast(dist256(mt) % mod_prec_40)), 0)); + tmp_col_dec_40_5->insert(DecimalField(Decimal(static_cast(dist256(mt) % mod_prec_40)), 5)); + tmp_col_dec_57_0->insert(DecimalField(Decimal(static_cast(dist256(mt) % mod_prec_57)), 0)); + tmp_col_dec_60_0->insert(DecimalField(Decimal(static_cast(dist256(mt) % mod_prec_60)), 0)); + tmp_col_dec_60_5->insert(DecimalField(Decimal(static_cast(dist256(mt) % mod_prec_60)), 5)); + tmp_col_date->insert(Field(static_cast(MyDate(2020, 10, 10).toPackedUInt()))); + tmp_col_datetime_fsp5->insert(Field(static_cast(MyDateTime(2020, 1, 10, 0, 0, 0, 0).toPackedUInt()))); } from_col_int8 = ColumnWithTypeAndName(std::move(tmp_col_int8), from_type_int8, "from_col_int8"); from_col_int16 = ColumnWithTypeAndName(std::move(tmp_col_int16), from_type_int16, "from_col_int16"); from_col_int32 = ColumnWithTypeAndName(std::move(tmp_col_int32), from_type_int32, "from_col_int32"); from_col_int64 = ColumnWithTypeAndName(std::move(tmp_col_int64), from_type_int64, "from_col_int64"); + from_col_int64_small = ColumnWithTypeAndName(std::move(tmp_col_int64_small), from_type_int64_small, "from_col_int64_small"); from_col_uint8 = ColumnWithTypeAndName(std::move(tmp_col_uint8), from_type_uint8, "from_col_uint8"); from_col_uint16 = ColumnWithTypeAndName(std::move(tmp_col_uint16), from_type_uint16, "from_col_uint16"); from_col_uint32 = ColumnWithTypeAndName(std::move(tmp_col_uint32), from_type_uint32, "from_col_uint32"); from_col_uint64 = ColumnWithTypeAndName(std::move(tmp_col_uint64), from_type_uint64, "from_col_uint64"); from_col_dec_2_1 = ColumnWithTypeAndName(std::move(tmp_col_dec_2_1), from_type_dec_2_1, "from_col_dec_2_1"); + from_col_dec_2_1_small = ColumnWithTypeAndName(std::move(tmp_col_dec_2_1_small), from_type_dec_2_1_small, "from_col_dec_2_1_small"); + from_col_dec_3_0 = ColumnWithTypeAndName(std::move(tmp_col_dec_3_0), from_type_dec_3_0, "from_col_dec_3_0"); from_col_dec_10_3 = ColumnWithTypeAndName(std::move(tmp_col_dec_10_3), from_type_dec_10_3, "from_col_dec_10_3"); from_col_dec_12_3 = ColumnWithTypeAndName(std::move(tmp_col_dec_12_3), from_type_dec_12_3, "from_col_dec_12_3"); from_col_dec_25_3 = ColumnWithTypeAndName(std::move(tmp_col_dec_25_3), from_type_dec_25_3, "from_col_dec_25_3"); + from_col_dec_35_0 = ColumnWithTypeAndName(std::move(tmp_col_dec_35_0), from_type_dec_35_0, "from_col_dec_35_0"); + from_col_dec_40_0 = ColumnWithTypeAndName(std::move(tmp_col_dec_40_0), from_type_dec_40_0, "from_col_dec_40_0"); + from_col_dec_40_5 = ColumnWithTypeAndName(std::move(tmp_col_dec_40_5), from_type_dec_40_5, "from_col_dec_40_5"); from_col_dec_57_0 = ColumnWithTypeAndName(std::move(tmp_col_dec_57_0), from_type_dec_57_0, "from_col_dec_57_0"); from_col_dec_60_0 = ColumnWithTypeAndName(std::move(tmp_col_dec_60_0), from_type_dec_60_0, "from_col_dec_60_0"); from_col_dec_60_5 = ColumnWithTypeAndName(std::move(tmp_col_dec_60_5), from_type_dec_60_5, "from_col_dec_60_5"); + from_col_date = ColumnWithTypeAndName(std::move(tmp_col_date), from_type_date, "from_col_date"); + from_col_datetime_fsp5 = ColumnWithTypeAndName(std::move(tmp_col_datetime_fsp5), from_type_datetime_fsp5, "from_col_datetime_fsp5"); DataTypePtr dest_type_dec_2_1 = createDecimal(2, 1); DataTypePtr dest_type_dec_2_2 = createDecimal(2, 2); DataTypePtr dest_type_dec_15_4 = createDecimal(15, 4); DataTypePtr dest_type_dec_25_4 = createDecimal(25, 4); DataTypePtr dest_type_dec_31_30 = createDecimal(31, 30); - DataTypePtr dest_type_dec_60_0 = createDecimal(60, 0); DataTypePtr dest_type_dec_60_5 = createDecimal(60, 5); DataTypePtr dest_type_dec_65_30 = createDecimal(65, 30); + DataTypePtr dest_type_dec_8_0 = createDecimal(8, 0); + DataTypePtr dest_type_dec_10_0 = createDecimal(10, 0); + DataTypePtr dest_type_dec_10_5 = createDecimal(10, 5); + DataTypePtr dest_type_dec_10_8 = createDecimal(10, 8); + DataTypePtr dest_type_dec_20_0 = createDecimal(20, 0); + DataTypePtr dest_type_dec_30_0 = createDecimal(30, 0); + DataTypePtr dest_type_dec_40_0 = createDecimal(40, 0); + DataTypePtr dest_type_dec_40_5 = createDecimal(40, 5); + DataTypePtr dest_type_dec_60_0 = createDecimal(60, 0); + DataTypePtr dest_type_dec_60_4 = createDecimal(60, 4); + DataTypePtr dest_type_dec_60_30 = createDecimal(60, 30); dest_col_dec_2_1 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_2_1->getName()), std::make_shared(), ""); dest_col_dec_2_2 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_2_2->getName()), std::make_shared(), ""); dest_col_dec_15_4 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_15_4->getName()), std::make_shared(), ""); dest_col_dec_25_4 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_25_4->getName()), std::make_shared(), ""); dest_col_dec_31_30 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_31_30->getName()), std::make_shared(), ""); - dest_col_dec_60_0 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_60_0->getName()), std::make_shared(), ""); dest_col_dec_60_5 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_60_5->getName()), std::make_shared(), ""); dest_col_dec_65_30 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_65_30->getName()), std::make_shared(), ""); + dest_col_dec_8_0 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_8_0->getName()), std::make_shared(), ""); + dest_col_dec_10_0 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_10_0->getName()), std::make_shared(), ""); + dest_col_dec_10_5 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_10_5->getName()), std::make_shared(), ""); + dest_col_dec_10_8 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_10_8->getName()), std::make_shared(), ""); + dest_col_dec_20_0 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_20_0->getName()), std::make_shared(), ""); + dest_col_dec_30_0 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_30_0->getName()), std::make_shared(), ""); + dest_col_dec_40_0 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_40_0->getName()), std::make_shared(), ""); + dest_col_dec_40_5 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_40_5->getName()), std::make_shared(), ""); + dest_col_dec_60_0 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_60_0->getName()), std::make_shared(), ""); + dest_col_dec_60_4 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_60_4->getName()), std::make_shared(), ""); + dest_col_dec_60_30 = ColumnWithTypeAndName(DataTypeString().createColumnConst(row_num, dest_type_dec_60_30->getName()), std::make_shared(), ""); + + // For test performance of casting Int64/Int128 to Int256. + from_int64_vec = std::vector(row_num); + from_int128_vec = std::vector(row_num); + from_int256_vec = std::vector(row_num); + dest_int64_vec = std::vector(row_num); + dest_int128_vec = std::vector(row_num); + dest_int256_vec = std::vector(row_num); + const Int256 mod_prec_19 = getScaleMultiplier(19); + const Int256 mod_prec_38 = getScaleMultiplier(38); + for (auto i = 0; i < row_num; ++i) + { + from_int64_vec[i] = dist64(mt); + from_int128_vec[i] = static_cast(dist256(mt) % (std::numeric_limits::max() % mod_prec_19)); + from_int256_vec[i] = static_cast(dist256(mt) % (std::numeric_limits::max()) % mod_prec_38); + } } const int row_num = 2000; @@ -122,27 +208,54 @@ class CastToDecimalBench : public benchmark::Fixture ColumnWithTypeAndName from_col_int16; ColumnWithTypeAndName from_col_int32; ColumnWithTypeAndName from_col_int64; + ColumnWithTypeAndName from_col_int64_small; ColumnWithTypeAndName from_col_uint8; ColumnWithTypeAndName from_col_uint16; ColumnWithTypeAndName from_col_uint32; ColumnWithTypeAndName from_col_uint64; ColumnWithTypeAndName from_col_dec_2_1; + ColumnWithTypeAndName from_col_dec_2_1_small; + ColumnWithTypeAndName from_col_dec_3_0; ColumnWithTypeAndName from_col_dec_10_3; ColumnWithTypeAndName from_col_dec_12_3; ColumnWithTypeAndName from_col_dec_25_3; + ColumnWithTypeAndName from_col_dec_35_0; + ColumnWithTypeAndName from_col_dec_40_0; + ColumnWithTypeAndName from_col_dec_40_5; ColumnWithTypeAndName from_col_dec_57_0; ColumnWithTypeAndName from_col_dec_60_0; ColumnWithTypeAndName from_col_dec_60_5; + ColumnWithTypeAndName from_col_date; + ColumnWithTypeAndName from_col_datetime_fsp5; + // second arg is a const string describing dest type. ColumnWithTypeAndName dest_col_dec_2_1; ColumnWithTypeAndName dest_col_dec_2_2; ColumnWithTypeAndName dest_col_dec_15_4; ColumnWithTypeAndName dest_col_dec_25_4; ColumnWithTypeAndName dest_col_dec_31_30; - ColumnWithTypeAndName dest_col_dec_60_0; ColumnWithTypeAndName dest_col_dec_60_5; ColumnWithTypeAndName dest_col_dec_65_30; + + ColumnWithTypeAndName dest_col_dec_8_0; + ColumnWithTypeAndName dest_col_dec_10_0; + ColumnWithTypeAndName dest_col_dec_10_5; + ColumnWithTypeAndName dest_col_dec_10_8; + ColumnWithTypeAndName dest_col_dec_20_0; + ColumnWithTypeAndName dest_col_dec_30_0; + ColumnWithTypeAndName dest_col_dec_40_0; + ColumnWithTypeAndName dest_col_dec_40_5; + ColumnWithTypeAndName dest_col_dec_60_0; + ColumnWithTypeAndName dest_col_dec_60_4; + ColumnWithTypeAndName dest_col_dec_60_30; + + std::vector from_int64_vec; + std::vector from_int128_vec; + std::vector from_int256_vec; + std::vector dest_int64_vec; + std::vector dest_int128_vec; + std::vector dest_int256_vec; }; #define CAST_BENCHMARK(CLASS_NAME, CASE_NAME, FROM_COL, DEST_TYPE) \ @@ -167,60 +280,161 @@ class CastToDecimalBench : public benchmark::Fixture CATCH \ BENCHMARK_REGISTER_F(CLASS_NAME, CASE_NAME)->Iterations(1000); -// int to decimal -// No check overflow; CastInterType is Int32 +// NOTE: There are three factors that will affects performance: +// 1. Need overflow check or not. The cost of handleOverflowError() is huge. +// 2. Which CastInternalType used. Because the cost of Int256 * Int256 is huge. +// 3. Does the type of return value is same with CastInternalType. Because there will be a static_cast if they are not same. +// 4. div. + +// cast(int as decimal) +// no; Int32; Int32 +CAST_BENCHMARK(CastToDecimalBench, int8_to_decimal_8_0, from_col_int8, dest_col_dec_8_0); +// no; Int32; Int64 +CAST_BENCHMARK(CastToDecimalBench, int8_to_decimal_10_0, from_col_int8, dest_col_dec_10_0); +// no; Int32; Int128 +CAST_BENCHMARK(CastToDecimalBench, int8_to_decimal_30_0, from_col_int8, dest_col_dec_30_0); +// no; Int32; Int256 CAST_BENCHMARK(CastToDecimalBench, int8_to_decimal_60_0, from_col_int8, dest_col_dec_60_0); -CAST_BENCHMARK(CastToDecimalBench, uint8_to_decimal_60_0, from_col_uint8, dest_col_dec_60_0); -// No check overflow; CastInterType is Int32 +// no; Int32; Int256 +CAST_BENCHMARK(CastToDecimalBench, int8_to_decimal_60_4, from_col_int8, dest_col_dec_60_4); + +// no; Int32; Int32 +CAST_BENCHMARK(CastToDecimalBench, int16_to_decimal_8_0, from_col_int16, dest_col_dec_8_0); +// no; Int32; Int64 +CAST_BENCHMARK(CastToDecimalBench, int16_to_decimal_10_0, from_col_int16, dest_col_dec_10_0); +// no; Int32; Int128 +CAST_BENCHMARK(CastToDecimalBench, int16_to_decimal_30_0, from_col_int16, dest_col_dec_30_0); +// no; Int32; Int256 CAST_BENCHMARK(CastToDecimalBench, int16_to_decimal_60_0, from_col_int16, dest_col_dec_60_0); -CAST_BENCHMARK(CastToDecimalBench, uint16_to_decimal_60_0, from_col_uint16, dest_col_dec_60_0); -// No check overflow; CastInterType is Int64 +// no; Int32; Int256 +CAST_BENCHMARK(CastToDecimalBench, int16_to_decimal_60_4, from_col_int16, dest_col_dec_60_4); + +// need; Int64; Int32 +CAST_BENCHMARK(CastToDecimalBench, int32_to_decimal_8_0, from_col_int32, dest_col_dec_8_0); +// no; Int64; Int64 +CAST_BENCHMARK(CastToDecimalBench, int32_to_decimal_10_0, from_col_int32, dest_col_dec_10_0); +// no; Int64; Int128 +CAST_BENCHMARK(CastToDecimalBench, int32_to_decimal_30_0, from_col_int32, dest_col_dec_30_0); +// no; Int64; Int256 CAST_BENCHMARK(CastToDecimalBench, int32_to_decimal_60_0, from_col_int32, dest_col_dec_60_0); -CAST_BENCHMARK(CastToDecimalBench, uint32_to_decimal_60_0, from_col_uint32, dest_col_dec_60_0); -// No check overflow; CastInterType is Int128 +// no; Int64; Int256 +CAST_BENCHMARK(CastToDecimalBench, int32_to_decimal_60_4, from_col_int32, dest_col_dec_60_4); + +// need; Int128; Int32 +CAST_BENCHMARK(CastToDecimalBench, int64_to_decimal_8_0, from_col_int64, dest_col_dec_8_0); +// need; Int128; Int64 +CAST_BENCHMARK(CastToDecimalBench, int64_to_decimal_10_0, from_col_int64, dest_col_dec_10_0); +// no; Int128; Int128 +CAST_BENCHMARK(CastToDecimalBench, int64_to_decimal_30_0, from_col_int64, dest_col_dec_30_0); +// no; Int128; Int256 CAST_BENCHMARK(CastToDecimalBench, int64_to_decimal_60_0, from_col_int64, dest_col_dec_60_0); -CAST_BENCHMARK(CastToDecimalBench, uint64_to_decimal_60_0, from_col_uint64, dest_col_dec_60_0); -// No check overflow; CastInterType is Int256 -CAST_BENCHMARK(CastToDecimalBench, int64_to_decimal_65_30, from_col_int64, dest_col_dec_65_30); -CAST_BENCHMARK(CastToDecimalBench, uint64_to_decimal_65_30, from_col_uint64, dest_col_dec_65_30); - -// int to decimal -// Need check overflow; CastInterType is Int32 -CAST_BENCHMARK(CastToDecimalBench, int8_to_decimal_2_1, from_col_int8, dest_col_dec_2_1); -CAST_BENCHMARK(CastToDecimalBench, uint8_to_decimal_2_1, from_col_uint8, dest_col_dec_2_1); -// Need check overflow; CastInterType is Int32 -CAST_BENCHMARK(CastToDecimalBench, int16_to_decimal_2_1, from_col_int16, dest_col_dec_2_1); -CAST_BENCHMARK(CastToDecimalBench, uint16_to_decimal_2_1, from_col_uint16, dest_col_dec_2_1); -// Need check overflow; CastInterType is Int64 -CAST_BENCHMARK(CastToDecimalBench, int32_to_decimal_2_1, from_col_int32, dest_col_dec_2_1); -CAST_BENCHMARK(CastToDecimalBench, uint32_to_decimal_2_1, from_col_uint32, dest_col_dec_2_1); -// Need check overflow; CastInterType is Int128 -CAST_BENCHMARK(CastToDecimalBench, int64_to_decimal_2_1, from_col_int64, dest_col_dec_2_1); -CAST_BENCHMARK(CastToDecimalBench, uint64_to_decimal_2_1, from_col_uint64, dest_col_dec_2_1); -// Need check overflow; CastInterType is Int256 -CAST_BENCHMARK(CastToDecimalBench, int64_to_decimal_31_30, from_col_int64, dest_col_dec_31_30); -CAST_BENCHMARK(CastToDecimalBench, uint64_to_decimal_31_30, from_col_uint64, dest_col_dec_31_30); - -// decimal(2, 1) -> decimal(60, 0): No check overflow; CastInterType is Int32 -CAST_BENCHMARK(CastToDecimalBench, decimal_2_1_to_decimal_60_0, from_col_dec_2_1, dest_col_dec_60_0); -// decimal(10, 3) -> decimal(15, 4): No check overflow; CastInterType is Int64 -CAST_BENCHMARK(CastToDecimalBench, decimal_10_3_to_decimal_15_4, from_col_dec_10_3, dest_col_dec_15_4); -// decimal(25, 3) -> decimal(60, 5): No check overflow; CastInterType is Int128 -CAST_BENCHMARK(CastToDecimalBench, decimal_25_3_to_decimal_60_5, from_col_dec_25_3, dest_col_dec_60_5); -// decimal(57, 0) -> decimal(60, 0): No check overflow; CastInterType is Int256 -CAST_BENCHMARK(CastToDecimalBench, decimal_57_0_to_decimal_60_0, from_col_dec_57_0, dest_col_dec_60_0); - -// decimal(2, 1) -> decimal(2, 2): No check overflow; CastInterType is Int32 -CAST_BENCHMARK(CastToDecimalBench, decimal_2_1_to_decimal_2_2, from_col_dec_2_1, dest_col_dec_2_2); -// decimal(12, 3) -> decimal(15, 4): Need check overflow; CastInterType is Int64 -CAST_BENCHMARK(CastToDecimalBench, decimal_12_3_to_decimal_15_4, from_col_dec_12_3, dest_col_dec_15_4); -// decimal(25, 3) -> decimal(25, 4): No check overflow; CastInterType is Int128 -CAST_BENCHMARK(CastToDecimalBench, decimal_25_3_to_decimal_25_4, from_col_dec_25_3, dest_col_dec_25_4); -// decimal(60, 0) -> decimal(60, 5): Need check overflow; CastInterType is Int256 -CAST_BENCHMARK(CastToDecimalBench, decimal_60_0_to_decimal_60_5, from_col_dec_60_0, dest_col_dec_60_5); - -// decimal(60, 5) -> decimal(60, 0): No check overflow; CastInterType is Int256 -CAST_BENCHMARK(CastToDecimalBench, decimal_60_5_to_decimal_60_0, from_col_dec_60_5, dest_col_dec_60_0); +// no; Int128; Int256 +CAST_BENCHMARK(CastToDecimalBench, int64_to_decimal_60_4, from_col_int64, dest_col_dec_60_4); + +// need; Int128; Int64; small value, so no overflow really hanppens +CAST_BENCHMARK(CastToDecimalBench, int64_small_to_decimal_10_0, from_col_int64_small, dest_col_dec_10_0); + +// cast(decimal as decimal) +// no; Int64; Int64 +CAST_BENCHMARK(CastToDecimalBench, decimal_3_0_to_decimal_10_0, from_col_dec_3_0, dest_col_dec_10_0); +// no; Int64; Int64 +CAST_BENCHMARK(CastToDecimalBench, decimal_3_0_to_decimal_10_5, from_col_dec_3_0, dest_col_dec_10_5); +// need; Int64; Int64 +CAST_BENCHMARK(CastToDecimalBench, decimal_3_0_to_decimal_10_8, from_col_dec_3_0, dest_col_dec_10_8); + +// no; Int64; Int128 +CAST_BENCHMARK(CastToDecimalBench, decimal_3_0_to_decimal_30_0, from_col_dec_3_0, dest_col_dec_30_0); +// need; Int64; Int128 + +// no; Int64; Int256 +CAST_BENCHMARK(CastToDecimalBench, decimal_3_0_to_decimal_60_0, from_col_dec_3_0, dest_col_dec_60_0); +// need; Int64; Int256 + +// no; Int128; Int256 +CAST_BENCHMARK(CastToDecimalBench, decimal_35_0_to_decimal_60_0, from_col_dec_35_0, dest_col_dec_60_0); +// need; Int128; Int256 +CAST_BENCHMARK(CastToDecimalBench, decimal_35_0_to_decimal_60_30, from_col_dec_35_0, dest_col_dec_60_30); + +// need; Int256; Int128 +CAST_BENCHMARK(CastToDecimalBench, decimal_60_0_to_decimal_30_0, from_col_dec_60_0, dest_col_dec_30_0); + +// no; Int128; Int128 +CAST_BENCHMARK(CastToDecimalBench, decimal_35_0_to_decimal_40_0, from_col_dec_35_0, dest_col_dec_40_0); +// no; Int256; Int256 +CAST_BENCHMARK(CastToDecimalBench, decimal_40_0_to_decimal_60_0, from_col_dec_40_0, dest_col_dec_60_0); + +// no; Int128; Int128. Int128 div +CAST_BENCHMARK(CastToDecimalBench, decimal_25_3_to_decimal_30_0, from_col_dec_25_3, dest_col_dec_30_0); +// no; Int128; Int256. Int256 div +CAST_BENCHMARK(CastToDecimalBench, decimal_40_5_to_decimal_60_0, from_col_dec_40_5, dest_col_dec_60_0); + +// cast(date as decimal) +CAST_BENCHMARK(CastToDecimalBench, date_to_decimal_10_0, from_col_date, dest_col_dec_10_0); +CAST_BENCHMARK(CastToDecimalBench, datetime_fsp5_to_decimal_10_0, from_col_datetime_fsp5, dest_col_dec_10_0); +CAST_BENCHMARK(CastToDecimalBench, datetime_fsp5_to_decimal_10_8, from_col_datetime_fsp5, dest_col_dec_10_8); +CAST_BENCHMARK(CastToDecimalBench, datetime_fsp5_to_decimal_40_0, from_col_datetime_fsp5, dest_col_dec_40_0); +CAST_BENCHMARK(CastToDecimalBench, datetime_fsp5_to_decimal_40_5, from_col_datetime_fsp5, dest_col_dec_40_5); + +#define STATIC_CAST_BENCHMARK(CLASS_NAME, FROM_TYPE, TO_TYPE) \ + BENCHMARK_DEFINE_F(CastToDecimalBench, Int##FROM_TYPE##_static_cast_Int##TO_TYPE) \ + (benchmark::State & state) \ + try \ + { \ + for (auto _ : state) \ + { \ + for (int i = 0; i < row_num; ++i) \ + { \ + dest_int##TO_TYPE##_vec[i] = static_cast(from_int##FROM_TYPE##_vec[i]); \ + } \ + } \ + } \ + CATCH \ + BENCHMARK_REGISTER_F(CastToDecimalBench, Int##FROM_TYPE##_static_cast_Int##TO_TYPE)->Iterations(1000); + +STATIC_CAST_BENCHMARK(CastToDecimalBench, 64, 128); +STATIC_CAST_BENCHMARK(CastToDecimalBench, 64, 256); +STATIC_CAST_BENCHMARK(CastToDecimalBench, 128, 128); +STATIC_CAST_BENCHMARK(CastToDecimalBench, 128, 256); + +#define DIV_BENCHMARK(CLASS_NAME, TYPE) \ + BENCHMARK_DEFINE_F(CastToDecimalBench, div_##TYPE) \ + (benchmark::State & state) \ + try \ + { \ + for (auto _ : state) \ + { \ + for (int i = 0; i < row_num; ++i) \ + { \ + dest_int##TYPE##_vec[i] = from_int##TYPE##_vec[i] / from_int##TYPE##_vec[0]; \ + } \ + } \ + } \ + CATCH \ + BENCHMARK_REGISTER_F(CastToDecimalBench, div_##TYPE)->Iterations(1000); + +DIV_BENCHMARK(CastToDecimalBench, 64); +DIV_BENCHMARK(CastToDecimalBench, 128); +DIV_BENCHMARK(CastToDecimalBench, 256); + +#define MUL_BENCHMARK(CLASS_NAME, TYPE) \ + BENCHMARK_DEFINE_F(CastToDecimalBench, mul_##TYPE) \ + (benchmark::State & state) \ + try \ + { \ + for (auto _ : state) \ + { \ + for (int i = 0; i < row_num; ++i) \ + { \ + dest_int##TYPE##_vec[i] = from_int##TYPE##_vec[i] * from_int##TYPE##_vec[0]; \ + } \ + } \ + } \ + CATCH \ + BENCHMARK_REGISTER_F(CastToDecimalBench, mul_##TYPE)->Iterations(1000); + +MUL_BENCHMARK(CastToDecimalBench, 64); +MUL_BENCHMARK(CastToDecimalBench, 128); +MUL_BENCHMARK(CastToDecimalBench, 256); } // namespace tests } // namespace DB