YannByron commented on code in PR #1842:
URL: https://github.com/apache/incubator-paimon/pull/1842#discussion_r1317190277


##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/sources/PaimonMicroBatchStream.scala:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.paimon.spark.sources
+
+import org.apache.paimon.CoreOptions._
+import org.apache.paimon.CoreOptions.StartupMode._
+import org.apache.paimon.options.Options
+import org.apache.paimon.spark.{SparkInputPartition, SparkReaderFactory}
+import org.apache.paimon.table.FileStoreTable
+import org.apache.paimon.table.source.ReadBuilder
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.connector.read.{InputPartition, 
PartitionReaderFactory}
+import org.apache.spark.sql.connector.read.streaming.{MicroBatchStream, Offset}
+
+import scala.util.Try
+
+class PaimonMicroBatchStream(
+    originTable: FileStoreTable,
+    readBuilder: ReadBuilder,
+    checkpointLocation: String)
+  extends MicroBatchStream
+  with StreamHelper
+  with Logging {
+
+  private var _table: FileStoreTable = originTable
+
+  private lazy val options = Options.fromMap(table.options())
+
+  private lazy val currentSnapshot = 
Option(table.snapshotManager().latestSnapshot())
+
+  private val (startSnapshotId, withStartSnapshot) = checkAndInitScanOptions()
+
+  private var committedOffset: Option[PaimonSourceOffset] = None
+
+  lazy val initOffset: PaimonSourceOffset = {
+    PaimonSourceOffset(startSnapshotId, -1, withStartSnapshot)
+  }
+
+  override def latestOffset(): Offset = {
+    getLatestOffset
+  }
+
+  override def planInputPartitions(start: Offset, end: Offset): 
Array[InputPartition] = {
+    val startOffset = {
+      val startOffset0 = PaimonSourceOffset.apply(start)
+      if (startOffset0.compareTo(initOffset) < 0) {
+        initOffset
+      } else {
+        startOffset0
+      }
+    }
+    val endOffset = PaimonSourceOffset.apply(end)
+
+    getBatch(startOffset, endOffset)
+      .map(ids => new SparkInputPartition(ids.entry))
+      .toArray[InputPartition]
+  }
+
+  override def createReaderFactory(): PartitionReaderFactory = {
+    new SparkReaderFactory(readBuilder)
+  }
+
+  override def initialOffset(): Offset = {
+    initOffset
+  }
+
+  override def deserializeOffset(json: String): Offset = {
+    PaimonSourceOffset.apply(json)
+  }
+
+  override def commit(end: Offset): Unit = {
+    committedOffset = Some(PaimonSourceOffset.apply(end))
+    logInfo(s"$committedOffset is committed.")
+  }
+
+  override def stop(): Unit = {}
+
+  override def table: FileStoreTable = _table
+
+  /** Parse [[Options]] to initialize the state that used to generate the init 
offset. */
+  private def checkAndInitScanOptions(): (Long, Boolean) = {
+    val scanSnapshotIdOptional = options.getOptional(SCAN_SNAPSHOT_ID)
+    val scanTSOptional = options.getOptional(SCAN_TIMESTAMP_MILLIS)
+    if (scanSnapshotIdOptional.isPresent && scanTSOptional.isPresent) {
+      throw new IllegalArgumentException(
+        "scan.snapshot-id and scan.timestamp-millis can be provided at the 
same time.")
+    }
+
+    // choose the bigger one between the earliest snapshotId and the provided 
snapshotId if exists as the init snapshotId
+    val earliestSnapshotId = Try(table.snapshotManager().earliestSnapshotId())
+      .map(_.longValue())
+      .getOrElse(Long.MinValue)
+
+    var scanMode = FROM_SNAPSHOT
+    var startSnapshotId = currentSnapshot.map(_.id()).getOrElse(1L)
+    var withStartSnapshot = false
+    options.get(SCAN_MODE) match {

Review Comment:
   related to https://github.com/apache/incubator-paimon/pull/1955.



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

Reply via email to