{"record":{"id":"ea332b7e5e040f9c","repo":"apache/iceberg","slug":"expected-truncation-width-to-be-tinyint-shortint-ea332b","errorCode":null,"errorMessage":"Expected truncation width to be tinyint, shortint or int","messagePattern":"Expected truncation width to be tinyint, shortint or int","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java","lineNumber":72,"sourceCode":"public class TruncateFunction implements UnboundFunction {\n\n  private static final int WIDTH_ORDINAL = 0;\n  private static final int VALUE_ORDINAL = 1;\n\n  private static final Set<DataType> SUPPORTED_WIDTH_TYPES =\n      ImmutableSet.of(DataTypes.ByteType, DataTypes.ShortType, DataTypes.IntegerType);\n\n  @Override\n  public BoundFunction bind(StructType inputType) {\n    if (inputType.size() != 2) {\n      throw new UnsupportedOperationException(\"Wrong number of inputs (expected width and value)\");\n    }\n\n    StructField widthField = inputType.fields()[WIDTH_ORDINAL];\n    StructField valueField = inputType.fields()[VALUE_ORDINAL];\n\n    if (!SUPPORTED_WIDTH_TYPES.contains(widthField.dataType())) {\n      throw new UnsupportedOperationException(\n          \"Expected truncation width to be tinyint, shortint or int\");\n    }\n\n    DataType valueType = valueField.dataType();\n    if (valueType instanceof ByteType) {\n      return new TruncateTinyInt();\n    } else if (valueType instanceof ShortType) {\n      return new TruncateSmallInt();\n    } else if (valueType instanceof IntegerType) {\n      return new TruncateInt();\n    } else if (valueType instanceof LongType) {\n      return new TruncateBigInt();\n    } else if (valueType instanceof DecimalType) {\n      return new TruncateDecimal(\n          ((DecimalType) valueType).precision(), ((DecimalType) valueType).scale());\n    } else if (valueType instanceof StringType) {\n      return new TruncateString();\n    } else if (valueType instanceof BinaryType) {","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java#L54-L90","documentation":"Iceberg's truncate function requires the width argument to be an integral type: tinyint (byte), shortint (short), or int. bind() throws this UnsupportedOperationException when the first argument is any other type (e.g. string, bigint, long literal).","triggerScenarios":"Passing a width that is a string ('10'), a long/bigint (10L), decimal, or a column of non-integral type as the first argument to truncate().","commonSituations":"Passing width as a string literal from templated SQL; using a BIGINT column or parameter as width; Spark inferring an integer literal as bigint in some contexts.","solutions":["Cast the width to INT: truncate(CAST(10 AS INT), col)","Use a plain integer literal, e.g. truncate(10, col)","If width is stored as bigint/string, coerce it with CAST or try_cast before calling truncate"],"exampleFix":"// before\nSELECT truncate(width_col, some_col) FROM t; -- width_col is BIGINT\n// after\nSELECT truncate(CAST(width_col AS INT), some_col) FROM t;","handlingStrategy":"validation","validationCode":"// ensure width is integral\nval widthType = StringType etc; require(Seq(ByteType, ShortType, IntegerType).contains(widthExpr.dataType), \"width must be tinyint/short/int\")","typeGuard":"def isIntegralWidth(t: DataType): Boolean = t match { case _: ByteType | _: ShortType | _: IntegerType => true; case _ => false }","tryCatchPattern":"try { ... } catch { case e: UnsupportedOperationException if e.getMessage.contains(\"truncation width\") => ... }","preventionTips":["Use plain integer literals for width","CAST non-int widths to INT before the call","Avoid passing width columns of BIGINT or STRING type"],"tags":["spark","sql","type-mismatch","function-binding"],"backgroundTag":"type-mismatch","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"}