{"record":{"id":"0847a46c731b133b","repo":"apache/iceberg","slug":"expected-truncation-col-to-be-tinyint-shortint-i-0847a4","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.1/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.1/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java#L75-L111","documentation":"The truncate function supports truncating tinyint, shortint, int, bigint, decimal, string, and binary values. bind() throws this UnsupportedOperationException when the value argument's type is outside that set (e.g. date, timestamp, float, double, boolean, array, struct).","triggerScenarios":"Calling truncate(width, col) where col is a DOUBLE/FLOAT, DATE, TIMESTAMP, BOOLEAN, or complex type; also passing a null-typed literal that Spark cannot coerce.","commonSituations":"Attempting float truncation via the Iceberg function (truncation only supports integral/decimal/string/binary per the Iceberg spec); truncating timestamps instead of using date_trunc; applying truncate to struct/array columns.","solutions":["Cast the value to a supported type first (e.g. CAST(ts AS STRING) or CAST(f AS DECIMAL(38,s)))","For date/timestamp truncation use date_trunc instead of truncate","For float/double rounding use round, floor, or cast to DECIMAL before truncating"],"exampleFix":"// before\nSELECT truncate(10, price) FROM t; -- price is DOUBLE\n// after\nSELECT truncate(2, CAST(price AS DECIMAL(38, 2))) FROM t;","handlingStrategy":"validation","validationCode":"require(Seq(\"tinyint\",\"shortint\",\"int\",\"bigint\",\"decimal\",\"string\",\"binary\").contains(valueColType), s\"truncate unsupported on $valueColType\")","typeGuard":null,"tryCatchPattern":"try { ... } catch { case e: UnsupportedOperationException if e.getMessage.contains(\"truncation col\") => ... }","preventionTips":["Check the column type with DESCRIBE TABLE before truncating","Use date_trunc for timestamps and round/cast for floats","Cast unsupported types to a supported type (e.g. STRING or DECIMAL) first"],"tags":["spark","sql","unsupported-type","function-binding"],"backgroundTag":"unsupported-argument-type","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"}