POLICY_CONTEXT

functionsupportedGovernance (policies, tags, data quality)

docs.snowflake.com ↗

Context functions

Simulates the results of a query based on the value of one or more context functions or SYS_CONTEXT namespace properties, which lets you determine how policies affect query results. Context functions and SYS_CONTEXT properties return a value based on the current context of a query: for example, who is executing the query, which roles are activated, or whether an agent is invoking the query. Policy bodies often use these values to determine which value to return from the policy. This function evaluates the following policies to determine the query results: - Masking policies - Row access policies - Aggregation policies - Join policies - Projection policies

Syntax

EXECUTE USING
POLICY_CONTEXT(
  <arg> => '<string_literal>'
  [ , <arg> => '<string_literal>' , ... ]
  [ , <arg> => ( '<string_literal>' [ , '<string_literal>' , ... ] ) ]
)
AS
SELECT <query>

Parameters

NameRequiredDescription
context_function => 'string_literal'requiredSpecifies a context function and its value as a string. Snowflake supports the following context functions and their values as arguments: CURRENT_USER CURRENT_ROLE CURRENT_AVAILABLE_ROLES CURRENT_ACCOUNT To determine the format to use as a string value, execute a query using the function. For example:Copy code SELECT CURRENT_USER(); +----------------+ | CURRENT_USER() | |----------------| | JSMITH | +----------------+ The string value should be 'JSMITH'. Note that if specifying CURRENT_AVAILABLE_ROLES and multiple role values, such as ROLE1 and ROLE2, enclose the list of roles in square brackets as follows: ['ROLE1', 'ROLE2']
sys_context_key => 'string_literal'requiredSpecifies a SYS_CONTEXT namespace property and its simulated value as a string. The argument name combines the namespace and property, separated by an underscore. For example, SNOWFLAKE$SESSION_ROLE corresponds to the ROLE property in the SNOWFLAKE$SESSION namespace. Snowflake supports the following SYS_CONTEXT property arguments: Argument Description SNOWFLAKE$SESSION_ROLE Simulates the value of the ROLE property in the SNOWFLAKE$SESSION namespace. SNOWFLAKE$SESSION_PRINCIPAL_NAME Simulates the value of the PRINCIPAL_NAME property in the SNOWFLAKE$SESSION namespace. SNOWFLAKE$SESSION_PRINCIPAL_TYPE Simulates the value of the PRINCIPAL_TYPE property in the SNOWFLAKE$SESSION namespace. SNOWFLAKE$SESSION_DATABASE Simulates the value of the DATABASE property in the SNOWFLAKE$SESSION namespace. SNOWFLAKE$SESSION_SCHEMA Simulates the value of the SCHEMA property in the SNOWFLAKE$SESSION namespace. SNOWFLAKE$SESSION_WAREHOUSE Simulates the value of the WAREHOUSE property in the SNOWFLAKE$SESSION namespace.ExpandShow lessSee more
sys_context_key => ( 'string_literal' [ , 'string_literal' , ... ] )requiredSpecifies a SYS_CONTEXT namespace property and a list of simulated values. Enclose the values in parentheses to form a tuple. You can also specify a single value without parentheses. Snowflake supports the following SYS_CONTEXT list arguments: Argument Description SNOWFLAKE$SESSION_ACTIVATED_ROLES Simulates the set of activated account roles in the SNOWFLAKE$SESSION namespace. SNOWFLAKE$SESSION_ACTIVATED_DATABASE_ROLES Simulates the set of activated database roles in the SNOWFLAKE$SESSION namespace. SNOWFLAKE$CURRENT_ACTIVATED_ROLES Simulates the set of activated account roles in the SNOWFLAKE$CURRENT namespace (the current execution context). SNOWFLAKE$CURRENT_ACTIVATED_DATABASE_ROLES Simulates the set of activated database roles in the SNOWFLAKE$CURRENT namespace (the current execution context).ExpandShow lessSee more
queryrequiredSpecifies the SQL expression to query one or more tables or views. Required.

Usage notes