diff --git a/docs/en/sql-reference/aggregate-functions/reference/grouparraysorted.md b/docs/en/sql-reference/aggregate-functions/reference/grouparraysorted.md new file mode 100644 index 00000000000..0237885bcb6 --- /dev/null +++ b/docs/en/sql-reference/aggregate-functions/reference/grouparraysorted.md @@ -0,0 +1,48 @@ +--- +toc_priority: 108 +--- + +# groupArraySorted {#groupArraySorted} + +Returns an array with the first N items in ascending order. + +``` sql +groupArraySorted(N)(column) +``` + +**Arguments** + +- `N` – The number of elements to return. + +If the parameter is omitted, default value 10 is used. + +**Arguments** + +- `column` – The value. +- `expr` — Optional. The field or expresion to sort by. If not set values are sorted by themselves. + +**Example** + +Gets the first 10 numbers: + +``` sql +SELECT groupArraySorted(10)(number) FROM numbers(100) +``` + +``` text +┌─groupArraySorted(10)(number)─┐ +│ [0,1,2,3,4,5,6,7,8,9] │ +└──────────────────────────────┘ +``` + +Or the last 10: + +``` sql +SELECT groupArraySorted(10)(number, -number) FROM numbers(100) +``` + +``` text +┌─groupArraySorted(10)(number, negate(number))─┐ +│ [99,98,97,96,95,94,93,92,91,90] │ +└──────────────────────────────────────────────┘ +``` \ No newline at end of file diff --git a/docs/en/sql-reference/aggregate-functions/reference/index.md b/docs/en/sql-reference/aggregate-functions/reference/index.md index 59befed8785..2a8a2843510 100644 --- a/docs/en/sql-reference/aggregate-functions/reference/index.md +++ b/docs/en/sql-reference/aggregate-functions/reference/index.md @@ -35,6 +35,7 @@ ClickHouse-specific aggregate functions: - [groupArrayInsertAt](../../../sql-reference/aggregate-functions/reference/grouparrayinsertat.md) - [groupArrayMovingAvg](../../../sql-reference/aggregate-functions/reference/grouparraymovingavg.md) - [groupArrayMovingSum](../../../sql-reference/aggregate-functions/reference/grouparraymovingsum.md) +- [groupArraySorted](../../../sql-reference/aggregate-functions/reference/grouparraysorted.md) - [groupBitAnd](../../../sql-reference/aggregate-functions/reference/groupbitand.md) - [groupBitOr](../../../sql-reference/aggregate-functions/reference/groupbitor.md) - [groupBitXor](../../../sql-reference/aggregate-functions/reference/groupbitxor.md) diff --git a/docs/ja/sql-reference/aggregate-functions/reference/grouparraysorted.md b/docs/ja/sql-reference/aggregate-functions/reference/grouparraysorted.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/AggregateFunctions/AggregateFunctionGroupArraySorted.cpp b/src/AggregateFunctions/AggregateFunctionGroupArraySorted.cpp new file mode 100644 index 00000000000..50d5f075322 --- /dev/null +++ b/src/AggregateFunctions/AggregateFunctionGroupArraySorted.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +static inline constexpr UInt64 GROUP_SORTED_ARRAY_MAX_SIZE = 0xFFFFFF; +static inline constexpr UInt64 GROUP_SORTED_ARRAY_DEFAULT_THRESHOLD = 10; + + +namespace DB +{ +struct Settings; + +namespace ErrorCodes +{ + extern const int ARGUMENT_OUT_OF_BOUND; + extern const int ILLEGAL_TYPE_OF_ARGUMENT; + extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; +} + + +namespace +{ + template + class AggregateFunctionGroupArraySortedNumeric : public AggregateFunctionGroupArraySorted + { + using AggregateFunctionGroupArraySorted::AggregateFunctionGroupArraySorted; + }; + + template + class AggregateFunctionGroupArraySortedFieldType + : public AggregateFunctionGroupArraySorted + { + using AggregateFunctionGroupArraySorted:: + AggregateFunctionGroupArraySorted; + DataTypePtr getReturnType() const override { return std::make_shared(std::make_shared()); } + }; + + template