Status

Current state: "Accepted"

Discussion thread: https://lists.apache.org/thread/9xyjnkq4wxnmkbprskqrghyohqx85dpm

Vote thread: https://lists.apache.org/thread/96wzry93ph87rbtl35g3b8l1rtqmo85w

JIRA: FLINK-39779 - Getting issue details... STATUS

Released: n/a

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Motivation

Flink SQL features temporal table joins to join a table (probe-side) against a versioned table (build-side). Conceptually, the join fetches for each row of the probe-side a certain version of the versioned build-side table and evaluates the join predicate. The build-side version that the join retrieves depends on how the join is specified and which operator is used by the optimizer. The event-time temporal table join fetches the version that corresponds to the event time of the probe-side record, the processing-time temporal table join fetches the “current” version (i.e., the one that is currently held in state). There are a few limitations, such as a PK requirement on the build-side and that the join predicate must include equality predicates against the build-side PK. Both joins are specified via the FOR SYSTEM_TIME AS OF syntax:

-- event-time temporal table join (orders.order_time is event-time column)
SELECT *
FROM orders
LEFT JOIN currency_rates FOR SYSTEM_TIME AS OF orders.order_time
ON orders.currency = currency_rates.currency;

-- processing-time temporal table join (orders.proc_time is proc-time column)
SELECT *
FROM orders
LEFT JOIN currency_rates FOR SYSTEM_TIME AS OF orders.proc_time
ON orders.currency = currency_rates.currency;

While there is an operator implementation for the processing-time temporal table join (TemporalProcessTimeJoinOperator), the corresponding translation rule is disabled with an exception (see StreamExecTemporalJoin line 293, FLINK-19830) because the operator does not guarantee that the build-side of the join (the temporal table) is fully loaded before the probe-side is processed. Hence, the join can produce unexpected results until the build-side is loaded.

There is also the Temporal Table Function Join, which is based on temporal table functions. However, this feature is only available to Table API users.

Today, Flink SQL users have to choose the event-time temporal table join when they want to join a table against a versioned table. However, the strict event-time guarantees of this operator come at a price and there are use cases/scenarios for which this join implementation is not suitable:

  • infrequent watermarks of an input: the event-time join only emits results on combined watermark advancement. If the build-side input is not providing periodic watermarks (because it is rarely updated), the join stalls and buffers all probe-side records, possibly resulting in rapidly growing state.

  • low-latency requirement: even if both inputs provide periodic watermarks, the event-time join introduces latency because it only emits results on watermarks.

To address these use cases, we propose to implement a new temporal table function join that operates in processing-time, addresses the problems of build-side initialization, dependency on continuous build-side watermarks and join results with watermark-induced latency. In addition, the new implementation won’t have restrictions related to build-side primary keys.

The new SNAPSHOT join is expressed in SQL as a lateral table function:

SELECT *
FROM 
  t, 
  CROSS JOIN LATERAL SNAPSHOT(TABLE s)
    ON t.a = s.a;

Hence, the new join is specified just like a temporal table function join, i.e., we make this join available to SQL user via a built-in process table function.

This FLIP discusses:

  • A minor syntactic improvement for LATERAL calls of table functions

  • The translation from SNAPSHOT table function (in a LATERAL context) into a stream operator

  • Semantics and implementation of the new LateralSnapshotJoin operator

  • Future work to move the event-time temporal table join to the same LATERAL table function syntax and aligning its behavior with the new processing-time temporal table join

Note, the name of the SNAPSHOT() function has been proposed before as part of FLIP-517.

Terminology and Background

Assumptions

We make the following assumptions:

  • All data that should be used to initialize the build-side is immediately ready for consumption. For example it resides in a Kafka topic or as files in a folder. This means, we can expect immediate and steady consumption of this data and periodic watermarks during that time. Also, the time to initialize the build-side is expected to be short (seconds to a few minutes).

  • After the build-side has been initialized, the build-side input might only infrequently serve data (e.g., updates once a day). Hence, there can be longer pauses during which no data and watermarks are received for this input. This is a strong improvement over the event-time temporal table join which stalls if the build-side does not advance watermarks.

  • Watermark alignment has been enabled for the build- and probe-side of the join.

These assumptions are not strict requirements but rather define the scenarios for which the join has been designed and in which it will optimally perform.

Semantics of the LateralSnapshotJoin Operator

  • The join supports INNER JOIN and LEFT OUTER JOIN modes (RIGHT OUTER and FULL OUTER joins are not supported by LATERAL table function joins).

  • Inputs

    • Probe-side input (left):

      • only append changelog mode supported.

        • This ensures append-only output and prevents late data.

      • no PK requirement

    • Build-side input (right):

      • all changelog modes supported, full deletes required

      • watermarking strategy required

      • no PK requirement

  • Output:

    • append-only

    • timestamp attribute of probe-side input is preserved, watermarks of probe-side are forwarded

  • Join predicate: Join requires a conjunctive equality join predicate (ensures parallel processing)

  • A probe-side record is joined with the “current” version of the build-side table using the join predicate

    • “current” version refers to the version that the operator currently holds (aka processing time version).

    • Watermark alignment across build-side and probe-side should limit the temporal drift of the input but cannot provide exact guarantees on temporal proximity. The build-side record can be older or newer than the probe-side record. There might also be a temporally closer build-side record that is not used. Users that need these guarantees can chose the event-time temporal table join (SNAPSHOT with correlated event-time column).

  • The join operator operates in two phases: LOAD and JOIN .

  • LOAD phase,

    • The operator stays in the LOAD phase until the build-side input is consumed up to a certain point. This point is called flip point in this proposal and is defined as a timestamp that is evaluated against the watermark of the build-side input. At the flip point, the build-side state is ready to be joined with the probe-side and the operator switches into the JOIN phase. There’s also an optional timeout mechanism to switch into JOIN phase if the build-side input is inactive for a configurable duration.

    • The build-side state is created by applying all build-side change records

    • All records of the probe-side are buffered in state until the flip point is reached.

    • Once the flip point is reached, all buffered probe-side records are joined against the latest state of the build-side table. After joining, the probe-side state is cleared.

    • During load phase, no watermarks are emitted.

  • JOIN phase

    • After the flip point, probe-side records are directly joined against the build-side state. No probe-side buffering occurs.

    • Build-side change records are consumed and update the build-side state.

    • After the flip point, the watermarks of the probe-side are forwarded to the output.

  • As with all processing-time operators, the join results are not deterministic (also not after failure recovery).

Related FLIPs & Jiras

Public Interfaces

SNAPSHOT Table Function

This FLIP introduces support for the SNAPSHOT table function. The function is used in a LATERAL context to express a processing-time temporal join.

-- INNER JOIN on SNAPSHOT function
SELECT *
FROM 
  t 
  CROSS JOIN TABLE(LATERAL SNAPSHOT(TABLE s)) ON t.a = s.a;

-- LEFT JOIN on SNAPSHOT function with named arguments
SELECT *
FROM 
  t 
  LEFT JOIN TABLE(LATERAL SNAPSHOT(
      input => s, 
      load_completed_condition => 'user_time', 
      load_completed_time => '2026-07-01 00:00:00.001',
      load_completed_idle_timeout => INTERVAL '10' second,
      state_ttl => INTERVAL '1' day))
  ON t.a = s.a;

Conceptually, the SNAPSHOT function returns a version of the input table s. Since no correlation attribute from the outer table is passed into the SNAPSHOT function, “any” version is valid. However, the function returns the “current” (aka processing-time) version of the input table (watermark alignment will help to roughly align inputs).

Arguments:

  • input : specifies the build-side table of the join. The build-side input treated like a regular changelog, like other tables in Flink SQL.

  • load_completed_condition (optional): specifies which condition is used to determine when the load phase is completed. Possible values:

    • compile_time (default): function flips to JOIN phase when the build-side watermark reaches the wall-clock time when the query was compiled.

    • user_time : the function flips to JOIN phase when the build-side watermark reaches a user specified timestamp (via load_completed_time argument).

  • load_completed_time (optional): specifies the timestamp for the load_completed_condition => 'user_time' condition. The timestamp is checked against the build-side watermark.

  • load_completed_idle_timeout (optional): specifies a fallback for the function to flip to the JOIN phase if no build-side watermark has been received for  load_completed_idle_timeout of processing time. By default, the fallback is disabled.
  • state_ttl (optional): a TTL duration for build-side state. Join keys that are not updated or looked up for more than the TTL duration, can be deleted by the engine. State TTL only takes effect after the function has transitioned to the JOIN phase. Deletion is best-effort. By default, state TTL is disabled.

Make TABLE keyword for LATERAL table function calls optional

The SQL standard and Calcite strictly requires the TABLE key word for LATERAL table function calls. However, this keyword bloats the syntax and many vendors treat it as optional. The Flink community has already made it optional for non-lateral table function calls (see Flink 2.0.0 announcement).

We propose to make the keyword optional for LATERAL table function calls as well. This would result in the following syntax:

SELECT *
FROM 
  t 
  CROSS JOIN LATERAL SNAPSHOT(TABLE s) ON t.a = s.a;

This change is somewhat independent of the core of this proposal but will make the feature easier to use. Hence, it is included here.

Backward Compatibility

We are not changing any existing behavior, but only introduce the SNAPSHOT function to express processing-time temporal table joins via a built-in table function (implemented as a stream operator). Event-time temporal joins or temporal table function joins are not affected.

The TABLE  keyword for LATERAL  function calls is not removed, just made optional.

Proposed Changes

LATERAL SNAPSHOT Join Syntax

Implementing the LATERAL SNAPSHOT join syntax requires the following changes:

SQL Parser

We make the TABLE keyword that precedes a LATERAL table function clause optional.

SNAPSHOT Function Proxy

We will implement a definition for the SNAPSHOT function and register it as a built-in function. The function implementation is just a placeholder to enable syntactic and semantic checks and generate a logical operator tree.

Planner Rules

We implement planner rules to replace a SNAPSHOT function in a LATERAL join context with an operator that performs the join.

Streaming Mode

In streaming mode, the SNAPSHOT function is replaced a with a new LateralSnapshotJoin operator. The join operator implementation is discussed in the Implementation Details section.

Batch Mode

The semantics of the SNAPSHOT function is that it returns some version of the versioned input table. In batch mode, we choose to return the final version of the input table. Hence, the SNAPSHOT function is replaced by a regular join operator.

Validation

During planning we will ensure that the following validations are applied:

  • Validate that the build-side input has a watermark definition.

  • Validate that the probe-side input is append-only.

  • Validate consistency of optional SNAPSHOT function parameters.

    • load_completed_condition => 'user_time' requires load_completed_time to be set and vice versa.


In case a validation fails, an error is raised.

If the SNAPSHOT function is used outside of a LATERAL context, we reject the query with an error message. FLIP-517 proposes different usages of SNAPSHOT outside of a LATERAL context. Until these become available, we need to block the usage of this table function.

LateralSnapshotJoin Operator

Add a new internal LateralSnapshotJoin stream operator. The optimizer will use it to implement a LATERAL SNAPSHOT function join. It will assemble the arguments for the LateralSnapshotJoin operator from the optimizer node ( joinType , joinCondition , and the args that the user provided to the SNAPSHOT function. If the user specified load_completed_condition => 'compile_time', the optimizer passes the current wall-clock time as loadCompletedTime.

Implementation Details

LateralSnapshotJoin Operator

Overview

The new LateralSnapshotJoin will be implemented as a stream operator. The optimizer will use it to implement a LATERAL SNAPSHOT function join.

Operator Arguments

  • joinType: INNER  or LEFT_OUTER
  • joinCondition: a generated function to evaluate the non-key join condition
  • loadCompletedTime: a timestamp literal that is compared against the build-side watermark to determine when to flip from LOAD  to JOIN  phase.
  • loadCompletedIdleTimeout (optional): processing-time duration of build-side watermark idleness after which the operator flips from LOAD  to JOIN  phase. The timeout should be configured if the build-side input does not continuously provide watermarks (i.e., it can become idle due to no data)
  • stateTtl  (optional): a duration for the minimum state TTL (max state TTL is 3/2 * minStateTTL).

State

The operator will be executed partitioned on the equality join predicates and feature the following states:

StateTypeDescription

Build-side table state

MapState<RowData, Long> 
(keyed state)

The key is a build-side record, the value the number of times it occurred. Since the state is keyed on the full build-side record, we can retract rows by decreasing the value counter. When the counter reaches 0, the full map entry is removed.

Build-side change buffer

ListState<RowData> 
(keyed state)

State to temporarily buffer build-side changes received in JOIN phase to ensure atomic updates.

Probe-side buffer

ListState<RowData> 
(keyed state)

Stores probe-side rows during LOAD phase. Empty in JOIN phase.

operator-phase

UnionListState<String> 
(operator state)

Stores the phase of the operator, LOAD or JOIN. State is set from phase local variable before a checkpoint is taken.

The join operator supports state TTL on the build-side state to delete keys that haven’t been accessed for a long time. State TTL is only activated after the join switched into the JOIN phase.

Join Phases and Input Processing

The join operator operates in two phases: LOAD and JOIN.

Operator start-up

Each operator instance tracks its current phase in a local variable phase (checkpointed via the operator-phase state). When the operator is started, we check the operator-phase union-list state. If the state is empty (initial start) or if there is any LOAD value in the list, the operator phase is set to LOAD, otherwise phase is set to JOIN.

If a loadCompletedIdleTimeout is configured, we register a non-keyed processing-time timer at now() + loadCompletedIdleTimeout when the operator is started.

LOAD Phase

During the LOAD phase (determined via the phase variable), the operator builds up the build-side state and does not perform any join operation until the flip point is reached. During the LOAD phase, all probe-side records are buffered in state. When the flip point is reached, all probe-side records are joined with the current build-side state and the results are emitted.

  • Build-side input processing

    • If the build-side change buffer contains data (might happen after restore)

      • apply the changes and clear the state before handling the input record

    • Apply all changes to the build-side state. No special handling for atomic updates needed.

  • Probe-side record processing

    • Add the record to the probe-side buffer ( ListState )

    • register an event-time timer with timestamp 1.

      • This timer will fire on the first forwarded watermark (watermarks are not forwarded during LOAD phase).

  • Watermark processing

    • Build-side watermark:

      • the load completion condition is checked for each watermark

        • if the condition is met, we remove the idle timeout timer and transition from LOAD to JOIN phase

        • if the condition is not met, the idle timeout processing-time timer is updated

    • Watermarks from neither side are forwarded downstream during the LOAD phase.

Transition (LOAD -> JOIN)

Once the load completion condition is met (or the idle timeout processing-time timer fired), the operator transitions from LOAD to JOIN phase

  • If the local phase variable is already set to JOIN (might happen if the proc-time timeout fires shortly after the load-completion condition met), the transition logic is not applied.

  • We set the local phase variable to JOIN.

    • The forwarding of probe-side watermarks is enabled.

  • The current probe-side watermark is immediately processed, which triggers all registered event-time timers (registered on timestamp 1). The callback function is invoked for key with at least one probe-side record in the buffer state.

    • The build-side state might be empty or not. If there are any changes buffered in the build-side change buffer state (might happen after a restore), these changes are applied first.

    • For each buffered probe-side record, we apply the regular join logic ( INNER or LEFT OUTER ) against all build-side records and emit the result records.

    • After all probe-side records were processed, the probe-side buffer state is cleared.

  • After all timers have been processed, the current probe-side watermark is emitted downstream.

  • If no probe-side WM was received yet, we don’t manually trigger the timers. The timers will fire on the first probe-side WM received (and processed) after the flip, even though the operator is already in JOIN phase.

JOIN Phase

Once the local phase variable is set to JOIN, the operator behaves similar to the existing processing-time temporal table join:

  • Build-side record processing

    • Applying build-side changes requires a bit of care to ensure atomic updates. Updates can be split into a pair of updateBefore and updateAfter (or even delete and insert) change records. Flink cannot guarantee that these records are processed right after each other. Other records might arrives between both changes, including a probe-side record. If we would apply a delete that represents the first half of an update and then receive a probe-side record that would match the updated value, the join would produce wrong results. In order to prevent this situation, we process build-side records during the JOIN phase as follows:

      • all change records are persisted into a buffer list state

      • the changes are applied when a build-side watermark is received. Watermarks will never split a pair of update change records.

    • This behavior adds some latency until a build-side update is applied, but ensures correctness because Flink guarantees that two associated change records are never interrupted by a watermark or another change on the same record.

    • The mechanism doesn’t add latency on the join processing side. Probe-side records are immediately joined, possibly against a slightly older build-side version. Since the processing-time temporal join doesn’t give guarantees against which build-side version is joined, this behavior is valid.

    • Build-side changes won’t be buffered for long due to missing watermarks. As long as the build-side has data flowing, trailing watermarks will arrive and apply the buffer.

    • The problem of atomic updates has been discussed in FLIP-558 in the context of the SinkUpsertMaterializer using terms “Internal Consistency” and “Compaction”. That FLIP also proposes watermark-based consolidation (“compaction”) or split update change records. For the new join operator, the problem is more severe because partial updates cause permanent incorrect results (not just “temporary”).

  • Probe-side record processing

    • Join the probe-side record against the current build-side state and emit the result.

    • No probe-side state is persisted.

  • Watermark processing

    • Probe-side watermarks are directly forwarded downstream.

    • Build-side watermarks are not forwarded

      • they are consumed by the operator and trigger build-side updates

      • they do not contribute to downstream progress

Recovery

  • The operator's behavior is controlled by the local phase variable. This variable is not persisted as state and needs to be restored on recovery. This is done when the operator is initialized using the operator-phase union-list state (see “Operator start-up” for details).

  • If we are still in LOAD, we need to ensure that the load-completed condition can be evaluated and that the idle timeout can be triggered.

    • The loadCompletedTime timestamp is “baked in” as an operator argument

    • If loadCompletedIdleTimeout is configured, we register the operator processing-time timeout timer.

    • Once the load completed condition is met or the idle timeout triggers, we follow the regular transition logic

      • the local phase variable is set to JOIN

      • the probe-side WM is processed, triggering all (remaining) event-time timers

  • Since joining is not fully deterministic (build-side state may differ slightly between runs), replayed results may differ from results emitted before the failure.

Metrics

The join operator will track the following metrics.

  • State metrics
    • cnt_probe_side_buffered : the number of probe-side records buffered during LOAD phase
    • cnt_build_side_buffered : the number of build-side records buffered during JOIN phase for atomic updates
    • cnt_state_ttl_evictions : the number of keys that have been evicted from state
  • Progress and phase metrics
    • current_build_side_wm : the current build-side WM. WM is not forwarded downstream, but used as condition to flip into JOIN phase.
    • current_probe_side_wm : the current probe-side WM.
    • current_phase : the current phase of the operator, LOAD or JOIN
  • Join stat metrics
    • max_join_fan_out : the maximum number of joined records emitted for a probe-side record
    • avg_join_fan_out : the average number of joined records emitted for a probe-side record
    • cnt_join_unmatched : the number of probe-side records without a match (only meaningful for INNER joins, always 0 for LEFT joins)
  • Changelog metrics
    • cnt_missing_retractions : the number of retraction messages that could not be applied

Comparison with Existing Temporal Table Join Operators

Aspect

Event-time Temporal Table Join

Processing-time Temporal Table Join (deactivated, to be removed)

LateralSnapshotJoin (new)

Semantics

Ensures that each probe-side record is joined with exactly the build-side table’s most recent version (as of the probe-record’s event-time).

Provides rough time-alignment of build-side and probe-side records if WM alignment is enabled. Does not ensure that build-side is “sufficiently” initialized before a probe-side record is joined (might result in “data loss” if build-side join key is not present).
Does not handle updates atomically, potentially causing data loss.

Provides rough time-alignment of build-side and probe-side records via WM alignment. Probe-side records are joined with a build-side record if it exists before the flip point (idle-timeout might weaken this promise but ensures transition to JOIN phase).

PK and Join Predicate requirements

Build-side requires PK. Join predicate must have equality predicates on build-side PK columns

Build-side requires PK. Join predicate must have equality predicates on build-side PK columns

No PK requirement for build-side.
Join predicate requires at least one equality predicate.

State

Build-side: one or more versions per key (consolidated on WM advancement).
Probe-side: buffers all records until WM passed by.

Build-side: one version per key.
Probe-side: no state.

LOAD: Latest materialized build-side table, all probe-side records.

JOIN: Latest materialized build-side table, temp buffer for build-side changes.

Small operator state

Dependency on WMs

Requires continuous watermark advancement on both inputs. Otherwise, the join stalls and accumulates a lot of state.

No dependency.

Requires watermarks on both inputs. Continuous build-side WM advancement not strictly required. Idle-timeout can ensure operator moves to JOIN phase.

Latency

Records are joined and emitted on WM progress.

Immediate join.

LOAD: no output until transition to JOIN phase. At this point, all buffered probe-side records are joined.
JOIN: Immediate join

Changelog modes

Build-side: any changelog mode.
Probe-side: any changelog mode.
Output: depends on probe-side changelog mode.

Probe-side retraction changes are poorly handled because they can easily produce wrong results because deletes are typically late and corresponding table versions already cleared.

Build-side: any changelog mode.
Probe-side: any changelog mode.
Output: depends on probe-side changelog mode.

Processing time semantics invalidate retractions (insert of a row may be joined with a different version than it’s retraction)

Build-side: any changelog mode.
Probe-side: append-only.
Output: append-only.

Scenario Analysis

How does the LateralSnapshotJoin operator behave under different input scenarios and watermark features.

Scenario

WM alignment

no WM alignment

Build-side input idle in LOAD phase (build-side data WM does not reach flip point)

orange circle Idle timeout on build-side watermark (if configured) triggers flip to JOIN phase.

orange circle Idle timeout on build-side watermark (if configured) triggers flip to JOIN phase.

Build-side input idle in JOIN phase

green circle No effect on operator. Probe-side records are joined with last build-side version.

green circle No effect on operator. Probe-side records are joined with last build-side version.

Probe-side timestamps (much) lower than build-side

orange circle Probe-side input is consumed first and buffered in the join operator until build-side reaches flip-point. Build and probe-side of joined records roughly aligned.

orange circle Both inputs are consumed w/o alignment. Probe-side input is buffered until build-side reaches flip-point (probably earlier than with WM alignment). Build and probe-side of joined records not aligned.

Build-side timestamps (much) lower than probe-side

green circle Build-side is loaded before probe-side. Join results are time-aligned. Good case.

orange circle Both inputs are consumed w/o alignment. Probe-side input is buffered until build-side reaches flip point (probably later than with WM alignment).

Probe-side and build-side timestamps roughly aligned

orange circle Both sides are concurrently read w/ WM alignment. Probe-side is buffered until build-side WM reaches flip point.

orange circle Both sides are concurrently read (WM are aligned by coincidence). Probe-side is buffered until build-side WM reaches flip point.

The build-side input is already past the flip point at query start (w/o state or recovery)

green circle The operator flips to JOIN phase as soon as the first build-side WM is received.
This is a similar behavior as the current processing-time temporal table join (except for the short initial buffering until the WM arrives)

green circle The operator flips to JOIN phase as soon as the first build-side WM is received.
This is a similar behavior as the current processing-time temporal table join (except for the short initial buffering until the WM arrives)

Summary of Watermark Feature Impact:

  • Without idle source WM progress: Risk of build-side not reaching flip point / transitioning to JOIN phase. Consequence: probe-side state grows unbounded, no results or watermarks are emitted. Configuring an idle timeout on build-side watermark mitigates this risk.

  • Without WM alignment: Time-alignment of join results is not present and potentially much worse. State footprint not necessarily much worse than with alignment (better and worse scenarios can be constructed).

  • With WM alignment: Probe-side records that are "older" than build-side records would be consumed and buffered before the build-side is read. This means the join might temporarily buffer a lot of data. Could be countered by setting the scan-offset of the probe-side with a TABLE HINT.

  • If the probe-side source connector supports configuring scan offsets (like the Kafka connector), users can control the amount of probe-side input that is temporarily persisted during LOAD phase by configuring the scan offsets of the probe-side table with a TABLE HINT.

Compatibility, Deprecation, and Migration Plan

  • Optional TABLE keyword: The TABLE  keyword becomes optional in LATERAL context, but is still accepted. Hence, there are not compatibility concerns.
  • New feature: The LATERAL SNAPSHOT syntax and the new operator are additive. No existing functionality is modified or removed.

  • Existing temporal joins: The existing TemporalRowTimeJoinOperator and its FOR SYSTEM_TIME AS OF syntax remain unchanged. TemporalProcessTimeJoinOperator is removed. It is currently not usable, because the corresponding optimizer rule is deactivated. The CompiledPlan feature was added about a year after the TemporalProcessTimeJoinOperator was disabled, so there shouldn’t be any plans using this operator.

  • State compatibility: The new join operator introduces new state descriptors. There is no migration from existing temporal join operator state.

  • Plan compatibility: A new operator does not affect existing compiled plans. New plans using the SNAPSHOT syntax will use the new LateralSnapshotJoinOperator. Removing the TemporalProcessTimeJoinOperator will not cause plan compatibility issues because CompiledPlans were added after the operator was disabled.

Test Plan

  • Unit tests for the LateralSnapshotJoin operator:

    • LOAD phase: build-side record handling (+I, +U, -U, -D) with and without buffered probe-side records.

    • LOAD phase: probe-side record handling.

    • Transition: correct behavior when build-side WM reaches flip time (INNER and LEFT OUTER).

    • JOIN phase: immediate join, no probe-side buffering, atomic updates of build-side state.

    • State TTL: verify TTL is disabled during LOAD and enabled during JOIN.

    • Recovery:

      • Recovery with all tasks in LOAD

      • Recovery with all tasks in JOIN

      • Recovery with mixed tasks in LOAD and JOIN (incl. non-empty build-side change buffer)

      • Recovery with the idle timer (verifies it gets re-armed on open)

  • Integration tests using MiniClusterExtension:

    • End-to-end SQL tests with LATERAL SNAPSHOT() syntax.

    • Different WM alignment configurations.

    • Idle source scenarios.

  • Planner tests:

    • Plan translation tests for the new SNAPSHOT syntax.

      • tests ensuring that SNAPSHOT  function is not accepted outside of a LATERAL  context.
    • Validation tests (changelog types, missing watermarks, etc.).

    • Syntax tests ensuring LATERAL clauses with and without TABLE  keyword are accepted
  • Restore tests:

    • verify a savepoint taken with this FLIP's initial release can be restored by subsequent versions

    • verify operator state schema (operator-phase, build-side start WM) is forward-compatible

Rejected Alternatives

Implementing LateralSnapshotJoin as user-defined PTF

The proposed operator design requires non-keyed operator state and timers which are not supported by PTFs (yet).

Implementing FLIP-326 to resolve limitations of processing-time temporal join

  • FLIP-326 touches watermark framework which is a core functionality of Flink

    • Invasive change to improve a single operator

    • Source operators taking control of WM emission is orthogonal to our design (and also to changing the WM framework as proposed in FLIP-326).

  • This FLIP lifts the build-side PK limitations

  • LATERAL table function syntax (as exists for temporal function join and reused in this FLIP) captures the intend of a look-up for each outer row very well.

Future Work

These are out of scope for the initial implementation but documented for future work.

Extend SNAPSHOT function to accept correlated time-attribute

  • Adds support for event-time temporal join semantics for SNAPSHOT function join

  • Implementation aligned with processing-time SNAPSHOT function

    • no PK requirement on build-side, join predicate doesn’t need build-side PK

    • append-only requirement on probe-side to prevent late records on retractions

    • implementation as a new stream operator that the optimizer uses if the SNAPSHOT proxy function is called with a correlated time-attribute

  • Improved versioned table semantics

    • requires some changes to event-time handling of retract and upsert changelog modes (separate FLIP planned).

  • Possibly deprecate non-standard use of FOR SYSTEM_TIME AS OF syntax for temporal joins and the existing event-time temporal join implementation.

Reduce Buffering of Probe-Side via Build-Side Watermark Suppression

  • The build-side source could send data but not emit any watermarks until the watermark is past the flip point.

  • Watermark alignment would then kick in and hold back the probe-side source from sending records.

  • Some initial probe-side records would still be sent, but this is not a problem because the operator buffers them.

  • This would move evaluation of the flip condition from the join operator to the build-side source operator. The join operator would not need to be changed.

  • Caveat: If there are watermark-sensitive operators between the build-side source and the join operator, these operators would accumulate the build-side data as state and emit their results only when the flip-point watermark arrives.

  • Alternative: We could emit emit watermarks but hold back the watermark reporting of the build-side source.

Support other load completion conditions

  • support a condition that flips after the build-side watermark has advanced for a certain duration

    • tracks the first build-side watermark and checks how far following watermarks have advanced

    • first build-side WM needs to be stored in operator state

Eager join mode during LOAD phase

  • The current design buffers all probe-side records until the flip point is reached. This might cause:

    • Large state because all probe-side records are buffered

    • Suddenly large join output when the flip-point is reached

    • No watermarks for downstream operators (but also no data)

  • This motivates an early join mode during LOAD phase based on alignment of build-side and probe-side watermarks. This would be a behavior similar to the current event-time processing time join.

    • reduces probe-side state size and join output at flip point

    • combined WM can be emitted

  • Once the join switches to JOIN phase, probe-side records are joined on arrival

  • This mode could be added later as an alternative to the full-blocking mode during LOAD.



  • No labels