timothyw553 commented on code in PR #19122:
URL: https://github.com/apache/iceberg/pull/19122#discussion_r4810423190


##########
spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkChangelogScan.java:
##########
@@ -18,119 +18,55 @@
  */
 package org.apache.iceberg.spark.source;
 
-import java.io.IOException;
-import java.io.UncheckedIOException;
 import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
 import java.util.Objects;
-import org.apache.iceberg.ChangelogScanTask;
 import org.apache.iceberg.IncrementalChangelogScan;
-import org.apache.iceberg.ScanTaskGroup;
+import org.apache.iceberg.PartitionScanTask;
 import org.apache.iceberg.Schema;
 import org.apache.iceberg.Table;
 import org.apache.iceberg.expressions.Expression;
-import org.apache.iceberg.io.CloseableIterable;
-import org.apache.iceberg.relocated.com.google.common.collect.Lists;
-import org.apache.iceberg.spark.Spark3Util;
 import org.apache.iceberg.spark.SparkReadConf;
-import org.apache.iceberg.spark.SparkSchemaUtil;
-import org.apache.iceberg.types.Types;
-import org.apache.spark.api.java.JavaSparkContext;
 import org.apache.spark.sql.SparkSession;
-import org.apache.spark.sql.connector.read.Batch;
-import org.apache.spark.sql.connector.read.Scan;
-import org.apache.spark.sql.connector.read.Statistics;
-import org.apache.spark.sql.connector.read.SupportsReportStatistics;
-import org.apache.spark.sql.types.StructType;
 
-class SparkChangelogScan implements Scan, SupportsReportStatistics {
-
-  private static final Types.StructType EMPTY_GROUPING_KEY_TYPE = 
Types.StructType.of();
-
-  private final JavaSparkContext sparkContext;
-  private final Table table;
-  private final IncrementalChangelogScan scan;
-  private final SparkReadConf readConf;
-  private final Schema projection;
-  private final List<Expression> filters;
+class SparkChangelogScan extends SparkPartitioningAwareScan<PartitionScanTask> 
{
   private final Long startSnapshotId;
   private final Long endSnapshotId;
 
-  // lazy variables
-  private List<ScanTaskGroup<ChangelogScanTask>> taskGroups = null;
-  private StructType expectedSparkType = null;
-
   SparkChangelogScan(
       SparkSession spark,
       Table table,
       IncrementalChangelogScan scan,
       SparkReadConf readConf,
       Schema projection,
       List<Expression> filters) {
-
-    SparkSchemaUtil.validateMetadataColumnReferences(table.schema(), 
projection);
-
-    this.sparkContext = 
JavaSparkContext.fromSparkContext(spark.sparkContext());
-    this.table = table;
-    this.scan = scan;
-    this.readConf = readConf;
-    this.projection = projection;
-    this.filters = filters != null ? filters : Collections.emptyList();
-    this.startSnapshotId = readConf.startSnapshotId();
-    this.endSnapshotId = readConf.endSnapshotId();
-    if (scan == null) {
-      this.taskGroups = Collections.emptyList();
-    }
-  }
-
-  @Override
-  public Statistics estimateStatistics() {
-    long rowsCount = 
taskGroups().stream().mapToLong(ScanTaskGroup::estimatedRowsCount).sum();
-    long sizeInBytes = SparkSchemaUtil.estimateSize(readSchema(), rowsCount);
-    return new Stats(sizeInBytes, rowsCount, Collections.emptyMap());
-  }
-
-  @Override
-  public StructType readSchema() {
-    if (expectedSparkType == null) {
-      this.expectedSparkType = SparkSchemaUtil.convert(projection);
-    }
-
-    return expectedSparkType;
-  }
-
-  @Override
-  public Batch toBatch() {
-    return new SparkBatch(
-        sparkContext,
+    super(
+        spark,
         table,
-        null != scan ? scan.fileIO() : table::io,
+        table.schema(),
+        scan,
         readConf,
-        EMPTY_GROUPING_KEY_TYPE,
-        taskGroups(),
         projection,
-        hashCode());
+        filters,
+        Collections.emptyList(),
+        null);
+    this.startSnapshotId = readConf.startSnapshotId();
+    this.endSnapshotId = readConf.endSnapshotId();
+    tasks();

Review Comment:
   to my understanding `tasks()` plans the files up front, while `equals()` and 
`hashCode()` still compare `readConf.startSnapshotId()` and 
`readConf.endSnapshotId()` which stay `null` when users omit those options. 
couldn't two scans over different snapshots then compare equal and make Spark 
reuse an exchange across them (for example in `UNION ALL` or a full outer 
join), silently dropping rows? should we resolve the actual snapshot bounds in 
`SparkChangelogScanBuilder` (including resolving an omitted end bound to the 
current snapshot and converting timestamps) and pass them to all 
`SparkChangelogScan` constructors, plus add a combined-query test with exchange 
reuse enabled?
   
   ```diff
   - this.startSnapshotId = readConf.startSnapshotId();
   - this.endSnapshotId = readConf.endSnapshotId();
   + this.startSnapshotId = startSnapshotId;
   + this.endSnapshotId = endSnapshotId;
   ```



##########
spark/v4.2/spark/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/ChangelogDistribution.scala:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.catalyst.plans.logical
+
+import org.apache.iceberg.spark.Spark3Util
+import org.apache.spark.sql.Column
+import org.apache.spark.sql.Dataset
+import org.apache.spark.sql.Row
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.classic.{Dataset => ClassicDataset}
+import org.apache.spark.sql.classic.{SparkSession => ClassicSparkSession}
+
+case class ChangelogDistribution(keys: Seq[Expression], child: LogicalPlan) 
extends UnaryNode {
+  override def output: Seq[Attribute] = child.output
+
+  override protected def withNewChildInternal(newChild: LogicalPlan): 
LogicalPlan =
+    copy(child = newChild)
+}
+
+object ChangelogDistribution {
+  def prepare(df: Dataset[Row], columns: Array[Column]): Dataset[Row] = {
+    if (Spark3Util.extensionsEnabled(df.sparkSession)) {
+      val keys = df.select(columns: _*).queryExecution.analyzed.output
+      ClassicDataset.ofRows(
+        df.sparkSession.asInstanceOf[ClassicSparkSession],
+        ChangelogDistribution(keys, df.queryExecution.optimizedPlan))

Review Comment:
   why are we wrapping `df.queryExecution.optimizedPlan` instead of `analyzed` 
here? it looks like `optimizedPlan` freezes the built scan into the changelog 
view at creation time, so later inserts disappear and a view on an empty table 
stays empty (unlike the `else` path without extensions). could we use 
`analyzed` instead, drop the eager `tasks()` call in `SparkChangelogScan` if it 
isn't needed, and update `createChangelogView_preservesSnapshotAtCreation` and 
`createChangelogView_emptyTableRemainsEmpty` to expect live reads?
   
   ```diff
   -        ChangelogDistribution(keys, df.queryExecution.optimizedPlan))
   +        ChangelogDistribution(keys, df.queryExecution.analyzed))
   ```



##########
spark/v4.2/spark/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/ChangelogDistribution.scala:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.catalyst.plans.logical
+
+import org.apache.iceberg.spark.Spark3Util
+import org.apache.spark.sql.Column
+import org.apache.spark.sql.Dataset
+import org.apache.spark.sql.Row
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.classic.{Dataset => ClassicDataset}
+import org.apache.spark.sql.classic.{SparkSession => ClassicSparkSession}
+
+case class ChangelogDistribution(keys: Seq[Expression], child: LogicalPlan) 
extends UnaryNode {
+  override def output: Seq[Attribute] = child.output
+
+  override protected def withNewChildInternal(newChild: LogicalPlan): 
LogicalPlan =
+    copy(child = newChild)
+}
+
+object ChangelogDistribution {
+  def prepare(df: Dataset[Row], columns: Array[Column]): Dataset[Row] = {
+    if (Spark3Util.extensionsEnabled(df.sparkSession)) {
+      val keys = df.select(columns: _*).queryExecution.analyzed.output
+      ClassicDataset.ofRows(
+        df.sparkSession.asInstanceOf[ClassicSparkSession],
+        ChangelogDistribution(keys, df.queryExecution.optimizedPlan))
+    } else {
+      df.repartition(columns: _*)

Review Comment:
   all the new procedure tests enable extensions, so this `else` fallback isn't 
covered (and right now behaves differently on later inserts). also extending 
`SparkPartitioningAwareScan` in `SparkChangelogScan` changes direct 
`<table>.changes` reads with partition reporting and adaptive split sizing. 
could we add tests without extensions and compare direct `.changes` query 
results with grouping on and off, including a join or aggregation?



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