diff --git a/ydb/docs/en/core/yql/reference/syntax/group_by.md b/ydb/docs/en/core/yql/reference/syntax/group_by.md index 6fb297631cbf..cfd46ee18607 100644 --- a/ydb/docs/en/core/yql/reference/syntax/group_by.md +++ b/ydb/docs/en/core/yql/reference/syntax/group_by.md @@ -1,87 +1,17 @@ -{% if select_command == "SELECT STREAM" %} - -## GROUP BY ... HOP - -Group the table by the values of the specified columns or expressions and the time window. - -If GROUP BY is present in the query, then when selecting columns (between `SELECT STREAM ... FROM`) you can **only** use the following constructs: - -1. Columns by which grouping is performed (they are included in the `GROUP BY` argument). -2. Aggregate functions (see the next section). Columns by which **no** grouping is made can only be included as arguments for an aggregate function. -3. Functions that output the start and end time of the current window (`HOP_START` and `HOP_END`) -4. Arbitrary calculations combining paragraphs 1-3. - -You can group by the result of calculating an arbitrary expression from the source columns. In this case, to access the result of this expression, we recommend you to assign a name to it using `AS`, see the second example. - -Aggregate functions automatically skip `NULL` in their arguments. - -Among the columns by which grouping is performed, make sure to use the `HOP` construct to define the time window for grouping. - -```yql -HOP(time_extractor, hop, interval, delay) -``` - -The implemented version of the time window is called the **hopping window**. This is a window that moves forward in discrete intervals (the `hop` parameter). The total duration of the window is set by the `interval` parameter. To determine the time of each input event, the `time_extractor` parameter is used. This expression depends only on the input values of the stream's columns and must have the `Timestamp` type. It indicates where exactly to get the time value from input events. - -In each stream defined by the values of all the grouping columns, the window moves forward independently of other streams. Advancement of the window is totally dependent on the latest event of the stream. Since records in streams get somewhat mixed in time, the `delay` parameter has been added so you can delay the closing of the window by a specified period. Events arriving before the current window are ignored. - -The `interval` and `delay` parameters must be multiples of the `hop` parameter. Non-multiple intervals will be rounded down. - -To set `hop`, `interval` and `delay`, use a string expression compliant with [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). This is the format that is used to construct the built-in type `Interval` [from a string](../builtins/basic.md#data-type-literals). - -Functions with omitted `HOP_START` and `HOP_END` parameters, return a value of the `Timestamp` type and correspond to the start and end of the current window. - -The **tumbling window** known in other systems is a special case of a **hopping window** when `interval` == `hop`. - -## Examples - -```yql -SELECT STREAM - key, - COUNT(*) -FROM my_stream -GROUP BY - HOP(CAST(subkey AS Timestamp), "PT10S", "PT1M", "PT30S"), - key; --- hop = 10 seconds --- interval = 1 minute --- delay = 30 seconds -``` - -```yql -SELECT STREAM - double_key, - HOP_END() as time, - COUNT(*) as count -FROM my_stream -GROUP BY - key + key AS double_key, - HOP(ts, "PT1M", "PT1M", "PT1M"); -``` - - - -## HAVING {#having} - -Filtering a `SELECT STREAM` based on the calculation results of [aggregate functions](../builtins/aggregation.md). The syntax is similar to [WHERE](select_stream.md#where). - -### Examples - -```yql -SELECT STREAM - key -FROM my_table -GROUP BY key, HOP(ts, "PT1M", "PT1M", "PT1M") -HAVING COUNT(value) > 100; -``` +{% if select_command != "SELECT STREAM" %} +## GROUP BY +Group the `SELECT` results by the values of the specified columns or expressions. `GROUP BY` is often combined with [aggregate functions](../builtins/aggregation.md) (`COUNT`, `MAX`, `MIN`, `SUM`, `AVG`) to perform calculations in each group. -{% else %} +If `GROUP BY` is present in the query, then when selecting columns (between `SELECT ... FROM`), you can use the following constructs: -## GROUP BY +1. Columns by which grouping is performed (included in the `GROUP BY` argument). +2. Aggregate functions (see the next section). Columns that **are not** used for grouping can only be included as arguments for an aggregate function. +3. Functions that return the start and end times of the current window (`HOP_START` and `HOP_END`). +4. Arbitrary calculations combining items 1–3. -Group the `SELECT` results by the values of the specified columns or expressions. `GROUP BY` is often combined with [aggregate functions](../builtins/aggregation.md) (`COUNT`, `MAX`, `MIN`, `SUM`, `AVG`) to perform calculations in each group. +You can group by the result of an arbitrary expression computed from the source columns. In this case, to access the result of this expression, we recommend assigning a name to it using `AS`. See the second [example](#examples). ### Syntax @@ -112,7 +42,7 @@ Aggregate functions ignore `NULL` in their arguments, except for `COUNT`. YQL also provides aggregation factories implemented by the functions [`AGGREGATION_FACTORY`](../builtins/basic.md#aggregationfactory) and [`AGGREGATE_BY`](../builtins/aggregation.md#aggregateby). -### Examples +### Examples {#examples} ```yql SELECT key, COUNT(*) FROM my_table @@ -295,8 +225,6 @@ GROUP BY ; ``` - - {% endif %} ## DISTINCT {#distinct} @@ -343,7 +271,72 @@ ORDER BY count DESC LIMIT 3; ``` +{% endif %} + +## GROUP BY ... HOP + +Group the table by the values of the specified columns or expressions and subsets by time (the time window). +Among the columns used for grouping, make sure to use the `HOP` construct to define the time window for grouping. + +```yql +HOP(time_extractor, hop, interval, delay) +``` + +The implemented version of the time window is called the **hopping window**. This is a window that moves forward in discrete intervals (the `hop` parameter). The total duration of the window is set by the `interval` parameter. To determine the time of each input event, the `time_extractor` parameter is used. This expression depends only on the input values of the columns and must have the `Timestamp` type. It specifies where to extract the time value from data. + +{% if select_command != "SELECT STREAM" %} +The following happens in this case: + +1. The input table is partitioned by the grouping keys specified in `GROUP BY`, ignoring HOP. If `GROUP BY` includes nothing more than HOP, then the input table gets into one partition. +2. Each partition is sorted in ascending order of the expression `time_extractor`. +3. Each partition is split into subsets of rows (possibly intersecting), on which aggregate functions are calculated. +{% endif %} + +In each partition defined by the values of all the grouping columns, the window moves forward independently of other streams. The advancement of the window depends entirely on the latest event in the partition. + +{% if select_command == "SELECT STREAM" %} +Since records in streams can be somewhat out of order, the `delay` parameter allows you to delay the closing of the window by a specified period. Events arriving before the current window are ignored. +{% endif %} + +The `interval` and `delay` parameters must be multiples of the `hop` parameter. Non-multiple intervals are prohibited in the current implementation. +The `interval` and `hop` parameters must be positive. + +{% if select_command != "SELECT STREAM" %} +The `delay` parameter is ignored in the current implementation because the data in one partition is already sorted. +{% endif %} + +To set `hop`, `interval`, and `delay`, use a string expression compliant with [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). This format is used to construct the built-in `Interval` type [from a string](../builtins/basic.md#data-type-literals). + +When selecting columns (between `SELECT ... FROM`) you can use the `HOP_START` and `HOP_END` functions (without parameters), which return a value of `Timestamp` type, corresponding to the start and end of the current window. + +The **tumbling window**, known in other systems, is a special case of a **hopping window** where `interval` == `hop`. + +## Examples + +```yql +SELECT + key, + COUNT(*) +FROM my_table +GROUP BY + HOP(CAST(subkey AS Timestamp), "PT10S", "PT1M", "PT30S"), + key; +-- hop = 10 seconds +-- interval = 1 minute +-- delay = 30 seconds +``` + +```yql +SELECT + double_key, + HOP_END() as time, + COUNT(*) as count +FROM my_table +GROUP BY + key + key AS double_key, + HOP(ts, "PT1M", "PT1M", "PT1M"); +``` ## HAVING {#having} @@ -358,9 +351,3 @@ FROM my_table GROUP BY key HAVING COUNT(value) > 100; ``` - - - -{% endif %} - - diff --git a/ydb/docs/ru/core/yql/reference/syntax/group_by.md b/ydb/docs/ru/core/yql/reference/syntax/group_by.md index d69010762f8d..3ea0e69e621d 100644 --- a/ydb/docs/ru/core/yql/reference/syntax/group_by.md +++ b/ydb/docs/ru/core/yql/reference/syntax/group_by.md @@ -1,82 +1,18 @@ -{% if select_command == "SELECT STREAM" %} +{% if select_command != "SELECT STREAM" %} - ## GROUP BY ... HOP +## GROUP BY -Сгруппировать таблицу по значениям указанных столбцов или выражений, а также окну времени. +Группирует результаты `SELECT` по значениям указанных столбцов или выражений. Вместе с `GROUP BY` часто применяются [агрегатные функции](../builtins/aggregation.md) (`COUNT`, `MAX`, `MIN`, `SUM`, `AVG`) для выполнения вычислений в каждой группе. -Если GROUP BY присутствует в запросе, то при выборке столбцов (между `SELECT STREAM ... FROM`) допустимы **только** следующие конструкции: +Если `GROUP BY` присутствует в запросе, то при выборке столбцов (между `SELECT ... FROM`) допустимы следующие конструкции: 1. Столбцы, по которым производится группировка (присутствующие в аргументе `GROUP BY`). -2. Агрегатные функции (см. следующий раздел). Столбцы, по которым **не** идет группировка, можно включать только в качестве аргументов агрегатной функции. -3. Функции, выдающие начальное и конечное время текущего окна (`HOP_START` и `HOP_END`) +2. Агрегатные функции (см. следующий раздел). Столбцы, по которым **не** идёт группировка, можно включать только в качестве аргументов агрегатной функции. +3. Функции, выдающие начальное и конечное время текущего окна (`HOP_START` и `HOP_END`) (для `GROUP BY HOP`). 4. Произвольные вычисления, комбинирующие пункты 1-3. -Имеется возможность выполнять группировку по результату вычисления произвольного выражения от исходных столбцов. В этом случае для получения доступа к результату этого выражения рекомендуется присваивать ему имя с помощью `AS`, см. второй пример. - -Агрегатные функции автоматически пропускают `NULL` в своих аргументах. - -Среди столбцов, по которым производится группировка, должна присутствовать конструкция `HOP`, определяющая характеристики окна времени для группировки. -```yql -HOP(time_extractor, hop, interval, delay) -``` -Реализованный вариант окна времени называется **hopping window**. Это окно, продвигающееся вперёд дискретными интервалами (параметр `hop`). Общая длительность окна задаётся параметром `interval`. Для определения времени каждого входного события используется параметр `time_extractor`. Это выражение, зависящее только от входных значений столбцов стрима, должно иметь тип `Timestamp`. Оно указывает, откуда именно во входных событиях доставать значение времени. - -В каждом потоке, определяемом значениями всех столбцов группировки, окно продвигается независимо от других потоков. Продвижение окна полностью зависит от самого позднего события потока. Поскольку записи в потоках слегка перемешиваются во времени, добавлен параметр `delay`, позволяющий отложить закрытие окна на указанную величину. События, приходящие до текущего окна, игнорируются. - -Параметры `interval` и `delay` следует задавать кратными параметру `hop`. Некратные интервалы будут округлены в меньшую сторону. - -Для задания `hop`, `interval` и `delay` используется строковое выражение, соответствующее стандарту [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). Это формат, который используется для конструирования встроенного типа `Interval` [из строки](../builtins/basic.md#data-type-literals). - -Функции без параметров `HOP_START` и `HOP_END` возвращают значение типа `Timestamp` и соответствуют началу и концу текущего окна. +Имеется возможность выполнять группировку по результату вычисления произвольного выражения от исходных столбцов. В этом случае для получения доступа к результату этого выражения рекомендуется присваивать ему имя с помощью `AS`, см. второй [пример](#examples). -Известное в других системах **tumbling window** является частным случаем **hopping window**, когда `interval` == `hop`. - -### Примеры - -```yql -SELECT STREAM - key, - COUNT(*) -FROM my_stream -GROUP BY - HOP(CAST(subkey AS Timestamp), "PT10S", "PT1M", "PT30S"), - key; --- hop = 10 секунд --- interval = 1 минута --- delay = 30 секунд -``` - -```yql -SELECT STREAM - double_key, - HOP_END() as time, - COUNT(*) as count -FROM my_stream -GROUP BY - key + key AS double_key, - HOP(ts, "PT1М", "PT1M", "PT1M"); -``` - - - ## HAVING {#having} - -Фильтрация выборки `SELECT STREAM` по результатам вычисления [агрегатных функций](../builtins/aggregation.md). Синтаксис аналогичен [WHERE](select_stream.md#where). - -### Примеры - -```yql -SELECT STREAM - key -FROM my_table -GROUP BY key, HOP(ts, "PT1M", "PT1M", "PT1M") -HAVING COUNT(value) > 100; -``` - -{% else %} - -## GROUP BY - -Группирует результаты `SELECT` по значениям указанных столбцов или выражений. Вместе с `GROUP BY` часто применяются [агрегатные функции](../builtins/aggregation.md) (`COUNT`, `MAX`, `MIN`, `SUM`, `AVG`) для выполнения вычислений в каждой группе. ### Синтаксис @@ -108,7 +44,7 @@ GROUP BY Также в YQL доступен механизм фабрик агрегатных функций, реализованный с помощью функций [`AGGREGATION_FACTORY`](../builtins/basic.md#aggregationfactory) и [`AGGREGATE_BY`](../builtins/aggregation.md#aggregateby). -### Примеры +### Примеры {#examples} ```yql SELECT key, COUNT(*) FROM my_table @@ -331,6 +267,72 @@ ORDER BY count DESC LIMIT 3; ``` +{% endif %} + + ## GROUP BY ... HOP + +Сгруппировать таблицу по значениям указанных столбцов или выражений, а также подмножества по времени (окно времени). + +Среди столбцов, по которым производится группировка, должна присутствовать конструкция `HOP`, определяющая характеристики окна времени для группировки. + +```yql +HOP(time_extractor, hop, interval, delay) +``` + +Реализованный вариант окна времени называется **hopping window**. Это окно, продвигающееся вперёд дискретными интервалами (параметр `hop`). Общая длительность окна задаётся параметром `interval`. Для определения времени каждого входного события используется параметр `time_extractor`. Это выражение, зависящее только от входных значений столбцов, должно иметь тип `Timestamp`. Оно указывает, откуда именно в данных доставать значение времени. + +{% if select_command != "SELECT STREAM" %} +При этом происходит следующее: + +1. Входная таблица партиционируется по ключам группировки, указанным в `GROUP BY`, без учета HOP. Если кроме HOP в `GROUP BY` ничего нет, то входная таблица попадает в одну партицию. +2. Каждая партиция сортируется по возрастанию значения выражения `time_extractor`. +3. Каждая партиция делится на подмножества (возможно пересекащиеся), на которых вычисляются агрегатные функции. +{% endif %} + +В каждом потоке (партиции), определяемом значениями всех столбцов группировки, окно продвигается независимо от других потоков. Продвижение окна полностью зависит от самого позднего события в партиции. + +{% if select_command == "SELECT STREAM" %} +Поскольку записи в потоках слегка перемешиваются во времени, добавлен параметр `delay`, позволяющий отложить закрытие окна на указанную величину. События, приходящие до текущего окна, игнорируются. +{% endif %} + +Параметры `interval` и `delay` следует задавать кратными параметру `hop`. Некратные интервалы в текущей реализации запрещены. +Параметры `interval` и `hop` следует задавать положительными. + +{% if select_command != "SELECT STREAM" %} +Параметр `delay` в текущей реализации игнорируется т.к. данные в одной партиции уже отсортированы. +{% endif %} + +Для задания `hop`, `interval` и `delay` используется строковое выражение, соответствующее стандарту [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). Это формат, который используется для конструирования встроенного типа `Interval` [из строки](../builtins/basic.md#data-type-literals). + +При выборке столбцов (между `SELECT ... FROM`) можно использовать функции `HOP_START` и `HOP_END` (без параметров), которые возвращают значение типа `Timestamp` и соответствуют началу и концу текущего окна. + +Известное в других системах **tumbling window** является частным случаем **hopping window**, когда `interval` == `hop`. + +### Примеры + +```yql +SELECT + key, + COUNT(*) +FROM my_table +GROUP BY + HOP(CAST(subkey AS Timestamp), "PT10S", "PT1M", "PT30S"), + key; +-- hop = 10 секунд +-- interval = 1 минута +-- delay = 30 секунд +``` + +```yql +SELECT + double_key, + HOP_END() as time, + COUNT(*) as count +FROM my_table +GROUP BY + key + key AS double_key, + HOP(ts, "PT1М", "PT1M", "PT1M"); +``` ## HAVING {#having} @@ -345,8 +347,3 @@ FROM my_table GROUP BY key HAVING COUNT(value) > 100; ``` - - -{% endif %} - -