Shekharrajak commented on code in PR #16454:
URL: https://github.com/apache/iceberg/pull/16454#discussion_r3276632073


##########
spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkScan.java:
##########
@@ -1170,4 +1170,176 @@ private static NamedReference fieldRef(String col) {
   private static UserDefinedScalarFunc toUDF(BoundFunction function, 
Expression[] expressions) {
     return new UserDefinedScalarFunc(function.name(), 
function.canonicalName(), expressions);
   }
+
+  // 
---------------------------------------------------------------------------
+  // SupportsReportOrdering — tests that sort_order_id is surfaced as
+  // outputOrdering so Spark can skip redundant sorts above sorted scans.
+  // Tracks https://github.com/apache/iceberg/issues/16430.
+  // 
---------------------------------------------------------------------------
+
+  @TestTemplate
+  public void testOutputOrderingForSingleAscSortKey() {
+    sql(
+        "CREATE TABLE %s (user_id BIGINT, event_time TIMESTAMP) USING iceberg"
+            + " TBLPROPERTIES ('%s'='%s', 'read.split.target-size'='1',"
+            + " 'read.split.open-file-cost'='1')",
+        tableName, TableProperties.DEFAULT_FILE_FORMAT, format);
+    Table table = validationCatalog.loadTable(tableIdent);
+    table.replaceSortOrder().asc("event_time").commit();
+
+    sql(
+        "INSERT INTO %s VALUES (1, TIMESTAMP '2024-01-01 00:00:00'),"
+            + " (2, TIMESTAMP '2024-01-01 01:00:00')",
+        tableName);
+    sql(
+        "INSERT INTO %s VALUES (3, TIMESTAMP '2024-01-02 00:00:00'),"
+            + " (4, TIMESTAMP '2024-01-02 01:00:00')",
+        tableName);
+
+    String plan = explainPlan("SELECT * FROM %s ORDER BY event_time LIMIT 
100", tableName);
+    assertThat(plan)
+        .as("Sort eliminated when scan advertises outputOrdering for ASC sort 
key")
+        .doesNotContain("Sort [");
+  }
+
+  @TestTemplate
+  public void testOutputOrderingForDescSortKey() {
+    sql(
+        "CREATE TABLE %s (user_id BIGINT, event_time TIMESTAMP) USING iceberg"
+            + " TBLPROPERTIES ('%s'='%s', 'read.split.target-size'='1',"
+            + " 'read.split.open-file-cost'='1')",
+        tableName, TableProperties.DEFAULT_FILE_FORMAT, format);
+    
validationCatalog.loadTable(tableIdent).replaceSortOrder().desc("event_time").commit();
+
+    sql(
+        "INSERT INTO %s VALUES (1, TIMESTAMP '2024-01-02 00:00:00'),"
+            + " (2, TIMESTAMP '2024-01-01 00:00:00')",
+        tableName);
+
+    String plan = explainPlan("SELECT * FROM %s ORDER BY event_time DESC LIMIT 
100", tableName);
+    assertThat(plan).as("Sort eliminated for DESC sort 
key").doesNotContain("Sort [");

Review Comment:
   ```
   + TakeOrderedAndProject(limit=100, orderBy=[event_time DESC])
   +   +- BatchScan ...                              Sort eliminated, DESC works
   ```



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