bozhang2820 commented on a change in pull request #33763:
URL: https://github.com/apache/spark/pull/33763#discussion_r693901210



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/streaming/TriggerAvailableNowSuite.scala
##########
@@ -0,0 +1,170 @@
+/*
+ * 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.io.File
+
+import org.apache.spark.sql.{DataFrame, Dataset}
+import org.apache.spark.sql.catalyst.plans.logical.Range
+import org.apache.spark.sql.catalyst.util.stringToFile
+import org.apache.spark.sql.connector.read.streaming
+import org.apache.spark.sql.connector.read.streaming.{ReadLimit, 
SupportsAdmissionControl}
+import org.apache.spark.sql.execution.streaming.{LongOffset, MemoryStream, 
Offset, SerializedOffset, Source, StreamingExecutionRelation}
+import org.apache.spark.sql.types.{LongType, StructType}
+
+class TriggerAvailableNowSuite extends FileStreamSourceTest {
+
+  import testImplicits._
+
+  abstract class TestDataFrameProvider {
+    @volatile var currentOffset = 0L
+
+    def toDF: DataFrame
+
+    def incrementAvailableOffset(numNewRows: Int): Unit
+  }
+
+  class TestSource extends TestDataFrameProvider with Source {
+    override def getOffset: Option[Offset] = {
+      if (currentOffset <= 0) None else Some(LongOffset(currentOffset))
+    }
+
+    override def getBatch(start: Option[Offset], end: Offset): DataFrame = {
+      if (currentOffset == 0) currentOffset = getOffsetValue(end)
+      val plan = Range(
+        start.map(getOffsetValue).getOrElse(0L) + 1L, getOffsetValue(end) + 
1L, 1, None,
+        isStreaming = true)
+      Dataset.ofRows(spark, plan)
+    }
+
+    override def incrementAvailableOffset(numNewRows: Int): Unit = {
+      currentOffset += numNewRows
+    }
+
+    override def toDF: DataFrame =
+      Dataset.ofRows(spark, StreamingExecutionRelation(this, spark))
+
+    override def schema: StructType = new StructType().add("value", LongType)
+
+    override def stop(): Unit = {}
+
+    private def getOffsetValue(offset: Offset): Long = {
+      offset match {
+        case s: SerializedOffset => LongOffset(s).offset
+        case l: LongOffset => l.offset
+        case _ => throw new IllegalArgumentException("incorrect offset type: " 
+ offset)
+      }
+    }
+  }
+
+  class TestSourceWithAdmissionControl extends TestSource with 
SupportsAdmissionControl {
+    override def getDefaultReadLimit: ReadLimit = ReadLimit.maxRows(1)  // 
this will be overridden
+
+    override def latestOffset(startOffset: streaming.Offset, limit: 
ReadLimit): streaming.Offset = {
+      val currentOffset = getOffset
+      assert(currentOffset.nonEmpty,
+        "the latestOffset should be called after incrementAvailableOffset")
+      currentOffset.get
+    }
+  }
+
+  class TestMicroBatchStream extends TestDataFrameProvider {
+
+    private lazy val memoryStream = MemoryStream[Long](0, spark.sqlContext)
+
+    override def toDF: DataFrame = memoryStream.toDF()
+
+    override def incrementAvailableOffset(numNewRows: Int): Unit = {
+      for (_ <- 1 to numNewRows) {
+        currentOffset += 1
+        memoryStream.addData(currentOffset)
+      }
+    }
+  }
+

Review comment:
       Sure. Will do.

##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/FileStreamSource.scala
##########
@@ -48,7 +48,11 @@ class FileStreamSource(
     override val schema: StructType,
     partitionColumns: Seq[String],
     metadataPath: String,
-    options: Map[String, String]) extends SupportsAdmissionControl with Source 
with Logging {
+    options: Map[String, String])
+  extends SupportsAdmissionControl

Review comment:
       Thanks for catching this. Will do.

##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/MicroBatchExecution.scala
##########
@@ -47,10 +47,14 @@ class MicroBatchExecution(
     triggerClock, plan.outputMode, plan.deleteCheckpointOnStop) {
 
   @volatile protected var sources: Seq[SparkDataStream] = Seq.empty
+  @volatile protected var wrappedSources: MutableMap[
+    SparkDataStream, FakeLatestOffsetSupportsTriggerAvailableNow] = new
+      MutableHashMap[SparkDataStream, 
FakeLatestOffsetSupportsTriggerAvailableNow]()
 
   private val triggerExecutor = trigger match {
     case t: ProcessingTimeTrigger => ProcessingTimeExecutor(t, triggerClock)
-    case OneTimeTrigger => OneTimeExecutor()
+    case OneTimeTrigger => OneBatchExecutor()

Review comment:
       `MultiBatchExecutor` is really just running `batchRunner` multiple 
times. The semantic of processing all available data is implemented in 
`MicroBatchExecution`.

##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/FakeLatestOffsetMicroBatchStream.scala
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.sql.connector.read.{InputPartition, 
PartitionReaderFactory}
+import org.apache.spark.sql.connector.read.streaming
+import org.apache.spark.sql.connector.read.streaming.MicroBatchStream
+
+class FakeLatestOffsetMicroBatchStream(source: MicroBatchStream)
+  extends FakeLatestOffsetSupportsTriggerAvailableNow(source)
+    with MicroBatchStream {

Review comment:
       Will do.




-- 
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]

Reply via email to