merge into {playlist_table_name} as target
using 
(
    select 
        $1:id as id,
        $1:created_at as created_at,
        $1:updated_at as updated_at,
        $1:name as name,
        $1:description as description,
        $1:user_id as user_id,
        $1:is_public as is_public,
        $1:is_discover_playlist as is_discover_playlist,
        $1:is_trashed as is_trashed,
        $1:dislike_count as dislike_count,
        $1:flag_count as flag_count,
        $1:is_public_approved as is_public_approved,
        $1:play_count as play_count,
        $1:skip_count as skip_count,
        $1:upvote_count as upvote_count,
        $1:created_session_id as created_session_id,
        date(to_timestamp(created_at)) as p_date,
        hour(to_timestamp(created_at)) as p_hour
    from {stage_path} (file_format => 'BASIC_PARQUET_LOADER', pattern => ('.*.parquet'))
) as source
on (target.id = source.id and target.p_date = source.p_date and target.p_hour = source.p_hour)
when matched then
    update set 
        target.created_at = source.created_at,
        target.updated_at = source.updated_at,
        target.name = source.name,
        target.description = source.description,
        target.user_id = source.user_id,
        target.is_public = source.is_public,
        target.is_discover_playlist = source.is_discover_playlist,
        target.is_trashed = source.is_trashed,
        target.dislike_count = source.dislike_count,
        target.flag_count = source.flag_count,
        target.is_public_approved = source.is_public_approved,
        target.play_count = source.play_count,
        target.skip_count = source.skip_count,
        target.upvote_count = source.upvote_count,
        target.created_session_id = source.created_session_id,
        target.p_date = source.p_date,
        target.p_hour = source.p_hour
when not matched then
    insert (id, created_at, updated_at, name, description, user_id, is_public, is_discover_playlist, is_trashed, dislike_count, flag_count, is_public_approved, play_count, skip_count, upvote_count, created_session_id, p_date, p_hour)
    values (source.id, source.created_at, source.updated_at, source.name, source.description, source.user_id, source.is_public, source.is_discover_playlist, source.is_trashed, source.dislike_count, source.flag_count, source.is_public_approved, source.play_count, source.skip_count, source.upvote_count, source.created_session_id, source.p_date, source.p_hour);
