Hi Flink SQL pros, In a <Top-N> query, is there s...
# troubleshooting
s
Hi Flink SQL pros, In a Top-N query, is there something similar to
TUMBLE_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:
Copy code
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 <= 5
m
What defines your
rowtime
?
s
Hi Martijn. In my made up example we can say that
match_scores
stream has a
match_time
column which I declare as
rowtime
.
So perhaps I could write something like:
Copy code
select
  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 conditions
m
Why would you need max and not just emit the current rowtime?
s
is there an expression to access current rowtime of the entire calculation, not just of one record?
I'm currently testing this:
Copy code
select
  *,
  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 <= 5
nope,
current_watermark
gives very strange results...
m
Weird how?
s
when the rank shift occurs, "update-before"
-U
events have different
ranked_at
value than what they were previously inserted with
one min, I'll prepare a more visual example
I got this set up under a unit test: Match scores stream:
Copy code
| match_time | player  | score |
| ---------- | ------- | ----- |
| t1         | Alice   | 100   |
| t2         | Bob     | 80    |
| t3         | Charlie | 110   |
Desired Top-2 results stream:
Copy code
| 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   |
When testing this query:
Copy code
select
  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:
Copy code
+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]
•
current_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 insertion
m
The first one is as expected
Since the first event always has a null watermark; it needs a second event to start measure progress
s
oh, I didn't expect that as I'm using a custom strategy in
assignTimestampsAndWatermarks
that basically sets watermark to equal exactly last seen
match_time
, but ok
the retraction with different time than insertion is weird, isn't it?
m
I think it’s because the watermark has progressed for the row
I can’t look further at this point unfortunately. Will try to check later
s
I thought Flink would store the whole Row in the buffer and issue the retraction for its original values, but yeah looks like its being re-evaluated ... at least partially