Using variables in a semantic view¶
In a semantic view, you can use variables to customize the calculations performed for a fact, dimension, or metric without having to change the definition of the view.
In the definitions of facts, dimensions, and metrics, you can include a variable and assign it a default value. When you query the semantic view, you can specify values for each variable.
Defining a variable with DDL¶
To define a variable in a semantic view, use the VARIABLES clause in the CREATE SEMANTIC VIEW command:
where:
-
variable_nameis a name for the variable. The name must follow the identifier rules.Note
- You can’t use the same name as a fact, dimension, or metric.
- If you use the same name as a column in the base table, unqualified references to the name resolve to the variable, while qualified names resolve to the column.
-
data_typeis one of the supported data types. -
default_expris a supported SQL expression that determines the default value of the variable. -
descriptionis a comment that describes the variable.
You can use variables in the definitions of facts, dimensions, and metrics. You can also use variables in the WHERE clause when you
query the semantic view with SEMANTIC_VIEW(...).
Note
You can’t use variables in UNIQUE, PRIMARY KEY, or RELATIONSHIP key constraints:
You also can’t use variables in calculations that are used in key constraints:
Defining a variable with YAML¶
You can also define variables in the
YAML specification for a semantic view.
Add a top-level variables section:
For example:
You can reference these variables in expr fields for facts, dimensions, and metrics, just as you would with DDL-defined
variables.
Example: semantic view with variables¶
Suppose that you have a table containing sales data:
Suppose that you want to set up a semantic view that includes:
- A
sale_categorydimension that is set toLOWorHIGH, depending on a minimum amount that you specify. - A
weighted_salesmetric that is the sum of the sales amounts adjusted by a multiplier that you specify.
Suppose that you also want to filter the regions based on a specified region.
You can create a semantic view that defines variables for the above:
This example defines three variables:
min_amountdetermines the value of thesale_categorydimension. If the sale amount is equal to or greater than this variable, the category isHIGH.multiplierdetermines the calculation of theweighted_salesmetric. The sales amounts are multiplied by this variable before being added up.target_regionfilters results by region in the query WHERE clause. Unlike the other two variables, it isn’t referenced in a fact, dimension, or metric definition.
Both min_amount and multiplier have default values, which means that you don’t need to set these variables when
querying the semantic view. The example also shows the two supported ways of specifying a default value:
Supported data types for variables¶
You can specify one of the following data types:
- Numeric types, including NUMBER, INTEGER, FLOAT, and DOUBLE.
- String and binary types, including VARCHAR, STRING, TEXT, and BINARY.
- Date and time types, including DATE, TIME, TIMESTAMP, and variants of TIMESTAMP.
- Logical types, including BOOLEAN.
- Geospatial types, including GEOGRAPHY and GEOMETRY.
- Vector types, including VECTOR on INT and FLOAT.
Note
VARIANT, OBJECT, and ARRAY aren’t supported.
The following example defines variables of different types and calls functions to set the default values of some of the variables:
Supported expressions for variables¶
You can specify the following types of expressions for the value of a variable:
- Literal values and arithmetic expressions (for example,
10 + 20 + 2) - Calls to scalar functions and system functions (for example,
ABS(-5),UPPER('hello'),CURRENT_DATE(), andUUID_STRING()) - Conditional expressions (for example,
CASE WHEN ... THEN ... END,COALESCE(), andIFF()) - Data type conversions (for example,
TO_NUMBER('42'),TO_VARCHAR(12345), andTO_DATE('2024-01-01')) - Calls to user-defined functions
You can use a bind variable or session variable when querying a semantic view but not when defining a semantic view.
You can’t use a subquery for the value of a variable (when creating the semantic view or when querying the semantic view).
Setting a variable in a query¶
When you query the semantic view, you can set values for variables in the SEMANTIC_VIEW expression:
You can use either = or => between the variable name and value. The two operators are equivalent.
For example, to query the semantic view that you defined earlier:
This example overrides the default values of the min_amount and multiplier variables.
The example also sets the target_region variable and uses the variable in the WHERE clause of the query.
Note
If you define a variable that doesn’t have a default value and you don’t set that variable in the query, the query fails.
Limitations¶
Currently, variables have the following limitations:
- The SHOW VARIABLES command doesn’t return variables in semantic views.
- Variables can’t be used in window function frame bounds.