Hi everyone! I might have an interesting question ...
# troubleshooting
h
Hi everyone! I might have an interesting question for you that not even ChatGPT can figure out 🙂 Imagine you have to streams that you want to join on a common ID, and the goal is to end up with “unioned” events, combining the event with the latest event from the other stream. So in the end you’d have something like
Copy code
{
    'A_event_ts`: '2023-07-25T00:00:00',
    'B_event_ts': '2023-07-24T00:00:00',
    'A_value': 12,
    'B_value': 13
}
when A arrives after B, or something like:
Copy code
{
    'A_event_ts`: '2023-07-25T00:00:00',
    'B_event_ts': '2023-07-26T00:00:00',
    'A_value': 12,
    'B_value': 14
}
when B arrives after A. In effect, it’s a union of two asof joins, but that doesn’t work because the schemas become incompatible due to the definition of the system time. Any hints? To make the question a bit more challenging, I’d like to achieve it with Flink SQL only, if possible :)
s
Yes, use a type like
Copy code
Wrapper(Option[A], Option[B])
h
Is that possible with SQL?
s
Sure, you can use ROW as a wrapper type, with a and b be nullable fields in that (also ROWs).
h
ok, I’m not 100% sure how I would apply that here, can you give a little more context? 🙂
The query that doesn’t work because of incompatbile rowtimes:
Copy code
select
  A.`id` as `id`,
  A.event_ts as A_event_ts,
  A.`value` as A_value,
  B.event_ts as B_event_ts,
  B.value as B_value
FROM A
LEFT JOIN B FOR SYSTEM TIME AS OF A.event_TS
ON A.id = B.id
UNION ALL
select
  B.`id` as `id,
  A.event_ts as A_event_ts,
  A.`value` as A_value,
  B.event_ts as B_event_ts,
  B.value as B_value
FROM B
LEFT JOIN A FOR SYSTEM TIME AS OF A.event_TS
ON A.id = B.id
explicitly casting to timestamp(3) works 🙂 no idea why I didn’t try that