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


##########
spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestStoragePartitionedJoins.java:
##########
@@ -131,6 +137,232 @@ public static void setupSparkConf() {
   public void removeTables() {
     sql("DROP TABLE IF EXISTS %s", tableName);
     sql("DROP TABLE IF EXISTS %s", tableName(OTHER_TABLE_NAME));
+    sql("DROP TABLE IF EXISTS %s", tableName(UNPARTITIONED_TABLE_NAME));
+  }
+
+  @TestTemplate
+  void runtimeFilteringWithTrailingPartitionKey() {
+    sql(
+        "CREATE TABLE %s (store_id INT, dept_id INT, data STRING) USING 
iceberg "
+            + "PARTITIONED BY (store_id, dept_id) TBLPROPERTIES (%s)",
+        tableName, tablePropsAsString(TABLE_PROPERTIES));
+    configurePlanningMode(tableName, planningMode);
+    sql(
+        "INSERT INTO %s VALUES (100, 1, 'aa'), (100, 2, 'ab'), (200, 1, 'ac'), 
(200, 3, 'ad')",
+        tableName);
+    sql(
+        "CREATE TABLE %s (dept_id INT, data STRING) USING iceberg "
+            + "PARTITIONED BY (dept_id) TBLPROPERTIES (%s)",
+        tableName(OTHER_TABLE_NAME), tablePropsAsString(TABLE_PROPERTIES));
+    configurePlanningMode(tableName(OTHER_TABLE_NAME), planningMode);
+    sql("INSERT INTO %s VALUES (1, 'x'), (2, 'y'), (3, 'x')", 
tableName(OTHER_TABLE_NAME));
+
+    String query =
+        String.format(
+            "SELECT /*+ MERGE(t1, t2) */ t1.store_id, t1.dept_id, t1.data, 
t2.data "
+                + "FROM %s t1 JOIN %s t2 ON t1.dept_id = t2.dept_id "
+                + "WHERE t2.data = 'x' ORDER BY t1.store_id, t1.dept_id, 
t1.data",
+            tableName, tableName(OTHER_TABLE_NAME));
+
+    withSQLConf(
+        ImmutableMap.of(
+            SQLConf.DYNAMIC_PARTITION_PRUNING_ENABLED().key(), "true",
+            SQLConf.DYNAMIC_PARTITION_PRUNING_REUSE_BROADCAST_ONLY().key(), 
"false",
+            SQLConf.DYNAMIC_PARTITION_PRUNING_FALLBACK_FILTER_RATIO().key(), 
"10"),
+        () -> {
+          withSQLConf(
+              ENABLED_SPJ_SQL_CONF,
+              () -> {
+                Dataset<Row> df = spark.sql(query);
+                SparkPlan plan = df.queryExecution().executedPlan();
+                assertThat(CollectionConverters.asJava(plan.collectLeaves()))
+                    .anySatisfy(
+                        leaf -> {
+                          assertThat(leaf).isInstanceOf(BatchScanExec.class);
+                          BatchScanExec scan = (BatchScanExec) leaf;
+                          
assertThat(scan.runtimeFilters().isEmpty()).isFalse();

Review Comment:
   this only checks that spark planned a filter, and the join drops `dept_id = 
2` anyway so the row comparison doesn't prove files were skipped. could we 
adapt `deleteNotMatchingFiles` from `TestRuntimeFiltering` to remove the 
`dept_id=2` file from disk while keeping its metadata (same in 4.1 and 4.2)?
   
   ```diff
   + deleteNotMatchingFiles(Expressions.in("dept_id", 1, 3), 1);
   ```



##########
spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestStoragePartitionedJoins.java:
##########
@@ -777,6 +1009,121 @@ public void testJoinsHourToDays() throws 
NoSuchTableException {
         otherTableName);
   }
 
+  private void checkBroadcastJoinOrdering(String direction) {
+    sql(
+        "CREATE TABLE %s (id BIGINT) USING iceberg " + "PARTITIONED BY (id) 
TBLPROPERTIES (%s)",
+        tableName, tablePropsAsString(TABLE_PROPERTIES));
+    configurePlanningMode(planningMode);
+    sql("INSERT INTO %s VALUES (-3), (-2), (-1), (1), (2), (3)", tableName);
+
+    sql("CREATE TABLE %s (id BIGINT) USING iceberg", 
tableName(OTHER_TABLE_NAME));
+    configurePlanningMode(tableName(OTHER_TABLE_NAME), planningMode);
+    sql("INSERT INTO %s VALUES (1), (2), (3)", tableName(OTHER_TABLE_NAME));
+
+    withSQLConf(
+        ImmutableMap.of(
+            SQLConf.V2_BUCKETING_SORTING_ENABLED().key(),
+            "true",
+            SQLConf.DYNAMIC_PARTITION_PRUNING_ENABLED().key(),
+            "false"),
+        () -> {
+          for (String joinKey : List.of("-p.id", "4 - p.id")) {
+            assertPartitioningAwarePlan(
+                1,
+                2,
+                "SELECT /*+ BROADCAST(p) */ p.id FROM %s i JOIN %s p "
+                    + "ON i.id = %s ORDER BY %s %s",
+                tableName,
+                tableName(OTHER_TABLE_NAME),
+                joinKey,
+                joinKey,
+                direction);
+          }
+        });
+  }
+
+  @TestTemplate
+  void subsetJoinKeysWithMixedPartitionTypes() {
+    checkSubsetJoinKeysWithMixedPartitionTypes(false);
+  }
+
+  @TestTemplate
+  void partiallyClusteredSubsetJoinKeysWithMixedPartitionTypes() {
+    checkSubsetJoinKeysWithMixedPartitionTypes(true);
+  }
+
+  private void checkSubsetJoinKeysWithMixedPartitionTypes(boolean 
partiallyClustered) {
+    sql(
+        "CREATE TABLE %s (id STRING, part STRING, data STRING) USING iceberg "
+            + "PARTITIONED BY (part, bucket(32, id), bucket(32, data)) 
TBLPROPERTIES (%s)",
+        tableName, tablePropsAsString(TABLE_PROPERTIES));
+    configurePlanningMode(tableName, planningMode);
+    sql(
+        "INSERT INTO %s VALUES ('a', '2026-05-01', 'aa'), ('a', '2026-05-02', 
'cc'), "
+            + "('b', '2026-05-01', 'bb'), ('c', '2026-05-02', 'aa')",
+        tableName);
+    sql(
+        "CREATE TABLE %s (id STRING, part STRING, data STRING) USING iceberg "
+            + "PARTITIONED BY (bucket(32, id)) TBLPROPERTIES (%s)",
+        tableName(OTHER_TABLE_NAME), tablePropsAsString(TABLE_PROPERTIES));
+    configurePlanningMode(tableName(OTHER_TABLE_NAME), planningMode);
+    sql(
+        "INSERT INTO %s VALUES ('a', '2026-05-01', 'xx'), ('b', '2026-05-01', 
'yy'), "
+            + "('d', '2026-05-01', 'zz')",
+        tableName(OTHER_TABLE_NAME));
+    String query =
+        String.format(
+            "SELECT /*+ MERGE(l, r) */ l.id, l.part, l.data, r.data FROM %s l 
JOIN %s r "
+                + "ON l.id = r.id ORDER BY l.id, l.part, l.data, r.data",
+            tableName, tableName(OTHER_TABLE_NAME));
+    AtomicReference<List<Object[]>> expected = new AtomicReference<>();
+    withSQLConf(DISABLED_SPJ_SQL_CONF, () -> expected.set(sql(query)));
+    assertThat(expected.get()).hasSize(3);
+    withSQLConf(
+        ENABLED_SPJ_SQL_CONF,
+        () ->
+            withSQLConf(
+                ImmutableMap.of(
+                    SQLConf.ADAPTIVE_EXECUTION_ENABLED().key(),
+                    "true",
+                    SQLConf.SKEW_JOIN_ENABLED().key(),
+                    "true",
+                    
SQLConf.V2_BUCKETING_PARTIALLY_CLUSTERED_DISTRIBUTION_ENABLED().key(),
+                    Boolean.toString(partiallyClustered)),
+                () -> {
+                  SparkPlan plan = executeAndKeepPlan(query);
+                  assertThat(plan.toString()).contains("SortMergeJoin");
+                  assertThat(hasNonLeadingPartitionKeyProjection(plan))

Review Comment:
   this checks the join key projection, but doesn't verify that partial 
clustering actually applied in the `partiallyClustered = true` variant. could 
we pass `partiallyClustered` into `hasNonLeadingPartitionKeyProjection` and 
check `scan.spjParams().applyPartialClustering()` in 4.0/4.1 (and 
`distributePartitions()` in 4.2)?



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