{"record":{"id":"97f0316ce9ea5660","repo":"apache/iceberg","slug":"expected-value-to-be-date-or-timestamp-valuetyp-97f031","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/v3.5/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/v3.5/spark/src/main/java/org/apache/iceberg/spark/functions/MonthsFunction.java#L28-L64","documentation":"The months() SQL catalog function only accepts date or timestamp (incl. timestamp_ntz) values. Binding with any other value type throws UnsupportedOperationException including the actual type via catalogString().","triggerScenarios":"Calling system.months(value) where value is a string, long, decimal, or other non-date/timestamp type.","commonSituations":"Passing string dates without CAST; passing epoch values; passing an int year-month number expecting month extraction.","solutions":["Cast the value: system.months(CAST(ts_str AS TIMESTAMP)) or system.months(CAST(d AS DATE))","Convert numeric epochs with timestamp_millis()/timestamp_micros() before months()","Use the months() only for temporal columns; otherwise use month() extraction functions"],"exampleFix":"// before\nspark.sql(\"SELECT system.months('2024-03-15')\")\n// after\nspark.sql(\"SELECT system.months(CAST('2024-03-15' AS DATE))\")","handlingStrategy":"type-guard","validationCode":"// months() accepts date/timestamp only\nboolean monthsOk(DataType t) { return t instanceof DateType || t instanceof TimestampType || t instanceof TimestampNTZType; }","typeGuard":"boolean monthsOk(DataType t) { return t instanceof DateType || t instanceof TimestampType || t instanceof TimestampNTZType; }","tryCatchPattern":null,"preventionTips":["CAST string/numeric inputs to DATE or TIMESTAMP before months()","Use Spark's month() for plain extraction instead of the transform function when types don't fit","Validate schemas in SQL-generating code paths"],"tags":["spark","sql-functions","temporal","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-14T16:17:12.679Z"}