From 699c625f0576f674584f830d7eba5acb3078ec1a Mon Sep 17 00:00:00 2001 From: WanYixian Date: Wed, 6 Nov 2024 16:38:35 +0800 Subject: [PATCH 1/3] Update window-functions.mdx --- sql/functions/window-functions.mdx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sql/functions/window-functions.mdx b/sql/functions/window-functions.mdx index 8548f578..fa32e073 100644 --- a/sql/functions/window-functions.mdx +++ b/sql/functions/window-functions.mdx @@ -86,3 +86,7 @@ last_value ( value anyelement ) → anyelement All aggregate functions, including `sum()`, `min()`, `max()`, `avg()` and `count()` etc., can be used as window functions. For the complete list of aggregate functions and their usage, see [Aggregate functions](/docs/current/sql-function-aggregate/). + +## UDAF window functions + +RisingWave supports using User-Defined Aggregate Functions (UDAFs) as window functions. To use a UDAF as a window function, you need to define the UDAF using the CREATE AGGREGATE statement. After defining the UDAF, you can call it within a query using the OVER clause, just like any built-in aggregate function. \ No newline at end of file From 8fde9310ab5159dd33022c1aa342632d9bf7b60e Mon Sep 17 00:00:00 2001 From: WanYixian Date: Wed, 6 Nov 2024 16:58:26 +0800 Subject: [PATCH 2/3] Update window-functions.mdx --- sql/functions/window-functions.mdx | 32 +++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/sql/functions/window-functions.mdx b/sql/functions/window-functions.mdx index fa32e073..3c86a044 100644 --- a/sql/functions/window-functions.mdx +++ b/sql/functions/window-functions.mdx @@ -89,4 +89,34 @@ For the complete list of aggregate functions and their usage, see [Aggregate fun ## UDAF window functions -RisingWave supports using User-Defined Aggregate Functions (UDAFs) as window functions. To use a UDAF as a window function, you need to define the UDAF using the CREATE AGGREGATE statement. After defining the UDAF, you can call it within a query using the OVER clause, just like any built-in aggregate function. \ No newline at end of file +To use the User-Defined Aggregate Functions (UDAFs) as window functions, you can define the UDAF using the [CREATE AGGREGATE](/docs/current/sql-create-aggregate/) statement, then call it within a query using the OVER clause, just like any built-in aggregate function. See the example below: + +```sql +CREATE AGGREGATE sum00(value INT) +RETURNS INT +LANGUAGE python AS $$ +def create_state(): + return 0 +def accumulate(state, value): + return state + value +def retract(state, value): + return state - value +def finish(state): + return state +$$; + +SELECT t.value, sum00(weight) OVER (PARTITION BY value) +FROM (VALUES (1, 1), (NULL, 2), (3, 3)) AS t(value, weight); +``` + +The output: + +```sql + value | weighted_avg +-------+-------------- + 1 | 1 + 3 | 3 +(2 rows) +``` + +If you no longer need this UDAF, remove it using [DROP AGGREGATE](/docs/current/sql-drop-aggregate/). \ No newline at end of file From e94d8987ed308521ad16b879045b8ce7bfb9b2b2 Mon Sep 17 00:00:00 2001 From: IrisWan <150207222+WanYixian@users.noreply.github.com> Date: Wed, 6 Nov 2024 17:48:18 +0800 Subject: [PATCH 3/3] Update sql/functions/window-functions.mdx Co-authored-by: Richard Chien --- sql/functions/window-functions.mdx | 38 ++---------------------------- 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/sql/functions/window-functions.mdx b/sql/functions/window-functions.mdx index 3c86a044..3bc0d4f7 100644 --- a/sql/functions/window-functions.mdx +++ b/sql/functions/window-functions.mdx @@ -83,40 +83,6 @@ last_value ( value anyelement ) → anyelement ## Aggregate window functions -All aggregate functions, including `sum()`, `min()`, `max()`, `avg()` and `count()` etc., can be used as window functions. +All aggregate functions, including builtin ones such as `sum()` and `min()`, user-defined ones and `AGGREGATE:`-prefixed scalar functions, can be used as window functions. -For the complete list of aggregate functions and their usage, see [Aggregate functions](/docs/current/sql-function-aggregate/). - -## UDAF window functions - -To use the User-Defined Aggregate Functions (UDAFs) as window functions, you can define the UDAF using the [CREATE AGGREGATE](/docs/current/sql-create-aggregate/) statement, then call it within a query using the OVER clause, just like any built-in aggregate function. See the example below: - -```sql -CREATE AGGREGATE sum00(value INT) -RETURNS INT -LANGUAGE python AS $$ -def create_state(): - return 0 -def accumulate(state, value): - return state + value -def retract(state, value): - return state - value -def finish(state): - return state -$$; - -SELECT t.value, sum00(weight) OVER (PARTITION BY value) -FROM (VALUES (1, 1), (NULL, 2), (3, 3)) AS t(value, weight); -``` - -The output: - -```sql - value | weighted_avg --------+-------------- - 1 | 1 - 3 | 3 -(2 rows) -``` - -If you no longer need this UDAF, remove it using [DROP AGGREGATE](/docs/current/sql-drop-aggregate/). \ No newline at end of file +For the complete list of builtin aggregate functions and their usage, see [Aggregate functions](/docs/current/sql-function-aggregate/). \ No newline at end of file