Skip to content

Commit 21b93da

Browse files
authored
perf(chainlink_polygon_vrf_request_fulfilled_daily): push incremental time bounds below the aggregation (#9766)
* perf(chainlink_polygon_vrf_request_fulfilled_daily): push incremental time bounds below the aggregation * materialize v1 request logs; drop the 7-day lookback assumption * Address review: align schema tests with vrf_v1_random_request_logs merge key unique_combination_of_columns was on (blockchain, block_number, tx_hash, index) but the model's incremental unique_key is [tx_hash, index]. Drop the extra columns and add not_null tests on tx_hash and index.
1 parent 5819978 commit 21b93da

3 files changed

Lines changed: 74 additions & 9 deletions

File tree

dbt_subprojects/daily_spellbook/models/chainlink/polygon/chainlink_polygon_schema.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,6 @@ models:
918918
- dbt_utils.unique_combination_of_columns:
919919
arguments:
920920
combination_of_columns:
921-
- blockchain
922-
- block_number
923921
- tx_hash
924922
- index
925923
columns:
@@ -931,10 +929,16 @@ models:
931929
- *topic1
932930
- *topic2
933931
- *topic3
934-
- *tx_hash
932+
- name: tx_hash
933+
description: "Transaction Hash"
934+
data_tests:
935+
- not_null
935936
- *block_number
936937
- *block_time
937-
- *index
938+
- name: index
939+
description: "Index"
940+
data_tests:
941+
- not_null
938942
- *tx_index
939943

940944
- name: chainlink_polygon_vrf_v2_random_fulfilled_logs

dbt_subprojects/daily_spellbook/models/chainlink/polygon/chainlink_polygon_vrf_request_fulfilled_daily.sql

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,56 @@
1111
)
1212
}}
1313

14+
{%- set incremental_lower_bound -%}
15+
date_trunc('{{ var("DBT_ENV_INCREMENTAL_TIME_UNIT") }}', now() - interval '{{ var("DBT_ENV_INCREMENTAL_TIME") }}' {{ var("DBT_ENV_INCREMENTAL_TIME_UNIT") }})
16+
{%- endset -%}
17+
18+
-- The upstream view chainlink_polygon_vrf_request_fulfilled computes
19+
-- evt_block_time = MAX(block_time) above a GROUP BY, so filtering its output
20+
-- with incremental_predicate('evt_block_time') cannot reach the polygon.logs
21+
-- scans (3 full scans, ~2.4 TiB/run, to merge a handful of rows). The view
22+
-- body is inlined here so the time bounds apply BELOW the aggregations and
23+
-- prune the Delta scans via block_time file skipping.
24+
WITH vrf_request_fulfilled AS (
25+
SELECT
26+
'polygon' as blockchain,
27+
MAX(bytearray_to_uint256(bytearray_substring(v1_request.data, 110, 19)) / 1e18) AS token_value,
28+
MAX(v1_fulfilled.tx_from) as operator_address,
29+
MAX(v1_fulfilled.block_time) as evt_block_time
30+
FROM
31+
{{ ref('chainlink_polygon_vrf_v1_random_request_logs') }} v1_request
32+
INNER JOIN {{ ref('chainlink_polygon_vrf_v1_random_fulfilled_logs') }} v1_fulfilled ON bytearray_substring(v1_fulfilled.data, 1, 32) = bytearray_substring(v1_request.data, 129, 32)
33+
{% if is_incremental() %}
34+
-- The request side is a materialized ~6.3M-row table read in full, so no
35+
-- request-side lookback (or delay assumption) is needed. The fulfilled-side
36+
-- bound is exact: a group passes the post-agg evt_block_time predicate iff
37+
-- its MAX(fulfilled.block_time) row is itself in the window, and dropping
38+
-- older sibling fulfillments cannot change MAX() or the request-side values.
39+
WHERE {{ incremental_predicate('v1_fulfilled.block_time') }}
40+
{% endif %}
41+
GROUP BY
42+
v1_request.tx_hash,
43+
v1_fulfilled.tx_from
44+
45+
UNION
46+
47+
SELECT
48+
'polygon' as blockchain,
49+
MAX(bytearray_to_uint256(bytearray_substring(v2_fulfilled.data, 33, 32)) / 1e18) AS token_value,
50+
MAX(v2_fulfilled.tx_from) as operator_address,
51+
MAX(v2_fulfilled.block_time) as evt_block_time
52+
FROM
53+
{{ ref('chainlink_polygon_vrf_v2_random_fulfilled_logs') }} v2_fulfilled
54+
{% if is_incremental() %}
55+
-- exact: each group is a single log row (tx_hash + index), so the pre-agg
56+
-- bound is equivalent to the post-agg evt_block_time predicate
57+
WHERE {{ incremental_predicate('v2_fulfilled.block_time') }}
58+
{% endif %}
59+
GROUP BY
60+
v2_fulfilled.tx_hash,
61+
v2_fulfilled.tx_from,
62+
v2_fulfilled.index
63+
)
1464

1565
SELECT
1666
'polygon' as blockchain,
@@ -19,11 +69,11 @@ SELECT
1969
vrf_request_fulfilled.operator_address,
2070
SUM(token_value) as token_amount
2171
FROM
22-
{{ref('chainlink_polygon_vrf_request_fulfilled')}} vrf_request_fulfilled
72+
vrf_request_fulfilled
2373
{% if is_incremental() %}
2474
WHERE {{ incremental_predicate('evt_block_time') }}
2575
{% endif %}
2676
GROUP BY
2777
2, 4
2878
ORDER BY
29-
2, 4
79+
2, 4

dbt_subprojects/daily_spellbook/models/chainlink/polygon/chainlink_polygon_vrf_v1_random_request_logs.sql

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
{{
22
config(
3-
3+
44
alias='vrf_v1_random_request_logs',
5-
materialized='view'
5+
materialized='incremental',
6+
file_format='delta',
7+
incremental_strategy='merge',
8+
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
9+
unique_key=['tx_hash', 'index']
610
)
711
}}
812

13+
-- Materialized (was a view) so downstream consumers join the full ~6.3M-row
14+
-- request history without rescanning polygon.logs (50B+ rows); logs are
15+
-- append-only, so an incremental block_time window is exact.
16+
917
SELECT
1018
'polygon' as blockchain,
1119
block_hash,
@@ -23,4 +31,7 @@ SELECT
2331
FROM
2432
{{ source('polygon', 'logs') }} logs
2533
WHERE
26-
topic0 = 0x56bd374744a66d531874338def36c906e3a6cf31176eb1e9afd9f1de69725d51 -- RandomnessRequest
34+
topic0 = 0x56bd374744a66d531874338def36c906e3a6cf31176eb1e9afd9f1de69725d51 -- RandomnessRequest
35+
{% if is_incremental() %}
36+
AND {{ incremental_predicate('block_time') }}
37+
{% endif %}

0 commit comments

Comments
 (0)