MySQL 9.7.0
Source Code Documentation
sql_masking_policy.h
Go to the documentation of this file.
1/* Copyright (c) 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_MASKING_POLICY_INCLUDED
25#define SQL_MASKING_POLICY_INCLUDED
26
27#include <optional>
28#include <string>
29
30#include "lex_string.h"
31#include "my_inttypes.h"
32
33class Create_field;
34class Item;
35class Item_field;
36class THD;
37struct TABLE;
38
43};
44
45/**
46 Returns the masking policy with the given name if it can be found.
47
48 Returns an empty result if the masking policy with that name could not be
49 found. No error is raised, but the output parameter `reason` contains the
50 reason why the masking policy was not found.
51
52 @param thd Thread context
53 @param policy_name The name of the policy to look for
54 @param[out] reason If the function returns an empty value, this parameter
55 will return the reason why the policy was not found.
56 @return The policy specification if found; otherwise empty.
57*/
58std::optional<Sql_masking_policy_spec> get_masking_policy_spec(
59 THD *thd, LEX_CSTRING policy_name, std::string *reason);
60
61/**
62 Checks if the current user has the MANAGE_DATA_MASKING_POLICY privilege.
63
64 Raises an error if the privilege is missing.
65
66 @param thd Thread context
67 @retval false Privilege is granted
68 @retval true Privilege is missing (error is reported)
69*/
71
72/**
73 Drop the masking policy with the given name.
74
75 @param thd Thread context
76 @param policy_name The name of the policy to drop
77 @param if_exists True if DROP IF EXISTS, which means it is not an error
78 to call it with a policy name that does not exist.
79 @retval true If an error is raised
80 @retval false On success
81*/
82bool drop_masking_policy(THD *thd, LEX_CSTRING policy_name, bool if_exists);
83
84/**
85 Create a masking policy with the given specification.
86
87 @param thd Thread context
88 @param if_not_exists True if CREATE MASKING POLICY IF NOT EXIST, which means
89 it is not an error if the policy already exists. The existing policy is kept.
90 @param spec Masking policy specification
91 @retval false On success
92 @retval true If an error has been raised
93*/
94bool create_masking_policy(THD *thd, bool if_not_exists,
95 const Sql_masking_policy_spec &spec);
96
97/**
98 Check if the name is valid for a masking policy name or a masking policy
99 argument name. Raises an error if it is not valid.
100
101 @param name Name to validate
102 @retval false Name is valid
103 @retval true Validation failed (error is reported)
104*/
106
107/**
108 Validate structural and semantic restrictions for a masking policy expression.
109
110 Rules enforced:
111 - Must use the form `CASE WHEN <CURRENT_USER_IN|CURRENT_ROLE_IN>(...) THEN
112 <expr> ELSE <expr>`
113 - Exactly one WHEN clause and a required ELSE clause
114 - THEN/ELSE must meet generated-column-like rules (UDFs allowed)
115 - Only the policy argument may reference a column
116 - Either THEN or ELSE must return the unmasked value
117
118 @param thd Thread context
119 @param argument_name Name of the argument used in the policy
120 @param expr Expression tree of the policy
121 @retval true Validation failed (error reported)
122 @retval false Validation succeeded
123*/
124bool validate_masking_policy_syntax(THD *thd, LEX_CSTRING argument_name,
125 Item *expr);
126
127/**
128 Parse and resolve the column’s masking expression under the column’s security
129 context.
130
131 Replaces the policy argument with the actual Item_field and keeps name
132 resolution otherwise empty to prevent references to other columns.
133
134 @param thd Thread context
135 @param item_field Column reference substituted for the policy argument
136 @param spec Masking policy specification (previously fetched)
137 @return a pointer to the resolved Item on success, or nullptr on error (error
138 is reported)
139*/
141 const Sql_masking_policy_spec &spec);
142
143/**
144 Validates masking policies for CREATE/ALTER TABLE.
145
146 Performs validation in three categories and delegates details to helpers:
147 - Column eligibility for masking
148 (validate_masking_policy_column_constraints()).
149 - Masking function resolution and post-resolve validation
150 (validate_masking_function_post_resolve()).
151 - Column/function type compatibility (compatible_types()).
152
153 See the referenced helpers for detailed rules and rationale.
154
155 @param thd Thread context
156 @param buf Row buffer used to back a temporary Field instance
157 @param table Table being created/altered
158 @param field Column definition being validated
159 @retval true Validation failed (error was reported)
160 @retval false Validation succeeded
161*/
163 TABLE *table,
164 const Create_field &field);
165
166#endif // SQL_MASKING_POLICY_INCLUDED
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:51
Definition: item.h:4529
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:929
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
constexpr const LEX_CSTRING NULL_CSTR
Definition: lex_string.h:48
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
Definition: buf0block_hint.cc:30
bool validate_masking_policy_syntax(THD *thd, LEX_CSTRING argument_name, Item *expr)
Validate structural and semantic restrictions for a masking policy expression.
Definition: sql_masking_policy.cc:415
bool validate_masking_policy_for_create_alter_table(THD *thd, uchar *buf, TABLE *table, const Create_field &field)
Validates masking policies for CREATE/ALTER TABLE.
Definition: sql_masking_policy.cc:671
Item * resolve_masking_expression(THD *thd, Item_field *item_field, const Sql_masking_policy_spec &spec)
Parse and resolve the column’s masking expression under the column’s security context.
Definition: sql_masking_policy.cc:610
bool check_masking_policy_manage_privilege(THD *thd)
Checks if the current user has the MANAGE_DATA_MASKING_POLICY privilege.
Definition: sql_masking_policy.cc:225
bool check_masking_policy_name(LEX_CSTRING name)
Check if the name is valid for a masking policy name or a masking policy argument name.
Definition: sql_masking_policy.cc:324
std::optional< Sql_masking_policy_spec > get_masking_policy_spec(THD *thd, LEX_CSTRING policy_name, std::string *reason)
Returns the masking policy with the given name if it can be found.
Definition: sql_masking_policy.cc:183
bool create_masking_policy(THD *thd, bool if_not_exists, const Sql_masking_policy_spec &spec)
Create a masking policy with the given specification.
Definition: sql_masking_policy.cc:237
bool drop_masking_policy(THD *thd, LEX_CSTRING policy_name, bool if_exists)
Drop the masking policy with the given name.
Definition: sql_masking_policy.cc:147
case opt name
Definition: sslopt-case.h:29
Definition: mysql_lex_string.h:40
Definition: sql_masking_policy.h:39
LEX_CSTRING argument_name
Definition: sql_masking_policy.h:42
LEX_CSTRING masking_expression
Definition: sql_masking_policy.h:41
LEX_CSTRING policy_name
Definition: sql_masking_policy.h:40
Definition: table.h:1456