diff --git a/src/Functions/coalesce.cpp b/src/Functions/coalesce.cpp
index 96aa110a489..b2b234df515 100644
--- a/src/Functions/coalesce.cpp
+++ b/src/Functions/coalesce.cpp
@@ -112,7 +112,6 @@ public:
auto is_not_null = FunctionFactory::instance().get("isNotNull", context);
auto assume_not_null = FunctionFactory::instance().get("assumeNotNull", context);
- auto multi_if = FunctionFactory::instance().get("multiIf", context);
ColumnsWithTypeAndName multi_if_args;
ColumnsWithTypeAndName tmp_args(1);
@@ -144,7 +143,16 @@ public:
if (multi_if_args.size() == 1)
return multi_if_args.front().column;
- ColumnPtr res = multi_if->build(multi_if_args)->execute(multi_if_args, result_type, input_rows_count);
+ /// If there was only two arguments (3 arguments passed to multiIf)
+ /// use function "if" instead, because it's implemented more efficient.
+ /// TODO: make "multiIf" the same efficient.
+ FunctionOverloadResolverPtr if_function;
+ if (multi_if_args.size() == 3)
+ if_function = FunctionFactory::instance().get("if", context);
+ else
+ if_function = FunctionFactory::instance().get("multiIf", context);
+
+ ColumnPtr res = if_function->build(multi_if_args)->execute(multi_if_args, result_type, input_rows_count);
/// if last argument is not nullable, result should be also not nullable
if (!multi_if_args.back().column->isNullable() && res->isNullable())
diff --git a/tests/performance/conditional.xml b/tests/performance/conditional.xml
index 91b6cb95ff2..e1d15852767 100644
--- a/tests/performance/conditional.xml
+++ b/tests/performance/conditional.xml
@@ -8,4 +8,7 @@
SELECT count() FROM zeros(10000000) WHERE NOT ignore(multiIf(rand() % 2, toDateTime(rand()), toDate(rand())))
SELECT count() FROM zeros(10000000) WHERE NOT ignore(if(rand() % 2, [toDateTime(rand())], [toDate(rand())]))
SELECT count() FROM zeros(10000000) WHERE NOT ignore(multiIf(rand() % 2, [toDateTime(rand())], [toDate(rand())]))
+
+ SELECT count() FROM numbers(50000000) WHERE NOT ignore(COALESCE(toNullable(number), 0))
+ SELECT count() FROM numbers(10000000) WHERE NOT ignore(multiIf(number % 7 = 0, 1, number % 3 = 0, 2, number % 5 = 0, 3, number % 11 = 0, 4, 5))