luoyuxia commented on code in PR #2956:
URL: https://github.com/apache/fluss/pull/2956#discussion_r3049985569


##########
fluss-spark/fluss-spark-common/src/main/scala/org/apache/fluss/spark/read/FlussLakePartitionReader.scala:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.fluss.spark.read
+
+import org.apache.fluss.lake.source.{LakeSource, LakeSplit}
+import org.apache.fluss.metadata.TablePath
+import org.apache.fluss.record.LogRecord
+import org.apache.fluss.spark.row.DataConverter
+import org.apache.fluss.types.RowType
+import org.apache.fluss.utils.CloseableIterator
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.read.PartitionReader
+
+/** Partition reader that reads data from a single lake split via lake storage 
(no Fluss connection). */
+class FlussLakePartitionReader(
+    tablePath: TablePath,
+    rowType: RowType,
+    partition: FlussLakeInputPartition,
+    lakeSource: LakeSource[LakeSplit])
+  extends PartitionReader[InternalRow]
+  with Logging {
+
+  private var currentRow: InternalRow = _
+  private var closed = false
+  private var recordIterator: CloseableIterator[LogRecord] = _
+
+  initialize()
+
+  private def initialize(): Unit = {
+    logInfo(s"Reading lake split for table $tablePath 
bucket=${partition.tableBucket.getBucket}")
+
+    val splitSerializer = lakeSource.getSplitSerializer
+    val split = splitSerializer.deserialize(splitSerializer.getVersion, 
partition.lakeSplitBytes)
+
+    recordIterator = lakeSource
+      .createRecordReader(new LakeSource.ReaderContext[LakeSplit] {
+        override def lakeSplit(): LakeSplit = split
+      })
+      .read()
+  }
+
+  override def next(): Boolean = {
+    if (closed || recordIterator == null) {
+      return false
+    }
+
+    if (recordIterator.hasNext) {
+      val logRecord = recordIterator.next()
+      currentRow = DataConverter.toSparkInternalRow(logRecord.getRow, rowType)

Review Comment:
   try with the following test:
   ```
   test("Spark Lake Read: log table lake-only projection on timestamp column") {
       withTable("t_lake_timestamp") {
         sql(s"""
                |CREATE TABLE $DEFAULT_DATABASE.t_lake_timestamp (
                |  id INT,
                |  ts TIMESTAMP,
                |  name STRING)
                | TBLPROPERTIES (
                |  '${ConfigOptions.TABLE_DATALAKE_ENABLED.key()}' = true,
                |  '${ConfigOptions.TABLE_DATALAKE_FRESHNESS.key()}' = '1s',
                |  '${BUCKET_NUMBER.key()}' = 1)
                |""".stripMargin)
   
         sql(s"""
                |INSERT INTO $DEFAULT_DATABASE.t_lake_timestamp VALUES
                |(1, TIMESTAMP "2026-01-01 12:00:00", "alpha"),
                |(2, TIMESTAMP "2026-01-02 12:00:00", "beta"),
                |(3, TIMESTAMP "2026-01-03 12:00:00", "gamma")
                |""".stripMargin)
   
         tierToLake("t_lake_timestamp")
   
         checkAnswer(
           sql(s"SELECT ts FROM $DEFAULT_DATABASE.t_lake_timestamp ORDER BY 
ts"),
           Row(java.sql.Timestamp.valueOf("2026-01-01 12:00:00")) ::
             Row(java.sql.Timestamp.valueOf("2026-01-02 12:00:00")) ::
             Row(java.sql.Timestamp.valueOf("2026-01-03 12:00:00")) :: Nil
         )
       }
     }
   ```
   Then it throws:
   ```
   Caused by: java.lang.ClassCastException: class 
org.apache.paimon.format.parquet.reader.ParquetTimestampVector cannot be cast 
to class org.apache.paimon.data.columnar.LongColumnVector 
(org.apache.paimon.format.parquet.reader.ParquetTimestampVector and 
org.apache.paimon.data.columnar.LongColumnVector are in unnamed module of 
loader 'app')
        at 
org.apache.paimon.data.columnar.VectorizedColumnBatch.getLong(VectorizedColumnBatch.java:84)
        at 
org.apache.paimon.data.columnar.ColumnarRow.getLong(ColumnarRow.java:111)
        at 
org.apache.fluss.lake.paimon.utils.PaimonRowAsFlussRow.getLong(PaimonRowAsFlussRow.java:80)
        at org.apache.fluss.row.ProjectedRow.getLong(ProjectedRow.java:95)
        at 
org.apache.fluss.spark.row.FlussAsSparkRow.getLong(FlussAsSparkRow.scala:69)
        at 
org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown
 Source)
        at 
org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
        at 
org.apache.spark.sql.execution.WholeStageCodegenEvaluatorFactory$WholeStageCodegenPartitionEvaluator$$anon$1.hasNext(WholeStageCodegenEvaluatorFactory.scala:43)
        at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
        at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
        at 
org.apache.spark.util.random.SamplingUtils$.reservoirSampleAndCount(SamplingUtils.scala:41)
        at 
org.apache.spark.RangePartitioner$.$anonfun$sketch$1(Partitioner.scala:322)
        at 
org.apache.spark.RangePartitioner$.$anonfun$sketch$1$adapted(Partitioner.scala:320)
        at 
org.apache.spark.rdd.RDD.$anonfun$mapPartitionsWithIndex$2(RDD.scala:910)
        at 
org.apache.spark.rdd.RDD.$anonfun$mapPartitionsWithIndex$2$adapted(RDD.scala:910)
        at 
org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
        at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:367)
        at org.apache.spark.rdd.RDD.iterator(RDD.scala:331)
        at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:93)
        at 
org.apache.spark.TaskContext.runTaskWithListeners(TaskContext.scala:166)
        at org.apache.spark.scheduler.Task.run(Task.scala:141)
        at 
org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$4(Executor.scala:621)
        at 
org.apache.spark.util.SparkErrorUtils.tryWithSafeFinally(SparkErrorUtils.scala:64)
        at 
org.apache.spark.util.SparkErrorUtils.tryWithSafeFinally$(SparkErrorUtils.scala:61)
        at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:94)
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:624)
        at 
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at 
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)
   ```
   
   This row is already projected by the lake source, but it is still being 
decoded with the full table schema.
   
     - In FlussLakeAppendBatch, projection is pushed down into the lake source.
     - Paimon then returns a projected ProjectedRow.
     - But FlussLakePartitionReader still calls 
DataConverter.toSparkInternalRow(logRecord.getRow, rowType) with the full 
tableInfo.getRowType.
   
     That becomes visible with a query like SELECT ts ...:
   
     - ordinal 0 in the projected lake row is ts
     - ordinal 0 in the full table schema is still id INT
   
     Because of that mismatch, FlussAsSparkRow.getLong takes the non-timestamp 
branch and calls row.getLong(0), which matches the failure I hit locally:
   
     ParquetTimestampVector cannot be cast to LongColumnVector
   
     So I think the reader needs the projected Fluss RowType here, not the full 
table RowType.



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