{"record":{"id":"94d6984800f69e0e","repo":"apache/iceberg","slug":"expected-value-to-be-date-or-timestamp-valuetyp-94d698","errorCode":null,"errorMessage":"Expected value to be date or timestamp: ${valueType.catalogString()}","messagePattern":"Expected value to be date or timestamp: (.+?)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/functions/MonthsFunction.java","lineNumber":46,"sourceCode":"import org.apache.spark.sql.types.TimestampType;\n\n/**\n * A Spark function implementation for the Iceberg month transform.\n *\n * <p>Example usage: {@code SELECT system.months('source_col')}.\n */\npublic class MonthsFunction extends UnaryUnboundFunction {\n\n  @Override\n  protected BoundFunction doBind(DataType valueType) {\n    if (valueType instanceof DateType) {\n      return new DateToMonthsFunction();\n    } else if (valueType instanceof TimestampType) {\n      return new TimestampToMonthsFunction();\n    } else if (valueType instanceof TimestampNTZType) {\n      return new TimestampNtzToMonthsFunction();\n    } else {\n      throw new UnsupportedOperationException(\n          \"Expected value to be date or timestamp: \" + valueType.catalogString());\n    }\n  }\n\n  @Override\n  public String description() {\n    return name()\n        + \"(col) - Call Iceberg's month transform\\n\"\n        + \"  col :: source column (must be date or timestamp)\";\n  }\n\n  @Override\n  public String name() {\n    return \"months\";\n  }\n\n  private abstract static class BaseToMonthsFunction extends BaseScalarFunction<Integer> {\n    @Override","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/functions/MonthsFunction.java#L28-L64","documentation":"Iceberg's months() transform function binds its single argument at planning time. Only DATE, TIMESTAMP, and TIMESTAMP_NTZ support month granularity; any other type throws UnsupportedOperationException that echoes the offending type.","triggerScenarios":"Calling iceberg.months(col) where col is STRING, INT, BIGINT, or any non-temporal type — e.g. months(order_ts_str).","commonSituations":"Monthly partitioning attempts on string-typed timestamps; passing epoch BIGINT columns; type drift after schema migration changed a timestamp column to string.","solutions":["Cast the column: months(cast(ts_str AS TIMESTAMP)).","For epoch seconds: months(timestamp_seconds(epoch_col)).","Verify the column type with typeof(col) before applying the transform."],"exampleFix":"// before\nSELECT iceberg.months(order_ts_str) FROM t;\n// after\nSELECT iceberg.months(CAST(order_ts_str AS TIMESTAMP)) FROM t;","handlingStrategy":"type-guard","validationCode":"require(Seq(DateType, TimestampType, TimestampNTZType).exists(_.acceptsType(col.dataType)),\n  s\"months() requires date/timestamp, got ${col.dataType}\")","typeGuard":"def isTemporalForMonths(t: DataType): Boolean =\n  t == DateType || t == TimestampType || t == TimestampNTZType","tryCatchPattern":null,"preventionTips":["Validate column types after schema migrations — a timestamp changed to string will break months()","Cast string timestamps explicitly before temporal transforms"],"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"}