From b13bf00d774f951a2e9628ff23e5030cc0d8352a Mon Sep 17 00:00:00 2001 From: drethereum <71602799+drethereum@users.noreply.github.com> Date: Fri, 28 Feb 2025 18:45:38 -0700 Subject: [PATCH] fix/dml-dupe (#197) qualify --- models/silver/core/silver__decoded_logs.sql | 66 ++++++++++++--------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/models/silver/core/silver__decoded_logs.sql b/models/silver/core/silver__decoded_logs.sql index a552960..7058bef 100644 --- a/models/silver/core/silver__decoded_logs.sql +++ b/models/silver/core/silver__decoded_logs.sql @@ -175,35 +175,36 @@ missing_data AS ( t.is_pending AND l.block_timestamp IS NOT NULL ) -{% endif %} -SELECT - tx_hash, - block_number, - event_index, - event_name, - contract_address, - decoded_data, - transformed, - _log_id, - _inserted_timestamp, - decoded_flat, - block_timestamp, - origin_function_signature, - origin_from_address, - origin_to_address, - topics, - DATA, - event_removed, - tx_status, - is_pending, - {{ dbt_utils.generate_surrogate_key( - ['tx_hash', 'event_index'] - ) }} AS decoded_logs_id, - SYSDATE() AS inserted_timestamp, - SYSDATE() AS modified_timestamp, - '{{ invocation_id }}' AS _invocation_id -FROM - new_records +{% endif %}, +complete_data AS ( + SELECT + tx_hash, + block_number, + event_index, + event_name, + contract_address, + decoded_data, + transformed, + _log_id, + _inserted_timestamp, + decoded_flat, + block_timestamp, + origin_function_signature, + origin_from_address, + origin_to_address, + topics, + DATA, + event_removed, + tx_status, + is_pending, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash', 'event_index'] + ) }} AS decoded_logs_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id + FROM + new_records {% if is_incremental() %} UNION @@ -236,3 +237,10 @@ SELECT FROM missing_data {% endif %} +) +SELECT + * +FROM + complete_data qualify(ROW_NUMBER() over (PARTITION BY block_number, event_index +ORDER BY + _inserted_timestamp DESC, is_pending ASC)) = 1 \ No newline at end of file