This is a read-only copy of the MariaDB Knowledgebase generated on 2024-12-22. For the latest, interactive version please visit https://mariadb.com/kb/.

ColumnStore Window Functions

Introduction

MariaDB ColumnStore provides support for window functions broadly following the SQL 2003 specification. A window function allows for calculations relating to a window of data surrounding the current row in a result set. This capability provides for simplified queries in support of common business questions such as cumulative totals, rolling averages, and top 10 lists.

Aggregate functions are utilized for window functions however differ in behavior from a group by query because the rows remain ungrouped. This provides support for cumulative sums and rolling averages, for example.

Two key concepts for window functions are Partition and Frame:

  • A Partition is a group of rows, or window, that have the same value for a specific column, for example a Partition can be created over a time period such as a quarter or lookup values.
  • The Frame for each row is a subset of the row's Partition. The frame typically is dynamic allowing for a sliding frame of rows within the Partition. The Frame determines the range of rows for the windowing function. A Frame could be defined as the last X rows and next Y rows all the way up to the entire Partition.

Window functions are applied after joins, group by, and having clauses are calculated.

Syntax

A window function is applied in the select clause using the following syntax:

function_name ([expression [, expression ... ]]) OVER ( window_definition )

where window_definition is defined as:

[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]

PARTITION BY:

  • Divides the window result set into groups based on one or more expressions.
  • An expression may be a constant, column, and non window function expressions.
  • A query is not limited to a single partition by clause. Different partition clauses can be used across different window function applications.
  • The partition by columns do not need to be in the select list but do need to be available from the query result set.
  • If there is no PARTITION BY clause, all rows of the result set define the group.

ORDER BY

  • Defines the ordering of values within the partition.
  • Can be ordered by multiple keys which may be a constant, column or non window function expression.
  • The order by columns do not need to be in the select list but need to be available from the query result set.
  • Use of a select column alias from the query is not supported.
  • ASC (default) and DESC options allow for ordering ascending or descending.
  • NULLS FIRST and NULL_LAST options specify whether null values come first or last in the ordering sequence. NULLS_FIRST is the default for ASC order, and NULLS_LAST is the default for DESC order.

and the optional frame_clause is defined as:

{ RANGE | ROWS } frame_start
{ RANGE | ROWS } BETWEEN frame_start AND frame_end

and the optional frame_start and frame_end are defined as (value being a numeric expression):

UNBOUNDED PRECEDING
value PRECEDING
CURRENT ROW
value FOLLOWING
UNBOUNDED FOLLOWING

RANGE/ROWS:

  • Defines the windowing clause for calculating the set of rows that the function applies to for calculating a given rows window function result.
  • Requires an ORDER BY clause to define the row order for the window.
  • ROWS specify the window in physical units, i.e. result set rows and must be a constant or expression evaluating to a positive numeric value.
  • RANGE specifies the window as a logical offset. If the the expression evaluates to a numeric value then the ORDER BY expression must be a numeric or DATE type. If the expression evaluates to an interval value then the ORDER BY expression must be a DATE data type.
  • UNBOUNDED PRECEDING indicates the window starts at the first row of the partition.
  • UNBOUNDED FOLLOWING indicates the window ends at the last row of the partition.
  • CURRENT ROW specifies the window start or ends at the current row or value.
  • If omitted, the default is ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

Supported Functions

FunctionDescription
AVG()The average of all input values.
CORR(ColumnY, ColumnX)The correlation coefficient for non-null pairs in a group.
COUNT()Number of input rows.
COVAR_POP(ColumnY, ColumnX)The population covariance for non-null pairs in a group.
COVAR_SAMP(ColumnY, ColumnX)The sample covariance for non-null pairs in a group.
CUME_DIST()Calculates the cumulative distribution, or relative rank, of the current row to other rows in the same partition. Number of peer or preceding rows / number of rows in partition.
DENSE_RANK()Ranks items in a group leaving no gaps in ranking sequence when there are ties.
FIRST_VALUE()The value evaluated at the row that is the first row of the window frame (counting from 1); null if no such row.
LAG()The value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead return default. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null. LAG provides access to more than one row of a table at the same time without a self-join. Given a series of rows returned from a query and a position of the cursor, LAG provides access to a row at a given physical offset prior to that position.
LAST_VALUE()The value evaluated at the row that is the last row of the window frame (counting from 1); null if no such row.
LEAD()Provides access to a row at a given physical offset beyond that position. Returns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead return default. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null.
MAX()Maximum value of expression across all input values.
MEDIAN()An inverse distribution function that assumes a continuous distribution model. It takes a numeric or datetime value and returns the middle value or an interpolated value that would be the middle value once the values are sorted. Nulls are ignored in the calculation. Not available in MariaDB Columnstore 1.1
MIN()Minimum value of expression across all input values.
NTH_VALUE()The value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row.
NTILE()Divides an ordered data set into a number of buckets indicated by expr and assigns the appropriate bucket number to each row. The buckets are numbered 1 through expr. The expr value must resolve to a positive constant for each partition. Integer ranging from 1 to the argument value, dividing the partition as equally as possible.
PERCENT_RANK()Relative rank of the current row: (rank - 1) / (total rows - 1).
PERCENTILE_CONT()An inverse distribution function that assumes a continuous distribution model. It takes a percentile value and a sort specification, and returns an interpolated value that would fall into that percentile value with respect to the sort specification. Nulls are ignored in the calculation. Not available in MariaDB Columnstore 1.1
PERCENTILE_DISC()An inverse distribution function that assumes a discrete distribution model. It takes a percentile value and a sort specification and returns an element from the set. Nulls are ignored in the calculation. Not available in MariaDB Columnstore 1.1
RANK()Rank of the current row with gaps; same as row_number of its first peer.
REGR_AVGX(ColumnY, ColumnX)Average of the independent variable (sum(ColumnX)/N), where N is number of rows processed by the query
REGR_AVGY(ColumnY, ColumnX)Average of the dependent variable (sum(ColumnY)/N), where N is number of rows processed by the query
REGR_COUNT(ColumnY, ColumnX)The total number of input rows in which both column Y and column X are nonnull
REGR_SLOPE(ColumnY, ColumnX)The slope of the least-squares-fit linear equation determined by the (ColumnX, ColumnY) pairs
REGR_INTERCEPT(ColumnY, ColumnX)The y-intercept of the least-squares-fit linear equation determined by the (ColumnX, ColumnY) pairs
REGR_R2(ColumnY, ColumnX)Square of the correlation coefficient. correlation coefficient is the regr_intercept(ColumnY, ColumnX) for linear model
REGR_SXX(ColumnY, ColumnX)REGR_COUNT(y, x) * VAR_POP(x) for non-null pairs.
REGR_SXY(ColumnY, ColumnX)REGR_COUNT(y, x) * COVAR_POP(y, x) for non-null pairs.
REGR_SYY(ColumnY, ColumnX)REGR_COUNT(y, x) * VAR_POP(y) for non-null pairs.
ROW_NUMBER()Number of the current row within its partition, counting from 1
STDDEV() STDDEV_POP()Computes the population standard deviation and returns the square root of the population variance.
STDDEV_SAMP()Computes the cumulative sample standard deviation and returns the square root of the sample variance.
SUM()Sum of expression across all input values.
VARIANCE() VAR_POP()Population variance of the input values (square of the population standard deviation).
VAR_SAMP()Sample variance of the input values (square of the sample standard deviation).

Note

  • Regression functions (REGR_AVGX to REGR_YY), CORR, COVAR_POP and COVAR_SAMP are supported for version 1.2.0 and higher

Examples

Example Schema

The examples are all based on the following simplified sales opportunity table:

create table opportunities (
id int,
accountName varchar(20),
name varchar(128),
owner varchar(7),
amount decimal(10,2),
closeDate date,
stageName varchar(11)
) engine=columnstore;

Some example values are (thanks to https://www.mockaroo.com for sample data generation):

idaccountNamenameowneramountcloseDatestageName
1BrowseblabMulti-lateral executive functionBob26444.862016-10-20Negotiating
2MitaOrganic demand-driven benchmarkMaria477878.412016-11-28ClosedWon
3MibooDe-engineered hybrid groupwareOlivier80181.782017-01-05ClosedWon
4YoubridgeEnterprise-wide bottom-line Graphic InterfaceChris946245.292016-07-02ClosedWon
5SkybaReverse-engineered fresh-thinking standardizationMaria696241.822017-02-17Negotiating
6EayoFundamental well-modulated artificial intelligenceBob765605.522016-08-27Prospecting
7YotzExtended secondary infrastructureChris319624.202017-01-06ClosedLost
8OlooConfigurable web-enabled data-warehouseChris321016.262017-03-08ClosedLost
9KaymboMulti-lateral web-enabled definitionBob690881.012017-01-02Developing
10RhylooPublic-key coherent infrastructureChris965477.742016-11-07Prospecting

The schema, sample data, and queries are available as an attachment to this article.

Cumulative Sum and Running Max Example

Window functions can be used to achieve cumulative / running calculations on a detail report. In this case a won opportunity report for a 7 day period adds columns to show the accumulated won amount as well as the current highest opportunity amount in preceding rows.

select owner, 
accountName, 
CloseDate, 
amount, 
sum(amount) over (order by CloseDate rows between unbounded preceding and current row) cumeWon, 
max(amount) over (order by CloseDate rows between unbounded preceding and current row) runningMax
from opportunities 
where stageName='ClosedWon' 
and closeDate >= '2016-10-02' and closeDate <= '2016-10-09' 
order by CloseDate;

with example results:

owneraccountNameCloseDateamountcumeWonrunningMax
BillBabbleopia2016-10-02437636.47437636.47437636.47
BillThoughtworks2016-10-04146086.51583722.98437636.47
OlivierDevpulse2016-10-05834235.931417958.91834235.93
ChrisLinkbridge2016-10-07539977.452458738.65834235.93
OlivierTrupe2016-10-07500802.291918761.20834235.93
BillLatz2016-10-08857254.873315993.52857254.87
ChrisAvamm2016-10-09699566.864015560.38857254.87

Partitioned Cumulative Sum and Running Max Example

The above example can be partitioned, so that the window functions are over a particular field grouping such as owner and accumulate within that grouping. This is achieved by adding the syntax "partition by <columns>" in the window function clause.

select owner,  
accountName,  
CloseDate,  
amount,  
sum(amount) over (partition by owner order by CloseDate rows between unbounded preceding and current row) cumeWon,  
max(amount) over (partition by owner order by CloseDate rows between unbounded preceding and current row) runningMax 
from opportunities  
where stageName='ClosedWon' 
and closeDate >= '2016-10-02' and closeDate <= '2016-10-09'  
order by owner, CloseDate;

with example results:

owneraccountNameCloseDateamountcumeWonrunningMax
BillBabbleopia2016-10-02437636.47437636.47437636.47
BillThoughtworks2016-10-04146086.51583722.98437636.47
BillLatz2016-10-08857254.871440977.85857254.87
ChrisLinkbridge2016-10-07539977.45539977.45539977.45
ChrisAvamm2016-10-09699566.861239544.31699566.86
OlivierDevpulse2016-10-05834235.93834235.93834235.93
OlivierTrupe2016-10-07500802.291335038.22834235.93

Ranking / Top Results

The rank window function allows for ranking or assigning a numeric order value based on the window function definition. Using the Rank() function will result in the same value for ties / equal values and the next rank value skipped. The Dense_Rank() function behaves similarly except the next consecutive number is used after a tie rather than skipped. The Row_Number() function will provide a unique ordering value. The example query shows the Rank() function being applied to rank sales reps by the number of opportunities for Q4 2016.

select owner, 
wonCount, 
rank() over (order by wonCount desc) rank 
from (
  select owner, 
  count(*) wonCount 
  from opportunities 
  where stageName='ClosedWon' 
  and closeDate >= '2016-10-01' and closeDate < '2016-12-31'  
  group by owner
) t
order by rank;

with example results (note the query is technically incorrect by using closeDate < '2016-12-31' however this creates a tie scenario for illustrative purposes):

ownerwonCountrank
Bill191
Chris152
Maria143
Bob143
Olivier105

If the dense_rank function is used the rank values would be 1,2,3,3,4 and for the row_number function the values would be 1,2,3,4,5.

First and Last Values

The first_value and last_value functions allow determining the first and last values of a given range. Combined with a group by this allows summarizing opening and closing values. The example shows a more complex case where detailed information is presented for first and last opportunity by quarter.

select a.year, 
a.quarter, 
f.accountName firstAccountName, 
f.owner firstOwner, 
f.amount firstAmount, 
l.accountName lastAccountName, 
l.owner lastOwner, 
l.amount lastAmount 
from (
  select year, 
  quarter, 
  min(firstId) firstId, 
  min(lastId) lastId 
  from (
    select year(closeDate) year, 
    quarter(closeDate) quarter, 
    first_value(id) over (partition by year(closeDate), quarter(closeDate) order by closeDate rows between unbounded preceding and current row) firstId, 
    last_value(id) over (partition by year(closeDate), quarter(closeDate) order by closeDate rows between current row and unbounded following) lastId 
    from opportunities  where stageName='ClosedWon'
  ) t 
  group by year, quarter order by year,quarter
) a 
join opportunities f on a.firstId = f.id 
join opportunities l on a.lastId = l.id 
order by year, quarter;

with example results:

yearquarterfirstAccountNamefirstOwnerfirstAmountlastAccountNamelastOwnerlastAmount
20163SkidooBill523295.07SkipstormBill151420.86
20164SkimiaChris961513.59AvammMaria112493.65
20171YombuBob536875.51SkabooChris270273.08

Prior and Next Example

Sometimes it useful to understand the previous and next values in the context of a given row. The lag and lead window functions provide this capability. By default the offset is one providing the prior or next value but can also be provided to get a larger offset. The example query is a report of opportunities by account name showing the opportunity amount, and the prior and next opportunity amount for that account by close date.

select accountName, 
closeDate,  
amount currentOppAmount, 
lag(amount) over (partition by accountName order by closeDate) priorAmount, lead(amount) over (partition by accountName order by closeDate) nextAmount 
from opportunities 
order by accountName, closeDate 
limit 9;

with example results:

accountNamecloseDatecurrentOppAmountpriorAmountnextAmount
Abata2016-09-10645098.45NULL161086.82
Abata2016-10-14161086.82645098.45350235.75
Abata2016-12-18350235.75161086.82878595.89
Abata2016-12-31878595.89350235.75922322.39
Abata2017-01-21922322.39878595.89NULL
Abatz2016-10-19795424.15NULLNULL
Agimba2016-07-09288974.84NULL914461.49
Agimba2016-09-07914461.49288974.84176645.52
Agimba2016-09-20176645.52914461.49NULL

Quartiles Example

The NTile window function allows for breaking up a data set into portions assigned a numeric value to each portion of the range. NTile(4) breaks the data up into quartiles (4 sets). The example query produces a report of all opportunities summarizing the quartile boundaries of amount values.

select t.quartile, 
min(t.amount) min, 
max(t.amount) max 
from (
  select amount, 
  ntile(4) over (order by amount asc) quartile 
  from opportunities 
  where closeDate >= '2016-10-01' and closeDate <= '2016-12-31'
  ) t 
group by quartile 
order by quartile;

With example results:

quartileminmax
16337.15287634.01
2288796.14539977.45
3540070.04748727.51
4753670.77998864.47

Percentile Example

The percentile functions have a slightly different syntax from other window functions as can be seen in the example below. These functions can be only applied against numeric values. The argument to the function is the percentile to evaluate. Following 'within group' is the sort expression which indicates the sort column and optionally order. Finally after 'over' is an optional partition by clause, for no partition clause use 'over ()'. The example below utilizes the value 0.5 to calculate the median opportunity amount in the rows. The values differ sometimes because percentile_cont will return the average of the 2 middle rows for an even data set while percentile_desc returns the first encountered in the sort.

Note that the percentile syntax is supported in MariaDB ColumnStore 1.0 but not 1.1. It is anticipated that this will be restored in MariaDB ColumnStore 1.2 after this is implemented in MariaDB server 10.3.

select owner,  
accountName,  
CloseDate,  
amount,
percentile_cont(0.5) within group (order by amount) over (partition by owner) pct_cont,
percentile_disc(0.5) within group (order by amount) over (partition by owner) pct_disc
from opportunities  
where stageName='ClosedWon' 
and closeDate >= '2016-10-02' and closeDate <= '2016-10-09'  
order by owner, CloseDate;

With example results:

owneraccountNameCloseDateamountpct_contpct_disc
BillBabbleopia2016-10-02437636.47437636.4700000000437636.47
BillThoughtworks2016-10-04146086.51437636.4700000000437636.47
BillLatz2016-10-08857254.87437636.4700000000437636.47
ChrisLinkbridge2016-10-07539977.45619772.1550000000539977.45
ChrisAvamm2016-10-09699566.86619772.1550000000539977.45
OlivierDevpulse2016-10-05834235.93667519.1100000000500802.29
OlivierTrupe2016-10-07500802.29667519.1100000000500802.29
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.