Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: JSON_ARRAY_AGG
title: JSON_ARRAY_AGG
title_includes: JSON_AGG
---

import FunctionDescription from '@site/src/components/FunctionDescription';
Expand Down Expand Up @@ -53,4 +54,4 @@ aggregated_b: ["abc","de","xyz"]
aggregated_c: [100,200,300]
aggregated_d: [{"k":"v"},null,"uvw",[1,2,3]]
aggregated_e: [["a","b"],[],["x","y"],["z"]]
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: GET_BY_KEYPATH
---

使用“键路径”(key path) 字符串从 `VARIANT` 中提取嵌套值:

- `GET_BY_KEYPATH` 返回 `VARIANT`。
- `GET_BY_KEYPATH_STRING` 返回 `STRING`。

键路径采用 Postgres 风格的 `{segment}` 语法,多个段之间用逗号分隔,例如 `'{user,profile,name}'`,数组索引用数字表示,如 `'{items,0}'`。

## 语法

```sql
GET_BY_KEYPATH(<variant>, <keypath>)
GET_BY_KEYPATH_STRING(<variant>, <keypath>)
```

## 返回类型

- `GET_BY_KEYPATH`:`VARIANT`
- `GET_BY_KEYPATH_STRING`:`STRING`

## 示例

```sql
SELECT GET_BY_KEYPATH(PARSE_JSON('{"user":{"name":"Ada","tags":["a","b"]}}'), '{user,name}') AS profile_name;
```

```
+--------------+
| profile_name |
+--------------+
| "Ada" |
+--------------+
```

```sql
SELECT GET_BY_KEYPATH(PARSE_JSON('[10,{"a":{"k1":[1,2,3]}}]'), '{1,a,k1}') AS inner_array;
```

```
+-------------+
| inner_array |
+-------------+
| [1,2,3] |
+-------------+
```

```sql
SELECT GET_BY_KEYPATH_STRING(PARSE_JSON('{"user":{"name":"Ada"}}'), '{user,name}') AS name_text;
```

```
+-----------+
| name_text |
+-----------+
| Ada |
+-----------+
```

```sql
SELECT GET_BY_KEYPATH_STRING(PARSE_JSON('[10,{"a":{"scores":[100,98]}}]'), '{1,a,scores,0}') AS first_score;
```

```
+-------------+
| first_score |
+-------------+
| 100 |
+-------------+
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: GET
title_includes: GET_STRING
---

通过 `index` 从包含 `ARRAY` 的 `Variant` 中提取值,或通过 `field_name` 从包含 `OBJECT` 的 `Variant` 中提取值。
Expand Down Expand Up @@ -50,4 +51,4 @@ SELECT get(parse_json('{"aa":1, "aA":2, "Aa":3}'), 'AA');
+---------------------------------------------------+
| NULL |
+---------------------------------------------------+
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ title: JSON 函数
|----------|-------------|---------|
| [GET](get) | 按索引或字段名从 JSON 提取值 | `GET('{"name":"John"}', 'name')` → `"John"` |
| [GET_IGNORE_CASE](get-ignore-case) | 以不区分大小写的方式按字段名提取值 | `GET_IGNORE_CASE('{"Name":"John"}', 'name')` → `"John"` |
| [GET_BY_KEYPATH](get-by-keypath) | 使用 `{segment}` 键路径提取嵌套值 | `GET_BY_KEYPATH('{"user":{"name":"Ada"}}', '{user,name}')` → `"Ada"` |
| [GET_PATH](get-path) | 使用路径表示法提取值 | `GET_PATH('{"user":{"name":"John"}}', 'user.name')` → `"John"` |
| [JSON_EXTRACT_PATH_TEXT](json-extract-path-text) | 使用路径从 JSON 提取文本值 | `JSON_EXTRACT_PATH_TEXT('{"name":"John"}', 'name')` → `'John'` |
| [JSON_EACH](json-each) | 将 JSON 对象展开为键值对 | `JSON_EACH('{"a":1,"b":2}')` → `[("a",1),("b",2)]` |
Expand All @@ -50,4 +51,13 @@ title: JSON 函数
|----------|-------------|---------|
| [JSON_PRETTY](json-pretty) | 以适当缩进格式化 JSON | `JSON_PRETTY('{"a":1}')` → 格式化后的 JSON 字符串 |
| [STRIP_NULL_VALUE](strip-null-value) | 从 JSON 中移除 null 值 | `STRIP_NULL_VALUE('{"a":1,"b":null}')` → `{"a":1}` |
| [JQ](jq) | 使用 jq 风格查询处理 JSON | `JQ('{"name":"John"}', '.name')` → `"John"` |
| [JQ](jq) | 使用 jq 风格查询处理 JSON | `JQ('{"name":"John"}', '.name')` → `"John"` |

## JSON 包含与键检测

| 函数 | 描述 | 示例 |
|----------|-------------|---------|
| [JSON_CONTAINS_IN_LEFT](json-contains) | 判断左侧 JSON 是否包含右侧 JSON | `JSON_CONTAINS_IN_LEFT('{"a":1,"b":2}', '{"b":2}')` → `true` |
| [JSON_EXISTS_KEY](json-exists-keys) | 检查单个键是否存在 | `JSON_EXISTS_KEY('{"a":1}', 'a')` → `true` |
| [JSON_EXISTS_ANY_KEYS](json-exists-keys) | 只要键列表中任意一个存在即返回 `true` | `JSON_EXISTS_ANY_KEYS('{"a":1}', ['x','a'])` → `true` |
| [JSON_EXISTS_ALL_KEYS](json-exists-keys) | 仅当所有键都存在时返回 `true` | `JSON_EXISTS_ALL_KEYS('{"a":1,"b":2}', ['a','b'])` → `true` |
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: JSON_CONTAINS_IN_LEFT
---

判断两个 JSON 值的包含关系:

- `JSON_CONTAINS_IN_LEFT(left, right)`:当 `left` 包含 `right`(即 `left` 是 `right` 的超集)时返回 `TRUE`。
- `JSON_CONTAINS_IN_RIGHT(left, right)`:当 `right` 包含 `left` 时返回 `TRUE`。

适用于对象和数组。

## 语法

```sql
JSON_CONTAINS_IN_LEFT(<variant_left>, <variant_right>)
JSON_CONTAINS_IN_RIGHT(<variant_left>, <variant_right>)
```

## 返回类型

`BOOLEAN`

## 示例

```sql
SELECT JSON_CONTAINS_IN_LEFT(PARSE_JSON('{"a":1,"b":{"c":2}}'),
PARSE_JSON('{"b":{"c":2}}')) AS left_contains;
```

```
+--------------+
| left_contains|
+--------------+
| true |
+--------------+
```

```sql
SELECT JSON_CONTAINS_IN_LEFT(PARSE_JSON('[1,2,3]'), PARSE_JSON('[2,3]')) AS left_contains;
```

```
+--------------+
| left_contains|
+--------------+
| true |
+--------------+
```

```sql
SELECT JSON_CONTAINS_IN_LEFT(PARSE_JSON('[1,2]'), PARSE_JSON('[2,4]')) AS left_contains;
```

```
+--------------+
| left_contains|
+--------------+
| false |
+--------------+
```

```sql
SELECT JSON_CONTAINS_IN_RIGHT(PARSE_JSON('{"a":1}'), PARSE_JSON('{"a":1,"b":2}')) AS right_contains;
```

```
+---------------+
| right_contains|
+---------------+
| true |
+---------------+
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: JSON_EXISTS_KEY
---

检测 JSON 对象中是否存在指定键:

- `JSON_EXISTS_KEY`:检查单个键。
- `JSON_EXISTS_ANY_KEYS`:传入键数组,只要其中任意一个存在即返回 `TRUE`。
- `JSON_EXISTS_ALL_KEYS`:仅当数组中的每个键都存在时返回 `TRUE`。

## 语法

```sql
JSON_EXISTS_KEY(<variant>, <key>)
JSON_EXISTS_ANY_KEYS(<variant>, <array_of_keys>)
JSON_EXISTS_ALL_KEYS(<variant>, <array_of_keys>)
```

## 返回类型

`BOOLEAN`

## 示例

```sql
SELECT JSON_EXISTS_KEY(PARSE_JSON('{"a":1,"b":2}'), 'b') AS has_b;
```

```
+-------+
| has_b |
+-------+
| true |
+-------+
```

```sql
SELECT JSON_EXISTS_ANY_KEYS(PARSE_JSON('{"a":1,"b":2}'), ['x','b']) AS any_key;
```

```
+---------+
| any_key |
+---------+
| true |
+---------+
```

```sql
SELECT JSON_EXISTS_ALL_KEYS(PARSE_JSON('{"a":1,"b":2}'), ['a','b','c']) AS all_keys;
```

```
+---------+
| all_keys|
+---------+
| false |
+---------+
```

```sql
SELECT JSON_EXISTS_ALL_KEYS(PARSE_JSON('{"a":1,"b":2}'), ['a','b']) AS all_keys;
```

```
+---------+
| all_keys|
+---------+
| true |
+---------+
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: STRIP_NULL_VALUE
title_includes: JSON_STRIP_NULLS
---
import FunctionDescription from '@site/src/components/FunctionDescription';

Expand All @@ -25,4 +26,4 @@ SELECT STRIP_NULL_VALUE(PARSE_JSON('{"name": "Alice", "age": 30, "city": null}')
strip_null_value(parse_json('{"name": "alice", "age": 30, "city": null}'))|
--------------------------------------------------------------------------+
{"age":30,"name":"Alice"} |
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: ARRAY_ANY
---

返回数组中第一个非 NULL 的元素。等价于 `ARRAY_AGGREGATE(array, 'ANY')`。

## 语法

```sql
ARRAY_ANY(<array>)
```

## 返回类型

与数组元素类型相同。

## 示例

```sql
SELECT ARRAY_ANY(['a', 'b', 'c']) AS first_item;
```

结果:

```
+------------+
| first_item |
+------------+
| a |
+------------+
```

```sql
SELECT ARRAY_ANY([NULL, 'x', 'y']) AS first_non_null;
```

结果:

```
+----------------+
| first_non_null |
+----------------+
| x |
+----------------+
```

```sql
SELECT ARRAY_ANY([NULL, 10, 20]) AS first_number;
```

结果:

```
+--------------+
| first_number |
+--------------+
| 10 |
+--------------+
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: ARRAY_APPROX_COUNT_DISTINCT
---

返回数组中不同元素数量的近似值(忽略 NULL)。实现基于与 [`APPROX_COUNT_DISTINCT`](../../07-aggregate-functions/aggregate-approx-count-distinct.md) 相同的 HyperLogLog 算法。

## 语法

```sql
ARRAY_APPROX_COUNT_DISTINCT(<array>)
```

## 返回类型

`BIGINT`

## 示例

```sql
SELECT ARRAY_APPROX_COUNT_DISTINCT([1, 1, 2, 3, 3, 3]) AS approx_cnt;
```

```
+------------+
| approx_cnt |
+------------+
| 3 |
+------------+
```

```sql
SELECT ARRAY_APPROX_COUNT_DISTINCT([NULL, 'a', 'a', 'b']) AS approx_cnt_text;
```

```
+------------------+
| approx_cnt_text |
+------------------+
| 2 |
+------------------+
```
Loading
Loading