merge into {clip_video_table_name} as target
using (select $1:id as id, $1:created_at as created_at, $1:updated_at as updated_at, $1:clip_id as clip_id, $1:video_s3_id as video_s3_id, $1:is_cover as is_cover, $1:gif_s3_id as gif_s3_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.clip_id = source.clip_id, target.video_s3_id = source.video_s3_id, target.is_cover = source.is_cover, target.gif_s3_id = source.gif_s3_id
when not matched then insert (id, created_at, updated_at, clip_id, video_s3_id, is_cover, gif_s3_id, p_date, p_hour) values (source.id, source.created_at, source.updated_at, source.clip_id, source.video_s3_id, source.is_cover, source.gif_s3_id, source.p_date, source.p_hour);

