You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by ka...@apache.org on 2025/02/06 08:23:10 UTC
(spark) branch branch-4.0 updated: [SPARK-51096][SQL][TESTS] Splitting TransformWithStateSuite into UnsafeRow and Avro encoding suites
This is an automated email from the ASF dual-hosted git repository.
kabhwan pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.0 by this push:
new 10a7431f956a [SPARK-51096][SQL][TESTS] Splitting TransformWithStateSuite into UnsafeRow and Avro encoding suites
10a7431f956a is described below
commit 10a7431f956a2f0370c2fb82b2c2a86a40a26d71
Author: Eric Marnadi <er...@databricks.com>
AuthorDate: Thu Feb 6 17:13:47 2025 +0900
[SPARK-51096][SQL][TESTS] Splitting TransformWithStateSuite into UnsafeRow and Avro encoding suites
### What changes were proposed in this pull request?
Splitting the TransformWithStateSuite into TransformWithStateUnsafeRowEncodingSuite and TransformWithStateAvroEncodingSuite, in order to remove it from the slow test category
### Why are the changes needed?
To remove TransformWithState-related suites from the Slow test category
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
Just moving tests around, no need to test
### Was this patch authored or co-authored using generative AI tooling?
No
Closes #49815 from ericm-db/split-tws-suite.
Authored-by: Eric Marnadi <er...@databricks.com>
Signed-off-by: Jungtaek Lim <ka...@gmail.com>
(cherry picked from commit 9dbbb5bd63ba89ff987931e5e72fcf3f3692d4b9)
Signed-off-by: Jungtaek Lim <ka...@gmail.com>
---
.../streaming/TransformWithStateAvroSuite.scala | 659 +++++++++++++++
.../sql/streaming/TransformWithStateSuite.scala | 915 +++------------------
.../TransformWithStateUnsafeRowSuite.scala | 133 +++
3 files changed, 896 insertions(+), 811 deletions(-)
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateAvroSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateAvroSuite.scala
new file mode 100644
index 000000000000..bddcece85486
--- /dev/null
+++ b/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateAvroSuite.scala
@@ -0,0 +1,659 @@
+/*
+ * 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.streaming
+
+import org.scalactic.source.Position
+import org.scalatest.Tag
+
+import org.apache.spark.SparkUnsupportedOperationException
+import org.apache.spark.sql.Row
+import org.apache.spark.sql.execution.datasources.v2.state.StateSourceOptions
+import org.apache.spark.sql.execution.streaming.MemoryStream
+import org.apache.spark.sql.execution.streaming.state.{RocksDBStateStoreProvider, StateStoreInvalidValueSchemaEvolution, StateStoreValueSchemaEvolutionThresholdExceeded}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.streaming.util.StreamManualClock
+
+class TransformWithStateAvroSuite extends TransformWithStateSuite {
+
+ import testImplicits._
+
+ override protected def test(testName: String, testTags: Tag*)(testBody: => Any)
+ (implicit pos: Position): Unit = {
+ super.test(s"$testName (encoding = Avro)", testTags: _*) {
+ withSQLConf(SQLConf.STREAMING_STATE_STORE_ENCODING_FORMAT.key -> "avro") {
+ testBody
+ }
+ }
+ }
+
+ test("transformWithState - incompatible schema evolution should fail") {
+ withSQLConf(
+ SQLConf.STATE_STORE_PROVIDER_CLASS.key -> classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.STREAMING_STATE_STORE_ENCODING_FORMAT.key -> "avro",
+ SQLConf.SHUFFLE_PARTITIONS.key -> "1") {
+ withTempDir { dir =>
+ val inputData = MemoryStream[String]
+
+ // First run with String field
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new ProcessorV1(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dir.getCanonicalPath),
+ AddData(inputData, "test1"),
+ CheckNewAnswer("test1"),
+ StopStream
+ )
+
+ // Second run with Long field
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new ProcessorV2(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dir.getCanonicalPath),
+ AddData(inputData, "test2"),
+ CheckNewAnswer("test2"),
+ StopStream
+ )
+
+ // Third run with Int field - should fail
+ val result3 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new ProcessorV3(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result3, OutputMode.Update())(
+ StartStream(checkpointLocation = dir.getCanonicalPath),
+ AddData(inputData, "test3"),
+ ExpectFailure[StateStoreInvalidValueSchemaEvolution] { e =>
+ checkError(
+ e.asInstanceOf[SparkUnsupportedOperationException],
+ condition = "STATE_STORE_INVALID_VALUE_SCHEMA_EVOLUTION",
+ parameters = Map(
+ "oldValueSchema" -> "StructType(StructField(value1,StringType,true))",
+ "newValueSchema" -> "StructType(StructField(value1,IntegerType,true))")
+ )
+ }
+ )
+ }
+ }
+ }
+
+ test("transformWithState - value schema threshold exceeded") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString,
+ SQLConf.STREAMING_VALUE_STATE_SCHEMA_EVOLUTION_THRESHOLD.key -> "0") {
+ withTempDir { chkptDir =>
+ val dirPath = chkptDir.getCanonicalPath
+ val inputData = MemoryStream[String]
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorInt(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ Execute { q =>
+ assert(q.lastProgress.stateOperators(0).customMetrics.get("numValueStateVars") > 0)
+ assert(q.lastProgress.stateOperators(0).customMetrics.get("numRegisteredTimers") == 0)
+ assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
+ },
+ AddData(inputData, "a", "b"),
+ CheckNewAnswer(("a", "2"), ("b", "1")),
+ StopStream,
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a", "b"), // should remove state for "a" and not return anything for a
+ CheckNewAnswer(("b", "2")),
+ StopStream,
+ Execute { q =>
+ assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
+ assert(q.lastProgress.stateOperators(0).numRowsRemoved === 1)
+ },
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a", "c"), // should recreate state for "a" and return count as 1 and
+ CheckNewAnswer(("a", "1"), ("c", "1"))
+ )
+
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ ExpectFailure[StateStoreValueSchemaEvolutionThresholdExceeded] { t =>
+ checkError(
+ t.asInstanceOf[StateStoreValueSchemaEvolutionThresholdExceeded],
+ condition = "STATE_STORE_VALUE_SCHEMA_EVOLUTION_THRESHOLD_EXCEEDED",
+ parameters = Map(
+ "numSchemaEvolutions" -> "1",
+ "maxSchemaEvolutions" -> "0",
+ "colFamilyName" -> "countState"
+ )
+ )
+ }
+ )
+ }
+ }
+ }
+
+ test("transformWithState - upcasting should succeed") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
+ withTempDir { chkptDir =>
+ val dirPath = chkptDir.getCanonicalPath
+ val inputData = MemoryStream[String]
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorInt(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ Execute { q =>
+ assert(q.lastProgress.stateOperators(0).customMetrics.get("numValueStateVars") > 0)
+ assert(q.lastProgress.stateOperators(0).customMetrics.get("numRegisteredTimers") == 0)
+ assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
+ },
+ AddData(inputData, "a", "b"),
+ CheckNewAnswer(("a", "2"), ("b", "1")),
+ StopStream,
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a", "b"), // should remove state for "a" and not return anything for a
+ CheckNewAnswer(("b", "2")),
+ StopStream,
+ Execute { q =>
+ assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
+ assert(q.lastProgress.stateOperators(0).numRowsRemoved === 1)
+ },
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a", "c"), // should recreate state for "a" and return count as 1 and
+ CheckNewAnswer(("a", "1"), ("c", "1"))
+ )
+
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "2")),
+ AddData(inputData, "d"),
+ CheckNewAnswer(("d", "1")),
+ StopStream
+ )
+ }
+ }
+ }
+
+ test("transformWithState - reordering fields should succeed") {
+ withSQLConf(
+ SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
+ withTempDir { chkptDir =>
+ val dirPath = chkptDir.getCanonicalPath
+ val inputData = MemoryStream[String]
+
+ // First run with initial field order
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorInitialOrder(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ StopStream
+ )
+
+ // Second run with reordered fields
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorReorderedFields(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "2")), // Should continue counting from previous state
+ StopStream
+ )
+ }
+ }
+ }
+
+ test("transformWithState - adding field should succeed") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
+ withTempDir { chkptDir =>
+ val dirPath = chkptDir.getCanonicalPath
+ val inputData = MemoryStream[String]
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ Execute { q =>
+ assert(q.lastProgress.stateOperators(0).customMetrics.get("numValueStateVars") > 0)
+ assert(q.lastProgress.stateOperators(0).customMetrics.get("numRegisteredTimers") == 0)
+ assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
+ },
+ AddData(inputData, "a", "b"),
+ CheckNewAnswer(("a", "2"), ("b", "1")),
+ StopStream,
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a", "b"), // should remove state for "a" and not return anything for a
+ CheckNewAnswer(("b", "2")),
+ StopStream,
+ Execute { q =>
+ assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
+ assert(q.lastProgress.stateOperators(0).numRowsRemoved === 1)
+ },
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a", "c"), // should recreate state for "a" and return count as 1 and
+ CheckNewAnswer(("a", "1"), ("c", "1"))
+ )
+
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorNestedLongs(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "2")),
+ StopStream
+ )
+ }
+ }
+ }
+
+ test("transformWithState - add and remove field between runs") {
+ withSQLConf(
+ SQLConf.STATE_STORE_PROVIDER_CLASS.key -> classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key -> "1") {
+ withTempDir { dir =>
+ val inputData = MemoryStream[String]
+
+ // First run with original field names
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorInitialOrder(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dir.getCanonicalPath),
+ AddData(inputData, "test1"),
+ CheckNewAnswer(("test1", "1")),
+ StopStream
+ )
+
+ // Second run with renamed field
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RenameEvolvedProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dir.getCanonicalPath),
+ // Uses default value, does not factor previous value1 into this
+ AddData(inputData, "test1"),
+ CheckNewAnswer(("test1", "1")),
+ // Verify we can write state with new field name
+ AddData(inputData, "test2"),
+ CheckNewAnswer(("test2", "1")),
+ StopStream
+ )
+ }
+ }
+ }
+
+ test("state data source - schema evolution with time travel support") {
+ withSQLConf(
+ rocksdbChangelogCheckpointingConfKey -> "true",
+ SQLConf.STREAMING_MAINTENANCE_INTERVAL.key -> "100",
+ SQLConf.STATE_STORE_PROVIDER_CLASS.key -> classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key -> "1",
+ SQLConf.STATE_STORE_MIN_DELTAS_FOR_SNAPSHOT.key -> "1") {
+
+ withTempDir { chkptDir =>
+ val dirPath = chkptDir.getCanonicalPath
+ val inputData = MemoryStream[String]
+
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorTwoLongs(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ AddData(inputData, "b"),
+ CheckNewAnswer(("b", "1")),
+ ProcessAllAvailable(),
+ Execute { _ => Thread.sleep(5000) },
+ StopStream
+ )
+
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RenameEvolvedProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "c"),
+ CheckNewAnswer(("c", "1")),
+ AddData(inputData, "d"),
+ CheckNewAnswer(("d", "1")),
+ ProcessAllAvailable(),
+ Execute { _ => Thread.sleep(5000) },
+ StopStream
+ )
+
+ val oldStateDf = spark.read
+ .format("statestore")
+ .option("snapshotStartBatchId", 0)
+ .option("batchId", 1)
+ .option("snapshotPartitionId", 0)
+ .option(StateSourceOptions.STATE_VAR_NAME, "countState")
+ .load(dirPath)
+
+ checkAnswer(
+ oldStateDf.selectExpr(
+ "key.value AS groupingKey",
+ "value.value1 AS count"),
+ Seq(Row("a", 1), Row("b", 1))
+ )
+
+ val evolvedStateDf1 = spark.read
+ .format("statestore")
+ .option("snapshotStartBatchId", 0)
+ .option("batchId", 3)
+ .option("snapshotPartitionId", 0)
+ .option(StateSourceOptions.STATE_VAR_NAME, "countState")
+ .load(dirPath)
+
+ checkAnswer(
+ evolvedStateDf1.selectExpr(
+ "key.value AS groupingKey",
+ "value.value4 AS count"),
+ Seq(
+ Row("a", null),
+ Row("b", null),
+ Row("c", 1),
+ Row("d", 1)
+ )
+ )
+
+ val evolvedStateDf = spark.read
+ .format("statestore")
+ .option("snapshotStartBatchId", 3)
+ .option("snapshotPartitionId", 0)
+ .option(StateSourceOptions.STATE_VAR_NAME, "countState")
+ .load(dirPath)
+
+ checkAnswer(
+ evolvedStateDf.selectExpr(
+ "key.value AS groupingKey",
+ "value.value4 AS count"),
+ Seq(
+ Row("a", null),
+ Row("b", null),
+ Row("c", 1),
+ Row("d", 1)
+ )
+ )
+ }
+ }
+ }
+
+ test("transformWithState - verify default values during schema evolution") {
+ withSQLConf(
+ SQLConf.STATE_STORE_PROVIDER_CLASS.key -> classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key -> "1") {
+ withTempDir { dir =>
+ val inputData = MemoryStream[String]
+
+ // First run with basic schema
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new DefaultValueInitialProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dir.getCanonicalPath),
+ AddData(inputData, "test1"),
+ CheckNewAnswer(("test1", BasicState("test1".hashCode, "test1"))),
+ StopStream
+ )
+
+ // Second run with evolved schema to check defaults
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new DefaultValueEvolvedProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dir.getCanonicalPath),
+
+ // Check existing state - new fields should get default values
+ AddData(inputData, "test1"),
+ CheckNewAnswer(
+ ("test1", EvolvedState(
+ id = "test1".hashCode,
+ name = "test1",
+ count = 0L,
+ active = false,
+ score = 0.0
+ ))
+ ),
+
+ // New state should get initialized values, not defaults
+ AddData(inputData, "test2"),
+ CheckNewAnswer(
+ ("test2", EvolvedState(
+ id = "test2".hashCode,
+ name = "test2",
+ count = 100L,
+ active = true,
+ score = 99.9
+ ))
+ ),
+ StopStream
+ )
+ }
+ }
+ }
+
+ test("transformWithState - removing field should succeed") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
+ withTempDir { chkptDir =>
+ val dirPath = chkptDir.getCanonicalPath
+ val inputData = MemoryStream[String]
+
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorTwoLongs(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ StopStream
+ )
+
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = dirPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ StopStream
+ )
+ }
+ }
+ }
+
+ test("test that invalid schema evolution " +
+ "fails query for column family") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
+ withTempDir { checkpointDir =>
+ val inputData = MemoryStream[String]
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ StopStream
+ )
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorInt(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
+ AddData(inputData, "a"),
+ ExpectFailure[StateStoreInvalidValueSchemaEvolution] { e =>
+ checkError(
+ e.asInstanceOf[SparkUnsupportedOperationException],
+ condition = "STATE_STORE_INVALID_VALUE_SCHEMA_EVOLUTION",
+ parameters = Map(
+ "oldValueSchema" -> "StructType(StructField(value,LongType,true))",
+ "newValueSchema" -> "StructType(StructField(value,IntegerType,true))")
+ )
+ }
+ )
+ }
+ }
+ }
+
+ test("test that introducing TTL after restart fails query") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
+ withTempDir { checkpointDir =>
+ val inputData = MemoryStream[String]
+ val clock = new StreamManualClock
+ val result = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessor(),
+ TimeMode.ProcessingTime(),
+ OutputMode.Update())
+
+ testStream(result, OutputMode.Update())(
+ StartStream(
+ trigger = Trigger.ProcessingTime("1 second"),
+ checkpointLocation = checkpointDir.getCanonicalPath,
+ triggerClock = clock),
+ AddData(inputData, "a"),
+ AdvanceManualClock(1 * 1000),
+ CheckNewAnswer(("a", "1")),
+ AdvanceManualClock(1 * 1000),
+ StopStream
+ )
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorWithTTL(),
+ TimeMode.ProcessingTime(),
+ OutputMode.Update())
+ testStream(result2, OutputMode.Update())(
+ StartStream(
+ trigger = Trigger.ProcessingTime("1 second"),
+ checkpointLocation = checkpointDir.getCanonicalPath,
+ triggerClock = clock),
+ AddData(inputData, "a"),
+ AdvanceManualClock(1 * 1000),
+ ExpectFailure[StateStoreInvalidValueSchemaEvolution] { e =>
+ checkError(
+ e.asInstanceOf[SparkUnsupportedOperationException],
+ condition = "STATE_STORE_INVALID_VALUE_SCHEMA_EVOLUTION",
+ parameters = Map(
+ "newValueSchema" -> ("StructType(StructField(value,StructType(StructField(" +
+ "value,LongType,true)),true),StructField(ttlExpirationMs,LongType,true))"),
+ "oldValueSchema" -> "StructType(StructField(value,LongType,true))")
+ )
+ }
+ )
+ }
+ }
+ }
+}
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateSuite.scala
index a76e63df873e..2b37fd156266 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateSuite.scala
@@ -38,7 +38,6 @@ import org.apache.spark.sql.functions.timestamp_seconds
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.streaming.util.StreamManualClock
import org.apache.spark.sql.types._
-import org.apache.spark.tags.SlowSQLTest
object TransformWithStateSuiteUtils {
val NUM_SHUFFLE_PARTITIONS = 5
@@ -735,9 +734,8 @@ class SleepingTimerProcessor extends StatefulProcessor[String, String, String] {
/**
* Class that adds tests for transformWithState stateful streaming operator
*/
-@SlowSQLTest
-class TransformWithStateSuite extends StateStoreMetricsTest
- with AlsoTestWithEncodingTypes with AlsoTestWithRocksDBFeatures {
+abstract class TransformWithStateSuite extends StateStoreMetricsTest
+ with AlsoTestWithRocksDBFeatures {
import testImplicits._
@@ -924,467 +922,6 @@ class TransformWithStateSuite extends StateStoreMetricsTest
}
}
- testWithEncoding("avro")("transformWithState - value schema threshold exceeded") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString,
- SQLConf.STREAMING_VALUE_STATE_SCHEMA_EVOLUTION_THRESHOLD.key -> "0") {
- withTempDir { chkptDir =>
- val dirPath = chkptDir.getCanonicalPath
- val inputData = MemoryStream[String]
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorInt(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- Execute { q =>
- assert(q.lastProgress.stateOperators(0).customMetrics.get("numValueStateVars") > 0)
- assert(q.lastProgress.stateOperators(0).customMetrics.get("numRegisteredTimers") == 0)
- assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
- },
- AddData(inputData, "a", "b"),
- CheckNewAnswer(("a", "2"), ("b", "1")),
- StopStream,
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a", "b"), // should remove state for "a" and not return anything for a
- CheckNewAnswer(("b", "2")),
- StopStream,
- Execute { q =>
- assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
- assert(q.lastProgress.stateOperators(0).numRowsRemoved === 1)
- },
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a", "c"), // should recreate state for "a" and return count as 1 and
- CheckNewAnswer(("a", "1"), ("c", "1"))
- )
-
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- ExpectFailure[StateStoreValueSchemaEvolutionThresholdExceeded] { t =>
- checkError(
- t.asInstanceOf[StateStoreValueSchemaEvolutionThresholdExceeded],
- condition = "STATE_STORE_VALUE_SCHEMA_EVOLUTION_THRESHOLD_EXCEEDED",
- parameters = Map(
- "numSchemaEvolutions" -> "1",
- "maxSchemaEvolutions" -> "0",
- "colFamilyName" -> "countState"
- )
- )
- }
- )
- }
- }
- }
-
- testWithEncoding("avro")("transformWithState - upcasting should succeed") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { chkptDir =>
- val dirPath = chkptDir.getCanonicalPath
- val inputData = MemoryStream[String]
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorInt(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- Execute { q =>
- assert(q.lastProgress.stateOperators(0).customMetrics.get("numValueStateVars") > 0)
- assert(q.lastProgress.stateOperators(0).customMetrics.get("numRegisteredTimers") == 0)
- assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
- },
- AddData(inputData, "a", "b"),
- CheckNewAnswer(("a", "2"), ("b", "1")),
- StopStream,
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a", "b"), // should remove state for "a" and not return anything for a
- CheckNewAnswer(("b", "2")),
- StopStream,
- Execute { q =>
- assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
- assert(q.lastProgress.stateOperators(0).numRowsRemoved === 1)
- },
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a", "c"), // should recreate state for "a" and return count as 1 and
- CheckNewAnswer(("a", "1"), ("c", "1"))
- )
-
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "2")),
- AddData(inputData, "d"),
- CheckNewAnswer(("d", "1")),
- StopStream
- )
- }
- }
- }
-
- testWithEncoding("avro")("transformWithState - reordering fields should succeed") {
- withSQLConf(
- SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { chkptDir =>
- val dirPath = chkptDir.getCanonicalPath
- val inputData = MemoryStream[String]
-
- // First run with initial field order
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorInitialOrder(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- StopStream
- )
-
- // Second run with reordered fields
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorReorderedFields(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "2")), // Should continue counting from previous state
- StopStream
- )
- }
- }
- }
-
- testWithEncoding("avro")("transformWithState - adding field should succeed") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { chkptDir =>
- val dirPath = chkptDir.getCanonicalPath
- val inputData = MemoryStream[String]
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- Execute { q =>
- assert(q.lastProgress.stateOperators(0).customMetrics.get("numValueStateVars") > 0)
- assert(q.lastProgress.stateOperators(0).customMetrics.get("numRegisteredTimers") == 0)
- assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
- },
- AddData(inputData, "a", "b"),
- CheckNewAnswer(("a", "2"), ("b", "1")),
- StopStream,
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a", "b"), // should remove state for "a" and not return anything for a
- CheckNewAnswer(("b", "2")),
- StopStream,
- Execute { q =>
- assert(q.lastProgress.stateOperators(0).numRowsUpdated === 1)
- assert(q.lastProgress.stateOperators(0).numRowsRemoved === 1)
- },
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a", "c"), // should recreate state for "a" and return count as 1 and
- CheckNewAnswer(("a", "1"), ("c", "1"))
- )
-
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorNestedLongs(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "2")),
- StopStream
- )
- }
- }
- }
-
- testWithEncoding("avro")("transformWithState - add and remove field between runs") {
- withSQLConf(
- SQLConf.STATE_STORE_PROVIDER_CLASS.key -> classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key -> "1") {
- withTempDir { dir =>
- val inputData = MemoryStream[String]
-
- // First run with original field names
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorInitialOrder(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dir.getCanonicalPath),
- AddData(inputData, "test1"),
- CheckNewAnswer(("test1", "1")),
- StopStream
- )
-
- // Second run with renamed field
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RenameEvolvedProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dir.getCanonicalPath),
- // Uses default value, does not factor previous value1 into this
- AddData(inputData, "test1"),
- CheckNewAnswer(("test1", "1")),
- // Verify we can write state with new field name
- AddData(inputData, "test2"),
- CheckNewAnswer(("test2", "1")),
- StopStream
- )
- }
- }
- }
-
- testWithEncoding("avro")("state data source - schema evolution with time travel support") {
- withSQLConf(
- rocksdbChangelogCheckpointingConfKey -> "true",
- SQLConf.STREAMING_MAINTENANCE_INTERVAL.key -> "100",
- SQLConf.STATE_STORE_PROVIDER_CLASS.key -> classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key -> "1",
- SQLConf.STATE_STORE_MIN_DELTAS_FOR_SNAPSHOT.key -> "1") {
-
- withTempDir { chkptDir =>
- val dirPath = chkptDir.getCanonicalPath
- val inputData = MemoryStream[String]
-
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorTwoLongs(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- AddData(inputData, "b"),
- CheckNewAnswer(("b", "1")),
- ProcessAllAvailable(),
- Execute { _ => Thread.sleep(5000) },
- StopStream
- )
-
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RenameEvolvedProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "c"),
- CheckNewAnswer(("c", "1")),
- AddData(inputData, "d"),
- CheckNewAnswer(("d", "1")),
- ProcessAllAvailable(),
- Execute { _ => Thread.sleep(5000) },
- StopStream
- )
-
- val oldStateDf = spark.read
- .format("statestore")
- .option("snapshotStartBatchId", 0)
- .option("batchId", 1)
- .option("snapshotPartitionId", 0)
- .option(StateSourceOptions.STATE_VAR_NAME, "countState")
- .load(dirPath)
-
- checkAnswer(
- oldStateDf.selectExpr(
- "key.value AS groupingKey",
- "value.value1 AS count"),
- Seq(Row("a", 1), Row("b", 1))
- )
-
- val evolvedStateDf1 = spark.read
- .format("statestore")
- .option("snapshotStartBatchId", 0)
- .option("batchId", 3)
- .option("snapshotPartitionId", 0)
- .option(StateSourceOptions.STATE_VAR_NAME, "countState")
- .load(dirPath)
-
- checkAnswer(
- evolvedStateDf1.selectExpr(
- "key.value AS groupingKey",
- "value.value4 AS count"),
- Seq(
- Row("a", null),
- Row("b", null),
- Row("c", 1),
- Row("d", 1)
- )
- )
-
- val evolvedStateDf = spark.read
- .format("statestore")
- .option("snapshotStartBatchId", 3)
- .option("snapshotPartitionId", 0)
- .option(StateSourceOptions.STATE_VAR_NAME, "countState")
- .load(dirPath)
-
- checkAnswer(
- evolvedStateDf.selectExpr(
- "key.value AS groupingKey",
- "value.value4 AS count"),
- Seq(
- Row("a", null),
- Row("b", null),
- Row("c", 1),
- Row("d", 1)
- )
- )
- }
- }
- }
-
- testWithEncoding("avro")("transformWithState - verify default values during schema evolution") {
- withSQLConf(
- SQLConf.STATE_STORE_PROVIDER_CLASS.key -> classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key -> "1") {
- withTempDir { dir =>
- val inputData = MemoryStream[String]
-
- // First run with basic schema
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new DefaultValueInitialProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dir.getCanonicalPath),
- AddData(inputData, "test1"),
- CheckNewAnswer(("test1", BasicState("test1".hashCode, "test1"))),
- StopStream
- )
-
- // Second run with evolved schema to check defaults
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new DefaultValueEvolvedProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dir.getCanonicalPath),
-
- // Check existing state - new fields should get default values
- AddData(inputData, "test1"),
- CheckNewAnswer(
- ("test1", EvolvedState(
- id = "test1".hashCode,
- name = "test1",
- count = 0L,
- active = false,
- score = 0.0
- ))
- ),
-
- // New state should get initialized values, not defaults
- AddData(inputData, "test2"),
- CheckNewAnswer(
- ("test2", EvolvedState(
- id = "test2".hashCode,
- name = "test2",
- count = 100L,
- active = true,
- score = 99.9
- ))
- ),
- StopStream
- )
- }
- }
- }
-
- testWithEncoding("avro")("transformWithState - removing field should succeed") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { chkptDir =>
- val dirPath = chkptDir.getCanonicalPath
- val inputData = MemoryStream[String]
-
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorTwoLongs(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- StopStream
- )
-
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dirPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- StopStream
- )
- }
- }
- }
-
test("transformWithState - streaming with rocksdb and processing time timer " +
"and updating timers should succeed") {
withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
@@ -1806,104 +1343,108 @@ class TransformWithStateSuite extends StateStoreMetricsTest
}
}
- Seq(false, true).foreach { useImplicits =>
- test("transformWithState - verify StateSchemaV3 writes " +
- s"correct SQL schema of key/value with useImplicits=$useImplicits") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { checkpointDir =>
- // When Avro is used, we want to set the StructFields to nullable
- val shouldBeNullable = usingAvroEncoding()
- val metadataPathPostfix = "state/0/_stateSchema/default"
- val stateSchemaPath = new Path(checkpointDir.toString,
- s"$metadataPathPostfix")
- val hadoopConf = spark.sessionState.newHadoopConf()
- val fm = CheckpointFileManager.create(stateSchemaPath, hadoopConf)
-
- val keySchema = new StructType().add("value", StringType)
- val schema0 = StateStoreColFamilySchema(
- "countState", 0,
- keySchema, 0,
- new StructType().add("value", LongType, nullable = shouldBeNullable),
- Some(NoPrefixKeyStateEncoderSpec(keySchema)),
- None
- )
- val schema1 = StateStoreColFamilySchema(
- "listState", 0,
- keySchema, 0,
- new StructType()
- .add("id", LongType, nullable = shouldBeNullable)
- .add("name", StringType),
- Some(NoPrefixKeyStateEncoderSpec(keySchema)),
- None
- )
-
- val userKeySchema = new StructType()
- .add("id", IntegerType, false)
- .add("name", StringType)
- val compositeKeySchema = new StructType()
- .add("key", new StructType().add("value", StringType))
- .add("userKey", userKeySchema)
- val schema2 = StateStoreColFamilySchema(
- "mapState", 0,
- compositeKeySchema, 0,
- new StructType().add("value", StringType),
- Some(PrefixKeyScanStateEncoderSpec(compositeKeySchema, 1)),
- Option(userKeySchema)
- )
-
- val schema3 = StateStoreColFamilySchema(
- "$rowCounter_listState", 0,
- keySchema, 0,
- new StructType().add("count", LongType, nullable = shouldBeNullable),
- Some(NoPrefixKeyStateEncoderSpec(keySchema)),
- None
- )
-
- val schema4 = StateStoreColFamilySchema(
- "default", 0,
- keySchema, 0,
- new StructType().add("value", BinaryType),
- Some(NoPrefixKeyStateEncoderSpec(keySchema)),
- None
- )
-
- val inputData = MemoryStream[String]
- val result = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new StatefulProcessorWithCompositeTypes(useImplicits),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result, OutputMode.Update())(
- StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
- AddData(inputData, "a", "b"),
- CheckNewAnswer(("a", "1"), ("b", "1")),
- Execute { q =>
- q.lastProgress.runId
- val schemaFilePath = fm.list(stateSchemaPath).toSeq.head.getPath
- val providerId = StateStoreProviderId(StateStoreId(
- checkpointDir.getCanonicalPath, 0, 0), q.lastProgress.runId)
- val checker = new StateSchemaCompatibilityChecker(providerId,
- hadoopConf, List(schemaFilePath))
- val colFamilySeq = checker.readSchemaFile()
-
- assert(TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS ==
- q.lastProgress.stateOperators.head.customMetrics.get("numValueStateVars").toInt)
- assert(TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS ==
- q.lastProgress.stateOperators.head.customMetrics.get("numListStateVars").toInt)
- assert(TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS ==
- q.lastProgress.stateOperators.head.customMetrics.get("numMapStateVars").toInt)
-
- assert(colFamilySeq.length == 5)
- assert(colFamilySeq.map(_.toString).toSet == Set(
- schema0, schema1, schema2, schema3, schema4
- ).map(_.toString))
- },
- StopStream
- )
+ Seq("unsaferow", "avro").foreach { encoding =>
+ Seq(false, true).foreach { useImplicits =>
+ test("transformWithState - verify StateSchemaV3 writes " +
+ s"correct SQL schema of key/value with useImplicits=$useImplicits " +
+ s"(encoding = $encoding)") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString,
+ SQLConf.STREAMING_STATE_STORE_ENCODING_FORMAT.key -> encoding) {
+ withTempDir { checkpointDir =>
+ // When Avro is used, we want to set the StructFields to nullable
+ val shouldBeNullable = encoding == "avro"
+ val metadataPathPostfix = "state/0/_stateSchema/default"
+ val stateSchemaPath = new Path(checkpointDir.toString,
+ s"$metadataPathPostfix")
+ val hadoopConf = spark.sessionState.newHadoopConf()
+ val fm = CheckpointFileManager.create(stateSchemaPath, hadoopConf)
+
+ val keySchema = new StructType().add("value", StringType)
+ val schema0 = StateStoreColFamilySchema(
+ "countState", 0,
+ keySchema, 0,
+ new StructType().add("value", LongType, nullable = shouldBeNullable),
+ Some(NoPrefixKeyStateEncoderSpec(keySchema)),
+ None
+ )
+ val schema1 = StateStoreColFamilySchema(
+ "listState", 0,
+ keySchema, 0,
+ new StructType()
+ .add("id", LongType, nullable = shouldBeNullable)
+ .add("name", StringType),
+ Some(NoPrefixKeyStateEncoderSpec(keySchema)),
+ None
+ )
+
+ val userKeySchema = new StructType()
+ .add("id", IntegerType, false)
+ .add("name", StringType)
+ val compositeKeySchema = new StructType()
+ .add("key", new StructType().add("value", StringType))
+ .add("userKey", userKeySchema)
+ val schema2 = StateStoreColFamilySchema(
+ "mapState", 0,
+ compositeKeySchema, 0,
+ new StructType().add("value", StringType),
+ Some(PrefixKeyScanStateEncoderSpec(compositeKeySchema, 1)),
+ Option(userKeySchema)
+ )
+
+ val schema3 = StateStoreColFamilySchema(
+ "$rowCounter_listState", 0,
+ keySchema, 0,
+ new StructType().add("count", LongType, nullable = shouldBeNullable),
+ Some(NoPrefixKeyStateEncoderSpec(keySchema)),
+ None
+ )
+
+ val schema4 = StateStoreColFamilySchema(
+ "default", 0,
+ keySchema, 0,
+ new StructType().add("value", BinaryType),
+ Some(NoPrefixKeyStateEncoderSpec(keySchema)),
+ None
+ )
+
+ val inputData = MemoryStream[String]
+ val result = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new StatefulProcessorWithCompositeTypes(useImplicits),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result, OutputMode.Update())(
+ StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
+ AddData(inputData, "a", "b"),
+ CheckNewAnswer(("a", "1"), ("b", "1")),
+ Execute { q =>
+ q.lastProgress.runId
+ val schemaFilePath = fm.list(stateSchemaPath).toSeq.head.getPath
+ val providerId = StateStoreProviderId(StateStoreId(
+ checkpointDir.getCanonicalPath, 0, 0), q.lastProgress.runId)
+ val checker = new StateSchemaCompatibilityChecker(providerId,
+ hadoopConf, List(schemaFilePath))
+ val colFamilySeq = checker.readSchemaFile()
+
+ assert(TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS ==
+ q.lastProgress.stateOperators.head.customMetrics.get("numValueStateVars").toInt)
+ assert(TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS ==
+ q.lastProgress.stateOperators.head.customMetrics.get("numListStateVars").toInt)
+ assert(TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS ==
+ q.lastProgress.stateOperators.head.customMetrics.get("numMapStateVars").toInt)
+
+ assert(colFamilySeq.length == 5)
+ assert(colFamilySeq.map(_.toString).toSet == Set(
+ schema0, schema1, schema2, schema3, schema4
+ ).map(_.toString))
+ },
+ StopStream
+ )
+ }
}
}
}
@@ -1953,88 +1494,6 @@ class TransformWithStateSuite extends StateStoreMetricsTest
}
}
- testWithEncoding("unsaferow")("test that invalid schema evolution " +
- "fails query for column family") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { checkpointDir =>
- val inputData = MemoryStream[String]
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- StopStream
- )
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorInt(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
- AddData(inputData, "a"),
- ExpectFailure[StateStoreValueSchemaNotCompatible] {
- (t: Throwable) => {
- assert(t.getMessage.contains("Please check number and type of fields."))
- }
- }
- )
- }
- }
- }
-
- testWithEncoding("avro")("test that invalid schema evolution " +
- "fails query for column family") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { checkpointDir =>
- val inputData = MemoryStream[String]
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessor(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
- AddData(inputData, "a"),
- CheckNewAnswer(("a", "1")),
- StopStream
- )
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorInt(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
- AddData(inputData, "a"),
- ExpectFailure[StateStoreInvalidValueSchemaEvolution] { e =>
- checkError(
- e.asInstanceOf[SparkUnsupportedOperationException],
- condition = "STATE_STORE_INVALID_VALUE_SCHEMA_EVOLUTION",
- parameters = Map(
- "oldValueSchema" -> "StructType(StructField(value,LongType,true))",
- "newValueSchema" -> "StructType(StructField(value,IntegerType,true))")
- )
- }
- )
- }
- }
- }
-
test("test that different outputMode after query restart fails") {
withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
classOf[RocksDBStateStoreProvider].getName,
@@ -2167,112 +1626,6 @@ class TransformWithStateSuite extends StateStoreMetricsTest
}
}
- testWithEncoding("unsaferow")("test that introducing TTL after restart fails query") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { checkpointDir =>
- val inputData = MemoryStream[String]
- val clock = new StreamManualClock
- val result = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessor(),
- TimeMode.ProcessingTime(),
- OutputMode.Update())
-
- testStream(result, OutputMode.Update())(
- StartStream(
- trigger = Trigger.ProcessingTime("1 second"),
- checkpointLocation = checkpointDir.getCanonicalPath,
- triggerClock = clock),
- AddData(inputData, "a"),
- AdvanceManualClock(1 * 1000),
- CheckNewAnswer(("a", "1")),
- AdvanceManualClock(1 * 1000),
- StopStream
- )
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorWithTTL(),
- TimeMode.ProcessingTime(),
- OutputMode.Update())
- testStream(result2, OutputMode.Update())(
- StartStream(
- trigger = Trigger.ProcessingTime("1 second"),
- checkpointLocation = checkpointDir.getCanonicalPath,
- triggerClock = clock),
- AddData(inputData, "a"),
- AdvanceManualClock(1 * 1000),
- ExpectFailure[StateStoreValueSchemaNotCompatible] { t =>
- checkError(
- t.asInstanceOf[SparkUnsupportedOperationException],
- condition = "STATE_STORE_VALUE_SCHEMA_NOT_COMPATIBLE",
- parameters = Map(
- "storedValueSchema" -> "StructType(StructField(value,LongType,false))",
- "newValueSchema" ->
- ("StructType(StructField(value,StructType(StructField(value,LongType,false))," +
- "true),StructField(ttlExpirationMs,LongType,true))")
- )
- )
- }
- )
- }
- }
- }
-
- testWithEncoding("avro")("test that introducing TTL after restart fails query") {
- withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
- classOf[RocksDBStateStoreProvider].getName,
- SQLConf.SHUFFLE_PARTITIONS.key ->
- TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
- withTempDir { checkpointDir =>
- val inputData = MemoryStream[String]
- val clock = new StreamManualClock
- val result = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessor(),
- TimeMode.ProcessingTime(),
- OutputMode.Update())
-
- testStream(result, OutputMode.Update())(
- StartStream(
- trigger = Trigger.ProcessingTime("1 second"),
- checkpointLocation = checkpointDir.getCanonicalPath,
- triggerClock = clock),
- AddData(inputData, "a"),
- AdvanceManualClock(1 * 1000),
- CheckNewAnswer(("a", "1")),
- AdvanceManualClock(1 * 1000),
- StopStream
- )
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new RunningCountStatefulProcessorWithTTL(),
- TimeMode.ProcessingTime(),
- OutputMode.Update())
- testStream(result2, OutputMode.Update())(
- StartStream(
- trigger = Trigger.ProcessingTime("1 second"),
- checkpointLocation = checkpointDir.getCanonicalPath,
- triggerClock = clock),
- AddData(inputData, "a"),
- AdvanceManualClock(1 * 1000),
- ExpectFailure[StateStoreInvalidValueSchemaEvolution] { e =>
- checkError(
- e.asInstanceOf[SparkUnsupportedOperationException],
- condition = "STATE_STORE_INVALID_VALUE_SCHEMA_EVOLUTION",
- parameters = Map(
- "newValueSchema" -> ("StructType(StructField(value,StructType(StructField(" +
- "value,LongType,true)),true),StructField(ttlExpirationMs,LongType,true))"),
- "oldValueSchema" -> "StructType(StructField(value,LongType,true))")
- )
- }
- )
- }
- }
- }
-
test("test query restart with new state variable succeeds") {
withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
classOf[RocksDBStateStoreProvider].getName,
@@ -2936,66 +2289,6 @@ class TransformWithStateSuite extends StateStoreMetricsTest
}
}
}
-
- testWithEncoding("avro")("transformWithState - incompatible schema evolution should fail") {
- withSQLConf(
- SQLConf.STATE_STORE_PROVIDER_CLASS.key -> classOf[RocksDBStateStoreProvider].getName,
- SQLConf.STREAMING_STATE_STORE_ENCODING_FORMAT.key -> "avro",
- SQLConf.SHUFFLE_PARTITIONS.key -> "1") {
- withTempDir { dir =>
- val inputData = MemoryStream[String]
-
- // First run with String field
- val result1 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new ProcessorV1(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result1, OutputMode.Update())(
- StartStream(checkpointLocation = dir.getCanonicalPath),
- AddData(inputData, "test1"),
- CheckNewAnswer("test1"),
- StopStream
- )
-
- // Second run with Long field
- val result2 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new ProcessorV2(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result2, OutputMode.Update())(
- StartStream(checkpointLocation = dir.getCanonicalPath),
- AddData(inputData, "test2"),
- CheckNewAnswer("test2"),
- StopStream
- )
-
- // Third run with Int field - should fail
- val result3 = inputData.toDS()
- .groupByKey(x => x)
- .transformWithState(new ProcessorV3(),
- TimeMode.None(),
- OutputMode.Update())
-
- testStream(result3, OutputMode.Update())(
- StartStream(checkpointLocation = dir.getCanonicalPath),
- AddData(inputData, "test3"),
- ExpectFailure[StateStoreInvalidValueSchemaEvolution] { e =>
- checkError(
- e.asInstanceOf[SparkUnsupportedOperationException],
- condition = "STATE_STORE_INVALID_VALUE_SCHEMA_EVOLUTION",
- parameters = Map(
- "oldValueSchema" -> "StructType(StructField(value1,StringType,true))",
- "newValueSchema" -> "StructType(StructField(value1,IntegerType,true))")
- )
- }
- )
- }
- }
- }
}
class TransformWithStateValidationSuite extends StateStoreMetricsTest {
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateUnsafeRowSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateUnsafeRowSuite.scala
new file mode 100644
index 000000000000..b8d29560521a
--- /dev/null
+++ b/sql/core/src/test/scala/org/apache/spark/sql/streaming/TransformWithStateUnsafeRowSuite.scala
@@ -0,0 +1,133 @@
+/*
+ * 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.streaming
+
+import org.scalactic.source.Position
+import org.scalatest.Tag
+
+import org.apache.spark.SparkUnsupportedOperationException
+import org.apache.spark.sql.execution.streaming.MemoryStream
+import org.apache.spark.sql.execution.streaming.state.{RocksDBStateStoreProvider, StateStoreValueSchemaNotCompatible}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.streaming.util.StreamManualClock
+
+class TransformWithStateUnsafeRowSuite extends TransformWithStateSuite {
+
+ import testImplicits._
+
+ override protected def test(testName: String, testTags: Tag*)(testBody: => Any)
+ (implicit pos: Position): Unit = {
+ super.test(s"$testName (encoding = UnsafeRow)", testTags: _*) {
+ withSQLConf(SQLConf.STREAMING_STATE_STORE_ENCODING_FORMAT.key -> "unsaferow") {
+ testBody
+ }
+ }
+ }
+
+ test("test that invalid schema evolution fails query for column family") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
+ withTempDir { checkpointDir =>
+ val inputData = MemoryStream[String]
+ val result1 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessor(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result1, OutputMode.Update())(
+ StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
+ AddData(inputData, "a"),
+ CheckNewAnswer(("a", "1")),
+ StopStream
+ )
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorInt(),
+ TimeMode.None(),
+ OutputMode.Update())
+
+ testStream(result2, OutputMode.Update())(
+ StartStream(checkpointLocation = checkpointDir.getCanonicalPath),
+ AddData(inputData, "a"),
+ ExpectFailure[StateStoreValueSchemaNotCompatible] {
+ (t: Throwable) => {
+ assert(t.getMessage.contains("Please check number and type of fields."))
+ }
+ }
+ )
+ }
+ }
+ }
+
+ test("test that introducing TTL after restart fails query") {
+ withSQLConf(SQLConf.STATE_STORE_PROVIDER_CLASS.key ->
+ classOf[RocksDBStateStoreProvider].getName,
+ SQLConf.SHUFFLE_PARTITIONS.key ->
+ TransformWithStateSuiteUtils.NUM_SHUFFLE_PARTITIONS.toString) {
+ withTempDir { checkpointDir =>
+ val inputData = MemoryStream[String]
+ val clock = new StreamManualClock
+ val result = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessor(),
+ TimeMode.ProcessingTime(),
+ OutputMode.Update())
+
+ testStream(result, OutputMode.Update())(
+ StartStream(
+ trigger = Trigger.ProcessingTime("1 second"),
+ checkpointLocation = checkpointDir.getCanonicalPath,
+ triggerClock = clock),
+ AddData(inputData, "a"),
+ AdvanceManualClock(1 * 1000),
+ CheckNewAnswer(("a", "1")),
+ AdvanceManualClock(1 * 1000),
+ StopStream
+ )
+ val result2 = inputData.toDS()
+ .groupByKey(x => x)
+ .transformWithState(new RunningCountStatefulProcessorWithTTL(),
+ TimeMode.ProcessingTime(),
+ OutputMode.Update())
+ testStream(result2, OutputMode.Update())(
+ StartStream(
+ trigger = Trigger.ProcessingTime("1 second"),
+ checkpointLocation = checkpointDir.getCanonicalPath,
+ triggerClock = clock),
+ AddData(inputData, "a"),
+ AdvanceManualClock(1 * 1000),
+ ExpectFailure[StateStoreValueSchemaNotCompatible] { t =>
+ checkError(
+ t.asInstanceOf[SparkUnsupportedOperationException],
+ condition = "STATE_STORE_VALUE_SCHEMA_NOT_COMPATIBLE",
+ parameters = Map(
+ "storedValueSchema" -> "StructType(StructField(value,LongType,false))",
+ "newValueSchema" ->
+ ("StructType(StructField(value,StructType(StructField(value,LongType,false))," +
+ "true),StructField(ttlExpirationMs,LongType,true))")
+ )
+ )
+ }
+ )
+ }
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org