bogao007 commented on code in PR #41558: URL: https://github.com/apache/spark/pull/41558#discussion_r1230200585
########## connector/connect/common/src/main/java/org/apache/spark/api/java/function/FlatMapGroupsWithStateFunction.java: ########## @@ -0,0 +1,39 @@ +/* + * 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.api.java.function; + +import java.io.Serializable; +import java.util.Iterator; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.annotation.Experimental; +import org.apache.spark.sql.streaming.GroupState; + +/** + * ::Experimental:: + * Base interface for a map function used in + * {@code org.apache.spark.sql.KeyValueGroupedDataset.flatMapGroupsWithState( + * FlatMapGroupsWithStateFunction, org.apache.spark.sql.streaming.OutputMode, + * org.apache.spark.sql.Encoder, org.apache.spark.sql.Encoder)} + * @since 2.1.1 Review Comment: The funcs need to be under common package since it's also being used by `UdfUtils`. I don't see a util package in common, do you want me to create one? I think previously you mentioned that there was an effort to build that util package and framework, is that already completed? ########## connector/connect/client/jvm/src/test/scala/org/apache/spark/sql/KeyValueGroupedDatasetE2ETestSuite.scala: ########## @@ -447,4 +449,20 @@ class KeyValueGroupedDatasetE2ETestSuite extends QueryTest with SQLHelper { checkDataset(keys, "1", "2", "10", "20") } + + test("flatMapGroupsWithState") { + val stateFunc = (key: String, values: Iterator[String], state: GroupState[Int]) => { + if (state.exists) throw new IllegalArgumentException("state.exists should be false") + Iterator((key, values.size)) + } + + val session: SparkSession = spark + import session.implicits._ + val values = Seq("a", "a", "b", "c", "c", "c", "c").toDS() + .groupByKey(x => x) + .flatMapGroupsWithState(Append, GroupStateTimeout.NoTimeout)(stateFunc) Review Comment: Thanks @zhenlineo, it worked after updating the conversion func! ########## connector/connect/common/src/main/scala/org/apache/spark/sql/connect/common/UdfUtils.scala: ########## @@ -95,6 +96,26 @@ private[sql] object UdfUtils extends Serializable { } } + def mapValuesAdaptor[K, V, S, U, IV]( + f: (K, Iterator[V], GroupState[S]) => U, + valueMapFunc: IV => V): (K, Iterator[IV], GroupState[S]) => U = { + (k: K, itr: Iterator[IV], s: GroupState[S]) => + { + f(k, itr.map(v => valueMapFunc(v)), s) + } + } + + def mapGroupsWithStateFuncToScalaFunc[K, V, S, U]( + f: MapGroupsWithStateFunction[K, V, S, U]): (K, Iterator[V], GroupState[S]) => U = { + (key, data, groupState) => f.call(key, data.asJava, groupState) + } + + def flatMapGroupsWithStateFuncToScalaFunc[K, V, S, U]( Review Comment: Added jave specific ones -- 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]
