HeartSaVioR commented on a change in pull request #34333:
URL: https://github.com/apache/spark/pull/34333#discussion_r739939842



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/sources/RatePerMicroBatchStream.scala
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.sources
+
+import org.json4s.NoTypeHints
+import org.json4s.jackson.Serialization
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.util.DateTimeUtils
+import org.apache.spark.sql.connector.read.{InputPartition, PartitionReader, 
PartitionReaderFactory}
+import org.apache.spark.sql.connector.read.streaming.{MicroBatchStream, 
Offset, ReadLimit, ReadMaxRows, SupportsAdmissionControl}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+class RatePerMicroBatchStream(
+    rowsPerBatch: Long,
+    numPartitions: Int,
+    startTimestamp: Long,
+    advanceMsPerBatch: Int,
+    options: CaseInsensitiveStringMap)
+  extends SupportsAdmissionControl with MicroBatchStream with Logging {
+
+  override def initialOffset(): Offset = RatePerMicroBatchStreamOffset(0L, 
startTimestamp)
+
+  override def latestOffset(): Offset = {
+    throw new UnsupportedOperationException(
+      "latestOffset(Offset, ReadLimit) should be called instead of this 
method")
+  }
+
+  override def getDefaultReadLimit: ReadLimit = {
+    ReadLimit.maxRows(rowsPerBatch)
+  }
+
+  private def extractOffsetAndTimestamp(offset: Offset): (Long, Long) = {
+    offset match {
+      case o: RatePerMicroBatchStreamOffset => (o.offset, o.timestamp)
+      case _ => throw new IllegalStateException("The type of Offset should be 
" +
+        "RatePerMicroBatchStreamOffset")
+    }
+  }
+
+  override def latestOffset(startOffset: Offset, limit: ReadLimit): Offset = {
+    val (startOffsetLong, timestampAtStartOffset) = 
extractOffsetAndTimestamp(startOffset)
+    val numRows = limit.asInstanceOf[ReadMaxRows].maxRows()
+
+    val endOffsetLong = Math.min(startOffsetLong + numRows, Long.MaxValue)
+    val endOffset = RatePerMicroBatchStreamOffset(endOffsetLong,
+      timestampAtStartOffset + advanceMsPerBatch)
+
+    endOffset
+  }
+
+  override def deserializeOffset(json: String): Offset = {
+    RatePerMicroBatchStreamOffset.apply(json)
+  }
+
+  override def planInputPartitions(start: Offset, end: Offset): 
Array[InputPartition] = {
+    val (startOffset, startTimestamp) = extractOffsetAndTimestamp(start)
+    val (endOffset, endTimestamp) = extractOffsetAndTimestamp(end)
+
+    assert(startOffset <= endOffset, s"startOffset($startOffset) > 
endOffset($endOffset)")
+    assert(startTimestamp <= endTimestamp,
+      s"startTimestamp($startTimestamp) > endTimestamp($endTimestamp)")
+    logDebug(s"startOffset: $startOffset, startTimestamp: $startTimestamp, " +
+      s"endOffset: $endOffset, endTimestamp: $endTimestamp")
+
+    if (startOffset == endOffset) {
+      Array.empty
+    } else {
+      (0 until numPartitions).map { p =>
+        RatePerMicroBatchStreamInputPartition(p, numPartitions, startOffset,
+          startTimestamp, endOffset, endTimestamp)
+      }.toArray
+    }
+  }
+
+  override def createReaderFactory(): PartitionReaderFactory = {
+    RatePerMicroBatchStreamReaderFactory
+  }
+
+  override def commit(end: Offset): Unit = {}
+
+  override def stop(): Unit = {}
+
+  override def toString: String = 
s"RatePerMicroBatchStream[rowsPerBatch=$rowsPerBatch, " +
+    s"numPartitions=$numPartitions, startTimestamp=$startTimestamp, " +
+    s"advanceMsPerBatch=$advanceMsPerBatch]"
+}
+
+case class RatePerMicroBatchStreamOffset(offset: Long, timestamp: Long) 
extends Offset {
+  override def json(): String = {
+    Serialization.write(this)(RatePerMicroBatchStreamOffset.formats)
+  }
+}
+
+object RatePerMicroBatchStreamOffset {
+  implicit val formats = Serialization.formats(NoTypeHints)
+
+  def apply(json: String): RatePerMicroBatchStreamOffset =
+    Serialization.read[RatePerMicroBatchStreamOffset](json)
+}
+
+case class RatePerMicroBatchStreamInputPartition(
+    partitionId: Int,
+    numPartitions: Int,
+    startOffset: Long,
+    startTimestamp: Long,
+    endOffset: Long,
+    endTimestamp: Long) extends InputPartition
+
+object RatePerMicroBatchStreamReaderFactory extends PartitionReaderFactory {
+  override def createReader(partition: InputPartition): 
PartitionReader[InternalRow] = {
+    val p = partition.asInstanceOf[RatePerMicroBatchStreamInputPartition]
+    new RatePerMicroBatchStreamPartitionReader(p.partitionId, p.numPartitions,
+      p.startOffset, p.startTimestamp, p.endOffset, p.endTimestamp)
+  }
+}
+
+class RatePerMicroBatchStreamPartitionReader(
+    partitionId: Int,
+    numPartitions: Int,
+    startOffset: Long,
+    startTimestamp: Long,
+    endOffset: Long,
+    endTimestamp: Long) extends PartitionReader[InternalRow] {

Review comment:
       Yes, actually I thought about gradual increasing of timestamp even in 
same micro-batch, but decided to make it simple and add if someone claims with 
good use cases. I'll remove the unused parameter for now.




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