{"record":{"id":"c681463bf3799e8a","repo":"apache/iceberg","slug":"cannot-find-width-for-transform-s","errorCode":null,"errorMessage":"Cannot find width for transform: %s","messagePattern":"Cannot find width for transform: (.+?)","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/Spark3Util.java","lineNumber":495,"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":477,"sourceCodeEnd":513,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/Spark3Util.java#L477-L513","documentation":"findWidth extracts the width/parameter of a Spark truncate transform from its first literal argument. If the transform has no Long literal argument, the method exhausts its search and throws this IllegalArgumentException naming the transform description.","triggerScenarios":"Calling truncate without a width argument or with a non-literal width (e.g. truncate(cols(x)) or truncate(NON_LITERAL_EXPR, col)) through toPartitionSpec or toIcebergTerm.","commonSituations":"Programmatic transform construction omitting the width; DDL where the width is supplied as a parameter/config value that resolved to null; dynamic SQL generation bugs.","solutions":["Always provide truncate with a positive integer literal width, e.g. truncate(10, col)","Ensure the width argument is a Literal<Long>, not an expression or unresolved parameter","Validate the transform description before calling applySchemaChanges/applyPartitioning"],"exampleFix":"// before\nTransforms.truncate(widthParam, col) // widthParam unresolved\n// after\nTransforms.truncate(10, col)","handlingStrategy":"validation","validationCode":"boolean hasLiteralWidth(Transform t) { return t.arguments().length > 0 && t.arguments()[0] instanceof Literal<?> lit && lit.value() instanceof Long; }","typeGuard":"Long extractWidth(Transform t) { return t.arguments().length > 0 && t.arguments()[0] instanceof Literal<?> lit && lit.value() instanceof Long ? (Long) lit.value() : null; }","tryCatchPattern":"try { Spark3Util.toPartitionSpec(schema, parts); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith(\"Cannot find width for transform\")) { /* supply truncate(N, col) with a literal width */ } else throw e; }","preventionTips":["Always write truncate(N, col) with a literal positive integer N","Avoid passing unresolved parameters or expressions as the width","Unit-test DDL generation to confirm width literals are present"],"tags":["spark","transform","truncate","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}