From 61a5c4f49361b5e73ec029480100afc42d8b2f5f Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Tue, 15 Jun 2021 13:35:23 +0300 Subject: [PATCH] Fix min, max aggregate functions merge --- .../AggregateFunctionAvgWeighted.h | 48 ++++++----- .../AggregateFunctionMinMaxAny.h | 8 +- .../00165_jit_aggregate_functions.reference | 86 +++++++++++++++++++ .../00165_jit_aggregate_functions.sql | 29 +++++++ 4 files changed, 144 insertions(+), 27 deletions(-) create mode 100644 tests/queries/1_stateful/00165_jit_aggregate_functions.reference create mode 100644 tests/queries/1_stateful/00165_jit_aggregate_functions.sql diff --git a/src/AggregateFunctions/AggregateFunctionAvgWeighted.h b/src/AggregateFunctions/AggregateFunctionAvgWeighted.h index d9df661ab18..71b0cb6a735 100644 --- a/src/AggregateFunctions/AggregateFunctionAvgWeighted.h +++ b/src/AggregateFunctions/AggregateFunctionAvgWeighted.h @@ -48,42 +48,44 @@ public: bool isCompilable() const override { - bool can_be_compiled = Base::isCompilable(); - can_be_compiled &= canBeNativeType(); + /// TODO: FIX + // bool can_be_compiled = Base::isCompilable(); + // can_be_compiled &= canBeNativeType(); - return can_be_compiled; + return false; } - void compileAdd(llvm::IRBuilderBase & builder, llvm::Value * aggregate_data_ptr, const DataTypes & arguments_types, const std::vector & argument_values) const override - { - llvm::IRBuilder<> & b = static_cast &>(builder); + // void compileAdd(llvm::IRBuilderBase & builder, llvm::Value * aggregate_data_ptr, const DataTypes & arguments_types, const std::vector & argument_values) const override + // { + /// TODO: FIX + // llvm::IRBuilder<> & b = static_cast &>(builder); - auto * numerator_type = toNativeType(b); + // auto * numerator_type = toNativeType(b); - auto * numerator_ptr = b.CreatePointerCast(aggregate_data_ptr, numerator_type->getPointerTo()); - auto * numerator_value = b.CreateLoad(numerator_type, numerator_ptr); + // auto * numerator_ptr = b.CreatePointerCast(aggregate_data_ptr, numerator_type->getPointerTo()); + // auto * numerator_value = b.CreateLoad(numerator_type, numerator_ptr); - const auto & argument = nativeCast(b, arguments_types[0], argument_values[0], numerator_type); - const auto & weight = nativeCast(b, arguments_types[1], argument_values[1], numerator_type); + // const auto & argument = nativeCast(b, arguments_types[0], argument_values[0], numerator_type); + // const auto & weight = nativeCast(b, arguments_types[1], argument_values[1], numerator_type); - llvm::Value * value_weight_multiplication = argument->getType()->isIntegerTy() ? b.CreateMul(argument, weight) : b.CreateFMul(argument, weight); + // llvm::Value * value_weight_multiplication = argument->getType()->isIntegerTy() ? b.CreateMul(argument, weight) : b.CreateFMul(argument, weight); - /// TODO: Fix accuracy - auto * numerator_result_value = numerator_type->isIntegerTy() ? b.CreateAdd(numerator_value, value_weight_multiplication) : b.CreateFAdd(numerator_value, value_weight_multiplication); - b.CreateStore(numerator_result_value, numerator_ptr); + // /// TODO: Fix accuracy + // auto * numerator_result_value = numerator_type->isIntegerTy() ? b.CreateAdd(numerator_value, value_weight_multiplication) : b.CreateFAdd(numerator_value, value_weight_multiplication); + // b.CreateStore(numerator_result_value, numerator_ptr); - auto * denominator_type = toNativeType(b); + // auto * denominator_type = toNativeType(b); - auto * denominator_offset_ptr = b.CreateConstGEP1_32(nullptr, aggregate_data_ptr, sizeof(Numerator)); - auto * denominator_ptr = b.CreatePointerCast(denominator_offset_ptr, denominator_type->getPointerTo()); + // auto * denominator_offset_ptr = b.CreateConstGEP1_32(nullptr, aggregate_data_ptr, sizeof(Numerator)); + // auto * denominator_ptr = b.CreatePointerCast(denominator_offset_ptr, denominator_type->getPointerTo()); - auto * weight_cast_to_denominator = nativeCast(b, arguments_types[1], argument_values[1], numerator_type); + // auto * weight_cast_to_denominator = nativeCast(b, arguments_types[1], argument_values[1], numerator_type); - auto * denominator_value = b.CreateLoad(denominator_type, denominator_ptr); - auto * denominator_value_updated = denominator_type->isIntegerTy() ? b.CreateAdd(denominator_value, weight_cast_to_denominator) : b.CreateFAdd(denominator_value, weight_cast_to_denominator); + // auto * denominator_value = b.CreateLoad(denominator_type, denominator_ptr); + // auto * denominator_value_updated = denominator_type->isIntegerTy() ? b.CreateAdd(denominator_value, weight_cast_to_denominator) : b.CreateFAdd(denominator_value, weight_cast_to_denominator); - b.CreateStore(denominator_value_updated, denominator_ptr); - } + // b.CreateStore(denominator_value_updated, denominator_ptr); + // } #endif diff --git a/src/AggregateFunctions/AggregateFunctionMinMaxAny.h b/src/AggregateFunctions/AggregateFunctionMinMaxAny.h index d9093a83211..23dad0c097c 100644 --- a/src/AggregateFunctions/AggregateFunctionMinMaxAny.h +++ b/src/AggregateFunctions/AggregateFunctionMinMaxAny.h @@ -391,16 +391,16 @@ public: if constexpr (is_less) { if (value_src->getType()->isIntegerTy()) - should_change_after_comparison = is_signed ? b.CreateICmpSLT(value_dst, value_src) : b.CreateICmpULT(value_dst, value_src); + should_change_after_comparison = is_signed ? b.CreateICmpSLT(value_src, value_dst) : b.CreateICmpULT(value_src, value_dst); else - should_change_after_comparison = b.CreateFCmpOLT(value_dst, value_src); + should_change_after_comparison = b.CreateFCmpOLT(value_src, value_dst); } else { if (value_src->getType()->isIntegerTy()) - should_change_after_comparison = is_signed ? b.CreateICmpSGT(value_dst, value_src) : b.CreateICmpUGT(value_dst, value_src); + should_change_after_comparison = is_signed ? b.CreateICmpSGT(value_src, value_dst) : b.CreateICmpUGT(value_src, value_dst); else - should_change_after_comparison = b.CreateFCmpOGT(value_dst, value_src); + should_change_after_comparison = b.CreateFCmpOGT(value_src, value_dst); } b.CreateCondBr(b.CreateAnd(has_value_src, b.CreateOr(b.CreateNot(has_value_dst), should_change_after_comparison)), if_should_change, if_should_not_change); diff --git a/tests/queries/1_stateful/00165_jit_aggregate_functions.reference b/tests/queries/1_stateful/00165_jit_aggregate_functions.reference new file mode 100644 index 00000000000..f6dea736ccd --- /dev/null +++ b/tests/queries/1_stateful/00165_jit_aggregate_functions.reference @@ -0,0 +1,86 @@ +Aggregation using JIT compilation +Simple functions +1704509 4611700827100483880 9223360787015464643 10441337359398154812 19954243669348.844 +732797 4611701940806302259 9223355550934604746 977192643464016658 2054229034942.3723 +598875 4611701407242345792 9223362250391155632 9312163881623734456 27615161624211.875 +792887 4611699550286611812 9223290551912005343 6930300520201292824 27479710385933.586 +3807842 4611710821592843606 9223326163906184987 16710274896338005145 85240848090850.69 +25703952 4611709443519524003 9223353913449113943 9946868158853570839 67568783303242.086 +716829 4611852156092872082 9223361623076951140 15381015774917924786 170693446547158.72 +59183 4611730685242027332 9223354909338698162 8078812522502896568 94622946187035.42 +33010362 4611704682869732882 9223268545373999677 2064452191838585926 26532987929602.555 +800784 4611752907938305166 9223340418389788041 18082918611792817587 233352070043266.62 +20810645 4611712185532639162 9223218900001937412 4996531385439292694 68246505203164.63 +25843850 4611690025407720929 9223346023778617822 12755881190906812868 185015319325648.16 +23447120 4611796031755620254 9223329309291309758 17231649548755339966 255019232629204.38 +14739804 4611692230555590277 9223313509005166531 2458378896777063244 38308020331864.36 +32077710 4611884228437061959 9223352444952988904 12965822147651192908 214467085941034.7 +22446879 4611846229717089436 9223124373140579096 13530160492087688838 231724477077663.4 +170282 4611833225706935900 9223371583739401906 8076893424988479310 141657635880324.8 +11482817 4611708000353743073 9223337838355779113 14841435427430843458 283531099960470.8 +63469 4611695097019173921 9223353530156141191 6296784708578574520 120762239817777.88 +29103473 4611744585914335132 9223333530281362537 5908285283932344933 123712996438970.34 +Simple functions if combinator +1704509 4611700827100483880 9223310246721229500 16398241567152875142 62618822667209.71 +732797 4611721382223060002 9223355550934604746 16281585268876620522 68472164943295.68 +598875 4611701407242345792 9223362250391155632 3577699408183553052 21300140553347.42 +792887 4611699550286611812 9223164887726235740 7088177025760385824 56461952267903.89 +3807842 4611710821592843606 9223283397553859544 5756765290752687660 58835559208469.4 +25703952 4611784761593342388 9223241341744449690 4782279928971192568 65182094768443.91 +716829 4611852156092872082 9223361623076951140 8613712481895484190 191445613359755.62 +59183 4611730685242027332 9223354909338698162 18369075291092794110 429013599530392 +33010362 4611704682869732882 9223092117352620518 9991152681891671022 257099731913529.5 +800784 4611752907938305166 9223309994342931384 5251877538869750510 135472890315726.03 +20810645 4611712185532639162 9223218900001937412 11803718472901310700 323593455407553 +25843850 4611744529689964352 9223346023778617822 127137885677350808 3700925266420.715 +23447120 4611796031755620254 9223329309291309758 1841522159325376278 54534534450526.42 +14739804 4611762063154116632 9223007205463222212 16302703534054321116 506987919332451.8 +32077710 4612033458080771112 9223352444952988904 421072759851674408 13955745719596.793 +22446879 4611846229717089436 9223124373140579096 6577134317587565298 224866980668999.47 +170282 4611833225706935900 9223371583739401906 15764226366913732386 551447384017691 +11482817 4611990575414646848 9223302669582414438 9828522700609834800 378121905921203.2 +63469 4612175339998036670 9222961628400798084 17239621485933250238 663164390134376.5 +29103473 4611744585914335132 9223035551850347954 12590190375872647672 525927999326314.7 +Aggregation without JIT compilation +Simple functions +1704509 4611700827100483880 9223360787015464643 10441337359398154812 19954243669348.844 +732797 4611701940806302259 9223355550934604746 977192643464016658 2054229034942.3723 +598875 4611701407242345792 9223362250391155632 9312163881623734456 27615161624211.875 +792887 4611699550286611812 9223290551912005343 6930300520201292824 27479710385933.586 +3807842 4611710821592843606 9223326163906184987 16710274896338005145 85240848090850.69 +25703952 4611709443519524003 9223353913449113943 9946868158853570839 67568783303242.086 +716829 4611852156092872082 9223361623076951140 15381015774917924786 170693446547158.72 +59183 4611730685242027332 9223354909338698162 8078812522502896568 94622946187035.42 +33010362 4611704682869732882 9223268545373999677 2064452191838585926 26532987929602.555 +800784 4611752907938305166 9223340418389788041 18082918611792817587 233352070043266.62 +20810645 4611712185532639162 9223218900001937412 4996531385439292694 68246505203164.63 +25843850 4611690025407720929 9223346023778617822 12755881190906812868 185015319325648.16 +23447120 4611796031755620254 9223329309291309758 17231649548755339966 255019232629204.38 +14739804 4611692230555590277 9223313509005166531 2458378896777063244 38308020331864.36 +32077710 4611884228437061959 9223352444952988904 12965822147651192908 214467085941034.7 +22446879 4611846229717089436 9223124373140579096 13530160492087688838 231724477077663.4 +170282 4611833225706935900 9223371583739401906 8076893424988479310 141657635880324.8 +11482817 4611708000353743073 9223337838355779113 14841435427430843458 283531099960470.8 +63469 4611695097019173921 9223353530156141191 6296784708578574520 120762239817777.88 +29103473 4611744585914335132 9223333530281362537 5908285283932344933 123712996438970.34 +Simple functions if combinator +1704509 4611700827100483880 9223310246721229500 16398241567152875142 62618822667209.71 +732797 4611721382223060002 9223355550934604746 16281585268876620522 68472164943295.68 +598875 4611701407242345792 9223362250391155632 3577699408183553052 21300140553347.42 +792887 4611699550286611812 9223164887726235740 7088177025760385824 56461952267903.89 +3807842 4611710821592843606 9223283397553859544 5756765290752687660 58835559208469.4 +25703952 4611784761593342388 9223241341744449690 4782279928971192568 65182094768443.91 +716829 4611852156092872082 9223361623076951140 8613712481895484190 191445613359755.62 +59183 4611730685242027332 9223354909338698162 18369075291092794110 429013599530392 +33010362 4611704682869732882 9223092117352620518 9991152681891671022 257099731913529.5 +800784 4611752907938305166 9223309994342931384 5251877538869750510 135472890315726.03 +20810645 4611712185532639162 9223218900001937412 11803718472901310700 323593455407553 +25843850 4611744529689964352 9223346023778617822 127137885677350808 3700925266420.715 +23447120 4611796031755620254 9223329309291309758 1841522159325376278 54534534450526.42 +14739804 4611762063154116632 9223007205463222212 16302703534054321116 506987919332451.8 +32077710 4612033458080771112 9223352444952988904 421072759851674408 13955745719596.793 +22446879 4611846229717089436 9223124373140579096 6577134317587565298 224866980668999.47 +170282 4611833225706935900 9223371583739401906 15764226366913732386 551447384017691 +11482817 4611990575414646848 9223302669582414438 9828522700609834800 378121905921203.2 +63469 4612175339998036670 9222961628400798084 17239621485933250238 663164390134376.5 +29103473 4611744585914335132 9223035551850347954 12590190375872647672 525927999326314.7 diff --git a/tests/queries/1_stateful/00165_jit_aggregate_functions.sql b/tests/queries/1_stateful/00165_jit_aggregate_functions.sql new file mode 100644 index 00000000000..a0523186850 --- /dev/null +++ b/tests/queries/1_stateful/00165_jit_aggregate_functions.sql @@ -0,0 +1,29 @@ +SET compile_aggregate_expressions = 1; +SET min_count_to_compile_aggregate_expression = 0; + +SELECT 'Aggregation using JIT compilation'; +SELECT 'Simple functions'; + +SELECT CounterID, min(WatchID), max(WatchID), sum(WatchID), avg(WatchID) FROM test.hits +GROUP BY CounterID ORDER BY count() DESC LIMIT 20; + +SELECT 'Simple functions if combinator'; + +WITH (WatchID % 2 == 0) AS predicate +SELECT CounterID, minIf(WatchID,predicate), maxIf(WatchID, predicate), sumIf(WatchID, predicate), avgIf(WatchID, predicate) FROM test.hits +GROUP BY CounterID ORDER BY count() DESC LIMIT 20; + +SET compile_aggregate_expressions = 0; + +SELECT 'Aggregation without JIT compilation'; + +SELECT 'Simple functions'; + +SELECT CounterID, min(WatchID), max(WatchID), sum(WatchID), avg(WatchID) FROM test.hits +GROUP BY CounterID ORDER BY count() DESC LIMIT 20; + +SELECT 'Simple functions if combinator'; + +WITH (WatchID % 2 == 0) AS predicate +SELECT CounterID, minIf(WatchID,predicate), maxIf(WatchID, predicate), sumIf(WatchID, predicate), avgIf(WatchID, predicate) FROM test.hits +GROUP BY CounterID ORDER BY count() DESC LIMIT 20;