I like using Snowpipe a lot but I have no way to apply my upsert logic when using it.
This is how my upsert logic looks like:
create temp table temp_table (like target);
copy into temp_table from @snowflake_stage;
begin transaction;
delete from target using temp_table
where target.pk = temp_table.pk;
insert into target
select * from temp_table;
end transaction;
drop table temp_table;
However with Snowpipe I am only allowed to define a single copy command, hence I am not able to execute a sequence of commands.
I thought about using Tasks & Streams but Tasks do not seem to support transactions (more than a single query per task). I also thought about using MERGE but I have to explicitly define the columns I want to INSERT.
For example, I can't do things like (insert without defining what to insert):
merge into src using temp_table on src.pk = temp_table.pk
when not matched then insert;
Are there any other ways I could upsert my data while still using Snowpipe?
Refer to the usage notes of the following
https://docs.snowflake.com/en/sql-reference/sql/create-pipe.html
The use case for Snowpipes is low latency, small file size, frequent loading of data into Snowflake. Its source is a supported file in external or internal stage and its target is a Snowflake table. The idea is that the data will need minimal transformation during load which is supported by the COPY statement. In other words, you cannot run a MERGE INTO at ingestion time, that is not its use case. Once the data has landed as a Snowflake table then you're free to run Tasks on top of that data at a frequency of your choosing. Google "Snowflake Data Pipelines" and it will highlight the Stream concept (Snowflake's CDC) that ingests new data on the landed content. The awesome thing about Streams & Tasks is that you can have as many as you want on top of a single table, share or (recently add in private preview) views. And also note the modes of Streams, UPSERTS v APPEND_ONLY. Then process the data how you want.
A few things to think about though,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With