Skip to content

Commit 472ed21

Browse files
authored
docs: clarify column masking policy usage (#2960)
1 parent d0ef4cf commit 472ed21

File tree

4 files changed

+90
-71
lines changed

4 files changed

+90
-71
lines changed

docs/cn/sql-reference/10-sql-commands/00-ddl/01-table/90-alter-table-column.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ DROP [ COLUMN ] <column_name>
6565
- 尚不支持使用 ALTER TABLE 添加存储计算列。
6666
- 当更改表列的数据类型(Data Type)时,存在转换错误的风险。例如,如果尝试将文本(String)列转换为数字(Float)列,可能会引发问题。
6767
- 当为列设置脱敏策略(Masking Policy)时,请确保策略中定义的数据类型(请参考 [CREATE MASKING POLICY](../12-mask-policy/create-mask-policy.md) 语法中的 *arg_type_to_mask* 参数)与列的数据类型相匹配。
68+
- 如果策略需要额外的列,可结合 `USING` 子句使用,按照参数顺序列出对应的列;第一个参数始终代表正在脱敏的列。
69+
- 当声明了 `USING (...)` 时,必须至少提供被脱敏的列以及策略所需的其他列,并确保 `USING` 中的第一个标识符与正在修改的列一致。
70+
- 只有常规表支持绑定脱敏策略;视图、流表以及临时表均无法执行 `SET MASKING POLICY`
71+
- 单个列最多只能附加一个安全策略(无论是列脱敏还是行级策略)。在重新绑定之前,请先移除原有策略。
72+
:::
73+
74+
:::caution
75+
如果列已绑定脱敏策略,修改列定义或删除该列前必须先执行 `ALTER TABLE ... MODIFY COLUMN <col> UNSET MASKING POLICY`,否则操作会因安全策略仍然生效而失败。
6876
:::
6977

7078
## 示例
@@ -280,4 +288,4 @@ id|email |
280288
--+---------+
281289
2|*********|
282290
1|*********|
283-
```
291+
```

docs/cn/sql-reference/10-sql-commands/00-ddl/12-mask-policy/create-mask-policy.md

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,23 @@ CREATE [ OR REPLACE ] MASKING POLICY [ IF NOT EXISTS ] <policy_name> AS
2222
[ COMMENT = '<comment>' ]
2323
```
2424

25-
| 参数 | 描述 |
26-
|------------------------ |--------------------------------------------------------------------------------------------------------------------------------------- |
27-
| policy_name | 要创建的脱敏策略的名称。 |
28-
| arg_name_to_mask | 需要脱敏的原始数据参数的名称。 |
29-
| arg_type_to_mask | 要脱敏的原始数据参数的数据类型。 |
30-
| expression_on_arg_name | 一个表达式,用于确定如何处理原始数据以生成脱敏数据。 |
31-
| comment | 提供有关脱敏策略信息或说明的可选注释。 |
25+
| 参数 | 描述 |
26+
|------|------|
27+
| `policy_name` | 要创建的脱敏策略名称。 |
28+
| `arg_name_to_mask` | 表示被脱敏列的参数。该参数必须放在第一位,并自动绑定到 `SET MASKING POLICY` 中指向的列。 |
29+
| `arg_type_to_mask` | 被脱敏列的数据类型,必须与实际列一致。 |
30+
| `arg_1 ... arg_n` | 策略逻辑需要的可选额外列参数。绑定策略时,通过 `USING` 子句提供这些列。 |
31+
| `arg_type_1 ... arg_type_n` | 每个额外参数对应的数据类型,需要与 `USING` 子句中的列匹配。 |
32+
| `expression_on_arg_name` | 描述如何处理输入列以生成脱敏结果的表达式。 |
33+
| `comment` | 可选注释,用于补充策略说明。 |
3234

3335
:::note
34-
确保 *arg_type_to_mask* 与将应用脱敏策略的列的数据类型匹配。
36+
确保 *arg_type_to_mask* 与将应用脱敏策略的列的数据类型匹配。当策略包含多个参数时,必须在 `ALTER TABLE ... SET MASKING POLICY``USING` 子句中按相同顺序列出对应列。
3537
:::
3638

3739
## 示例
3840

39-
此示例演示了设置脱敏策略以根据用户角色选择性地显示或脱敏敏感数据的过程
41+
此示例演示了如何结合 `USING` 子句引用额外列,根据用户角色或其他列的值选择性地显示或脱敏敏感数据
4042

4143
```sql
4244
-- 创建表并插入示例数据
@@ -57,37 +59,33 @@ GRANT ALL ON *.* TO ROLE 'MANAGERS';
5759
CREATE USER manager_user IDENTIFIED BY 'databend';
5860
GRANT ROLE 'MANAGERS' TO 'manager_user';
5961

60-
-- 创建脱敏策略
61-
CREATE MASKING POLICY email_mask
62+
-- 创建需要额外列参与判断的脱敏策略
63+
CREATE MASKING POLICY contact_mask
6264
AS
63-
(val nullable(string))
65+
(contact_val nullable(string), phone_ref nullable(string))
6466
RETURNS nullable(string) ->
6567
CASE
66-
WHEN current_role() IN ('MANAGERS') THEN
67-
val
68-
ELSE
69-
'*********'
68+
WHEN current_role() IN ('MANAGERS') THEN contact_val
69+
WHEN phone_ref LIKE '91%' THEN contact_val
70+
ELSE '*********'
7071
END
71-
COMMENT = 'hide_email';
72+
COMMENT = 'mask contact data with phone check';
7273

73-
CREATE MASKING POLICY phone_mask AS (val nullable(string)) RETURNS nullable(string) -> CASE
74-
WHEN current_role() IN ('MANAGERS') THEN val
75-
ELSE '*********'
76-
END COMMENT = 'hide_phone';
74+
-- 将脱敏策略与 'email' 列关联,并通过 USING 传入额外列
75+
ALTER TABLE user_info
76+
MODIFY COLUMN email SET MASKING POLICY contact_mask USING (email, phone);
7777

78-
-- 将脱敏策略与 'email' 列关联
79-
ALTER TABLE user_info MODIFY COLUMN email SET MASKING POLICY email_mask;
80-
81-
-- 将脱敏策略与 'phone' 列关联
82-
ALTER TABLE user_info MODIFY COLUMN phone SET MASKING POLICY phone_mask;
78+
-- 将脱敏策略与 'phone' 列关联(此处同样传递自身和参考列)
79+
ALTER TABLE user_info
80+
MODIFY COLUMN phone SET MASKING POLICY contact_mask USING (phone, phone);
8381

8482
-- 使用 Root 用户查询
85-
SELECT * FROM user_info;
83+
SELECT user_id, phone, email FROM user_info ORDER BY user_id;
8684

8785
user_id │ phone │ email │
8886
Nullable(Int32) │ Nullable(String) │ Nullable(String) │
8987
─────────────────┼──────────────────┼──────────────────┤
88+
191234567 │ sue@example.com
9089
2******************
91-
1******************
9290

93-
```
91+
```

docs/en/sql-reference/10-sql-commands/00-ddl/01-table/90-alter-table-column.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ MODIFY [ COLUMN ] <column_name> [ COMMENT '<comment>' ]
5050
-- Set / Unset masking policy for a column
5151
ALTER TABLE [ IF EXISTS ] [ <database_name>. ]<table_name>
5252
MODIFY [ COLUMN ] <column_name> SET MASKING POLICY <policy_name>
53+
[ USING ( <column_reference> [ , <column_reference> ... ] ) ]
5354

5455
ALTER TABLE [ IF EXISTS ] [ <database_name>. ]<table_name>
5556
MODIFY [ COLUMN ] <column_name> UNSET MASKING POLICY
@@ -64,6 +65,14 @@ DROP [ COLUMN ] <column_name>
6465
- Adding a stored computed column with ALTER TABLE is not supported yet.
6566
- When you change the data type of a table's columns, there's a risk of conversion errors. For example, if you try to convert a column with text (String) to numbers (Float), it might cause problems.
6667
- When you set a masking policy for a column, make sure that the data type (refer to the parameter *arg_type_to_mask* in the syntax of [CREATE MASKING POLICY](../12-mask-policy/create-mask-policy.md)) defined in the policy matches the column.
68+
- Use the optional `USING` clause when the policy definition expects additional parameters. List the column mapped to each policy argument in order; the first argument always represents the column being masked.
69+
- If you include `USING`, provide at least the masked column plus any additional columns needed by the policy. The first identifier in `USING (...)` must match the column being modified.
70+
- Masking policies can only be attached to regular tables. Views, streams, and temporary tables do not allow `SET MASKING POLICY`.
71+
- A column can belong to at most one security policy (masking or row-level). Remove the existing policy before attaching a new one.
72+
:::
73+
74+
:::caution
75+
You must `ALTER TABLE ... MODIFY COLUMN <col> UNSET MASKING POLICY` before changing the column definition or dropping the column; otherwise the statement fails because the column is still protected by a security policy.
6776
:::
6877

6978
## Examples
@@ -234,19 +243,20 @@ SHOW CREATE TABLE students_info;
234243
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
235244
```
236245

237-
### Example 5: Setting Masking Policy for a Column
246+
### Example 5: Setting a Masking Policy with the USING Clause
238247

239-
This example illustrates the process of setting up a masking policy to selectively reveal or mask sensitive data based on user roles.
248+
This example illustrates how to set up a masking policy that references additional columns by using the `USING` clause.
240249

241250
```sql
242251
-- Create a table and insert sample data
243252
CREATE TABLE user_info (
244253
id INT,
254+
phone STRING,
245255
email STRING
246256
);
247257

248-
INSERT INTO user_info (id, email) VALUES (1, '[email protected]');
249-
INSERT INTO user_info (id, email) VALUES (2, '[email protected]');
258+
INSERT INTO user_info (id, phone, email) VALUES (1, '91234567', '[email protected]');
259+
INSERT INTO user_info (id, phone, email) VALUES (2, '81234567', '[email protected]');
250260

251261
-- Create a role
252262
CREATE ROLE 'MANAGERS';
@@ -256,27 +266,28 @@ GRANT ALL ON *.* TO ROLE 'MANAGERS';
256266
CREATE USER manager_user IDENTIFIED BY 'databend';
257267
GRANT ROLE 'MANAGERS' TO 'manager_user';
258268

259-
-- Create a masking policy
269+
-- Create a masking policy that inspects another column
260270
CREATE MASKING POLICY email_mask
261271
AS
262-
(val string)
263-
RETURNS string ->
272+
(email_val nullable(string), phone_ref nullable(string))
273+
RETURNS nullable(string) ->
264274
CASE
265-
WHEN current_role() IN ('MANAGERS') THEN
266-
val
267-
ELSE
268-
'*********'
275+
WHEN current_role() IN ('MANAGERS') THEN email_val
276+
WHEN phone_ref LIKE '91%' THEN email_val
277+
ELSE '*********'
269278
END
270-
COMMENT = 'hide_email';
279+
COMMENT = 'hide_email_when_phone_is_masked';
271280

272-
-- Associate the masking policy with the 'email' column
273-
ALTER TABLE user_info MODIFY COLUMN email SET MASKING POLICY email_mask;
281+
-- Associate the masking policy with the 'email' column and provide
282+
-- the additional column required by the policy by using USING(...)
283+
ALTER TABLE user_info
284+
MODIFY COLUMN email SET MASKING POLICY email_mask USING (email, phone);
274285

275286
-- Query with the Root user
276-
SELECT * FROM user_info;
287+
SELECT id, phone, email FROM user_info ORDER BY id;
277288

278-
id|email |
279-
--+---------+
280-
2|*********|
281-
1|*********|
289+
id|phone |email |
290+
--+--------+---------------+
291+
1|91234567|sue@example.com|
292+
2|81234567|********* |
282293
```

docs/en/sql-reference/10-sql-commands/00-ddl/12-mask-policy/create-mask-policy.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ CREATE [ OR REPLACE ] MASKING POLICY [ IF NOT EXISTS ] <policy_name> AS
2222
[ COMMENT = '<comment>' ]
2323
```
2424

25-
| Parameter | Description |
26-
|------------------------ |--------------------------------------------------------------------------------------------------------------------------------------- |
27-
| policy_name | The name of the masking policy to be created. |
28-
| arg_name_to_mask | The name of the original data parameter that needs to be masked. |
29-
| arg_type_to_mask | The data type of the original data parameter to be masked. |
30-
| expression_on_arg_name | An expression that determines how the original data should be treated to generate the masked data. |
31-
| comment | An optional comment providing information or notes about the masking policy. |
25+
| Parameter | Description |
26+
|------------------------|-------------|
27+
| `policy_name` | Name of the masking policy to be created. |
28+
| `arg_name_to_mask` | Parameter that represents the column being masked. This argument must appear first and automatically binds to the column referenced in `SET MASKING POLICY`. |
29+
| `arg_type_to_mask` | Data type of the masked column. It must match the data type of the column where the policy is applied. |
30+
| `arg_1 ... arg_n` | Optional extra parameters for additional columns that the policy logic depends on. Provide these columns through the `USING` clause when you attach the policy. |
31+
| `arg_type_1 ... arg_type_n` | Data types for each optional parameter. They must match the columns listed in the `USING` clause. |
32+
| `expression_on_arg_name` | Expression that determines how the input columns should be treated to generate the masked data. |
33+
| `comment` | Optional comment that stores notes about the masking policy. |
3234

3335
:::note
34-
Ensure that *arg_type_to_mask* matches the data type of the column where the masking policy will be applied.
36+
Ensure that *arg_type_to_mask* matches the data type of the column where the masking policy will be applied. When your policy defines multiple parameters, list each referenced column in the same order within the `USING` clause of `ALTER TABLE ... SET MASKING POLICY`.
3537
:::
3638

3739
## Examples
@@ -57,37 +59,37 @@ GRANT ALL ON *.* TO ROLE 'MANAGERS';
5759
CREATE USER manager_user IDENTIFIED BY 'databend';
5860
GRANT ROLE 'MANAGERS' TO 'manager_user';
5961

60-
-- Create a masking policy
61-
CREATE MASKING POLICY email_mask
62+
-- Create a masking policy that expects an extra column
63+
CREATE MASKING POLICY contact_mask
6264
AS
63-
(val nullable(string))
65+
(contact_val nullable(string), phone_ref nullable(string))
6466
RETURNS nullable(string) ->
6567
CASE
6668
WHEN current_role() IN ('MANAGERS') THEN
67-
val
69+
contact_val
70+
WHEN phone_ref LIKE '91%'
71+
THEN
72+
contact_val
6873
ELSE
6974
'*********'
7075
END
71-
COMMENT = 'hide_email';
72-
73-
CREATE MASKING POLICY phone_mask AS (val nullable(string)) RETURNS nullable(string) -> CASE
74-
WHEN current_role() IN ('MANAGERS') THEN val
75-
ELSE '*********'
76-
END COMMENT = 'hide_phone';
76+
COMMENT = 'mask contact data with phone check';
7777

7878
-- Associate the masking policy with the 'email' column
79-
ALTER TABLE user_info MODIFY COLUMN email SET MASKING POLICY email_mask;
79+
ALTER TABLE user_info
80+
MODIFY COLUMN email SET MASKING POLICY contact_mask USING (email, phone);
8081

8182
-- Associate the masking policy with the 'phone' column
82-
ALTER TABLE user_info MODIFY COLUMN phone SET MASKING POLICY phone_mask;
83+
ALTER TABLE user_info
84+
MODIFY COLUMN phone SET MASKING POLICY contact_mask USING (phone, phone);
8385

8486
-- Query with the Root user
85-
SELECT * FROM user_info;
87+
SELECT user_id, phone, email FROM user_info ORDER BY user_id;
8688

8789
user_id │ phone │ email │
8890
Nullable(Int32) │ Nullable(String) │ Nullable(String) │
8991
─────────────────┼──────────────────┼──────────────────┤
92+
191234567 │ sue@example.com
9093
2******************
91-
1******************
9294

9395
```

0 commit comments

Comments
 (0)