-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-35897][SS] Support user defined initial state with flatMapGroupsWithState in Structured Streaming #33093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
rahulsmahadev
wants to merge
15
commits into
apache:master
from
rahulsmahadev:flatMapGroupsWithState
Closed
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6c1443c
FlatMapGroupsWith state accept initial state
rahulsmahadev ec2b972
Fix build
rahulsmahadev f221358
makes tests cleaner
rahulsmahadev 673008d
simplify test
rahulsmahadev a25ea47
init -> initialState
rahulsmahadev ef50c79
merge with latest master
rahulsmahadev 7629d2e
address some stuff
rahulsmahadev e2234a8
Fix case class test
rahulsmahadev 7d7b5a9
remove dead code
rahulsmahadev 57c9c2f
Addressed a bunch of comments
rahulsmahadev 4e85a62
fix java apis
rahulsmahadev 0374bdb
address comments
rahulsmahadev b8c70ab
aadd java tests and remove extra tests
rahulsmahadev b47ac23
Add test
rahulsmahadev eb83b68
Address comments
rahulsmahadev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -440,7 +440,7 @@ object FlatMapGroupsWithState { | |
| isMapGroupsWithState: Boolean, | ||
| timeout: GroupStateTimeout, | ||
| child: LogicalPlan): LogicalPlan = { | ||
| val encoder = encoderFor[S] | ||
| val stateEncoder = encoderFor[S] | ||
|
|
||
| val mapped = new FlatMapGroupsWithState( | ||
| func, | ||
|
|
@@ -449,10 +449,49 @@ object FlatMapGroupsWithState { | |
| groupingAttributes, | ||
| dataAttributes, | ||
| CatalystSerde.generateObjAttr[U], | ||
| encoder.asInstanceOf[ExpressionEncoder[Any]], | ||
| stateEncoder.asInstanceOf[ExpressionEncoder[Any]], | ||
| outputMode, | ||
| isMapGroupsWithState, | ||
| timeout, | ||
| hasInitialState = false, | ||
| groupingAttributes, | ||
| dataAttributes, | ||
| UnresolvedDeserializer(encoderFor[K].deserializer, groupingAttributes), | ||
| LocalRelation(stateEncoder.schema.toAttributes), // empty data set | ||
| child | ||
| ) | ||
| CatalystSerde.serialize[U](mapped) | ||
| } | ||
|
|
||
| def apply[K: Encoder, V: Encoder, S: Encoder, U: Encoder]( | ||
| func: (Any, Iterator[Any], LogicalGroupState[Any]) => Iterator[Any], | ||
| groupingAttributes: Seq[Attribute], | ||
| dataAttributes: Seq[Attribute], | ||
| outputMode: OutputMode, | ||
| isMapGroupsWithState: Boolean, | ||
| timeout: GroupStateTimeout, | ||
| child: LogicalPlan, | ||
| initialStateGroupAttrs: Seq[Attribute], | ||
| initialStateDataAttrs: Seq[Attribute], | ||
| initialState: LogicalPlan): LogicalPlan = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: init and initial. be consistent
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| val stateEncoder = encoderFor[S] | ||
|
|
||
| val mapped = new FlatMapGroupsWithState( | ||
| func, | ||
| UnresolvedDeserializer(encoderFor[K].deserializer, groupingAttributes), | ||
| UnresolvedDeserializer(encoderFor[V].deserializer, dataAttributes), | ||
| groupingAttributes, | ||
| dataAttributes, | ||
| CatalystSerde.generateObjAttr[U], | ||
| stateEncoder.asInstanceOf[ExpressionEncoder[Any]], | ||
| outputMode, | ||
| isMapGroupsWithState, | ||
| timeout, | ||
| hasInitialState = true, | ||
| initialStateGroupAttrs, | ||
| initialStateDataAttrs, | ||
| UnresolvedDeserializer(encoderFor[S].deserializer, initialStateDataAttrs), | ||
| initialState, | ||
| child) | ||
| CatalystSerde.serialize[U](mapped) | ||
| } | ||
|
|
@@ -474,6 +513,12 @@ object FlatMapGroupsWithState { | |
| * @param outputMode the output mode of `func` | ||
| * @param isMapGroupsWithState whether it is created by the `mapGroupsWithState` method | ||
| * @param timeout used to timeout groups that have not received data in a while | ||
| * @param hasInitialState Indicates whether initial state needs to be applied or not. | ||
| * @param initialStateGroupAttrs grouping attributes for the initial state | ||
| * @param initialStateDataAttrs used to read the initial state | ||
| * @param initialStateDeserializer used to extract the initial state objects. | ||
| * @param initialState user defined initial state that is applied in the first batch. | ||
| * @param child logical plan of the underlying data | ||
| */ | ||
| case class FlatMapGroupsWithState( | ||
| func: (Any, Iterator[Any], LogicalGroupState[Any]) => Iterator[Any], | ||
|
|
@@ -486,14 +531,24 @@ case class FlatMapGroupsWithState( | |
| outputMode: OutputMode, | ||
| isMapGroupsWithState: Boolean = false, | ||
| timeout: GroupStateTimeout, | ||
| child: LogicalPlan) extends UnaryNode with ObjectProducer { | ||
| hasInitialState: Boolean = false, | ||
| initialStateGroupAttrs: Seq[Attribute], | ||
| initialStateDataAttrs: Seq[Attribute], | ||
| initialStateDeserializer: Expression, | ||
| initialState: LogicalPlan, | ||
| child: LogicalPlan) extends BinaryNode with ObjectProducer { | ||
|
|
||
| if (isMapGroupsWithState) { | ||
| assert(outputMode == OutputMode.Update) | ||
| } | ||
|
|
||
| override protected def withNewChildInternal(newChild: LogicalPlan): FlatMapGroupsWithState = | ||
| copy(child = newChild) | ||
| override def left: LogicalPlan = child | ||
|
|
||
| override def right: LogicalPlan = initialState | ||
|
|
||
| override protected def withNewChildrenInternal( | ||
| newLeft: LogicalPlan, newRight: LogicalPlan): FlatMapGroupsWithState = | ||
| copy(child = newLeft, initialState = newRight) | ||
| } | ||
|
|
||
| /** Factory for constructing new `FlatMapGroupsInR` nodes. */ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-streaming DataFrame/Dataset is not supported as the initial state in [flatMap|map]GroupsWithState operation on a streamiing DataFrame/Dataset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done