anishshri-db commented on code in PR #53159: URL: https://github.com/apache/spark/pull/53159#discussion_r2656016198
########## sql/core/src/main/scala/org/apache/spark/sql/streaming/TwsTester.scala: ########## @@ -0,0 +1,286 @@ +/* + * 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 java.sql.Timestamp +import java.time.{Clock, Instant, ZoneId} + +import scala.reflect.ClassTag + +import org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.statefulprocessor.ImplicitGroupingKeyTracker +import org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.testing.InMemoryStatefulProcessorHandle +import org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.timers.{ExpiredTimerInfoImpl, TimerValuesImpl} + +/** + * Testing utility for transformWithState stateful processors. + * + * This class enables unit testing of StatefulProcessor business logic by simulating the + * behavior of transformWithState. It processes input rows and returns output rows equivalent + * to those that would be produced by the processor in an actual Spark streaming query. + * + * '''Supported:''' + * - Processing input rows and producing output rows via `test()`. + * - Initial state setup via constructor parameter. + * - Direct state manipulation via `setValueState`, `setListState`, `setMapState`. + * - Direct state inspection via `peekValueState`, `peekListState`, `peekMapState`. + * - Timers in ProcessingTime mode (use `advanceProcessingTime` to fire timers). + * - Timers in EventTime mode (use `eventTimeExtractor` and `watermarkDelayMs` to configure; + * watermark advances automatically based on event times, or use `advanceWatermark` manually). + * - Late event filtering in EventTime mode (events older than the current watermark are dropped). + * - TTL for ValueState, ListState, and MapState (use ProcessingTime mode and + * `advanceProcessingTime` to test expiry). + * + * '''Testing EventTime Mode:''' + * To test with EventTime, provide `eventTimeExtractor` (a function extracting the event + * timestamp from each input row) and `watermarkDelayMs` (the watermark delay in milliseconds). + * The watermark is computed as `max(event_time_seen) - watermarkDelayMs` and is updated + * automatically after each `test()` call. Late events (with event time older than the current + * watermark) are filtered out before processing, matching production behavior. Timers with + * expiry time <= watermark will fire. You can also manually advance the watermark using + * `advanceWatermark()`. + * + * '''Use Cases:''' + * - '''Primary''': Unit testing business logic in `handleInputRows` implementations. + * - '''Not recommended''': End-to-end testing or performance testing - use actual Spark + * streaming queries for those scenarios. + * + * @param processor the StatefulProcessor to test. + * @param initialState initial state for each key as a list of (key, state) tuples. + * @param timeMode time mode (None, ProcessingTime or EventTime). + * @param outputMode output mode (Append, Update, or Complete). + * @param realTimeMode whether input rows should be processed one-by-one (separate call to + * handleInputRows) for each input row. + * @param eventTimeExtractor function to extract event time from input rows. Required if and + * only if timeMode is EventTime. + * @param watermarkDelayMs watermark delay in milliseconds. The watermark is computed as + * `max(event_time) - watermarkDelayMs`. Required if and only if timeMode is EventTime. + * @tparam K the type of grouping key. + * @tparam I the type of input rows. + * @tparam O the type of output rows. + * @since 4.0.2 + */ +class TwsTester[K, I, O]( + val processor: StatefulProcessor[K, I, O], + val initialState: List[(K, Any)] = List(), + val timeMode: TimeMode = TimeMode.None, + val outputMode: OutputMode = OutputMode.Append, + val realTimeMode: Boolean = false, + val eventTimeExtractor: I => Timestamp = null, + val watermarkDelayMs: Long = 0L) { + val clock: Clock = new Clock { + override def instant(): Instant = Instant.ofEpochMilli(currentProcessingTimeMs) + override def getZone: ZoneId = ZoneId.systemDefault() Review Comment: Do we need to override these methods ? -- 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]
