/* Table: {{ table_name }} */
{%- if parent %}
/* Parent: { parent }} */
{%- endif %}
{%- if description %}
/* Description: {{description }} */
{%- endif %}
{{ "{{" }}
    config(
        materialized='view'
    )
{{ "}}" }}

{%- if enable_load_id_incremental %}
-- depends_on: {{ "{{" }} ref('{{ active_table_name }}') {{ "}}" }}
{%- endif %}
{%- if parent_stage_name %}
-- depends_on: {{ "{{" }} ref('{{ parent_stage_name }}') {{ "}}" }}
{%- endif %}

SELECT
/* select which columns will be available for table '{{ table_name }}' */
{%- for c in columns %}
    {{ c }},
{%- endfor %}
FROM {{ "{{" }} source('raw_data', '{{ table_name }}') {{ "}}" }}
{% if parent_stage_name %}
/* we only load table items if the parent table has the parent item */
WHERE {{ c_dlt_parent_id }} IN ( SELECT {{ c_dlt_id }} FROM {{ "{{" }} ref('{{ parent_stage_name }}') {{ "}}" }} )
{% elif is_data_table and enable_load_id_incremental %}
/* we only load table items of the currently active load ids into the staging table */
WHERE {{ c_dlt_load_id }} IN ( SELECT {{ c_load_id }} FROM  {{ "{{" }} ref('{{ active_table_name }}') {{ "}}" }} )
{%- endif %}
