From 822edbd80cd3785c76eef73f94bef0a1ba4fe151 Mon Sep 17 00:00:00 2001 From: xufei Date: Fri, 3 Jul 2026 15:27:06 +0800 Subject: [PATCH] save work Signed-off-by: xufei --- .../aggregate-group-by-functions.md | 49 ++++++++++++++++++- .../expressions-pushed-down.md | 2 +- functions-and-operators/window-functions.md | 4 +- ...tiflash-supported-pushdown-calculations.md | 2 +- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/functions-and-operators/aggregate-group-by-functions.md b/functions-and-operators/aggregate-group-by-functions.md index 8563e02c1d242..6593f6d40d0fc 100644 --- a/functions-and-operators/aggregate-group-by-functions.md +++ b/functions-and-operators/aggregate-group-by-functions.md @@ -33,6 +33,53 @@ 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`. 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 by default. 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 this function: + + ```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) + ``` + + `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 Pk (`k` represents percentage) indicates that there are at least `k%` values in the data set that are less than or equal to Pk. @@ -86,7 +133,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 diff --git a/functions-and-operators/expressions-pushed-down.md b/functions-and-operators/expressions-pushed-down.md index 3d22ef497c040..d97f4a7bbe49f 100644 --- a/functions-and-operators/expressions-pushed-down.md +++ b/functions-and-operators/expressions-pushed-down.md @@ -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)
[JSON_ARRAY()](/functions-and-operators/json-functions/json-functions-create.md#json_array)
[JSON_CONTAINS()](/functions-and-operators/json-functions/json-functions-search.md#json_contains)
[JSON_EXTRACT()](/functions-and-operators/json-functions/json-functions-search.md#json_extract)
[JSON_INSERT()](/functions-and-operators/json-functions/json-functions-modify.md#json_insert)
[JSON_LENGTH()](/functions-and-operators/json-functions/json-functions-return.md#json_length)
[JSON_MERGE_PATCH()](/functions-and-operators/json-functions/json-functions-modify.md#json_merge_patch)
[JSON_MERGE()](/functions-and-operators/json-functions/json-functions-modify.md#json_merge)
[JSON_OBJECT()](/functions-and-operators/json-functions/json-functions-create.md#json_object)
[JSON_REMOVE()](/functions-and-operators/json-functions/json-functions-modify.md#json_remove)
[JSON_REPLACE()](/functions-and-operators/json-functions/json-functions-modify.md#json_replace)
[JSON_SET()](/functions-and-operators/json-functions/json-functions-modify.md#json_set)
[JSON_TYPE()](/functions-and-operators/json-functions/json-functions-return.md#json_type)
[JSON_UNQUOTE()](/functions-and-operators/json-functions/json-functions-modify.md#json_unquote)
[JSON_VALID()](/functions-and-operators/json-functions/json-functions-return.md#json_valid)
[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)
[DATE_FORMAT()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format)
[DATEDIFF()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_datediff)
[DAYOFMONTH()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_dayofmonth)
[DAYOFWEEK()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_dayofweek)
[DAYOFYEAR()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_dayofyear)
[FROM_DAYS()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-days)
[HOUR()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_hour)
[MAKEDATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_makedate)
[MAKETIME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_maketime)
[MICROSECOND()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_microsecond)
[MINUTE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_minute)
[MONTH()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_month)
[MONTHNAME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_monthname)
[PERIOD_ADD()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_period-add)
[PERIOD_DIFF()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_period-diff)
[SEC_TO_TIME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_sec-to-time)
[SECOND()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_second)
[SYSDATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_sysdate)
[TIME_TO_SEC()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-to-sec)
[TIMEDIFF()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timediff)
[WEEK()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week)
[WEEKOFYEAR()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_weekofyear)
[YEAR()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_year)
[DATE_ADD()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add)
[DATE_SUB()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-sub)
[ADDDATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_adddate)
[SUBDATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_subdate)
[FROM_UNIXTIME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-unixtime)
[TIMESTAMPDIFF()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff)
[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)
[BIT_LENGTH()](/functions-and-operators/string-functions.md#bit_length)
[CHAR()](/functions-and-operators/string-functions.md#char)
[CHAR_LENGTH()](/functions-and-operators/string-functions.md#char_length)
[CONCAT()](/functions-and-operators/string-functions.md#concat)
[CONCAT_WS()](/functions-and-operators/string-functions.md#concat_ws)
[ELT()](/functions-and-operators/string-functions.md#elt)
[FIELD()](/functions-and-operators/string-functions.md#field)
[HEX()](/functions-and-operators/string-functions.md#hex)
[LENGTH()](/functions-and-operators/string-functions.md#length)
[LIKE](/functions-and-operators/string-functions.md#like)
[LOWER()](/functions-and-operators/string-functions.md#lower)
[LTRIM()](/functions-and-operators/string-functions.md#ltrim)
[MID()](/functions-and-operators/string-functions.md#mid)
[NOT LIKE](/functions-and-operators/string-functions.md#not-like)
[NOT REGEXP](/functions-and-operators/string-functions.md#not-regexp)
[REGEXP](/functions-and-operators/string-functions.md#regexp)
[REGEXP_LIKE()](/functions-and-operators/string-functions.md#regexp_like)
[REGEXP_REPLACE()](/functions-and-operators/string-functions.md#regexp_replace)
[REGEXP_SUBSTR()](/functions-and-operators/string-functions.md#regexp_substr)
[REPLACE()](/functions-and-operators/string-functions.md#replace)
[REVERSE()](/functions-and-operators/string-functions.md#reverse)
[RIGHT()](/functions-and-operators/string-functions.md#right), [RLIKE](/functions-and-operators/string-functions.md#rlike)
[RTRIM()](/functions-and-operators/string-functions.md#rtrim)
[SPACE()](/functions-and-operators/string-functions.md#space)
[STRCMP()](/functions-and-operators/string-functions.md#strcmp)
[SUBSTR()](/functions-and-operators/string-functions.md#substr)
[SUBSTRING()](/functions-and-operators/string-functions.md#substring)
[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)
[COUNT(DISTINCT)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_count-distinct)
[SUM()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_sum)
[AVG()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_avg)
[MAX()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_max)
[MIN()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_min)
[VARIANCE()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_variance)
[VAR_POP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_var-pop)
[STD()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_std)
[STDDEV()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev)
[STDDEV_POP](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev-pop)
[VAR_SAMP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_var-samp)
[STDDEV_SAMP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev-samp)
[JSON_ARRAYAGG(key)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_json-arrayagg)
[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)
[COUNT(DISTINCT)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_count-distinct)
[SUM()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_sum)
[`SUM_INT()`](/functions-and-operators/aggregate-group-by-functions.md)
[AVG()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_avg)
[MAX()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_max)
[MIN()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_min)
[VARIANCE()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_variance)
[VAR_POP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_var-pop)
[STD()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_std)
[STDDEV()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev)
[STDDEV_POP](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev-pop)
[VAR_SAMP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_var-samp)
[STDDEV_SAMP()](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_stddev-samp)
[JSON_ARRAYAGG(key)](https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_json-arrayagg)
[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)
[SHA1(), SHA()](/functions-and-operators/encryption-and-compression-functions.md#sha1)
[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)
[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) | diff --git a/functions-and-operators/window-functions.md b/functions-and-operators/window-functions.md index d534be8cc2999..f68b0e5a3da67 100644 --- a/functions-and-operators/window-functions.md +++ b/functions-and-operators/window-functions.md @@ -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 the TiDB-specific `SUM_INT()` function. In addition, TiDB supports the following window functions: | Function name | Feature description | | :-------------------------------- | :------------------------------------- | @@ -474,4 +474,4 @@ FROM | 31 | 11 | +------+----------------------+ 11 rows in set (0.00 sec) -``` \ No newline at end of file +``` diff --git a/tiflash/tiflash-supported-pushdown-calculations.md b/tiflash/tiflash-supported-pushdown-calculations.md index f1af09f7516f6..2a82d4b80a22d 100644 --- a/tiflash/tiflash-supported-pushdown-calculations.md +++ b/tiflash/tiflash-supported-pushdown-calculations.md @@ -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()`, `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