forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
50 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/StateVariableInfo.scala
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,71 @@ | ||
/* | ||
* 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 | ||
|
||
import org.json4s.{DefaultFormats, Formats, JValue} | ||
import org.json4s.JsonAST.{JBool, JString} | ||
import org.json4s.JsonDSL._ | ||
|
||
// Enum to store the types of state variables we support | ||
sealed trait StateVariableType | ||
|
||
case object ValueState extends StateVariableType | ||
case object ListState extends StateVariableType | ||
case object MapState extends StateVariableType | ||
|
||
// This object is used to convert the state type from string to the corresponding enum | ||
object StateVariableType { | ||
def withName(name: String): StateVariableType = name match { | ||
case "ValueState" => ValueState | ||
case "ListState" => ListState | ||
case "MapState" => MapState | ||
case _ => throw new IllegalArgumentException(s"Unknown state type: $name") | ||
} | ||
} | ||
|
||
// This class is used to store the information about a state variable. | ||
// It is stored in operatorProperties for the TransformWithStateExec operator | ||
// to be able to validate that the State Variables are the same across restarts. | ||
class StateVariableInfo( | ||
val stateName: String, | ||
val stateType: StateVariableType, | ||
val isTtlEnabled: Boolean | ||
) { | ||
def jsonValue: JValue = { | ||
("stateName" -> JString(stateName)) ~ | ||
("stateType" -> JString(stateType.toString)) ~ | ||
("isTtlEnabled" -> JBool(isTtlEnabled)) | ||
} | ||
} | ||
|
||
// This object is used to convert the state variable information | ||
// from JSON to a list of StateVariableInfo | ||
object StateVariableInfo { | ||
implicit val formats: Formats = DefaultFormats | ||
def fromJson(json: Any): List[StateVariableInfo] = { | ||
assert(json.isInstanceOf[List[_]], s"Expected List but got ${json.getClass}") | ||
val stateVariables = json.asInstanceOf[List[Map[String, Any]]] | ||
// Extract each JValue to StateVariableInfo | ||
stateVariables.map { stateVariable => | ||
new StateVariableInfo( | ||
stateVariable("stateName").asInstanceOf[String], | ||
StateVariableType.withName(stateVariable("stateType").asInstanceOf[String]), | ||
stateVariable("isTtlEnabled").asInstanceOf[Boolean] | ||
) | ||
} | ||
} | ||
} |
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