{"record":{"id":"0b70569235046b3b","repo":"apache/iceberg","slug":"expected-truncation-width-to-be-tinyint-shortint-0b7056","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.0/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.0/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java#L54-L90","documentation":"In TruncateFunction.bind(), the first argument (truncation width) must be ByteType, ShortType, or IntegerType. bind() throws UnsupportedOperationException if the width field has any other type (long, decimal, string, null, etc.).","triggerScenarios":"Calling iceberg.truncate(10L, col), truncate('10', col), truncate(cast(10 as bigint), col), or passing a NULL without a cast.","commonSituations":"Numeric literals read as BIGINT from config-driven SQL; width pulled from a BIGINT column; width parameter bound from application code as a 64-bit value.","solutions":["Use an INT literal: truncate(10, col) — plain integer literals in Spark are INT.","Cast the width: truncate(cast(width_col AS INT), col).","Cast untyped NULL: truncate(cast(NULL AS INT), col).","Check the width expression's type with typeof() to confirm."],"exampleFix":"// before\nSELECT iceberg.truncate(100L, id) FROM t;\n// after\nSELECT iceberg.truncate(100, id) FROM t;\n-- or\nSELECT iceberg.truncate(CAST(width_col AS INT), id) FROM t;","handlingStrategy":"validation","validationCode":"val widthType = widthExpr.dataType\nrequire(widthType == ByteType || widthType == ShortType || widthType == IntegerType,\n  s\"truncate width must be tinyint/short/int, got $widthType\")","typeGuard":"def isSupportedWidth(t: DataType): Boolean =\n  t == ByteType || t == ShortType || t == IntegerType","tryCatchPattern":null,"preventionTips":["Use INT literals for truncate widths in SQL","Cast width values sourced from configs or BIGINT columns with CAST(... AS INT)"],"tags":["spark","sql-function-binding","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}