Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17179#discussion_r106736186
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/FlatMapGroupsWithStateExec.scala
 ---
    @@ -0,0 +1,270 @@
    +/*
    + * 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.apache.spark.rdd.RDD
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
    +import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, 
AttributeReference, Expression, Literal, SortOrder, SpecificInternalRow, 
UnsafeProjection, UnsafeRow}
    +import org.apache.spark.sql.catalyst.plans.logical.{LogicalKeyedState, 
ProcessingTimeTimeout}
    +import 
org.apache.spark.sql.catalyst.plans.physical.{ClusteredDistribution, 
Distribution, Partitioning}
    +import org.apache.spark.sql.execution._
    +import org.apache.spark.sql.execution.streaming.state._
    +import org.apache.spark.sql.streaming.{KeyedStateTimeout, OutputMode}
    +import org.apache.spark.sql.types.{BooleanType, IntegerType}
    +import org.apache.spark.util.CompletionIterator
    +
    +/**
    + * Physical operator for executing `FlatMapGroupsWithState.`
    + *
    + * @param func function called on each group
    + * @param keyDeserializer used to extract the key object for each group.
    + * @param valueDeserializer used to extract the items in the iterator from 
an input row.
    + * @param groupingAttributes used to group the data
    + * @param dataAttributes used to read the data
    + * @param outputObjAttr used to define the output object
    + * @param stateEncoder used to serialize/deserialize state before calling 
`func`
    + * @param outputMode the output mode of `func`
    + * @param timeout used to timeout groups that have not received data in a 
while
    + * @param batchTimestampMs processing timestamp of the current batch.
    + */
    +case class FlatMapGroupsWithStateExec(
    +    func: (Any, Iterator[Any], LogicalKeyedState[Any]) => Iterator[Any],
    +    keyDeserializer: Expression,
    +    valueDeserializer: Expression,
    +    groupingAttributes: Seq[Attribute],
    +    dataAttributes: Seq[Attribute],
    +    outputObjAttr: Attribute,
    +    stateId: Option[OperatorStateId],
    +    stateEncoder: ExpressionEncoder[Any],
    +    outputMode: OutputMode,
    +    timeout: KeyedStateTimeout,
    +    batchTimestampMs: Long,
    +    child: SparkPlan) extends UnaryExecNode with ObjectProducerExec with 
StateStoreWriter {
    +
    +  private val isTimeoutEnabled = timeout == ProcessingTimeTimeout
    +  private val timestampTimeoutAttribute =
    +    AttributeReference("timeoutTimestamp", dataType = IntegerType, 
nullable = false)()
    +  private val stateExistsAttribute =
    +    AttributeReference("stateExists", dataType = BooleanType, nullable = 
false)()
    +  private val stateAttributes: Seq[Attribute] = {
    +    val encoderSchemaAttributes = stateEncoder.schema.toAttributes
    +    if (isTimeoutEnabled) {
    +      encoderSchemaAttributes :+ stateExistsAttribute :+ 
timestampTimeoutAttribute
    +    } else encoderSchemaAttributes
    +  }
    +
    +  import KeyedStateImpl._
    +  override def outputPartitioning: Partitioning = child.outputPartitioning
    +
    +  /** Distribute by grouping attributes */
    +  override def requiredChildDistribution: Seq[Distribution] =
    +    ClusteredDistribution(groupingAttributes) :: Nil
    +
    +  /** Ordering needed for using GroupingIterator */
    +  override def requiredChildOrdering: Seq[Seq[SortOrder]] =
    +    Seq(groupingAttributes.map(SortOrder(_, Ascending)))
    +
    +  override protected def doExecute(): RDD[InternalRow] = {
    +    metrics // force lazy init at driver
    +
    +    child.execute().mapPartitionsWithStateStore[InternalRow](
    +      getStateId.checkpointLocation,
    +      getStateId.operatorId,
    +      getStateId.batchId,
    +      groupingAttributes.toStructType,
    +      stateAttributes.toStructType,
    +      sqlContext.sessionState,
    +      Some(sqlContext.streams.stateStoreCoordinator)) { case (store, 
iterator) =>
    +        val updater = new StateStoreUpdater(store)
    +
    +        // Generate a iterator that returns the rows grouped by the 
grouping function
    +        // Note that this code ensures that the filtering for timeout 
occurs only after
    +        // all the data has been processed. This is to ensure that the 
timeout information of all
    +        // the keys with data is updated before they are processed for 
timeouts.
    +        val outputIterator =
    +          Seq(
    --- End diff --
    
    The parameter in `Iterator.++` is call-by-name: `def ++[B >: A](that: => 
GenTraversableOnce[B]): Iterator[B]`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to