From 3c61afb1ca79982e131938991fc7f23e6c6edc2c Mon Sep 17 00:00:00 2001 From: avoiderboi Date: Fri, 24 Feb 2023 12:31:05 +0100 Subject: [PATCH] Add docs and proper aliasing to `WIDTH_BUCKET` --- .../sql-reference/functions/math-functions.md | 18 ++++++--- src/Functions/widthBucket.cpp | 37 ++++++++++++++++++- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/docs/en/sql-reference/functions/math-functions.md b/docs/en/sql-reference/functions/math-functions.md index e197e245235..10bc73c4a72 100644 --- a/docs/en/sql-reference/functions/math-functions.md +++ b/docs/en/sql-reference/functions/math-functions.md @@ -580,24 +580,32 @@ Result: └───────────────┘ ``` -## width_bucket(operand, low, high, integer) +## width_bucket(operand, low, high, count) Returns the number of the bucket in which `operand` falls in a histogram having `count` equal-width buckets spanning the range `low` to `high`. Returns `0` if `operand < low`, and returns `count+1` if `operand >= high`. `operand`, `low`, `high` can be any native number type. `count` can only be unsigned native integer and its value cannot be zero. +**Syntax** + +```sql +widthBucket(operand, low, high, count) +``` + +There is also a case insensitive alias called `WIDTH_BUCKET` to provide compatibility with other databases. + **Example** Query: ``` sql -SELECT WIDTH_BUCKET(10.15, -8.6, 23, 18); +SELECT widthBucket(10.15, -8.6, 23, 18); ``` Result: ``` text -┌─width_bucket(10.15, -8.6, 23, 18)─┐ -│ 11 │ -└───────────────────────────────────┘ +┌─widthBucket(10.15, -8.6, 23, 18)─┐ +│ 11 │ +└──────────────────────────────────┘ ``` \ No newline at end of file diff --git a/src/Functions/widthBucket.cpp b/src/Functions/widthBucket.cpp index cb25ed41bc4..1c3443e8e9a 100644 --- a/src/Functions/widthBucket.cpp +++ b/src/Functions/widthBucket.cpp @@ -132,7 +132,7 @@ class FunctionWidthBucket : public IFunction } public: - static inline const char * name = "width_bucket"; + static inline const char * name = "widthBucket"; explicit FunctionWidthBucket() = default; @@ -207,7 +207,40 @@ public: REGISTER_FUNCTION(WidthBucket) { - factory.registerFunction({}, FunctionFactory::CaseInsensitive); + factory.registerFunction({ + R"( +Returns the number of the bucket in which `operand` falls in a histogram having `count` equal-width buckets spanning the range `low` to `high`. Returns `0` if `operand < low`, and returns `count+1` if `operand >= high`. + +`operand`, `low`, `high` can be any native number type. `count` can only be unsigned native integer and its value cannot be zero. + +**Syntax** + +```sql +widthBucket(operand, low, high, count) +``` + +There is also a case insensitive alias called `WIDTH_BUCKET` to provide compatibility with other databases. + +**Example** + +Query: +[example:simple] + +Result: + +``` text +┌─widthBucket(10.15, -8.6, 23, 18)─┐ +│ 11 │ +└──────────────────────────────────┘ +``` +)", + Documentation::Examples{ + {"simple", "SELECT widthBucket(10.15, -8.6, 23, 18)"}, + }, + Documentation::Categories{"Mathematical"}, + }); + + factory.registerAlias("width_bucket", "widthBucket", FunctionFactory::CaseInsensitive); } }