Sergii Mikhtoniuk
01/04/2024, 3:34 AMTUMBLE_ROWTIME function, or CURRENT_TIMESTAMP but that returns the rowtime of an event currently being processed by the Rank operator?
Example scenario:
• we maintain a leaderboard of top 5 player scores
• a new score comes in that ranks as 1
• this causes scores ranked [2, 4] to be updated (-U, +U) and score ranked 5 to be retracted (-D)
• I would like to have a time column that represents when a certain score was assigned its current rank
◦ i.e. when existing score gets shifted down - it gets +U with rowtime of an event that caused the shift
Example query:
select
rank,
rowtime function() as ranked_at, // ???
player_name,
score
from (
select
row_number() over (order by score desc) AS rank,
player_name,
score
from match_scores
) where rank <= 5Martijn Visser
01/04/2024, 12:42 PMrowtime ?Sergii Mikhtoniuk
01/04/2024, 5:03 PMmatch_scores stream has a match_time column which I declare as rowtime.Sergii Mikhtoniuk
01/04/2024, 5:05 PMselect
rank,
max(match_time) as ranked_at, // !!!
player_name,
score
from (
select
row_number() over (order by score desc) AS rank,
match_time,
player_name,
score
from match_scores
) where rank <= 5
... but I'm not sure this would give me what I want under all conditionsMartijn Visser
01/04/2024, 5:34 PMSergii Mikhtoniuk
01/04/2024, 5:35 PMSergii Mikhtoniuk
01/04/2024, 5:40 PMselect
*,
current_watermark(match_time) as ranked_at
from (
select
row_number() over (order by score desc) AS place,
match_time,
player_name,
score
from match_scores
) where place <= 5Sergii Mikhtoniuk
01/04/2024, 5:43 PMcurrent_watermark gives very strange results...Martijn Visser
01/04/2024, 5:45 PMSergii Mikhtoniuk
01/04/2024, 5:48 PM-U events have different ranked_at value than what they were previously inserted withSergii Mikhtoniuk
01/04/2024, 5:48 PMSergii Mikhtoniuk
01/04/2024, 6:02 PM| match_time | player | score |
| ---------- | ------- | ----- |
| t1 | Alice | 100 |
| t2 | Bob | 80 |
| t3 | Charlie | 110 |
Desired Top-2 results stream:
| op | rank | ranked_at | player | score |
| --- | ---- | --------- | ------- | ----- |
| +I | 1 | t1 | Alice | 100 |
| +I | 2 | t2 | Bob | 80 |
| -U | 1 | t1 | Alice | 100 |
| +U | 1 | t3 | Charlie | 110 |
| -U | 2 | t2 | Bob | 80 |
| +U | 2 | t3 | Alice | 100 |Sergii Mikhtoniuk
01/04/2024, 6:03 PMselect
rank,
current_watermark(match_time) as ranked_at,
player,
score
from (
select
row_number() over (order by score desc) AS rank,
match_time,
player_name,
score
from match_scores
) where rank <= 2
I get this in output stream:
+I[1, null, Alice, 100]
+I[2, t1, Bob, 80]
-U[1, t2, Alice, 100]
+U[1, t2, Charlie, 110]
-U[2, t2, Bob, 80]
+U[2, t2, Alice, 100]Sergii Mikhtoniuk
01/04/2024, 6:04 PMcurrent_watermark() produced null at the first insertion and seems to be lagging one step behind
• then Alice gets retracted with ranked_at as t2 for some reason - I expected the time to be the same as during insertionMartijn Visser
01/04/2024, 6:05 PMMartijn Visser
01/04/2024, 6:05 PMSergii Mikhtoniuk
01/04/2024, 6:10 PMassignTimestampsAndWatermarks that basically sets watermark to equal exactly last seen match_time , but okSergii Mikhtoniuk
01/04/2024, 6:10 PMMartijn Visser
01/04/2024, 6:11 PMMartijn Visser
01/04/2024, 6:12 PMSergii Mikhtoniuk
01/04/2024, 6:13 PM