# VIEW

Config path: `/<database>/<schema>/view/<name>.yaml`

Example:

```yaml
columns:
  aircraft_code: "Aircraft code, IATA"
  model: "Aircraft model"
  range: "Maximal flying distance, km"

text: |-
  SELECT ml.aircraft_code,
      lang(ml.model) AS model,
      ml.range
  FROM aircrafts_data ml

comment: >-
  Each aircraft model is identified by its three-digit code (aircraft_code).
  The view also includes the name of the aircraft model (model) and the maximal flying distance, in kilometers (range).
```

## Schema

* **columns** (dict)
  * *{key}* (ident) - column name
  * *{value}* (str) - column comment
* <mark style="background-color:red;">**text**</mark> (str) - view text
* **is\_secure** (bool) - is view secure
* **change\_tracking** (bool) - enable change tracking for this VIEW and underlying tables
* **depends\_on** (list)
  * *{items}* (ident) - names of other views which this view depends on
* **comment** (str)

## Policy reference parameters

* **aggregation\_policy** (dict)
  * **policy\_name** (ident) - name of [AGGREGATION POLICY](https://docs.snowddl.com/basic/yaml-configs/aggregation-policy)
  * **columns** (list)
    * *{items}* (ident) - optional reference column names defining "entity"
* **join\_policy** (dict)
  * **policy\_name** (ident) - name of [JOIN POLICY](https://docs.snowddl.com/basic/yaml-configs/join-policy)
  * **columns** (list)
    * *{items}* (ident) - optional allowed join keys
* **masking\_policies** (list)
  * *{items}* (dict)
    * **policy\_name** (ident) - name of [MASKING POLICY](https://docs.snowddl.com/basic/yaml-configs/masking-policy)
    * **columns** (list)
      * *{items}* (ident) - reference column names
* **projection\_policies** (list)
  * *{items}* (dict)
    * **policy\_name** (ident) - name of [PROJECTION POLICY](https://docs.snowddl.com/basic/yaml-configs/projection-policy)
    * **column** (ident) - reference column name
* **row\_access\_policy** (dict)
  * **policy\_name** (ident) - name of [ROW ACCESS POLICY](https://docs.snowddl.com/basic/yaml-configs/row-access-policy)
  * **columns** (list)
    * *{items}* (ident) - reference column names

## Usage notes

1. Invalid views will be re-created automatically, even if view definition remains exactly the same.
2. If you want to access objects from another database in VIEW definition, and if you want to preserve [env prefix](https://docs.snowddl.com/guides/other-guides/env-prefix) support for such views, please use the `env_prefix` [placeholder](https://docs.snowddl.com/basic/yaml-placeholders).\
   \
   For example: `${{ env_prefix }}db_name.schema_name.object_name`.\
   \
   You may access objects in the same database by omitting database name altogether.
3. You may use [custom YAML tag](https://docs.snowddl.com/basic/yaml-tag-include) `!include` to store view SQL text in a separate file instead of storing it inside YAML.

## Links

* [CREATE VIEW](https://docs.snowflake.com/en/sql-reference/sql/create-view.html)
* [SHOW VIEWS](https://docs.snowflake.com/en/sql-reference/sql/show-views.html)
* [DESC VIEW](https://docs.snowflake.com/en/sql-reference/sql/desc-view.html)
* [Parser & JSON Schema (GitHub)](https://github.com/littleK0i/SnowDDL/blob/master/snowddl/parser/view.py)
