jerryshao commented on code in PR #14883:
URL: https://github.com/apache/gravitino/pull/14883#discussion_r4545848544
##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/job/PartitionUtils.java:
##########
@@ -156,16 +229,51 @@ private static String formatLiteral(Transform transform,
Column column, String r
case DECIMAL:
return rawValue;
case DATE:
- return "DATE '" + rawValue + "'";
case TIMESTAMP:
- return "TIMESTAMP '" + rawValue + "'";
case TIME:
- return "TIME '" + rawValue + "'";
+ // Same CALL embedding constraint as days(); escape " / \ like string
literals.
+ return type.name() + " \"" + escapeStringLiteral(rawValue) + "\"";
Review Comment:
Values are escaped with a backslash before a double quote, but the job only
doubles single quotes (IcebergJobUtils.escapeSqlString) when wrapping the
clause in where => '...'. Spark then unescapes the backslash in the outer
literal, so a value containing a double quote or backslash can break the inner
literal. Add a test that runs the final CALL string through the real job
escaping and Spark parser.
##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/job/PartitionUtils.java:
##########
@@ -110,40 +140,83 @@ private static String getColumnName(Transform transform) {
throw new IllegalArgumentException("Unsupported transform: " +
transform.getClass().getName());
}
+ /** Resolves the leaf type for {@code event.ts}-style paths under a
top-level struct column. */
+ private static Type resolveLeafDataType(Column column, String columnName) {
+ // Exact match (including odd top-level names that contain '.') — no
nested walk.
+ if (column.name().equalsIgnoreCase(columnName) || columnName.indexOf('.')
< 0) {
+ return column.dataType();
+ }
+ Type type = column.dataType();
+ String[] parts = columnName.split("\\.");
+ for (int i = 1; i < parts.length; i++) {
+ if (!(type instanceof Types.StructType)) {
+ throw new IllegalArgumentException(
+ "Cannot resolve nested field '" + columnName + "': not a struct
path");
+ }
+ Types.StructType.Field matched = null;
+ for (Types.StructType.Field field : ((Types.StructType) type).fields()) {
+ if (field.name().equalsIgnoreCase(parts[i])) {
+ matched = field;
+ break;
+ }
+ }
+ if (matched == null) {
+ throw new IllegalArgumentException(
+ "Nested field '" + columnName + "' not found in table schema");
+ }
+ type = matched.type();
+ }
+ return type;
+ }
+
+ /**
+ * 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 + ")";
} else if (transform instanceof Transforms.BucketTransform) {
return "bucket("
- + columnName
- + ", "
+ ((Transforms.BucketTransform) transform).numBuckets()
+ + ", "
+ + columnName
+ ")";
} else if (transform instanceof Transforms.TruncateTransform) {
return "truncate("
- + columnName
- + ", "
+ ((Transforms.TruncateTransform) transform).width()
+ + ", "
+ + columnName
+ ")";
}
throw new IllegalArgumentException("Unsupported transform: " +
transform.getClass().getName());
}
- private static String formatLiteral(Transform transform, Column column,
String rawValue) {
+ private static String formatLiteral(Transform transform, Type type, String
rawValue) {
+ // years/months/hours/bucket partition values are already Iceberg ints;
leave unchanged.
if (transform instanceof Transforms.YearTransform
|| transform instanceof Transforms.MonthTransform
- || transform instanceof Transforms.DayTransform
|| transform instanceof Transforms.HourTransform
|| transform instanceof Transforms.BucketTransform) {
return rawValue;
}
- Type type = column.dataType();
+ // days() → DateType; double-quoted DATE survives CALL where => '...'
embedding.
+ if (transform instanceof Transforms.DayTransform) {
+ return "DATE \"" +
escapeStringLiteral(toIsoDateFromDayPartitionValue(rawValue)) + "\"";
Review Comment:
A string identity value that contains a single quote becomes two quotes in
the outer literal, which Spark may read as concatenation and silently drop the
quote. Was this case verified against a real Spark session?
##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/job/PartitionUtils.java:
##########
@@ -110,40 +140,83 @@ private static String getColumnName(Transform transform) {
throw new IllegalArgumentException("Unsupported transform: " +
transform.getClass().getName());
}
+ /** Resolves the leaf type for {@code event.ts}-style paths under a
top-level struct column. */
+ private static Type resolveLeafDataType(Column column, String columnName) {
+ // Exact match (including odd top-level names that contain '.') — no
nested walk.
+ if (column.name().equalsIgnoreCase(columnName) || columnName.indexOf('.')
< 0) {
+ return column.dataType();
+ }
+ Type type = column.dataType();
+ String[] parts = columnName.split("\\.");
+ for (int i = 1; i < parts.length; i++) {
+ if (!(type instanceof Types.StructType)) {
+ throw new IllegalArgumentException(
+ "Cannot resolve nested field '" + columnName + "': not a struct
path");
+ }
+ Types.StructType.Field matched = null;
+ for (Types.StructType.Field field : ((Types.StructType) type).fields()) {
+ if (field.name().equalsIgnoreCase(parts[i])) {
+ matched = field;
+ break;
+ }
+ }
+ if (matched == null) {
+ throw new IllegalArgumentException(
+ "Nested field '" + columnName + "' not found in table schema");
+ }
+ type = matched.type();
+ }
+ return type;
+ }
+
+ /**
+ * 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 + ")";
} else if (transform instanceof Transforms.BucketTransform) {
return "bucket("
- + columnName
- + ", "
+ ((Transforms.BucketTransform) transform).numBuckets()
+ + ", "
+ + columnName
+ ")";
} else if (transform instanceof Transforms.TruncateTransform) {
return "truncate("
- + columnName
- + ", "
+ ((Transforms.TruncateTransform) transform).width()
+ + ", "
+ + columnName
+ ")";
}
throw new IllegalArgumentException("Unsupported transform: " +
transform.getClass().getName());
}
- private static String formatLiteral(Transform transform, Column column,
String rawValue) {
+ private static String formatLiteral(Transform transform, Type type, String
rawValue) {
+ // years/months/hours/bucket partition values are already Iceberg ints;
leave unchanged.
if (transform instanceof Transforms.YearTransform
|| transform instanceof Transforms.MonthTransform
- || transform instanceof Transforms.DayTransform
|| transform instanceof Transforms.HourTransform
|| transform instanceof Transforms.BucketTransform) {
return rawValue;
Review Comment:
Year, month, hour and bucket values are inserted unvalidated. Parse them as
integers (Long.parseLong) so a bad partition string fails fast instead of
producing a broken predicate.
--
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]