From 0959423d5b229b82385e53f08af5eb0bd0e63286 Mon Sep 17 00:00:00 2001 From: factosea <285808407@qq.com> Date: Sat, 8 Mar 2025 13:26:22 +0800 Subject: [PATCH] docs: greatest/least function desc --- .../14-reference/03-taos-sql/10-function.md | 34 +++++++++ .../14-reference/03-taos-sql/10-function.md | 34 +++++++++ tests/army/query/function/test_function.py | 73 +++++++++++++++++++ 3 files changed, 141 insertions(+) diff --git a/docs/en/14-reference/03-taos-sql/10-function.md b/docs/en/14-reference/03-taos-sql/10-function.md index 7875501e7e..7ddcea6897 100644 --- a/docs/en/14-reference/03-taos-sql/10-function.md +++ b/docs/en/14-reference/03-taos-sql/10-function.md @@ -600,6 +600,40 @@ taos> select radians(180); 3.141592653589793 | ``` +#### GREATEST +```sql +GREATEST(expr1, expr2[, expr]...) +``` + +**Function Description**: Get the maximum value of all input parameters. The minimum number of parameters for this function is 2. + +**Version**:ver-3.3.6.0 + +**Return Type**:Refer to the comparison rules. The comparison type is the final return type. + +**Applicable Data Types**: +- Numeric types: including bool, integer and floating point types +- nchar and varchar types. + +**Comparison rules**: The following rules describe the conversion method of the comparison operation: +- If any parameter is NULL, the comparison result is NULL. +- If all parameters in the comparison operation are string types, compare them as string types +- If all parameters are numeric types, compare them as numeric types. +- TIMESTAMP type is also a numeric type. When the types involved in the comparison with TIMESTAMP are all integer types, compare them as TIMESTAMP; +- If there are both string types and numeric types in the parameters, according to the `compareAsStrInGreatest` configuration item, they are uniformly compared as strings or numeric values. By default, they are compared as strings. +- In all cases, when different types are compared, the comparison type will choose the type with a larger range for comparison. For example, when comparing integer types, if there is a BIGINT type, BIGINT will definitely be selected as the comparison type. + +**Related configuration items**: Client configuration, compareAsStrInGreatest is 1, which means that both string types and numeric types are converted to string comparisons, and 0 means that they are converted to numeric types. The default is 1. + + +#### LEAST +```sql +LEAST(expr1, expr2[, expr]...) +``` + +**Function Description**:Get the minimum value of all input parameters. The arguments are compared using the same rules as for LEAST(). The rest of the description is the same as the greatest function. + + ### String Functions The input parameters for string functions are of string type, and the return results are of numeric type or string type. diff --git a/docs/zh/14-reference/03-taos-sql/10-function.md b/docs/zh/14-reference/03-taos-sql/10-function.md index ca7fb18b9f..296eab698b 100644 --- a/docs/zh/14-reference/03-taos-sql/10-function.md +++ b/docs/zh/14-reference/03-taos-sql/10-function.md @@ -578,6 +578,40 @@ taos> select truncate(8888.88, -1); 8880.000000000000000 | ``` +#### GREATEST +```sql +GREATEST(expr1, expr2[, expr]...) +``` + +**功能说明**:获得输入的所有参数中的最大值。该函数最小参数个数为 2 个。 + +**使用说明**:ver-3.3.6.0 + +**返回结果类型**:参考比较规则,比较类型即为最终返回类型。 + +**适用数据类型**: +- 数值类型:包括 bool 型,整型和浮点型 +- nchar 和 varchar 类型。 + +**比较规则**:以下规则描述了比较操作的转换方式: +- 如果有任何一个参数为 NULL,则比较结果为 NULL。 +- 如果比较操作中的所有参数都是字符串类型,按照字符串类型比较 +- 如果所有参数都是数值类型,则将它们作为数值类型进行比较。 +- TIMESTAMP 类型也是数值类型,当和 TIMESTAMP 参与比较的类型都是整数类型时,按照 TIMESTAMP 进行比较; +- 如果参数中既有字符串类型,也有数值类型,根据 compareAsStrInGreatest 配置项,统一作为字符串或者数值进行比较。默认按照字符串比较。 +- 在所有情况下,不同类型比较,比较类型会选择范围更大的类型进行比较,例如作为整数类型比较时,如果存在 BIGINT 类型,必定会选择 BIGINT 作为比较类型。 + +**相关配置项**:客户端配置,compareAsStrInGreatest 为 1 表示同时存在字符串类型和数值类型统一转为字符串比较,为 0 表示统一转为数值类型比较。默认为 1。 + + +#### LEAST +```sql +LEAST(expr1, expr2[, expr]...) +``` + +**功能说明**:获得输入的所有参数中的最小值。其余部分说明同 greatest 函数。 + + ### 字符串函数 字符串函数的输入参数为字符串类型,返回结果为数值类型或字符串类型。 diff --git a/tests/army/query/function/test_function.py b/tests/army/query/function/test_function.py index 0925a0e066..545007bd22 100644 --- a/tests/army/query/function/test_function.py +++ b/tests/army/query/function/test_function.py @@ -348,6 +348,10 @@ class TDTestCase(TBase): tdSql.checkRows(1) tdSql.checkData(0, 0, "102.000000") + tdSql.query("select GREATEST(now, 1);") + tdSql.query("select GREATEST(now, 1.0);") + tdSql.query("select GREATEST(now, '1');") + tdSql.error("select GREATEST(cast('a' as varbinary), cast('b' as varbinary), 'c', 'd');") tdSql.error("select GREATEST(6, cast('f' as varbinary), cast('b' as varbinary), 'c', 'd');") @@ -423,10 +427,78 @@ class TDTestCase(TBase): tdSql.query("select LEAST(cast(100 as float), cast(101 as varchar(20)), cast(102 as tinyint));") tdSql.checkRows(1) tdSql.checkData(0, 0, "100.000000") + + tdSql.query("select LEAST(now, 1);") + tdSql.checkRows(1) + tdSql.checkCols(1) + tdSql.checkData(0, 0, "1970-01-01 08:00:00.001") + + tdSql.query("select LEAST(now, 1.0);") + tdSql.checkRows(1) + tdSql.checkCols(1) + tdSql.checkData(0, 0, 1) + + tdSql.query("select LEAST(now, '1');") + tdSql.checkRows(1) + tdSql.checkCols(1) + tdSql.checkData(0, 0, "1") tdSql.error("select LEAST(cast('a' as varbinary), cast('b' as varbinary), 'c', 'd');") tdSql.error("select LEAST(cast('f' as varbinary), cast('b' as varbinary), 'c', 'd');") + def test_greatest_large_table(self): + tdLog.info("test greatest large table.") + + ts = 1741341251000 + create_table_sql = "CREATE TABLE `large_table` (`ts` TIMESTAMP" + for i in range(1, 1001): + if i % 5 == 1: + create_table_sql += f", `col{i}` INT" + elif i % 5 == 2: + create_table_sql += f", `col{i}` FLOAT" + elif i % 5 == 3: + create_table_sql += f", `col{i}` DOUBLE" + elif i % 5 == 4: + create_table_sql += f", `col{i}` VARCHAR(64)" + else: + create_table_sql += f", `col{i}` NCHAR(50)" + create_table_sql += ");" + tdSql.execute(create_table_sql) + + for j in range(1000): + insert_sql = f"INSERT INTO `large_table` VALUES ({ts +j}" + for i in range(1, 1001): + if i % 5 == 1: + insert_sql += f", {j + i}" + elif i % 5 == 2: + insert_sql += f", {j + i}.1" + elif i % 5 == 3: + insert_sql += f", {j + i}.2" + elif i % 5 == 4: + insert_sql += f", '{j + i}'" + else: + insert_sql += f", '{j + i}'" + insert_sql += ");" + tdSql.execute(insert_sql) + + greatest_query = "SELECT GREATEST(" + for i in range(1, 1001): + greatest_query += f"`col{i}`" + if i < 1000: + greatest_query += ", " + greatest_query += ") FROM `large_table` LIMIT 1;" + tdLog.info(f"greatest_query: {greatest_query}") + tdSql.execute(greatest_query) + + greatest_query = "SELECT " + for i in range(1, 1001): + greatest_query += f"`col{i}` > `col5`" + if i < 1000: + greatest_query += ", " + greatest_query += " FROM `large_table` LIMIT 1;" + tdLog.info(f"greatest_query: {greatest_query}") + tdSql.execute(greatest_query) + def run(self): tdLog.debug(f"start to excute {__file__}") @@ -445,6 +517,7 @@ class TDTestCase(TBase): self.test_rand() self.test_greatest() self.test_least() + self.test_greatest_large_table() # char function self.test_char_length()