Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 109 additions & 1 deletion functions-and-operators/aggregate-group-by-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,114 @@ This section describes the supported MySQL `GROUP BY` aggregate functions in TiD

In addition, TiDB also provides the following aggregate functions:

+ `SUM_INT(expr)`

This function returns the sum of the integer expression `expr`. It works similarly to `SUM(expr)`, but only accepts integer arguments, including `TINYINT`, `SMALLINT`, `MEDIUMINT`, `INT`, and `BIGINT` (both signed and unsigned). For a signed integer argument, the return type is `BIGINT`. For an unsigned integer argument, the return type is `BIGINT UNSIGNED`. If the sum of non-`NULL` values exceeds the range of the return type, TiDB returns an integer overflow error.

`SUM_INT()` ignores `NULL` values. If there are no non-`NULL` values, it returns `NULL`. `SUM_INT()` supports `DISTINCT` and can be used as a [window function](/functions-and-operators/window-functions.md).

The following example shows how to use `SUM_INT()`:

```sql
DROP TABLE IF EXISTS t;
CREATE TABLE t(id INT PRIMARY KEY, a BIGINT, b BIGINT UNSIGNED);
INSERT INTO t VALUES(1, 1, 1), (2, 1, 1), (3, 2, 2), (4, NULL, NULL);
```

```sql
SELECT SUM_INT(a), SUM_INT(DISTINCT a), SUM_INT(b) FROM t;
```

```sql
+------------+---------------------+------------+
| SUM_INT(a) | SUM_INT(DISTINCT a) | SUM_INT(b) |
+------------+---------------------+------------+
| 4 | 3 | 4 |
+------------+---------------------+------------+
1 row in set (0.00 sec)
```

The following example uses `SUM_INT()` as a window function:

```sql
SELECT id, a, SUM_INT(a) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS rolling_sum
FROM t
ORDER BY id;
```

```sql
+----+------+-------------+
| id | a | rolling_sum |
+----+------+-------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |
| 4 | NULL | 2 |
+----+------+-------------+
4 rows in set (0.00 sec)
```

+ `MAX_COUNT([ALL] expr)` and `MIN_COUNT([ALL] expr)`

These functions are TiDB-specific aggregate functions that count occurrences of the maximum or minimum value in a group. `MAX_COUNT(expr)` returns the number of rows whose value equals the maximum non-`NULL` value of `expr` in the current group. `MIN_COUNT(expr)` returns the number of rows whose value equals the minimum non-`NULL` value of `expr` in the current group.

These functions ignore `NULL` values by default. If there are no non-`NULL` values in the current group, they return `0`. The return type is `BIGINT`. These functions support omitting `ALL` or explicitly specifying `ALL`, but do not support `DISTINCT`. For example, `MAX_COUNT(DISTINCT expr)` and `MIN_COUNT(DISTINCT expr)` return a syntax error. These functions can also be used as [window functions](/functions-and-operators/window-functions.md).

The following example shows how to use these functions:

```sql
DROP TABLE IF EXISTS t;
CREATE TABLE t(a INT);
INSERT INTO t VALUES(1), (1), (2), (2), (2), (NULL);
```

```sql
SELECT MAX_COUNT(a), MIN_COUNT(a) FROM t;
```

```sql
+--------------+--------------+
| MAX_COUNT(a) | MIN_COUNT(a) |
+--------------+--------------+
| 3 | 2 |
+--------------+--------------+
1 row in set (0.00 sec)
```

If there are no non-`NULL` values, these functions return `0`:

```sql
SELECT MAX_COUNT(a), MIN_COUNT(a) FROM t WHERE a IS NULL;
```

```sql
+--------------+--------------+
| MAX_COUNT(a) | MIN_COUNT(a) |
+--------------+--------------+
| 0 | 0 |
+--------------+--------------+
1 row in set (0.00 sec)
```

The following example uses `MAX_COUNT()` and `MIN_COUNT()` as window functions:

```sql
SELECT
MAX_COUNT(a) OVER () AS max_count,
MIN_COUNT(a) OVER () AS min_count
FROM t
LIMIT 1;
```

```sql
+-----------+-----------+
| max_count | min_count |
+-----------+-----------+
| 3 | 2 |
+-----------+-----------+
1 row in set (0.00 sec)
```

+ `APPROX_PERCENTILE(expr, constant_integer_expr)`

This function returns the percentile of `expr`. The `constant_integer_expr` argument indicates the percentage value which is a constant integer in the range of `[1,100]`. A percentile P<sub>k</sub> (`k` represents percentage) indicates that there are at least `k%` values in the data set that are less than or equal to P<sub>k</sub>.
Expand Down Expand Up @@ -86,7 +194,7 @@ In addition, TiDB also provides the following aggregate functions:
2 rows in set (0.00 sec)
```

Except for the `GROUP_CONCAT()`, `APPROX_PERCENTILE()`, and `APPROX_COUNT_DISTINCT` functions, all the preceding functions can serve as [Window functions](/functions-and-operators/window-functions.md).
Except for the `GROUP_CONCAT()`, `APPROX_PERCENTILE()`, and `APPROX_COUNT_DISTINCT()` functions, all the preceding functions can serve as [window functions](/functions-and-operators/window-functions.md).

## GROUP BY modifiers

Expand Down
2 changes: 1 addition & 1 deletion functions-and-operators/expressions-pushed-down.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TiFlash also supports pushdown for the functions and operators [listed on this p
| [JSON functions](/functions-and-operators/json-functions.md) | [JSON_ARRAY_APPEND()](/functions-and-operators/json-functions/json-functions-modify.md#json_array_append) <br/>[JSON_ARRAY()](/functions-and-operators/json-functions/json-functions-create.md#json_array) <br/>[JSON_CONTAINS()](/functions-and-operators/json-functions/json-functions-search.md#json_contains) <br/>[JSON_EXTRACT()](/functions-and-operators/json-functions/json-functions-search.md#json_extract) <br/>[JSON_INSERT()](/functions-and-operators/json-functions/json-functions-modify.md#json_insert) <br/>[JSON_LENGTH()](/functions-and-operators/json-functions/json-functions-return.md#json_length) <br/>[JSON_MERGE_PATCH()](/functions-and-operators/json-functions/json-functions-modify.md#json_merge_patch) <br/>[JSON_MERGE()](/functions-and-operators/json-functions/json-functions-modify.md#json_merge) <br/>[JSON_OBJECT()](/functions-and-operators/json-functions/json-functions-create.md#json_object) <br/>[JSON_REMOVE()](/functions-and-operators/json-functions/json-functions-modify.md#json_remove) <br/>[JSON_REPLACE()](/functions-and-operators/json-functions/json-functions-modify.md#json_replace) <br/>[JSON_SET()](/functions-and-operators/json-functions/json-functions-modify.md#json_set) <br/>[JSON_TYPE()](/functions-and-operators/json-functions/json-functions-return.md#json_type) <br/>[JSON_UNQUOTE()](/functions-and-operators/json-functions/json-functions-modify.md#json_unquote) <br/>[JSON_VALID()](/functions-and-operators/json-functions/json-functions-return.md#json_valid) <br/>[MEMBER OF()](/functions-and-operators/json-functions/json-functions-search.md#member-of) |
| [Date and time functions](/functions-and-operators/date-and-time-functions.md) | [DATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date) <br/>[DATE_FORMAT()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format) <br/>[DATEDIFF()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_datediff) <br/>[DAYOFMONTH()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_dayofmonth) <br/>[DAYOFWEEK()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_dayofweek) <br/>[DAYOFYEAR()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_dayofyear) <br/>[FROM_DAYS()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-days) <br/>[HOUR()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_hour) <br/>[MAKEDATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_makedate) <br/>[MAKETIME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_maketime) <br/>[MICROSECOND()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_microsecond) <br/>[MINUTE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_minute) <br/>[MONTH()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_month) <br/>[MONTHNAME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_monthname) <br/>[PERIOD_ADD()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_period-add) <br/>[PERIOD_DIFF()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_period-diff) <br/>[SEC_TO_TIME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_sec-to-time) <br/>[SECOND()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_second) <br/>[SYSDATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_sysdate) <br/>[TIME_TO_SEC()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-to-sec) <br/>[TIMEDIFF()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timediff) <br/>[WEEK()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week) <br/>[WEEKOFYEAR()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_weekofyear) <br/>[YEAR()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_year) <br/>[DATE_ADD()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add) <br/>[DATE_SUB()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-sub) <br/>[ADDDATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_adddate) <br/>[SUBDATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_subdate) <br/>[FROM_UNIXTIME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-unixtime) <br/>[TIMESTAMPDIFF()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff) <br/>[UNIX_TIMESTAMP()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp) |
| [String functions](/functions-and-operators/string-functions.md) | [ASCII()](/functions-and-operators/string-functions.md#ascii) <br/>[BIT_LENGTH()](/functions-and-operators/string-functions.md#bit_length) <br/>[CHAR()](/functions-and-operators/string-functions.md#char) <br/>[CHAR_LENGTH()](/functions-and-operators/string-functions.md#char_length) <br/>[CONCAT()](/functions-and-operators/string-functions.md#concat) <br/>[CONCAT_WS()](/functions-and-operators/string-functions.md#concat_ws) <br/>[ELT()](/functions-and-operators/string-functions.md#elt) <br/>[FIELD()](/functions-and-operators/string-functions.md#field) <br/>[HEX()](/functions-and-operators/string-functions.md#hex) <br/>[LENGTH()](/functions-and-operators/string-functions.md#length) <br/>[LIKE](/functions-and-operators/string-functions.md#like) <br/>[LOWER()](/functions-and-operators/string-functions.md#lower) <br/>[LTRIM()](/functions-and-operators/string-functions.md#ltrim) <br/>[MID()](/functions-and-operators/string-functions.md#mid) <br/>[NOT LIKE](/functions-and-operators/string-functions.md#not-like) <br/>[NOT REGEXP](/functions-and-operators/string-functions.md#not-regexp) <br/>[REGEXP](/functions-and-operators/string-functions.md#regexp) <br/>[REGEXP_LIKE()](/functions-and-operators/string-functions.md#regexp_like) <br/>[REGEXP_REPLACE()](/functions-and-operators/string-functions.md#regexp_replace) <br/>[REGEXP_SUBSTR()](/functions-and-operators/string-functions.md#regexp_substr) <br/>[REPLACE()](/functions-and-operators/string-functions.md#replace) <br/>[REVERSE()](/functions-and-operators/string-functions.md#reverse) <br/>[RIGHT()](/functions-and-operators/string-functions.md#right), [RLIKE](/functions-and-operators/string-functions.md#rlike) <br/>[RTRIM()](/functions-and-operators/string-functions.md#rtrim) <br/>[SPACE()](/functions-and-operators/string-functions.md#space) <br/>[STRCMP()](/functions-and-operators/string-functions.md#strcmp) <br/>[SUBSTR()](/functions-and-operators/string-functions.md#substr) <br/>[SUBSTRING()](/functions-and-operators/string-functions.md#substring) <br/>[UPPER()](/functions-and-operators/string-functions.md#upper) |
| [Aggregation functions](/functions-and-operators/aggregate-group-by-functions.md#aggregate-group-by-functions) | [COUNT()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_count) <br/>[COUNT(DISTINCT)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_count-distinct) <br/>[SUM()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_sum) <br/>[AVG()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_avg) <br/>[MAX()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_max) <br/>[MIN()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_min) <br/>[VARIANCE()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_variance) <br/>[VAR_POP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_var-pop) <br/>[STD()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_std) <br/>[STDDEV()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev) <br/>[STDDEV_POP](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev-pop) <br/>[VAR_SAMP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_var-samp) <br/>[STDDEV_SAMP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev-samp) <br/>[JSON_ARRAYAGG(key)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_json-arrayagg) <br/>[JSON_OBJECTAGG(key, value)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_json-objectagg) |
| [Aggregation functions](/functions-and-operators/aggregate-group-by-functions.md#aggregate-group-by-functions) | [COUNT()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_count) <br/>[COUNT(DISTINCT)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_count-distinct) <br/>[SUM()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_sum) <br/>[`SUM_INT()`](/functions-and-operators/aggregate-group-by-functions.md) <br/>[AVG()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_avg) <br/>[MAX()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_max) <br/>[MIN()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_min) <br/>[VARIANCE()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_variance) <br/>[VAR_POP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_var-pop) <br/>[STD()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_std) <br/>[STDDEV()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev) <br/>[STDDEV_POP](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev-pop) <br/>[VAR_SAMP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_var-samp) <br/>[STDDEV_SAMP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev-samp) <br/>[JSON_ARRAYAGG(key)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_json-arrayagg) <br/>[JSON_OBJECTAGG(key, value)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_json-objectagg) |
| [Encryption and compression functions](/functions-and-operators/encryption-and-compression-functions.md#encryption-and-compression-functions) | [MD5()](/functions-and-operators/encryption-and-compression-functions.md#md5) <br/>[SHA1(), SHA()](/functions-and-operators/encryption-and-compression-functions.md#sha1) <br/>[UNCOMPRESSED_LENGTH()](/functions-and-operators/encryption-and-compression-functions.md#uncompressed_length) |
| [Cast functions and operators](/functions-and-operators/cast-functions-and-operators.md#cast-functions-and-operators) | [CAST()](/functions-and-operators/cast-functions-and-operators.md#cast) <br/>[CONVERT()](/functions-and-operators/cast-functions-and-operators.md#convert) |
| [Miscellaneous functions](/functions-and-operators/miscellaneous-functions.md#supported-functions) | [UUID()](/functions-and-operators/miscellaneous-functions.md#uuid) |
Expand Down
4 changes: 2 additions & 2 deletions functions-and-operators/window-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ In TiDB, you can control window functions using the following system variables:

The window functions [listed here](/tiflash/tiflash-supported-pushdown-calculations.md) can be pushed down to TiFlash.

Except for `GROUP_CONCAT()` and `APPROX_PERCENTILE()`, TiDB supports using all [`GROUP BY` aggregate functions](/functions-and-operators/aggregate-group-by-functions.md) as window functions. In addition, TiDB supports the following window functions:
Except for `GROUP_CONCAT()`, `APPROX_PERCENTILE()`, and `APPROX_COUNT_DISTINCT()`, TiDB supports using all [`GROUP BY` aggregate functions](/functions-and-operators/aggregate-group-by-functions.md) as window functions, including `SUM_INT()`, `MAX_COUNT()`, and `MIN_COUNT()`. In addition, TiDB supports the following window functions:

| Function name | Feature description |
| :-------------------------------- | :------------------------------------- |
Expand Down Expand Up @@ -474,4 +474,4 @@ FROM
| 31 | 11 |
+------+----------------------+
11 rows in set (0.00 sec)
```
```
2 changes: 1 addition & 1 deletion tiflash/tiflash-supported-pushdown-calculations.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ TiFlash supports the following push-down expressions:
| [JSON function](/functions-and-operators/json-functions.md) | `JSON_LENGTH()`, `->`, `->>`, `JSON_EXTRACT()`, `JSON_ARRAY()`, `JSON_DEPTH()`, `JSON_VALID()`, `JSON_KEYS()`, `JSON_CONTAINS_PATH()`, `JSON_UNQUOTE()` |
| [Vector function](/ai/reference/vector-search-functions-and-operators.md) | `VEC_L2_DISTANCE`, `VEC_COSINE_DISTANCE`, `VEC_NEGATIVE_INNER_PRODUCT`, `VEC_L1_DISTANCE`, `VEC_DIMS`, `VEC_L2_NORM`, `VEC_AS_TEXT` |
| [Conversion functions](/functions-and-operators/cast-functions-and-operators.md) | `CAST(int AS DOUBLE), CAST(int AS DECIMAL)`, `CAST(int AS STRING)`, `CAST(int AS TIME)`, `CAST(double AS INT)`, `CAST(double AS DECIMAL)`, `CAST(double AS STRING)`, `CAST(double AS TIME)`, `CAST(string AS INT)`, `CAST(string AS DOUBLE), CAST(string AS DECIMAL)`, `CAST(string AS TIME)`, `CAST(decimal AS INT)`, `CAST(decimal AS STRING)`, `CAST(decimal AS TIME)`, `CAST(decimal AS DOUBLE)`, `CAST(time AS INT)`, `CAST(time AS DECIMAL)`, `CAST(time AS STRING)`, `CAST(time AS REAL)`, `CAST(json AS JSON)`, `CAST(json AS STRING)`, `CAST(int AS JSON)`, `CAST(real AS JSON)`, `CAST(decimal AS JSON)`, `CAST(string AS JSON)`, `CAST(time AS JSON)`, `CAST(duration AS JSON)` |
| [Aggregate functions](/functions-and-operators/aggregate-group-by-functions.md) | `MIN()`, `MAX()`, `SUM()`, `COUNT()`, `AVG()`, `APPROX_COUNT_DISTINCT()`, `GROUP_CONCAT()` |
| [Aggregate functions](/functions-and-operators/aggregate-group-by-functions.md) | `MIN()`, `MAX()`, `MAX_COUNT()`, `MIN_COUNT()`, `SUM()`, `SUM_INT()`, `COUNT()`, `AVG()`, `APPROX_COUNT_DISTINCT()`, `GROUP_CONCAT()` |
| [Miscellaneous functions](/functions-and-operators/miscellaneous-functions.md) | `INET_NTOA()`, `INET_ATON()`, `INET6_NTOA()`, `INET6_ATON()` |

## Restrictions
Expand Down
Loading