|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | += Window Functions |
| 16 | + |
| 17 | +Window functions calculate a value for each row returned by a query. The value is calculated over a set of rows defined |
| 18 | +by the `PARTITION BY`, `ORDER BY`, and window frame clauses. This set of rows is called a window. |
| 19 | + |
| 20 | +A window function is identified by the `OVER` clause. The `OVER` clause defines how rows are partitioned, ordered, and |
| 21 | +framed for the function. Unlike regular aggregate functions, aggregate window functions do not collapse rows into a |
| 22 | +single grouped result. |
| 23 | + |
| 24 | +[NOTE] |
| 25 | +==== |
| 26 | +Window functions are supported by the Calcite-based SQL engine. |
| 27 | +==== |
| 28 | + |
| 29 | +== Supported Window Functions |
| 30 | + |
| 31 | +Ignite supports aggregate, ranking, and value functions with the `OVER` clause. |
| 32 | + |
| 33 | +[cols="1,3",opts="stretch,header"] |
| 34 | +|=== |
| 35 | +|Type |Functions |
| 36 | + |
| 37 | +|Aggregate |
| 38 | +|Any supported aggregate function (see link:sql-reference/aggregate-functions[Aggregate functions]) |
| 39 | + |
| 40 | +|Ranking |
| 41 | +|`CUME_DIST`, `DENSE_RANK`, `NTILE`, `PERCENT_RANK`, `RANK`, `ROW_NUMBER` |
| 42 | + |
| 43 | +|Value |
| 44 | +|`FIRST_VALUE`, `LAG`, `LAST_VALUE`, `LEAD`, `NTH_VALUE` |
| 45 | + |
| 46 | +|=== |
| 47 | + |
| 48 | +== Syntax |
| 49 | + |
| 50 | +[source,sql] |
| 51 | +---- |
| 52 | +windowFunction([expression[, expression]...]) OVER ( |
| 53 | + [PARTITION BY expression[, expression]...] |
| 54 | + [ORDER BY expression [ASC | DESC] [NULLS FIRST | NULLS LAST][, expression ...]] |
| 55 | + [{ROWS | RANGE} frameExtent] |
| 56 | +) |
| 57 | +---- |
| 58 | + |
| 59 | +The frame extent can use one of the following forms: |
| 60 | + |
| 61 | +[source,sql] |
| 62 | +---- |
| 63 | +frameStart |
| 64 | +BETWEEN frameStart AND frameEnd |
| 65 | +---- |
| 66 | + |
| 67 | +`frameStart` and `frameEnd` can use the following boundaries: |
| 68 | + |
| 69 | +[source,sql] |
| 70 | +---- |
| 71 | +UNBOUNDED PRECEDING |
| 72 | +offset PRECEDING |
| 73 | +CURRENT ROW |
| 74 | +offset FOLLOWING |
| 75 | +UNBOUNDED FOLLOWING |
| 76 | +---- |
| 77 | + |
| 78 | +== Arguments |
| 79 | + |
| 80 | +[cols="1,3",opts="stretch,header"] |
| 81 | +|=== |
| 82 | +|Clause |Description |
| 83 | + |
| 84 | +|`OVER` |
| 85 | +|Defines the window specification for the function. A query can use multiple window functions with the same or different |
| 86 | +window specifications. |
| 87 | + |
| 88 | +|`PARTITION BY` |
| 89 | +|Splits the query result into independent partitions. The window function is evaluated within the current row's |
| 90 | +partition. If this clause is omitted, the window function uses the whole query result as one partition. |
| 91 | + |
| 92 | +|`ORDER BY` |
| 93 | +|Defines row ordering inside each partition. This ordering belongs to the window specification and is independent from |
| 94 | +the query-level `ORDER BY` clause. Ranking functions and bounded frames use this ordering. |
| 95 | + |
| 96 | +|`ROWS` |
| 97 | +|Defines a frame by physical row offsets from the current row. |
| 98 | + |
| 99 | +|`RANGE` |
| 100 | +|Defines a frame by the ordered value range around the current row. Numeric offsets are supported for numeric ordering |
| 101 | +expressions. Interval offsets are supported for date and time ordering expressions. |
| 102 | + |
| 103 | +|`UNBOUNDED PRECEDING` |
| 104 | +|Starts the frame at the first row of the partition. |
| 105 | + |
| 106 | +|`offset PRECEDING` |
| 107 | +|Starts or ends the frame before the current row. |
| 108 | + |
| 109 | +|`CURRENT ROW` |
| 110 | +|Starts or ends the frame at the current row. |
| 111 | + |
| 112 | +|`offset FOLLOWING` |
| 113 | +|Starts or ends the frame after the current row. |
| 114 | + |
| 115 | +|`UNBOUNDED FOLLOWING` |
| 116 | +|Ends the frame at the last row of the partition. |
| 117 | + |
| 118 | +|=== |
| 119 | + |
| 120 | +== Examples |
| 121 | + |
| 122 | +The following query calculates a rank and a department salary total for each employee row: |
| 123 | + |
| 124 | +[source,sql] |
| 125 | +---- |
| 126 | +SELECT depname, empno, salary, |
| 127 | + RANK() OVER (PARTITION BY depname ORDER BY salary) AS salary_rank, |
| 128 | + SUM(salary) OVER (PARTITION BY depname) AS dep_salary |
| 129 | +FROM empsalary; |
| 130 | +---- |
| 131 | + |
| 132 | +The following query counts rows in a physical frame around the current row: |
| 133 | + |
| 134 | +[source,sql] |
| 135 | +---- |
| 136 | +SELECT depname, |
| 137 | + COUNT(*) OVER ( |
| 138 | + PARTITION BY depname |
| 139 | + ORDER BY empno |
| 140 | + ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING |
| 141 | + ) AS nearby_rows |
| 142 | +FROM empsalary; |
| 143 | +---- |
| 144 | + |
| 145 | +The following query counts rows in a date range around the current row: |
| 146 | + |
| 147 | +[source,sql] |
| 148 | +---- |
| 149 | +SELECT depname, |
| 150 | + COUNT(*) OVER ( |
| 151 | + PARTITION BY depname |
| 152 | + ORDER BY enroll_date |
| 153 | + RANGE BETWEEN INTERVAL 730 DAYS PRECEDING AND INTERVAL 360 DAYS FOLLOWING |
| 154 | + ) AS nearby_dates |
| 155 | +FROM empsalary; |
| 156 | +---- |
| 157 | + |
| 158 | +== Usage Notes |
| 159 | + |
| 160 | +Window functions are evaluated after the `WHERE`, `GROUP BY`, and `HAVING` clauses. They can be used in the `SELECT` |
| 161 | +list and in the query-level `ORDER BY` clause. |
| 162 | + |
| 163 | +Aggregate functions become aggregate window functions when used with the `OVER` clause. |
0 commit comments