fedimser commented on code in PR #53159: URL: https://github.com/apache/spark/pull/53159#discussion_r2632833175
########## sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/transformwithstate/testing/InMemoryStatefulProcessorHandle.scala: ########## @@ -0,0 +1,352 @@ +/* + * 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.operators.stateful.transformwithstate.testing + +import java.time.{Clock, Instant} +import java.util.UUID + +import scala.collection.mutable +import scala.reflect.ClassTag + +import org.apache.spark.sql.Encoder +import org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.statefulprocessor.ImplicitGroupingKeyTracker +import org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.statefulprocessor.QueryInfoImpl +import org.apache.spark.sql.streaming.{ListState, MapState, QueryInfo, StatefulProcessorHandle, TimeMode, TTLConfig, ValueState} + +/** Helper to track expired keys. */ +class TtlTracker(val clock: Clock, ttl: TTLConfig) { + require(!ttl.ttlDuration.isNegative()) + private val keyToLastUpdatedTime = mutable.Map[Any, Instant]() + + def isKeyExpired(): Boolean = { + if (ttl.ttlDuration.isZero()) { + return false + } + val key = ImplicitGroupingKeyTracker.getImplicitKeyOption.get + if (!keyToLastUpdatedTime.contains(key)) { + return false + } + val expiration: Instant = keyToLastUpdatedTime.get(key).get.plus(ttl.ttlDuration) + return expiration.isBefore(clock.instant()) + } + + def onKeyUpdated(): Unit = { + val key = ImplicitGroupingKeyTracker.getImplicitKeyOption.get + keyToLastUpdatedTime.put(key, clock.instant()) + } +} + +/** In-memory implementation of ValueState. */ +class InMemoryValueState[T](clock: Clock, ttl: TTLConfig) extends ValueState[T] { + private val keyToStateValue = mutable.Map[Any, T]() + private val ttlTracker = new TtlTracker(clock, ttl) + + private def getValue: Option[T] = { + if (ttlTracker.isKeyExpired()) { + return None + } + keyToStateValue.get(ImplicitGroupingKeyTracker.getImplicitKeyOption.get) + } + override def exists(): Boolean = getValue.isDefined + + override def get(): T = getValue.getOrElse(null.asInstanceOf[T]) Review Comment: This doesn't compile. Error is `Cannot prove that Null <:< T`. This is because T can be primitve class and primitive classes cannot be null. Also: * Yes, this will be runtime error for primitive types when key does not exist. * But, this is how it's implemented in real ValueStateImpl: https://sourcegraph.prod.databricks-corp.com/databricks-eng/runtime/-/blob/sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/operators/stateful/transformwithstate/statevariables/ValueStateImpl.scala?L76 - this is why I did it the same way. * Apparently, to avoid runtime error, users are supposed to check exists() first. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
