{"record":{"id":"5d2bd677fdb31c4c","repo":"apache/iceberg","slug":"expected-truncation-col-to-be-tinyint-shortint-i","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/v3.5/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/v3.5/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java#L75-L111","documentation":"The value (second) argument of `truncate` must be tinyint, shortint, int, bigint, decimal, string, or binary. `bind` throws this UnsupportedOperationException for any other value type because Iceberg's truncation transform is only defined for those types.","triggerScenarios":"Calling `truncate(width, col)` where col is date, timestamp, float, double, boolean, array, or struct.","commonSituations":"Attempting to truncate floating point or timestamp columns; expecting substring-like behavior on non-string types; porting truncate semantics from other engines.","solutions":["Cast the value to a supported type first, e.g. `truncate(16, CAST(ts AS STRING))`.","Use a type-appropriate function: date_trunc for timestamps, round/bround for doubles.","If truncating for partitioning, choose a supported column type or cast inside the transform expression."],"exampleFix":"// before\nspark.sql(\"SELECT truncate(1, ts_col) FROM t\")\n// after\nspark.sql(\"SELECT date_trunc('DAY', ts_col) FROM t\")","handlingStrategy":"type-guard","validationCode":"Set<String> supported = Set.of(\"tinyint\",\"shortint\",\"int\",\"bigint\",\"decimal\",\"string\",\"binary\");\nString t = df.schema().apply(\"col\").dataType().simpleString();\nif (!supported.contains(t)) throw new IllegalArgumentException(\"truncate unsupported for type: \" + t);","typeGuard":"boolean isTruncatable(DataType dt) { return dt instanceof ByteType || dt instanceof ShortType || dt instanceof IntegerType || dt instanceof LongType || dt instanceof DecimalType || dt instanceof StringType || dt instanceof BinaryType; }","tryCatchPattern":"try { spark.sql(\"SELECT truncate(10, col) FROM t\"); } catch (UnsupportedOperationException e) { if (e.getMessage().contains(\"Expected truncation col\")) { /* cast col or pick another function */ } throw e; }","preventionTips":["Check the column type with DESCRIBE TABLE before using truncate.","Use date_trunc/round for timestamps and floats instead of truncate.","Cast unsupported types to string explicitly when character truncation is intended."],"tags":["spark","sql-function","type-mismatch","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-14T16:17:12.679Z"}