https://slingdata.io logo
Hey!! I’m running an incremental
# sling-help
s
Hey!! I’m running an incremental pipeline with Sling from GCS CSV GZ to BigQuery, I tried to enable soft deletes in the Python wrapper using:
Copy code
sling = Sling(
    src_stream=source_path,
    src_options={
        "format": "csv",
        "header": True,
        "compression": "gzip"
    },
    
    tgt_conn="BIGQUERY",
    tgt_object=target_table,
    tgt_options={
        "use_bulk": True,
        "add_new_columns": True,
        "adjust_column_type": True,
        "delete_missing": "soft"
    },
    
    mode="incremental",
    primary_key=["vin", "source"],
    update_key="scraped_at",
    
    debug=True
)
But it doesn't work, Sling never adds _sling_deleted_at. From the docs, I see delete_missing mentioned in db to db incremental mode, but not in the file to db incremental examples. However, in reality what the pipeline is doing is creating a temp table in BigQuery and then upserting, it makes sense to have the _sling_deleted_at column in this case. I appreciate your help!
f
Hey, can you share the debug log? Also what version are you using?
I hid some info because of an NDA but the logs are exactly like that, there are more columns but I kept the pks and the update key
f
🧐
@samuelperezh you know what, it's because delete missing currently works only for database to database run types, what you're using is from file (google storage) to database. But let me see if this can be added in the next release
yeah, the problem with this is that the whole file would need to be read/downloaded again, to read only the PK values. In database source, you can just select only the PK columns. In files, you cannot do that as easily, (only parquet really). Furthermore, using incremental mode, the full data of the PK columns are selected (without using a filter) from the source database. If you're using a source file, what is the full data?
Ah from the log, I see
file stream using incremental_val
, so sling is filtering, but stills download the full files, and then filtering in-process. So the unfiltered PK values could be cached somewhere to use for
delete_missing
🤔. A bit tricky though.
s
Yeah, I was thinking about creating an external table in BigQuery and then using BQ to BQ but since the files are 20GB each, BQ doesn't allow it. In my case, the source files are a full snapshot of the data. Is there a way to achieve what I need? Even if it's like reading everything again from the temp table before upserting @flarco
@flarco this is how it could be implemented: MERGE
project.dataset.table
AS tgt USING ( SELECT * EXCEPT(_sling_loaded_at) FROM
project.dataset.table_tmp
WHERE load_date = @run_date ) AS src ON tgt.vin = src.vin AND tgt.source = src.source WHEN MATCHED THEN UPDATE SET scraped_at = GREATEST(tgt.scraped_at, src.scraped_at), /* ... other columns ... */ _sling_deleted_at = NULL WHEN NOT MATCHED BY TARGET THEN INSERT (
vin
,
source
,
scraped_at
,/*...*/,
_sling_deleted_at
) VALUES (src.vin, src.source, src.scraped_at, /*...*/, NULL) WHEN NOT MATCHED BY SOURCE THEN UPDATE SET _sling_deleted_at = CURRENT_TIMESTAMP();
f
@samuelperezh thanks, let me think about it. This could work great when assuming
project.dataset.table_tmp
has the full dataset
If you need an immediate/temporary solution, I would run this in
full-refresh
mode, into a separate table (
project.dataset.table_full
), and then run a [post-hook query](https://docs.slingdata.io/concepts/hooks/query) of what you've suggested above (merge into
project.dataset.table
), and then drop
project.dataset.table_full
. You'd just have to manually add the
_sling_deleted_at
column one time on
project.dataset.table
.
You'd need to use the
ReplicationStream
python class though, see [here](https://docs.slingdata.io/examples/sling-python#build-a-replication-dynamically), let me know if you have issues. Or use YAML :
Copy code
from sling import Replication
replication = Replication(file_path="path/to/replication.yaml")
replication.run()