Create Semantic View
Syntax
The definition syntax for each clause is as follows:
Logical Table Definition
Filter Definition
Dimension Definition
Metric Definition
Parameter Descriptions
Logical Table Parameters
| Parameter | Description |
|---|---|
<table_alias> AS <schema_name>.<physical_table_name> | Assign a logical alias to a physical table; subsequent dimension, metric, and foreign key definitions use this alias |
PRIMARY KEY ( <column_name> ) | Specifies the primary key column, used to determine relationship types between tables |
FOREIGN KEY ( <column_name> ) REFERENCES <alias> [ ( <referenced_column_name> ) ] | Defines a foreign key relationship. When the foreign key column name differs from the referenced table's primary key column name, the referenced column name must be specified explicitly. The foreign key column and referenced column must have the same data type, otherwise creation will fail |
WITH SYNONYMS ( '<synonym>' ) | Defines synonyms for the logical table to enhance discoverability |
COMMENT = '<description>' | Description of the logical table |
Dimension Parameters
| Parameter | Description |
|---|---|
is_unique = true | Indicates that dimension values are unique, e.g., customer name |
is_time = true | Indicates that the dimension is time-based, e.g., order date |
enum_values = [...] | Constrains the allowed value range for the dimension |
WITH SYNONYMS = (...) | Defines synonyms for the dimension, supporting multiple business terms referencing the same dimension |
Metric Aggregation Functions
Supported: COUNT, AVG, SUM, MIN, MAX, and conditional aggregations such as COUNT(CASE WHEN col = value THEN 1 END)
Not supported (do not error at creation but produce incorrect results or compiler errors at query time; do not use):
- Arithmetic expression metrics:
MAX(col) - MIN(col),SUM(col) / COUNT(col), etc. -- queries only return the value of the first operand - Window function metrics:
RANK() OVER (...)-- abnormal behavior at query time - Derived metrics (metrics referencing other metrics):
metric_a / metric_b-- reportscannot resolve columnat creation time
For composite metrics such as per-capita values or ratios, calculate them in the outer SQL of the semantic_view() query:
Usage Notes
- The
TABLESclause is mandatory and cannot be omitted.DIMENSIONSandMETRICSare optional. - Logical tables referenced by foreign keys must be defined in the
TABLESclause before the referencing tables. - Creating a semantic view that already exists will error. It is recommended to first execute
DROP SEMANTIC VIEW IF EXISTS. - Dimensions support expressions (computed dimensions), such as
YEAR(hire_date). Query results return integer types. - Dimension and metric naming supports both qualified names (
alias.name) and short names (name). When names are unique, the two forms are equivalent. - Named filters in the
FILTERSclause are semantic annotations for the AI/metadata layer and cannot be passed directly to thesemantic_view()function. Filtering must be done via the outerWHEREclause.
Examples
Basic Example: Single-table Semantic View
Notes:
deptsandempsare logical table aliases; the physical tables aredoc_test.departmentsanddoc_test.employeesrespectivelyFOREIGN KEY (dept) REFERENCES depts (dept_name):emps.dept(string) referencesdepts.dept_name(string), types matchhire_yearis a computed dimension derived from the date column via theYEAR()expression
Semantic View with Filters and Dimension Metadata
The following example demonstrates the full usage of FILTERS, is_unique, is_time, and enum_values:
