Skip to content

Commit

Permalink
Fix array_max and array_min due to ClickHouse/ClickHouse#60188
Browse files Browse the repository at this point in the history
  • Loading branch information
baibaichen committed Feb 23, 2024
1 parent 7708bb3 commit e576f70
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <Common/CHUtil.h>
#include <Core/Field.h>
#include <DataTypes/IDataType.h>
#include <DataTypes/DataTypeArray.h>

namespace DB
{
Expand All @@ -41,13 +42,33 @@ class BaseFunctionParserArrayMaxAndMin : public FunctionParser
const substrait::Expression_ScalarFunction & substrait_func,
ActionsDAGPtr & actions_dag) const override
{
/*
parse array_max(arr) as
if (isNull(arr))
null
else
arrayReduce("max", assumeNotNull(arr))
*/
auto parsed_args = parseFunctionArguments(substrait_func, "", actions_dag);
if (parsed_args.size() != 1)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Function {} requires exactly one arguments", getName());

const auto * max_const_node = addColumnToActionsDAG(actions_dag, std::make_shared<DataTypeString>(), getCHFunctionName(substrait_func));
const auto * array_reduce_node = toFunctionNode(actions_dag, "arrayReduce", {max_const_node, parsed_args[0]});
return convertNodeTypeIfNeeded(substrait_func, array_reduce_node, actions_dag);
const auto * arr_arg = parsed_args[0];
const auto * func_const_node = addColumnToActionsDAG(actions_dag, std::make_shared<DataTypeString>(), getCHFunctionName(substrait_func));

auto is_arr_nullable = arr_arg->result_type->isNullable();
if (!is_arr_nullable)
{
const auto * array_reduce_node = toFunctionNode(actions_dag, "arrayReduce", {func_const_node, arr_arg});
return convertNodeTypeIfNeeded(substrait_func, array_reduce_node, actions_dag);
}

const auto * arr_is_null_node = toFunctionNode(actions_dag, "isNull", {arr_arg});
const auto * arr_not_null_node = toFunctionNode(actions_dag, "assumeNotNull", {arr_arg});
const auto * array_reduce_node = toFunctionNode(actions_dag, "arrayReduce", {func_const_node, arr_not_null_node});
const auto * null_const_node = addColumnToActionsDAG(actions_dag, makeNullable(array_reduce_node->result_type), Field{});
const auto * if_node = toFunctionNode(actions_dag, "if", {arr_is_null_node, null_const_node, array_reduce_node});
return convertNodeTypeIfNeeded(substrait_func, if_node, actions_dag);
}
};

Expand Down

0 comments on commit e576f70

Please sign in to comment.