{"record":{"id":"25b1af356d0aae4d","repo":"apache/iceberg","slug":"unsupported-width-for-transform-s","errorCode":null,"errorMessage":"Unsupported width for transform: %s","messagePattern":"Unsupported width for transform: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/Spark3Util.java","lineNumber":487,"sourceCode":"      }\n    }\n\n    return builder.build();\n  }\n\n  @SuppressWarnings(\"unchecked\")\n  private static int findWidth(Transform transform) {\n    for (Expression expr : transform.arguments()) {\n      if (expr instanceof Literal) {\n        if (((Literal) expr).dataType() instanceof IntegerType) {\n          Literal<Integer> lit = (Literal<Integer>) expr;\n          Preconditions.checkArgument(\n              lit.value() > 0, \"Unsupported width for transform: %s\", transform.describe());\n          return lit.value();\n\n        } else if (((Literal) expr).dataType() instanceof LongType) {\n          Literal<Long> lit = (Literal<Long>) expr;\n          Preconditions.checkArgument(\n              lit.value() > 0 && lit.value() < Integer.MAX_VALUE,\n              \"Unsupported width for transform: %s\",\n              transform.describe());\n          if (lit.value() > Integer.MAX_VALUE) {\n            throw new IllegalArgumentException();\n          }\n          return lit.value().intValue();\n        }\n      }\n    }\n\n    throw new IllegalArgumentException(\"Cannot find width for transform: \" + transform.describe());\n  }\n\n  private static String leafName(String[] fieldNames) {\n    Preconditions.checkArgument(\n        fieldNames.length > 0, \"Invalid field name: at least one name is required\");\n    return fieldNames[fieldNames.length - 1];","sourceCodeStart":469,"sourceCodeEnd":505,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/Spark3Util.java#L469-L505","documentation":"findWidth extracts the integer width parameter from a Spark truncate(W) transform literal. The literal must be a positive Integer (or Long below Integer.MAX_VALUE); otherwise Preconditions.checkArgument fails with 'Unsupported width for transform'.","triggerScenarios":"CREATE TABLE ... PARTITIONED BY truncate(0, col) or truncate(-5, col); truncate(3000000000, col) where the Long literal exceeds Integer.MAX_VALUE; passing a non-integer literal that findWidth tries to coerce.","commonSituations":"Typos in DDL partition clause, programmatically generated transforms with invalid width, copy-pasted widths from systems allowing 0 or huge widths.","solutions":["Use a positive integer width for truncate that fits in an int (>0 and < 2147483648)","Fix the width literal in the PARTITIONED BY clause, e.g. truncate(10, col)","If the value comes from code/config, validate width > 0 and width < Integer.MAX_VALUE before building the transform"],"exampleFix":"// before\nPARTITIONED BY truncate(0, id)\n// after\nPARTITIONED BY truncate(10, id)","handlingStrategy":"validation","validationCode":"long width = /* width value */;\nif (width <= 0 || width >= Integer.MAX_VALUE) {\n  throw new IllegalArgumentException(\"truncate width must be > 0 and < Integer.MAX_VALUE: \" + width);\n}","typeGuard":"boolean isValidWidth(Object v) {\n  long w = (v instanceof Number n) ? n.longValue() : Long.MIN_VALUE;\n  return w > 0 && w < Integer.MAX_VALUE;\n}","tryCatchPattern":"try {\n  builder.truncate(colName, width);\n} catch (IllegalArgumentException e) {\n  throw new IllegalArgumentException(\"Invalid truncate width in partition clause: \" + e.getMessage(), e);\n}","preventionTips":["Validate truncate width is a positive int before building transforms","Reject zero/negative widths at DDL parse time","Clamp or reject widths sourced from user config"],"tags":["spark","truncate","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}