Window Join

Streaming

A window join adds the dimension of time into the join criteria themselves. In doing so, the window join joins the elements of two streams that share a common key and lie in the same window. The semantic of window join is same to the DataStream window join

For streaming queries, unlike other joins on continuous tables, window join does not emit intermediate results but only emits final results at the end of the window. Moreover, window join purge all intermediate state when no longer needed.

Usually, Window Join is used with Windowing TVF. Besides, Window Join could follow after other operations based on Windowing TVF, such as Window Aggregation, Window TopN and Window Join.

Currently, Window Join requires the join on condition contains window starts equality of input tables and window ends equality of input tables.

Window Join supports INNER/LEFT/RIGHT/FULL OUTER/ANTI/SEMI JOIN.

INNER/LEFT/RIGHT/FULL OUTER

The following shows the syntax of the INNER/LEFT/RIGHT/FULL OUTER Window Join statement.

  1. SELECT ...
  2. FROM L [LEFT|RIGHT|FULL OUTER] JOIN R -- L and R are relations applied windowing TVF
  3. ON L.window_start = R.window_start AND L.window_end = R.window_end AND ...

The syntax of INNER/LEFT/RIGHT/FULL OUTER WINDOW JOIN are very similar with each other, we only give an example for FULL OUTER JOIN here. When performing a window join, all elements with a common key and a common tumbling window are joined together. We only give an example for a Window Join which works on a Tumble Window TVF. By scoping the region of time for the join into fixed five-minute intervals, we chopped our datasets into two distinct windows of time: [12:00, 12:05) and [12:05, 12:10). The L2 and R2 rows could not join together because they fell into separate windows.

  1. Flink SQL> desc LeftTable;
  2. +----------+------------------------+------+-----+--------+----------------------------------+
  3. | name | type | null | key | extras | watermark |
  4. +----------+------------------------+------+-----+--------+----------------------------------+
  5. | row_time | TIMESTAMP(3) *ROWTIME* | true | | | `row_time` - INTERVAL '1' SECOND |
  6. | num | INT | true | | | |
  7. | id | STRING | true | | | |
  8. +----------+------------------------+------+-----+--------+----------------------------------+
  9. Flink SQL> SELECT * FROM LeftTable;
  10. +------------------+-----+----+
  11. | row_time | num | id |
  12. +------------------+-----+----+
  13. | 2020-04-15 12:02 | 1 | L1 |
  14. | 2020-04-15 12:06 | 2 | L2 |
  15. | 2020-04-15 12:03 | 3 | L3 |
  16. +------------------+-----+----+
  17. Flink SQL> desc RightTable;
  18. +----------+------------------------+------+-----+--------+----------------------------------+
  19. | name | type | null | key | extras | watermark |
  20. +----------+------------------------+------+-----+--------+----------------------------------+
  21. | row_time | TIMESTAMP(3) *ROWTIME* | true | | | `row_time` - INTERVAL '1' SECOND |
  22. | num | INT | true | | | |
  23. | id | STRING | true | | | |
  24. +----------+------------------------+------+-----+--------+----------------------------------+
  25. Flink SQL> SELECT * FROM RightTable;
  26. +------------------+-----+----+
  27. | row_time | num | id |
  28. +------------------+-----+----+
  29. | 2020-04-15 12:01 | 2 | R2 |
  30. | 2020-04-15 12:04 | 3 | R3 |
  31. | 2020-04-15 12:05 | 4 | R4 |
  32. +------------------+-----+----+
  33. Flink SQL> SELECT L.num as L_Num, L.id as L_Id, R.num as R_Num, R.id as R_Id, L.window_start, L.window_end
  34. FROM (
  35. SELECT * FROM TABLE(TUMBLE(TABLE LeftTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  36. ) L
  37. FULL JOIN (
  38. SELECT * FROM TABLE(TUMBLE(TABLE RightTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  39. ) R
  40. ON L.num = R.num AND L.window_start = R.window_start AND L.window_end = R.window_end;
  41. +-------+------+-------+------+------------------+------------------+
  42. | L_Num | L_Id | R_Num | R_Id | window_start | window_end |
  43. +-------+------+-------+------+------------------+------------------+
  44. | 1 | L1 | null | null | 2020-04-15 12:00 | 2020-04-15 12:05 |
  45. | null | null | 2 | R2 | 2020-04-15 12:00 | 2020-04-15 12:05 |
  46. | 3 | L3 | 3 | R3 | 2020-04-15 12:00 | 2020-04-15 12:05 |
  47. | 2 | L2 | null | null | 2020-04-15 12:05 | 2020-04-15 12:10 |
  48. | null | null | 4 | R4 | 2020-04-15 12:05 | 2020-04-15 12:10 |
  49. +-------+------+-------+------+------------------+------------------+

Note: in order to better understand the behavior of windowing, we simplify the displaying of timestamp values to not show the trailing zeros, e.g. 2020-04-15 08:05 should be displayed as 2020-04-15 08:05:00.000 in Flink SQL Client if the type is TIMESTAMP(3).

SEMI

Semi Window Joins returns a row from one left record if there is at least one matching row on the right side within the common window.

  1. Flink SQL> SELECT *
  2. FROM (
  3. SELECT * FROM TABLE(TUMBLE(TABLE LeftTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  4. ) L WHERE L.num IN (
  5. SELECT num FROM (
  6. SELECT * FROM TABLE(TUMBLE(TABLE RightTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  7. ) R WHERE L.window_start = R.window_start AND L.window_end = R.window_end);
  8. +------------------+-----+----+------------------+------------------+-------------------------+
  9. | row_time | num | id | window_start | window_end | window_time |
  10. +------------------+-----+----+------------------+------------------+-------------------------+
  11. | 2020-04-15 12:03 | 3 | L3 | 2020-04-15 12:00 | 2020-04-15 12:05 | 2020-04-15 12:04:59.999 |
  12. +------------------+-----+----+------------------+------------------+-------------------------+
  13. Flink SQL> SELECT *
  14. FROM (
  15. SELECT * FROM TABLE(TUMBLE(TABLE LeftTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  16. ) L WHERE EXISTS (
  17. SELECT * FROM (
  18. SELECT * FROM TABLE(TUMBLE(TABLE RightTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  19. ) R WHERE L.num = R.num AND L.window_start = R.window_start AND L.window_end = R.window_end);
  20. +------------------+-----+----+------------------+------------------+-------------------------+
  21. | row_time | num | id | window_start | window_end | window_time |
  22. +------------------+-----+----+------------------+------------------+-------------------------+
  23. | 2020-04-15 12:03 | 3 | L3 | 2020-04-15 12:00 | 2020-04-15 12:05 | 2020-04-15 12:04:59.999 |
  24. +------------------+-----+----+------------------+------------------+-------------------------+

Note: in order to better understand the behavior of windowing, we simplify the displaying of timestamp values to not show the trailing zeros, e.g. 2020-04-15 08:05 should be displayed as 2020-04-15 08:05:00.000 in Flink SQL Client if the type is TIMESTAMP(3).

ANTI

Anti Window Joins are the obverse of the Inner Window Join: they contain all of the unjoined rows within each common window.

  1. Flink SQL> SELECT *
  2. FROM (
  3. SELECT * FROM TABLE(TUMBLE(TABLE LeftTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  4. ) L WHERE L.num NOT IN (
  5. SELECT num FROM (
  6. SELECT * FROM TABLE(TUMBLE(TABLE RightTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  7. ) R WHERE L.window_start = R.window_start AND L.window_end = R.window_end);
  8. +------------------+-----+----+------------------+------------------+-------------------------+
  9. | row_time | num | id | window_start | window_end | window_time |
  10. +------------------+-----+----+------------------+------------------+-------------------------+
  11. | 2020-04-15 12:02 | 1 | L1 | 2020-04-15 12:00 | 2020-04-15 12:05 | 2020-04-15 12:04:59.999 |
  12. | 2020-04-15 12:06 | 2 | L2 | 2020-04-15 12:05 | 2020-04-15 12:10 | 2020-04-15 12:09:59.999 |
  13. +------------------+-----+----+------------------+------------------+-------------------------+
  14. Flink SQL> SELECT *
  15. FROM (
  16. SELECT * FROM TABLE(TUMBLE(TABLE LeftTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  17. ) L WHERE NOT EXISTS (
  18. SELECT * FROM (
  19. SELECT * FROM TABLE(TUMBLE(TABLE RightTable, DESCRIPTOR(row_time), INTERVAL '5' MINUTES))
  20. ) R WHERE L.num = R.num AND L.window_start = R.window_start AND L.window_end = R.window_end);
  21. +------------------+-----+----+------------------+------------------+-------------------------+
  22. | row_time | num | id | window_start | window_end | window_time |
  23. +------------------+-----+----+------------------+------------------+-------------------------+
  24. | 2020-04-15 12:02 | 1 | L1 | 2020-04-15 12:00 | 2020-04-15 12:05 | 2020-04-15 12:04:59.999 |
  25. | 2020-04-15 12:06 | 2 | L2 | 2020-04-15 12:05 | 2020-04-15 12:10 | 2020-04-15 12:09:59.999 |
  26. +------------------+-----+----+------------------+------------------+-------------------------+

Note: in order to better understand the behavior of windowing, we simplify the displaying of timestamp values to not show the trailing zeros, e.g. 2020-04-15 08:05 should be displayed as 2020-04-15 08:05:00.000 in Flink SQL Client if the type is TIMESTAMP(3).

Limitation

Limitation on Join clause

Currently, The window join requires the join on condition contains window starts equality of input tables and window ends equality of input tables. In the future, we can also simplify the join on clause to only include the window start equality if the windowing TVF is TUMBLE or HOP.

Limitation on windowing TVFs of inputs

Currently, the windowing TVFs must be the same of left and right inputs. This can be extended in the future, for example, tumbling windows join sliding windows with the same window size.

Limitation on Window Join which follows after Windowing TVFs directly

Currently, if Window Join follows after Windowing TVF, the Windowing TVF has to be with Tumble Windows, Hop Windows or Cumulate Windows instead of Session windows.