Right now the only way to do stateful operations with with Aggregator or UDAF. However, this does not give users control of emission or expiration of state making it hard to implement things like sessionization. We should add a more general construct (probably similar to DStream.mapWithState) to structured streaming. Here is the design.
- Users should be able to specify a function that can do the following
- Access the input row corresponding to a key
- Access the previous state corresponding to a key
- Optionally, update or remove the state
- Output any number of new rows (or none at all)
Key Semantics of the State class
- The state can be null.
- If the state.remove() is called, then state.exists() will return false, and getOption will returm None.
- After that state.update(newState) is called, then state.exists() will return true, and getOption will return Some(...).
- None of the operations are thread-safe. This is to avoid memory barriers.
- Timeout based state expiration (that has not received data for a while) - Done
- General expression based expiration - TODO. Any real usecases that cannot be done with timeouts?