Skip to content

Commit 355ef96

Browse files
committed
fix: cursor suggestions
1 parent 3070ac3 commit 355ef96

File tree

1 file changed

+76
-35
lines changed

1 file changed

+76
-35
lines changed

services/libs/tinybird/pipes/cdp_segment_metrics_copy_pipe.pipe

Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
DESCRIPTION >
22
Daily batch job that builds segment-level aggregate states from existing
33
member and organization segment aggregate datasources.
4-
Heavy DISTINCT logic lives here.
4+
Activities are computed from activityRelations_enriched_deduplicated_ds using the latest snapshotId.
5+
This version avoids CTEs and avoids *StateIf() combinators to keep AggregateFunction types consistent.
56

6-
NODE activitiesSegmentStates
7+
NODE activitiesAllTimeStates
78
SQL >
8-
-- Compute activity counters from event-level data (correct last-30-days behavior)
9-
SELECT
10-
segmentId,
11-
countState() AS activitiesTotalState,
12-
countStateIf(timestamp >= now() - INTERVAL 30 DAY) AS activitiesLast30DaysState
9+
-- Compute total activities from event-level data (latest snapshot only).
10+
-- Avoid countStateIf() to keep AggregateFunction(count) type consistent.
11+
SELECT segmentId, countState() AS activitiesTotalState
12+
FROM activityRelations_enriched_deduplicated_ds
13+
WHERE
14+
segmentId IS NOT NULL
15+
AND segmentId != ''
16+
AND snapshotId = (SELECT max(snapshotId) FROM activityRelations_enriched_deduplicated_ds)
17+
GROUP BY segmentId
18+
19+
NODE activitiesLast30States
20+
SQL >
21+
-- Compute last-30-days activities from event-level data (latest snapshot only).
22+
-- Use WHERE filtering instead of countStateIf() to avoid AggregateFunction(countIf) types.
23+
SELECT segmentId, countState() AS activitiesLast30DaysState
1324
FROM activityRelations_enriched_deduplicated_ds
1425
WHERE
1526
segmentId IS NOT NULL
1627
AND segmentId != ''
17-
AND snapshotId = (select max(snapshotId) from activityRelations_enriched_deduplicated_ds)
28+
AND snapshotId = (SELECT max(snapshotId) FROM activityRelations_enriched_deduplicated_ds)
29+
AND timestamp >= now() - INTERVAL 30 DAY
1830
GROUP BY segmentId
1931

2032
NODE memberPerSegment
@@ -24,14 +36,21 @@ SQL >
2436
FROM cdp_member_segment_aggregates_ds
2537
GROUP BY segmentId, memberId
2638

27-
NODE memberSegmentStates
39+
NODE memberAllTimeStates
2840
SQL >
29-
-- Build segment-level activity + member DISTINCT states
30-
SELECT
31-
segmentId,
32-
uniqCombinedState(memberId) AS membersUniqState,
33-
uniqCombinedStateIf(memberId, lastActive >= now() - INTERVAL 30 DAY) AS membersLast30UniqState
41+
-- Build segment-level DISTINCT member states (all time).
42+
-- Avoid uniqCombinedStateIf() to keep AggregateFunction(uniqCombined, String) type consistent.
43+
SELECT segmentId, uniqCombinedState(memberId) AS membersUniqState
44+
FROM memberPerSegment
45+
GROUP BY segmentId
46+
47+
NODE memberLast30States
48+
SQL >
49+
-- Build segment-level DISTINCT member states (last 30 days).
50+
-- Use WHERE filtering instead of uniqCombinedStateIf() to avoid uniqCombinedIf types.
51+
SELECT segmentId, uniqCombinedState(memberId) AS membersLast30UniqState
3452
FROM memberPerSegment
53+
WHERE lastActive >= now() - INTERVAL 30 DAY
3554
GROUP BY segmentId
3655

3756
NODE orgPerSegment
@@ -41,36 +60,55 @@ SQL >
4160
FROM cdp_organization_segment_aggregates_ds
4261
GROUP BY segmentId, organizationId
4362

44-
NODE orgSegmentStates
63+
NODE orgAllTimeStates
4564
SQL >
46-
-- Build segment-level organization DISTINCT states
47-
SELECT
48-
segmentId,
49-
uniqCombinedState(organizationId) AS orgsUniqState,
50-
uniqCombinedStateIf(
51-
organizationId, lastActive >= now() - INTERVAL 30 DAY
52-
) AS orgsLast30UniqState
65+
-- Build segment-level DISTINCT organization states (all time).
66+
SELECT segmentId, uniqCombinedState(organizationId) AS orgsUniqState
5367
FROM orgPerSegment
5468
GROUP BY segmentId
5569

70+
NODE orgLast30States
71+
SQL >
72+
-- Build segment-level DISTINCT organization states (last 30 days).
73+
-- Use WHERE filtering instead of uniqCombinedStateIf() to avoid uniqCombinedIf types.
74+
SELECT segmentId, uniqCombinedState(organizationId) AS orgsLast30UniqState
75+
FROM orgPerSegment
76+
WHERE lastActive >= now() - INTERVAL 30 DAY
77+
GROUP BY segmentId
78+
5679
NODE segmentAggStates
5780
SQL >
5881
-- Attach hierarchy and snapshot date.
59-
-- Coalesce NULL AggregateFunction states to empty states for segments without activity.
82+
-- Coalesce NULL AggregateFunction states to empty states without using *StateIf combinators.
83+
-- Empty states are generated by aggregating over an empty input (system.one WHERE 0).
6084
SELECT
6185
toDate(now()) AS snapshotDate,
6286
s.id AS segmentId,
6387
s.parentId AS parentId,
6488
s.grandparentId AS grandparentId,
65-
-- Activities (event-level truth)
66-
ifNull(a.activitiesTotalState, countStateIf(0)) AS activitiesTotalState,
67-
ifNull(a.activitiesLast30DaysState, countStateIf(0)) AS activitiesLast30DaysState,
68-
-- Members DISTINCT states
69-
ifNull(ms.membersUniqState, uniqCombinedStateIf('x', 0)) AS membersUniqState,
70-
ifNull(ms.membersLast30UniqState, uniqCombinedStateIf('x', 0)) AS membersLast30UniqState,
71-
-- Organizations DISTINCT states
72-
ifNull(os.orgsUniqState, uniqCombinedStateIf('x', 0)) AS orgsUniqState,
73-
ifNull(os.orgsLast30UniqState, uniqCombinedStateIf('x', 0)) AS orgsLast30UniqState
89+
-- Activities (AggregateFunction(count))
90+
ifNull(
91+
a.activitiesTotalState, (SELECT countState() FROM system.one WHERE 0)
92+
) AS activitiesTotalState,
93+
ifNull(
94+
al30.activitiesLast30DaysState, (SELECT countState() FROM system.one WHERE 0)
95+
) AS activitiesLast30DaysState,
96+
-- Members (AggregateFunction(uniqCombined, String))
97+
ifNull(
98+
m.membersUniqState, (SELECT uniqCombinedState(CAST('' AS String)) FROM system.one WHERE 0)
99+
) AS membersUniqState,
100+
ifNull(
101+
ml30.membersLast30UniqState,
102+
(SELECT uniqCombinedState(CAST('' AS String)) FROM system.one WHERE 0)
103+
) AS membersLast30UniqState,
104+
-- Organizations (AggregateFunction(uniqCombined, String))
105+
ifNull(
106+
o.orgsUniqState, (SELECT uniqCombinedState(CAST('' AS String)) FROM system.one WHERE 0)
107+
) AS orgsUniqState,
108+
ifNull(
109+
ol30.orgsLast30UniqState,
110+
(SELECT uniqCombinedState(CAST('' AS String)) FROM system.one WHERE 0)
111+
) AS orgsLast30UniqState
74112
FROM
75113
(
76114
SELECT id, parentId, grandparentId
@@ -83,9 +121,12 @@ SQL >
83121
AND parentId != ''
84122
AND grandparentId != ''
85123
) AS s
86-
LEFT JOIN activitiesSegmentStates AS a ON a.segmentId = s.id
87-
LEFT JOIN memberSegmentStates AS ms ON ms.segmentId = s.id
88-
LEFT JOIN orgSegmentStates AS os ON os.segmentId = s.id
124+
LEFT JOIN activitiesAllTimeStates AS a ON a.segmentId = s.id
125+
LEFT JOIN activitiesLast30States AS al30 ON al30.segmentId = s.id
126+
LEFT JOIN memberAllTimeStates AS m ON m.segmentId = s.id
127+
LEFT JOIN memberLast30States AS ml30 ON ml30.segmentId = s.id
128+
LEFT JOIN orgAllTimeStates AS o ON o.segmentId = s.id
129+
LEFT JOIN orgLast30States AS ol30 ON ol30.segmentId = s.id
89130

90131
TYPE COPY
91132
TARGET_DATASOURCE cdp_segment_metrics_ds

0 commit comments

Comments
 (0)