#pragma once #include "FunctionArrayMapped.h" #include namespace DB { namespace ErrorCodes { extern const int ILLEGAL_TYPE_OF_ARGUMENT; extern const int LOGICAL_ERROR; } /** Sort arrays, by values of its elements, or by values of corresponding elements of calculated expression (known as "schwartzsort"). */ template struct ArraySortImpl { static constexpr auto num_fixed_params = is_partial; static bool needBoolean() { return false; } static bool needExpression() { return false; } static bool needOneArray() { return false; } static DataTypePtr getReturnType(const DataTypePtr & /*expression_return*/, const DataTypePtr & array_element) { return std::make_shared(array_element); } static void checkArguments( const String & name, const ColumnWithTypeAndName * fixed_arguments) requires(num_fixed_params) { if (!fixed_arguments) throw Exception( ErrorCodes::LOGICAL_ERROR, "Expected fixed arguments to get the limit for partial array sort"); WhichDataType which(fixed_arguments[0].type.get()); if (!which.isUInt() && !which.isInt()) throw Exception( ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of limit argument of function {} (must be UInt or Int)", fixed_arguments[0].type->getName(), name); } static ColumnPtr execute( const ColumnArray & array, ColumnPtr mapped, const ColumnWithTypeAndName * fixed_arguments [[maybe_unused]] = nullptr); }; struct NameArraySort { static constexpr auto name = "arraySort"; }; struct NameArrayReverseSort { static constexpr auto name = "arrayReverseSort"; }; struct NameArrayPartialSort { static constexpr auto name = "arrayPartialSort"; }; struct NameArrayPartialReverseSort { static constexpr auto name = "arrayPartialReverseSort"; }; using FunctionArraySort = FunctionArrayMapped, NameArraySort>; using FunctionArrayReverseSort = FunctionArrayMapped, NameArrayReverseSort>; using FunctionArrayPartialSort = FunctionArrayMapped, NameArrayPartialSort>; using FunctionArrayPartialReverseSort = FunctionArrayMapped, NameArrayPartialReverseSort>; }