@@ -1299,9 +1299,9 @@ fn clickhouse_edge_rollup_refresh_query(window: NormalizedPlaybackAnalyticsWindo
12991299 "\
13001300 INSERT INTO analytics_edge_hourly \
13011301 SELECT \
1302- organization_id, \
1302+ rollup_organization_id AS organization_id, \
13031303 toStartOfHour(event_observed_at) AS bucket_start, \
1304- asset_id, \
1304+ rollup_asset_id AS asset_id, \
13051305 count() AS request_count, \
13061306 sum(bytes_served) AS bytes_served, \
13071307 countIf(cache_status = 'HIT') AS cache_hit_count, \
@@ -1312,8 +1312,8 @@ fn clickhouse_edge_rollup_refresh_query(window: NormalizedPlaybackAnalyticsWindo
13121312 FROM ( \
13131313 SELECT \
13141314 event_id, \
1315- assumeNotNull(any(organization_id)) AS organization_id , \
1316- any(asset_id) AS asset_id , \
1315+ assumeNotNull(any(organization_id)) AS rollup_organization_id , \
1316+ any(asset_id) AS rollup_asset_id , \
13171317 min(observed_at) AS event_observed_at, \
13181318 any(bytes_served) AS bytes_served, \
13191319 any(cache_status) AS cache_status, \
@@ -1326,7 +1326,7 @@ fn clickhouse_edge_rollup_refresh_query(window: NormalizedPlaybackAnalyticsWindo
13261326 AND observed_at < fromUnixTimestamp64Milli({}) \
13271327 GROUP BY event_id \
13281328 ) \
1329- GROUP BY organization_id , bucket_start, asset_id ",
1329+ GROUP BY rollup_organization_id , bucket_start, rollup_asset_id ",
13301330 window. started_at. timestamp_millis( ) ,
13311331 window. ended_at. timestamp_millis( ) ,
13321332 )
@@ -1337,40 +1337,40 @@ fn clickhouse_player_rollup_refresh_query(window: NormalizedPlaybackAnalyticsWin
13371337 "\
13381338 INSERT INTO analytics_player_hourly \
13391339 SELECT \
1340- organization_id, \
1340+ rollup_organization_id AS organization_id, \
13411341 bucket_start, \
1342- asset_id, \
1342+ rollup_asset_id AS asset_id, \
13431343 count() AS sessions, \
13441344 countIf(reached_first_frame) AS views, \
13451345 countIf(startup_failed) AS startup_failures, \
1346- sum(watch_time_ms ) AS watch_time_ms, \
1347- countIf(stall_duration_ms > 0) AS stalled_sessions, \
1348- sum(stall_count ) AS stall_count, \
1349- sum(stall_duration_ms ) AS stall_duration_ms, \
1350- sum(playback_failures ) AS playback_failures, \
1346+ sum(session_watch_time_ms ) AS watch_time_ms, \
1347+ countIf(session_stall_duration_ms > 0) AS stalled_sessions, \
1348+ sum(session_stall_count ) AS stall_count, \
1349+ sum(session_stall_duration_ms ) AS stall_duration_ms, \
1350+ sum(session_playback_failures ) AS playback_failures, \
13511351 quantileTDigestIf(0.5)(first_frame_ms, first_frame_ms > 0) AS first_frame_p50_ms, \
13521352 quantileTDigestIf(0.95)(first_frame_ms, first_frame_ms > 0) AS first_frame_p95_ms, \
13531353 now64(3) AS updated_at \
13541354 FROM ( \
13551355 SELECT \
1356- organization_id , \
1357- asset_id , \
1358- playback_session_id , \
1359- toStartOfHour(min(observed_at )) AS bucket_start, \
1356+ rollup_organization_id , \
1357+ rollup_asset_id , \
1358+ rollup_playback_session_id , \
1359+ toStartOfHour(min(event_observed_at )) AS bucket_start, \
13601360 countIf(phase = 'first_frame') > 0 AS reached_first_frame, \
13611361 countIf(phase = 'bootstrap_failure') > 0 AS startup_failed, \
13621362 minIf(first_frame_ms, phase = 'first_frame' AND first_frame_ms > 0) AS first_frame_ms, \
1363- sumIf(watch_delta_ms, phase = 'watch_heartbeat') AS watch_time_ms , \
1364- countIf(phase = 'stall_end') AS stall_count , \
1365- sumIf(stall_duration_ms, phase = 'stall_end') AS stall_duration_ms , \
1366- countIf(phase = 'playback_failure') AS playback_failures \
1363+ sumIf(watch_delta_ms, phase = 'watch_heartbeat') AS session_watch_time_ms , \
1364+ countIf(phase = 'stall_end') AS session_stall_count , \
1365+ sumIf(stall_duration_ms, phase = 'stall_end') AS session_stall_duration_ms , \
1366+ countIf(phase = 'playback_failure') AS session_playback_failures \
13671367 FROM ( \
13681368 SELECT \
13691369 event_id, \
1370- any(organization_id) AS organization_id , \
1371- any(asset_id) AS asset_id , \
1372- any(playback_session_id) AS playback_session_id , \
1373- min(observed_at) AS observed_at , \
1370+ any(organization_id) AS rollup_organization_id , \
1371+ any(asset_id) AS rollup_asset_id , \
1372+ any(playback_session_id) AS rollup_playback_session_id , \
1373+ min(observed_at) AS event_observed_at , \
13741374 any(phase) AS phase, \
13751375 any(first_frame_ms) AS first_frame_ms, \
13761376 any(stall_duration_ms) AS stall_duration_ms, \
@@ -1380,9 +1380,9 @@ fn clickhouse_player_rollup_refresh_query(window: NormalizedPlaybackAnalyticsWin
13801380 AND observed_at < fromUnixTimestamp64Milli({}) \
13811381 GROUP BY event_id \
13821382 ) \
1383- GROUP BY organization_id, asset_id, playback_session_id \
1383+ GROUP BY rollup_organization_id, rollup_asset_id, rollup_playback_session_id \
13841384 ) \
1385- GROUP BY organization_id , bucket_start, asset_id ",
1385+ GROUP BY rollup_organization_id , bucket_start, rollup_asset_id ",
13861386 window. started_at. timestamp_millis( ) ,
13871387 window. ended_at. timestamp_millis( ) ,
13881388 )
@@ -1927,6 +1927,36 @@ mod tests {
19271927 ) ;
19281928 }
19291929
1930+ #[ test]
1931+ fn rollup_queries_do_not_reuse_source_column_names_for_aggregates ( ) {
1932+ let window = NormalizedPlaybackAnalyticsWindow {
1933+ started_at : DateTime :: parse_from_rfc3339 ( "2026-06-13T11:00:00.000Z" )
1934+ . unwrap ( )
1935+ . with_timezone ( & Utc ) ,
1936+ ended_at : DateTime :: parse_from_rfc3339 ( "2026-06-13T12:00:00.000Z" )
1937+ . unwrap ( )
1938+ . with_timezone ( & Utc ) ,
1939+ } ;
1940+
1941+ let edge_query = clickhouse_edge_rollup_refresh_query ( window) ;
1942+ assert ! ( edge_query. contains( "rollup_organization_id AS organization_id" ) ) ;
1943+ assert ! ( edge_query. contains( "rollup_asset_id AS asset_id" ) ) ;
1944+ assert ! (
1945+ edge_query. contains( "GROUP BY rollup_organization_id, bucket_start, rollup_asset_id" )
1946+ ) ;
1947+
1948+ let player_query = clickhouse_player_rollup_refresh_query ( window) ;
1949+ assert ! ( player_query. contains( "rollup_organization_id AS organization_id" ) ) ;
1950+ assert ! ( player_query. contains( "rollup_asset_id AS asset_id" ) ) ;
1951+ assert ! ( player_query. contains( "min(observed_at) AS event_observed_at" ) ) ;
1952+ assert ! ( player_query. contains( "sum(session_watch_time_ms) AS watch_time_ms" ) ) ;
1953+ assert ! ( player_query. contains( "countIf(session_stall_duration_ms > 0)" ) ) ;
1954+ assert ! ( player_query. contains( "sum(session_playback_failures) AS playback_failures" ) ) ;
1955+ assert ! ( player_query. contains(
1956+ "GROUP BY rollup_organization_id, rollup_asset_id, rollup_playback_session_id"
1957+ ) ) ;
1958+ }
1959+
19301960 #[ test]
19311961 fn clickhouse_rows_use_artifact_billing_metadata_for_delivery ( ) {
19321962 let ingested_at = DateTime :: parse_from_rfc3339 ( "2026-06-13T12:00:01.000Z" )
0 commit comments