{"record":{"id":"60d83f3ea58dc7d0","repo":"apache/iceberg","slug":"cannot-find-width-for-transform","errorCode":null,"errorMessage":"Cannot find width for transform: ","messagePattern":"Cannot find width for transform: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/Spark3Util.java","lineNumber":499,"sourceCode":"          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];\n  }\n\n  private static String peerName(String[] fieldNames, String fieldName) {\n    if (fieldNames.length > 1) {\n      String[] peerNames = Arrays.copyOf(fieldNames, fieldNames.length);\n      peerNames[fieldNames.length - 1] = fieldName;\n      return DOT.join(peerNames);\n    }\n    return fieldName;\n  }\n\n  private static String parentName(String[] fieldNames) {","sourceCodeStart":481,"sourceCodeEnd":517,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/Spark3Util.java#L481-L517","documentation":"findWidth can only read widths from Integer or Long literals attached to a Spark truncate transform. If the expression is any other shape (non-literal, other numeric types, casts), it throws IllegalArgumentException 'Cannot find width for transform'.","triggerScenarios":"PARTITIONED BY truncate('10', col) (string literal), truncate(colW, col) (column instead of literal), or truncate(10L * 2, col) that Spark does not fold to a literal before conversion.","commonSituations":"Dynamic width from a config column or non-literal expression in generated DDL; string-typed width parameters; expressions Spark didn't constant-fold.","solutions":["Pass the width as an integer literal: truncate(10, col)","Cast or inline the width value directly in the DDL/code rather than referencing a column or config expression","If building transforms programmatically, use Literal.create(10, DataTypes.IntegerType)"],"exampleFix":"// before\nTransforms.truncate(widthStr, col) // widthStr is \"10\"\n// after\nTransforms.truncate(Integer.parseInt(widthStr), col) // ensure > 0 and < Integer.MAX_VALUE","handlingStrategy":"validation","validationCode":"if (!(expr instanceof org.apache.spark.sql.catalyst.expressions.Literal)) {\n  throw new IllegalArgumentException(\"truncate width must be a literal: \" + expr);\n}","typeGuard":"boolean isIntOrLongLiteral(org.apache.spark.sql.catalyst.expressions.Expression e) {\n  return e instanceof org.apache.spark.sql.catalyst.expressions.Literal\n      && (((Literal<?>) e).dataType() instanceof IntegerType\n          || ((Literal<?>) e).dataType() instanceof LongType);\n}","tryCatchPattern":"try {\n  int width = findWidth(transform);\n} catch (IllegalArgumentException e) {\n  throw new IllegalArgumentException(\"truncate() requires an integer literal width\", e);\n}","preventionTips":["Always pass the width as a plain integer literal in DDL","Avoid string or column-typed width parameters","Constant-fold programmatic widths before building the transform"],"tags":["spark","truncate","literal"],"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"}