{"record":{"id":"d8d95deaeef34a8a","repo":"apache/iceberg","slug":"expected-truncation-col-to-be-tinyint-shortint-i-d8d95d","errorCode":null,"errorMessage":"Expected truncation col to be tinyint, shortint, int, bigint, decimal, string, or binary","messagePattern":"Expected truncation col to be tinyint, shortint, int, bigint, decimal, string, or binary","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java","lineNumber":93,"sourceCode":"\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) {\n      return new TruncateBinary();\n    } else {\n      throw new UnsupportedOperationException(\n          \"Expected truncation col to be tinyint, shortint, int, bigint, decimal, string, or binary\");\n    }\n  }\n\n  @Override\n  public String description() {\n    return name()\n        + \"(width, col) - Call Iceberg's truncate transform\\n\"\n        + \"  width :: width for truncation, e.g. truncate(10, 255) -> 250 (must be an integer)\\n\"\n        + \"  col :: column to truncate (must be an integer, decimal, string, or binary)\";\n  }\n\n  @Override\n  public String name() {\n    return \"truncate\";\n  }\n\n  public abstract static class TruncateBase<T> extends BaseScalarFunction<T> {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java#L75-L111","documentation":"After validating the width, TruncateFunction.bind() dispatches on the value column's type to a typed implementation. Only tinyint, smallint, int, bigint, decimal, string, and binary are supported; other types (timestamp, date, double, float, boolean, complex types) are rejected with this UnsupportedOperationException.","triggerScenarios":"Calling iceberg.truncate(width, col) where col is TIMESTAMP, DATE, DOUBLE, FLOAT, BOOLEAN, ARRAY, STRUCT, or MAP — e.g. truncate(10, event_ts).","commonSituations":"Trying to truncate a timestamp column (use the days/hours/months transforms instead); truncating float/double measurements; passing nested-typed columns.","solutions":["For temporal columns, use the appropriate transform instead: iceberg.days(ts), iceberg.hours(ts), or iceberg.months(ts).","For floats/doubles, cast to DECIMAL: truncate(cast(x AS DECIMAL(38, 4)) width-adjusted).","For other cases cast to a supported type, e.g. CAST(col AS STRING) before truncating.","Verify the value column type with typeof(col)."],"exampleFix":"// before\nSELECT iceberg.truncate(10, event_ts) FROM t; -- TIMESTAMP not supported\n// after\nSELECT iceberg.days(event_ts) FROM t;\n-- or, for numeric\nSELECT iceberg.truncate(4, CAST(score AS DECIMAL(38, 4))) FROM t;","handlingStrategy":"type-guard","validationCode":"val t = col.dataType\nval supported = Seq(ByteType, ShortType, IntegerType, LongType, StringType, BinaryType)\nrequire(supported.contains(t) || t.isInstanceOf[DecimalType], s\"truncate value type $t unsupported\")","typeGuard":"def isTruncatable(t: DataType): Boolean = t match {\n  case _: DecimalType => true\n  case ByteType | ShortType | IntegerType | LongType | StringType | BinaryType => true\n  case _ => false\n}","tryCatchPattern":null,"preventionTips":["Use days()/hours()/months() for temporal columns instead of truncate()","Avoid float/double columns in truncate expressions; cast to DECIMAL"],"tags":["spark","sql-function-binding","type-mismatch"],"backgroundTag":"type-mismatch","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"}