lasdf1234 commented on code in PR #14883:
URL: https://github.com/apache/gravitino/pull/14883#discussion_r4536540700


##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/job/PartitionUtils.java:
##########
@@ -137,12 +151,21 @@ private static String buildExpression(Transform 
transform, String columnName) {
   private static String formatLiteral(Transform transform, Column column, 
String rawValue) {
     if (transform instanceof Transforms.YearTransform
         || transform instanceof Transforms.MonthTransform
-        || transform instanceof Transforms.DayTransform
         || transform instanceof Transforms.HourTransform
         || transform instanceof Transforms.BucketTransform) {
       return rawValue;
     }
 
+    // Iceberg Spark days() returns DATE; CALL where => '...' also eats 
single-quoted DATE
+    // literals (escapeSqlString doubling still parses as unquoted). Use 
double-quoted DATE.
+    // Epoch-day -> ISO date mirrors Iceberg DateTimeUtil.daysToIsoDate 
without depending on
+    // iceberg-api (ofEpochDay + ISO_LOCAL_DATE == 
dateFromDays(...).format(ISO_LOCAL_DATE)).
+    if (transform instanceof Transforms.DayTransform) {
+      String isoDate =
+          
LocalDate.ofEpochDay(Integer.parseInt(rawValue)).format(DateTimeFormatter.ISO_LOCAL_DATE);

Review Comment:
   Thanks — addressed.
   
   `toIsoDateFromDayPartitionValue` now accepts both ISO `yyyy-MM-dd` 
(update-stats / `String.valueOf(Spark date)`) and epoch-day integers (Iceberg 
metadata), and throws a clear `IllegalArgumentException` for anything else.
   
   Coverage:
   - UT: ISO path, epoch-day path, and reject non-integer/non-ISO
   - Adapter IT: ISO day from the stats path into `where_clause`



##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/job/PartitionUtils.java:
##########
@@ -156,11 +179,12 @@ private static String formatLiteral(Transform transform, 
Column column, String r
       case DECIMAL:
         return rawValue;
       case DATE:
-        return "DATE '" + rawValue + "'";
+        // Double quotes: CALL where => '...' eats single-quoted typed 
literals.
+        return "DATE \"" + rawValue + "\"";
       case TIMESTAMP:
-        return "TIMESTAMP '" + rawValue + "'";
+        return "TIMESTAMP \"" + rawValue + "\"";

Review Comment:
   Thanks — verified.
   
   Double-quoted typed literals (`DATE \"...\"` / `TIMESTAMP \"...\"` / `TIME 
\"...\"`) are intentional: single-quoted forms are eaten inside `CALL ... where 
=> '...'`.
   
   Verified with real Spark/Iceberg `rewrite_data_files` under both 
`spark.sql.ansi.doubleQuotedIdentifiers=false` and `true` (CLI CALL matrix + 
`TestBuiltinIcebergRewriteDataFiles.testPartitionUtilsWhereClausesAgainstIcebergRewriteCall`).
 String values also escape `\` / `"` in the literal.



##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/job/PartitionUtils.java:
##########
@@ -110,25 +114,35 @@ private static String getColumnName(Transform transform) {
     throw new IllegalArgumentException("Unsupported transform: " + 
transform.getClass().getName());
   }
 
+  /**
+   * Builds the left-hand side of a partition predicate using Iceberg Spark 
system functions.
+   *
+   * <p>Temporal transforms use plural names ({@code years}/{@code 
months}/{@code days}/{@code
+   * hours}) matching {@code 
org.apache.iceberg.spark.functions.SparkFunctions}. {@code bucket} and
+   * {@code truncate} put the width argument first, matching Iceberg's Spark 
UDF signatures.
+   */
   private static String buildExpression(Transform transform, String 
columnName) {
     if (transform instanceof Transforms.IdentityTransform) {
       return columnName;
-    } else if (transform instanceof Transforms.YearTransform
-        || transform instanceof Transforms.MonthTransform
-        || transform instanceof Transforms.DayTransform
-        || transform instanceof Transforms.HourTransform) {
-      return transform.name() + "(" + columnName + ")";
+    } else if (transform instanceof Transforms.YearTransform) {
+      return "years(" + columnName + ")";
+    } else if (transform instanceof Transforms.MonthTransform) {
+      return "months(" + columnName + ")";
+    } else if (transform instanceof Transforms.DayTransform) {
+      return "days(" + columnName + ")";
+    } else if (transform instanceof Transforms.HourTransform) {
+      return "hours(" + columnName + ")";

Review Comment:
   Thanks — confirmed.
   
   Iceberg Spark system functions return:
   - `years` / `months` / `hours` → `int` (compare to bare int partition values)
   - `days` → `date` (hence `DATE \"yyyy-MM-dd\"`, not epoch-day int)
   
   Covered by UT for all transforms (including nested) and by Spark `CALL 
rewrite_data_files` IT cases for years/months/days/hours/bucket/truncate (+ 
nested variants).



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