From cc0f8271e21a30c5aec495060533cac7ef44e3d6 Mon Sep 17 00:00:00 2001 From: Denny Crane Date: Mon, 25 Nov 2024 13:14:55 +0000 Subject: [PATCH 1/3] add a warning to lagInFrame/leadInFrame --- .../window-functions/lagInFrame.md | 18 +++++++--- .../window-functions/leadInFrame.md | 36 ++++++++++++++----- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/docs/en/sql-reference/window-functions/lagInFrame.md b/docs/en/sql-reference/window-functions/lagInFrame.md index 01bf809e76e..c4b7b377761 100644 --- a/docs/en/sql-reference/window-functions/lagInFrame.md +++ b/docs/en/sql-reference/window-functions/lagInFrame.md @@ -8,11 +8,17 @@ sidebar_position: 9 Returns a value evaluated at the row that is at a specified physical offset row before the current row within the ordered frame. +:::warning +`lagInFrame` behavior differs from the standard SQL `lag` window function. +Clickhouse window function `lagInFrame` respects the window frame. +To get behavior identical to the `lag`, use `rows between unbounded preceding and unbounded following`. +::: + **Syntax** ```sql lagInFrame(x[, offset[, default]]) - OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column] + OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column] [ROWS or RANGE expression_to_bound_rows_withing_the_group]] | [window_name]) FROM table_name WINDOW window_name as ([[PARTITION BY grouping_column] [ORDER BY sorting_column]) @@ -21,7 +27,7 @@ WINDOW window_name as ([[PARTITION BY grouping_column] [ORDER BY sorting_column] For more detail on window function syntax see: [Window Functions - Syntax](./index.md/#syntax). **Parameters** -- `x` — Column name. +- `x` — Column name. - `offset` — Offset to apply. [(U)Int*](../data-types/int-uint.md). (Optional - `1` by default). - `default` — Value to return if calculated row exceeds the boundaries of the window frame. (Optional - default value of column type when omitted). @@ -59,11 +65,13 @@ INSERT INTO stock_prices FORMAT Values SELECT date, close, - lagInFrame(close, 1, close) OVER (ORDER BY date ASC) AS previous_day_close, + lagInFrame(close, 1, close) OVER (ORDER BY date ASC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS previous_day_close, COALESCE(ROUND(close - previous_day_close, 2)) AS delta, COALESCE(ROUND((delta / previous_day_close) * 100, 2)) AS percent_change FROM stock_prices -ORDER BY date DESC; +ORDER BY date DESC ``` Result: @@ -76,4 +84,4 @@ Result: 4. │ 2024-06-04 │ 116.44 │ 115 │ 1.44 │ 1.25 │ 5. │ 2024-06-03 │ 115 │ 115 │ 0 │ 0 │ └────────────┴────────┴────────────────────┴───────┴────────────────┘ -``` \ No newline at end of file +``` diff --git a/docs/en/sql-reference/window-functions/leadInFrame.md b/docs/en/sql-reference/window-functions/leadInFrame.md index dae4353b582..16c7aefd81a 100644 --- a/docs/en/sql-reference/window-functions/leadInFrame.md +++ b/docs/en/sql-reference/window-functions/leadInFrame.md @@ -8,11 +8,17 @@ sidebar_position: 10 Returns a value evaluated at the row that is offset rows after the current row within the ordered frame. +:::warning +`leadInFrame` behavior differs from the standard SQL `lead` window function. +Clickhouse window function `leadInFrame` respects the window frame. +To get behavior identical to the `lead`, use `rows between unbounded preceding and unbounded following`. +::: + **Syntax** ```sql leadInFrame(x[, offset[, default]]) - OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column] + OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column] [ROWS or RANGE expression_to_bound_rows_withing_the_group]] | [window_name]) FROM table_name WINDOW window_name as ([[PARTITION BY grouping_column] [ORDER BY sorting_column]) @@ -36,25 +42,37 @@ This example looks at [historical data](https://www.kaggle.com/datasets/sazidthe Query: ```sql -CREATE OR REPLACE VIEW nobel_prize_laureates AS FROM file('nobel_laureates_data.csv') SELECT *; +CREATE OR REPLACE VIEW nobel_prize_laureates +AS SELECT * +FROM file('nobel_laureates_data.csv'); ``` ```sql -FROM nobel_prize_laureates SELECT fullName, leadInFrame(year, 1, year) OVER (PARTITION BY category ORDER BY year) AS year, category, motivation WHERE category == 'physics' ORDER BY year DESC LIMIT 9; +SELECT + fullName, + leadInFrame(year, 1, year) OVER (PARTITION BY category ORDER BY year ASC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS year, + category, + motivation +FROM nobel_prize_laureates +WHERE category = 'physics' +ORDER BY year DESC +LIMIT 9 ``` Result: ```response ┌─fullName─────────┬─year─┬─category─┬─motivation─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ -1. │ Pierre Agostini │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │ -2. │ Ferenc Krausz │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │ -3. │ Anne L Huillier │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │ +1. │ Anne L Huillier │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │ +2. │ Pierre Agostini │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │ +3. │ Ferenc Krausz │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │ 4. │ Alain Aspect │ 2022 │ physics │ for experiments with entangled photons establishing the violation of Bell inequalities and pioneering quantum information science │ 5. │ Anton Zeilinger │ 2022 │ physics │ for experiments with entangled photons establishing the violation of Bell inequalities and pioneering quantum information science │ 6. │ John Clauser │ 2022 │ physics │ for experiments with entangled photons establishing the violation of Bell inequalities and pioneering quantum information science │ -7. │ Syukuro Manabe │ 2021 │ physics │ for the physical modelling of Earths climate quantifying variability and reliably predicting global warming │ +7. │ Giorgio Parisi │ 2021 │ physics │ for the discovery of the interplay of disorder and fluctuations in physical systems from atomic to planetary scales │ 8. │ Klaus Hasselmann │ 2021 │ physics │ for the physical modelling of Earths climate quantifying variability and reliably predicting global warming │ -9. │ Giorgio Parisi │ 2021 │ physics │ for the discovery of the interplay of disorder and fluctuations in physical systems from atomic to planetary scales │ +9. │ Syukuro Manabe │ 2021 │ physics │ for the physical modelling of Earths climate quantifying variability and reliably predicting global warming │ └──────────────────┴──────┴──────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ -``` \ No newline at end of file +``` From 0ebee19f2e3494df1c53b1d260fce31472772dcb Mon Sep 17 00:00:00 2001 From: Denny Crane Date: Tue, 26 Nov 2024 09:21:58 -0400 Subject: [PATCH 2/3] Update docs/en/sql-reference/window-functions/leadInFrame.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: János Benjamin Antal --- docs/en/sql-reference/window-functions/leadInFrame.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/window-functions/leadInFrame.md b/docs/en/sql-reference/window-functions/leadInFrame.md index 16c7aefd81a..c2bc2f525c5 100644 --- a/docs/en/sql-reference/window-functions/leadInFrame.md +++ b/docs/en/sql-reference/window-functions/leadInFrame.md @@ -11,7 +11,7 @@ Returns a value evaluated at the row that is offset rows after the current row w :::warning `leadInFrame` behavior differs from the standard SQL `lead` window function. Clickhouse window function `leadInFrame` respects the window frame. -To get behavior identical to the `lead`, use `rows between unbounded preceding and unbounded following`. +To get behavior identical to the `lead`, use `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`. ::: **Syntax** From 5b1bdef54f047e3395031f8bc1bc4acba0c15eaf Mon Sep 17 00:00:00 2001 From: Denny Crane Date: Tue, 26 Nov 2024 09:24:42 -0400 Subject: [PATCH 3/3] Update lagInFrame.md --- docs/en/sql-reference/window-functions/lagInFrame.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/window-functions/lagInFrame.md b/docs/en/sql-reference/window-functions/lagInFrame.md index c4b7b377761..fb311ab6fcb 100644 --- a/docs/en/sql-reference/window-functions/lagInFrame.md +++ b/docs/en/sql-reference/window-functions/lagInFrame.md @@ -11,7 +11,7 @@ Returns a value evaluated at the row that is at a specified physical offset row :::warning `lagInFrame` behavior differs from the standard SQL `lag` window function. Clickhouse window function `lagInFrame` respects the window frame. -To get behavior identical to the `lag`, use `rows between unbounded preceding and unbounded following`. +To get behavior identical to the `lag`, use `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`. ::: **Syntax**