yiguolei commented on code in PR #2721: URL: https://github.com/apache/doris-website/pull/2721#discussion_r2293420398
########## i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/array-functions/array-intersect.md: ########## @@ -5,134 +5,142 @@ } --- -## 描述 -返回一个数组,包含 array1 和 array2 的交集中的所有元素,不包含重复项,如果输入参数为 NULL,则返回 NULL +## array_intersect + +<version since="2.0.0"> + +</version> + +### 描述 + +返回多个数组的交集,即所有数组中共同存在的元素。函数会找出所有输入数组中共同存在的元素,去重后组成新数组。 + +### 语法 -## 语法 ```sql -ARRAY_INTERSECT(<arr1> , <arr2> ) +array_intersect(ARRAY<T> arr1, ARRAY<T> arr2, [ARRAY<T> arr3, ...]) ``` -## 参数 -| Parameter | Description | -|---|---| -| `<arr1>` | 源数组 | -| `<arr2>` | 与 arr1 求交集的数组 | +### 参数 + +- `arr1, arr2, arr3, ...`:ARRAY<T> 类型,要计算交集的数组。支持两个或更多数组参数。 + +**T 支持的类型:** +- 数值类型:TINYINT、SMALLINT、INT、BIGINT、LARGEINT、FLOAT、DOUBLE、DECIMAL +- 字符串类型:CHAR、VARCHAR、STRING +- 日期时间类型:DATE、DATETIME、DATEV2、DATETIMEV2 +- 布尔类型:BOOLEAN +- IP 类型:IPV4、IPV6 + +### 返回值 + +返回类型:ARRAY<T> -## 返回值 -返回包含 array1 和 array2 的交集中的所有元素的数组,特殊情况: -- 如果输入参数为 NULL,则返回 NULL +返回值含义: +- 返回一个新数组,包含所有输入数组中共同存在的唯一元素 +- 空数组,输入的所有参数数组没有共同存在的元素 -## 注意事项 +使用说明: +- 函数会找出所有输入数组中共同存在的元素, 结果数组中的元素会被去重 +- 空数组和任何非NULL数组结果都是空数组,如果没有元素重叠,该函数将返回一个空数组。 +- 函数不支持数组为 NULL +- 元素比较遵循类型兼容性规则,类型不兼容时会尝试转换,失败则为 null +- 对数组元素中的 null 值:null 元素会被视为普通元素参与运算,null 与 null 被认为是相同的 -`仅支持向量化引擎中使用` +**查询示例:** -## 举例 +两个数组的交集: +```sql +SELECT array_intersect([1, 2, 3, 4, 5], [2, 4, 6, 8]); ++------------------------------------------------+ +| array_intersect([1, 2, 3, 4, 5], [2, 4, 6, 8]) | ++------------------------------------------------+ +| [4, 2] | ++------------------------------------------------+ +``` + +多个数组的交集: +```sql +SELECT array_intersect([1, 2, 3, 4, 5], [2, 4, 6, 8], [2, 4, 10, 12]); ++----------------------------------------------------------------+ +| array_intersect([1, 2, 3, 4, 5], [2, 4, 6, 8], [2, 4, 10, 12]) | ++----------------------------------------------------------------+ +| [2, 4] | ++----------------------------------------------------------------+ +``` + +字符串数组的交集: +```sql +SELECT array_intersect(['a', 'b', 'c'], ['b', 'c', 'd']); ++--------------------------------------------+ +| array_intersect(['a','b','c'], ['b','c','d']) | ++--------------------------------------------+ +| ["b", "c"] | ++--------------------------------------------+ +``` +包含 null 的数组, null 被视为可以比较相等性的值。 ```sql -CREATE TABLE array_type_table ( - k1 INT, - k2 ARRAY<INT>, - k3 ARRAY<INT> -) -duplicate key (k1) -distributed by hash(k1) buckets 1 -properties( - 'replication_num' = '1' -); -INSERT INTO array_type_table VALUES -(1, [1, 2, 3], [2, 4, 5]), -(2, [2, 3], [1, 5]), -(3, [1, 1, 1], [2, 2, 2]); -select k1,k2,k3,array_intersect(k2,k3) from array_type_table; -``` -```text -+------+-----------------+--------------+-----------------------------+ -| k1 | k2 | k3 | array_intersect(`k2`, `k3`) | -+------+-----------------+--------------+-----------------------------+ -| 1 | [1, 2, 3] | [2, 4, 5] | [2] | -| 2 | [2, 3] | [1, 5] | [] | -| 3 | [1, 1, 1] | [2, 2, 2] | [] | -+------+-----------------+--------------+-----------------------------+ +SELECT array_intersect([1, null, 2, null, 3], [null, 2, 3, 4]); ++---------------------------------------------------------+ +| array_intersect([1, null, 2, null, 3], [null, 2, 3, 4]) | ++---------------------------------------------------------+ +| [null, 2, 3] | ++---------------------------------------------------------+ ``` + +字符串数组和整数数组的交集: Review Comment: 我们为什么要支持这个?这个让用户自己在外面cast吧,不要支持隐式的cast了 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
