{{ 
    config(
        materialized='incremental',
        engine='MergeTree',
        order_by='(time_id, ts_code)',
        unique_key=['time_id', 'ts_code'],
        incremental_strategy='delete+insert',
        partition_by=['toYYYYMM(toDateTime(time_id))'],
        query_settings={
            "join_algorithm": "'full_sorting_merge'", 
        }
    )
}}

Select 
    OdsMinutes.ts_code As ts_code,
    OdsMinutes.time_id As time_id,
    OdsMinutes.close As close,
    OdsMinutes.open As open,
    OdsMinutes.high As high,
    OdsMinutes.low As low,
    OdsMinutes.vol As vol,
    OdsMinutes.amount As amount,
    OdsAdjFactor.adj_factor As adj_factor,
    now() As dt
From {{ ref('ods.tushare_minutes') }} As OdsMinutes
Any Left Join {{ ref('dw.dim_time') }} As DimTime
    On OdsMinutes.time_id = DimTime.time_id
Any Left Join {{ ref('ods.tushare_adj_factor') }} As OdsAdjFactor
    On OdsMinutes.ts_code = OdsAdjFactor.ts_code
        And OdsAdjFactor.date_id = DimTime.date_id
where 1 = 1
{% if is_incremental() %}
    And OdsMinutes.time_id >= (Select toYYYYMMDDhhmmss(toStartOfDay(YYYYMMDDhhmmssToDateTime(max(`time_id`)))) From {{ this }})
{% endif %}
{% if 'dev' in target.name %}
    And OdsMinutes.time_id >= toYYYYMMDDhhmmss(dateAdd(Month,-1,today()))
{% elif target.name == 'test' %}
    And OdsMinutes.time_id >= toYYYYMMDDhhmmss(dateAdd(Year,-1,today()))
{% endif %}