-
Notifications
You must be signed in to change notification settings - Fork 28k
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
[SS][SPARK-47331] Serialization using case classes/primitives/POJO based on SQL encoder for Arbitrary State API v2. #45447
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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
78 changes: 78 additions & 0 deletions
78
sql/core/src/test/java/org/apache/spark/sql/execution/streaming/state/POJOTestClass.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.execution.streaming.state; | ||
|
||
/** | ||
* A POJO class used for tests of arbitrary state SQL encoder. | ||
*/ | ||
public class POJOTestClass { | ||
// Fields | ||
private String name; | ||
private int id; | ||
|
||
// Constructors | ||
public POJOTestClass() { | ||
// Default constructor | ||
} | ||
|
||
public POJOTestClass(String name, int id) { | ||
this.name = name; | ||
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. indent/spacing seems off ? |
||
this.id = id; | ||
} | ||
|
||
// Getter methods | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getAge() { | ||
return id; | ||
} | ||
|
||
// Setter methods | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setAge(int id) { | ||
this.id = id; | ||
} | ||
|
||
// Additional methods if needed | ||
public void incrementId() { | ||
id++; | ||
System.out.println(name + " is now " + id + "!"); | ||
} | ||
|
||
// Override toString for better representation | ||
@Override | ||
public String toString() { | ||
return "POJOTestClass{" + | ||
"name='" + name + '\'' + | ||
jingz-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
", age=" + id + | ||
'}'; | ||
} | ||
|
||
// Override equals and hashCode for custom equality | ||
@Override | ||
public boolean equals(Object obj) { | ||
POJOTestClass testObj = (POJOTestClass) obj; | ||
return id == testObj.id && name.equals(testObj.name); | ||
} | ||
} | ||
|
This file contains 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 |
---|---|---|
|
@@ -33,6 +33,9 @@ import org.apache.spark.sql.streaming.ValueState | |
import org.apache.spark.sql.test.SharedSparkSession | ||
import org.apache.spark.sql.types._ | ||
|
||
/** A case class for SQL encoder test purpose */ | ||
case class TestClass(var id: Long, var name: String) | ||
|
||
/** | ||
* Class that adds tests for single value ValueState types used in arbitrary stateful | ||
* operators such as transformWithState | ||
|
@@ -93,7 +96,7 @@ class ValueStateSuite extends SharedSparkSession | |
Encoders.STRING.asInstanceOf[ExpressionEncoder[Any]]) | ||
|
||
val stateName = "testState" | ||
val testState: ValueState[Long] = handle.getValueState[Long]("testState") | ||
val testState: ValueState[Long] = handle.getValueState[Long]("testState", Encoders.scalaLong) | ||
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. Can we also add a test for other common types such as Double/String etc ? |
||
assert(ImplicitGroupingKeyTracker.getImplicitKeyOption.isEmpty) | ||
val ex = intercept[Exception] { | ||
testState.update(123) | ||
|
@@ -136,7 +139,7 @@ class ValueStateSuite extends SharedSparkSession | |
val handle = new StatefulProcessorHandleImpl(store, UUID.randomUUID(), | ||
Encoders.STRING.asInstanceOf[ExpressionEncoder[Any]]) | ||
|
||
val testState: ValueState[Long] = handle.getValueState[Long]("testState") | ||
val testState: ValueState[Long] = handle.getValueState[Long]("testState", Encoders.scalaLong) | ||
ImplicitGroupingKeyTracker.setImplicitKey("test_key") | ||
testState.update(123) | ||
assert(testState.get() === 123) | ||
|
@@ -162,8 +165,10 @@ class ValueStateSuite extends SharedSparkSession | |
val handle = new StatefulProcessorHandleImpl(store, UUID.randomUUID(), | ||
Encoders.STRING.asInstanceOf[ExpressionEncoder[Any]]) | ||
|
||
val testState1: ValueState[Long] = handle.getValueState[Long]("testState1") | ||
val testState2: ValueState[Long] = handle.getValueState[Long]("testState2") | ||
val testState1: ValueState[Long] = handle.getValueState[Long]( | ||
"testState1", Encoders.scalaLong) | ||
val testState2: ValueState[Long] = handle.getValueState[Long]( | ||
"testState2", Encoders.scalaLong) | ||
ImplicitGroupingKeyTracker.setImplicitKey("test_key") | ||
testState1.update(123) | ||
assert(testState1.get() === 123) | ||
|
@@ -217,4 +222,56 @@ class ValueStateSuite extends SharedSparkSession | |
matchPVals = true | ||
) | ||
} | ||
|
||
test("test SQL encoder - Value state operations for case class instances") { | ||
tryWithProviderResource(newStoreProviderWithValueState(true)) { provider => | ||
val store = provider.getStore(0) | ||
val handle = new StatefulProcessorHandleImpl(store, UUID.randomUUID(), | ||
Encoders.STRING.asInstanceOf[ExpressionEncoder[Any]]) | ||
|
||
val testState: ValueState[TestClass] = handle.getValueState[TestClass]("testState", | ||
Encoders.product[TestClass]) | ||
ImplicitGroupingKeyTracker.setImplicitKey("test_key") | ||
testState.update(TestClass(1, "testcase1")) | ||
assert(testState.get().equals(TestClass(1, "testcase1"))) | ||
testState.clear() | ||
assert(!testState.exists()) | ||
assert(testState.get() === null) | ||
|
||
testState.update(TestClass(2, "testcase2")) | ||
assert(testState.get() === TestClass(2, "testcase2")) | ||
testState.update(TestClass(3, "testcase3")) | ||
assert(testState.get() === TestClass(3, "testcase3")) | ||
|
||
testState.clear() | ||
assert(!testState.exists()) | ||
assert(testState.get() === null) | ||
} | ||
} | ||
|
||
test("test SQL encoder - Value state operations for POJO instances") { | ||
tryWithProviderResource(newStoreProviderWithValueState(true)) { provider => | ||
val store = provider.getStore(0) | ||
val handle = new StatefulProcessorHandleImpl(store, UUID.randomUUID(), | ||
Encoders.STRING.asInstanceOf[ExpressionEncoder[Any]]) | ||
|
||
val testState: ValueState[POJOTestClass] = handle.getValueState[POJOTestClass]("testState", | ||
Encoders.bean(classOf[POJOTestClass])) | ||
ImplicitGroupingKeyTracker.setImplicitKey("test_key") | ||
testState.update(new POJOTestClass("testcase1", 1)) | ||
assert(testState.get().equals(new POJOTestClass("testcase1", 1))) | ||
testState.clear() | ||
assert(!testState.exists()) | ||
assert(testState.get() === null) | ||
|
||
testState.update(new POJOTestClass("testcase2", 2)) | ||
assert(testState.get().equals(new POJOTestClass("testcase2", 2))) | ||
testState.update(new POJOTestClass("testcase3", 3)) | ||
assert(testState.get().equals(new POJOTestClass("testcase3", 3))) | ||
|
||
testState.clear() | ||
assert(!testState.exists()) | ||
assert(testState.get() === null) | ||
} | ||
} | ||
} |
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.
Nit: Could we rename type param to
V
instead ?