apache/iceberg · error · UnsupportedOperationException

Expected truncation col to be tinyint, shortint, int, bigint

Error message

Expected truncation col to be tinyint, shortint, int, bigint, decimal, string, or binary

What it means

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.

Source

Thrown at spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/functions/TruncateFunction.java:93

    DataType valueType = valueField.dataType();
    if (valueType instanceof ByteType) {
      return new TruncateTinyInt();
    } else if (valueType instanceof ShortType) {
      return new TruncateSmallInt();
    } else if (valueType instanceof IntegerType) {
      return new TruncateInt();
    } else if (valueType instanceof LongType) {
      return new TruncateBigInt();
    } else if (valueType instanceof DecimalType) {
      return new TruncateDecimal(
          ((DecimalType) valueType).precision(), ((DecimalType) valueType).scale());
    } else if (valueType instanceof StringType) {
      return new TruncateString();
    } else if (valueType instanceof BinaryType) {
      return new TruncateBinary();
    } else {
      throw new UnsupportedOperationException(
          "Expected truncation col to be tinyint, shortint, int, bigint, decimal, string, or binary");
    }
  }

  @Override
  public String description() {
    return name()
        + "(width, col) - Call Iceberg's truncate transform\n"
        + "  width :: width for truncation, e.g. truncate(10, 255) -> 250 (must be an integer)\n"
        + "  col :: column to truncate (must be an integer, decimal, string, or binary)";
  }

  @Override
  public String name() {
    return "truncate";
  }

  public abstract static class TruncateBase<T> extends BaseScalarFunction<T> {

View on GitHub (pinned to 86d9c8fc54)

Solutions

  1. Cast the value to a supported type first, e.g. `truncate(16, CAST(ts AS STRING))`.
  2. Use a type-appropriate function: date_trunc for timestamps, round/bround for doubles.
  3. If truncating for partitioning, choose a supported column type or cast inside the transform expression.

Example fix

// before
spark.sql("SELECT truncate(1, ts_col) FROM t")
// after
spark.sql("SELECT date_trunc('DAY', ts_col) FROM t")
Defensive patterns

Strategy: type-guard

Validate before calling

Set<String> supported = Set.of("tinyint","shortint","int","bigint","decimal","string","binary");
String t = df.schema().apply("col").dataType().simpleString();
if (!supported.contains(t)) throw new IllegalArgumentException("truncate unsupported for type: " + t);

Type guard

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; }

Try / catch

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; }

Prevention

When it happens

Trigger: Calling `truncate(width, col)` where col is date, timestamp, float, double, boolean, array, or struct.

Common situations: Attempting to truncate floating point or timestamp columns; expecting substring-like behavior on non-string types; porting truncate semantics from other engines.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/iceberg@86d9c8fc54 (2026-09-12). Data as JSON: /api/errors/5d2bd677fdb31c4c. Report an issue: GitHub.