tmnd1991 commented on code in PR #9233:
URL: https://github.com/apache/iceberg/pull/9233#discussion_r1433893181


##########
spark/v3.5/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestSPJWithBucketing.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.iceberg.spark.extensions;
+
+import java.util.Map;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.spark.SparkCatalogConfig;
+import org.apache.iceberg.spark.SparkSQLProperties;
+import org.apache.spark.scheduler.SparkListener;
+import org.apache.spark.scheduler.SparkListenerTaskEnd;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.internal.SQLConf;
+import org.assertj.core.api.Assertions;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runners.Parameterized;
+
+public class TestSPJWithBucketing extends SparkExtensionsTestBase {
+
+  @Test
+  public void testMergeSPJwithCondition() {
+    testWithCondition(
+        "  AND ("
+            + "(t.year_month='202306' AND t.day='01' AND 
testhive.system.bucket(4, t.id) = 0) OR\n"
+            + "(t.year_month='202306' AND t.day='01' AND 
testhive.system.bucket(4, t.id) = 1) OR\n"
+            + "(t.year_month='202306' AND t.day='02' AND 
testhive.system.bucket(4, t.id) = 0) OR\n"
+            + "(t.year_month='202307' AND t.day='01' AND 
testhive.system.bucket(4, t.id) = 3)\n"
+            + ")");
+  }
+
+  @Test
+  public void testMergeSPJwithoutCondition() {
+    testWithCondition("");
+  }
+
+  private void testWithCondition(String condition) {
+    createPartitionedTable(spark, targetTableName);
+    insertRecords(spark, targetTableName);
+    createPartitionedTable(spark, sourceTableName);
+    insertRecordsToUpdate(spark, sourceTableName);
+    int tasks =
+        executeAndCountTasks(
+            spark,
+            (s) ->
+                withSQLConf(
+                    ENABLED_SPJ_SQL_CONF,
+                    () ->
+                        // id STRING, year_month STRING, day STRING, data 
STRING
+                        sql(
+                            s,
+                            "MERGE INTO %s t USING (SELECT * FROM %s) s \n"
+                                + "ON t.id = s.id AND t.year_month = 
s.year_month AND t.day = s.day\n"
+                                + "%s\n"
+                                + "WHEN MATCHED THEN UPDATE SET\n"
+                                + "  t.data = s.data\n"
+                                + "WHEN NOT MATCHED THEN INSERT *",
+                            targetTableName,
+                            sourceTableName,
+                            condition)));
+    long affectedPartitions =
+        sql(spark, "SELECT DISTINCT(partition) FROM %s.files", 
sourceTableName).count();
+    int shufflePartitions = 
Integer.parseInt(spark.conf().get("spark.sql.shuffle.partitions"));
+    Assertions.assertThat(tasks).isEqualTo(affectedPartitions * 2 + 
shufflePartitions);

Review Comment:
   sure.
   the target table is created with the following partitions (year_month, day, 
bucket(4, id)):
   - **202306/01/0**
   - **202306/01/1**
   - 202306/01/2
   - 202306/01/3
   - **202306/02/0**
   - 202306/02/1
   - **202307/01/3**
   
   the source table is created with the following partitions:
   - 202306/01/0
   - 202306/01/1
   - 202306/02/0
   - 202307/01/3
   
   so the source table partitions are (is) a subset of the target table 
partitions.
   
   <u>Spark **statically knows** that info, because it's part of the metadata 
that iceberg keeps.</u>
   
   So copy-on-write "merge" consists of 2 jobs:
   1. left-semi join to understand which files are affected by the merge
   2. full-outer join where on the left side discards all the files not found 
while executing 1
   
   In our particular case (where we know that source table partitions are a 
subset of target table partitions) if we do that with a Storage Partitioned 
Join, the most efficient way to do it is to:
   1. create 1 task for each partition that will change, read all the files 
from both tables, join locally, collect the file names
   2. create 1 task for each partition that will change, for each task, read 
all the files from the target table / partition except the ones that will not 
change (that's the effect of the IN), read all the files from the source table, 
join and apply merge logic locally, write out new files, add these files to the 
snapshot and remove the original files from the snapshot
   
   Disclaimer: I know very little about internals and I can only imagine how 
hard can this be to actually done like that, but I'm quite sure that is 
"logically" doable 😄 
   
   so the reasoning of the number of tasks is:
   - 1 task per partition that is going to change
   - `spark.sql.shuffle.partitions` to shuffle the file list (which I thought 
could be broadcasted, but I think it's not important right now)
   - 1 task per partition that is going to change to actually rewrite it
   
   Let me know if there is any fallacy in my reasoning



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to