2020-04-16 22:28:08 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2020-04-17 03:13:21 +00:00
|
|
|
#include <DataTypes/NumberTraits.h>
|
2020-04-16 22:28:08 +00:00
|
|
|
#include <Interpreters/castColumn.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-04-16 22:28:08 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <ext/map.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum class LeastGreatest
|
|
|
|
{
|
|
|
|
Least,
|
|
|
|
Greatest
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <LeastGreatest kind>
|
|
|
|
class FunctionLeastGreatestGeneric : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = kind == LeastGreatest::Least ? "least" : "greatest";
|
2021-05-28 16:44:59 +00:00
|
|
|
static FunctionPtr create(ContextConstPtr) { return std::make_shared<FunctionLeastGreatestGeneric<kind>>(); }
|
2020-04-16 22:28:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & types) const override
|
|
|
|
{
|
|
|
|
if (types.empty())
|
|
|
|
throw Exception("Function " + getName() + " cannot be called without arguments", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
return getLeastSupertype(types);
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2020-04-16 22:28:08 +00:00
|
|
|
{
|
|
|
|
size_t num_arguments = arguments.size();
|
|
|
|
if (1 == num_arguments)
|
2020-10-19 15:27:41 +00:00
|
|
|
return arguments[0].column;
|
2020-04-16 22:28:08 +00:00
|
|
|
|
|
|
|
Columns converted_columns(num_arguments);
|
|
|
|
for (size_t arg = 0; arg < num_arguments; ++arg)
|
2020-10-19 15:27:41 +00:00
|
|
|
converted_columns[arg] = castColumn(arguments[arg], result_type)->convertToFullColumnIfConst();
|
2020-04-16 22:28:08 +00:00
|
|
|
|
|
|
|
auto result_column = result_type->createColumn();
|
|
|
|
result_column->reserve(input_rows_count);
|
|
|
|
|
|
|
|
for (size_t row_num = 0; row_num < input_rows_count; ++row_num)
|
|
|
|
{
|
|
|
|
size_t best_arg = 0;
|
|
|
|
for (size_t arg = 1; arg < num_arguments; ++arg)
|
|
|
|
{
|
|
|
|
auto cmp_result = converted_columns[arg]->compareAt(row_num, row_num, *converted_columns[best_arg], 1);
|
|
|
|
|
|
|
|
if constexpr (kind == LeastGreatest::Least)
|
|
|
|
{
|
|
|
|
if (cmp_result < 0)
|
|
|
|
best_arg = arg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cmp_result > 0)
|
|
|
|
best_arg = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result_column->insertFrom(*converted_columns[best_arg], row_num);
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
return result_column;
|
2020-04-16 22:28:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <LeastGreatest kind, typename SpecializedFunction>
|
2021-05-15 17:33:15 +00:00
|
|
|
class LeastGreatestOverloadResolver : public IFunctionOverloadResolver
|
2020-04-16 22:28:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = kind == LeastGreatest::Least ? "least" : "greatest";
|
|
|
|
|
2021-05-28 16:44:59 +00:00
|
|
|
static FunctionOverloadResolverPtr create(ContextConstPtr context)
|
2020-04-16 22:28:08 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<LeastGreatestOverloadResolver<kind, SpecializedFunction>>(context);
|
|
|
|
}
|
|
|
|
|
2021-05-28 16:44:59 +00:00
|
|
|
explicit LeastGreatestOverloadResolver(ContextConstPtr context_) : context(context_) {}
|
2020-04-16 22:28:08 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & return_type) const override
|
2020-04-16 22:28:08 +00:00
|
|
|
{
|
|
|
|
DataTypes argument_types;
|
|
|
|
|
|
|
|
/// More efficient specialization for two numeric arguments.
|
|
|
|
if (arguments.size() == 2 && isNumber(arguments[0].type) && isNumber(arguments[1].type))
|
2021-05-15 17:33:15 +00:00
|
|
|
return std::make_unique<FunctionToFunctionBaseAdaptor>(SpecializedFunction::create(context), argument_types, return_type);
|
2020-04-16 22:28:08 +00:00
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
return std::make_unique<FunctionToFunctionBaseAdaptor>(
|
2020-04-16 22:28:08 +00:00
|
|
|
FunctionLeastGreatestGeneric<kind>::create(context), argument_types, return_type);
|
|
|
|
}
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & types) const override
|
2020-04-16 22:28:08 +00:00
|
|
|
{
|
|
|
|
if (types.empty())
|
|
|
|
throw Exception("Function " + getName() + " cannot be called without arguments", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2020-04-17 03:13:21 +00:00
|
|
|
if (types.size() == 2 && isNumber(types[0]) && isNumber(types[1]))
|
|
|
|
return SpecializedFunction::create(context)->getReturnTypeImpl(types);
|
|
|
|
|
2020-04-16 22:28:08 +00:00
|
|
|
return getLeastSupertype(types);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-05-28 16:44:59 +00:00
|
|
|
ContextConstPtr context;
|
2020-04-16 22:28:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|