{{ 
    config(
        materialized='materialized_view',
        engine='MergeTree',
        order_by='ts_code, time_id',
        unique_key=['ts_code', 'time_id'],
        partition_by=['cast(floor(time_id / 1000000) As Int32)'],
        query_settings={"join_algorithm": "'partial_merge'"}
    )
}}

Select 
    StgMinutes.ts_code As ts_code,
    DimTime.time_id As time_id,
    StgMinutes.close As close,
    StgMinutes.open As open,
    StgMinutes.high As high,
    StgMinutes.low As low,
    StgMinutes.vol As vol,
    StgMinutes.amount As amount,
    StgAdjFactor.adj_factor As adj_factor,
    now() As dt
From {{ source('finance', 'stg.tushare_minutes') }} As StgMinutes 
Inner Join {{ ref('dw.dim_time') }} As DimTime
    On StgMinutes.trade_time = formatDateTimeInJodaSyntax(full_time, 'yyyy-MM-dd HH:mm:ss')
Any Left Join {{ source('finance', 'stg.tushare_adj_factor') }} As StgAdjFactor
    On StgMinutes.ts_code = StgAdjFactor.ts_code
        And StgAdjFactor.trade_date = Cast(DimTime.date_id As String)
where 1 = 1
{% if 'dev' in target.name %}
And StgMinutes.trade_time >= formatDateTimeInJodaSyntax(today(), 'yyyy-01-01 00:00:00')
{% elif target.name == 'test' %}
And StgMinutes.trade_time >= formatDateTimeInJodaSyntax(dateAdd(Year,-1,today()), 'yyyy-01-01 00:00:00')
{% endif %}