{"record":{"id":"fba91e0111726eab","repo":"apache/iceberg","slug":"expected-value-to-be-date-or-timestamp-fba91e","errorCode":null,"errorMessage":"Expected value to be date or timestamp: ","messagePattern":"Expected value to be date or timestamp: ","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"spark/v4.1/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.1/spark/src/main/java/org/apache/iceberg/spark/functions/MonthsFunction.java#L28-L64","documentation":"Bind-time type validation in MonthsFunction.doBind: the argument to system.months is not a date or timestamp type (DateType, TimestampType, TimestampNTZType are the accepted kinds). The appended value is the offending type; the function cannot be bound for it.","triggerScenarios":"Calling system.months(<expr>) with a string, numeric, or other non-temporal expression, e.g. system.months('2024-01-01') or system.months(id).","commonSituations":"Passing date strings instead of literals; applying months() to bigint epoch columns; partition predicates comparing months() with raw strings.","solutions":["Cast to date/timestamp: system.months(CAST(col AS DATE)) or system.months(to_date('2024-01-01'))","Use years() or days() where appropriate; ensure the partition transform column is temporal","Convert epoch numbers: system.months(timestamp_millis(col))"],"exampleFix":"// before\nSELECT * FROM t WHERE system.months(dt_str) = 650;\n// after\nSELECT * FROM t WHERE system.months(CAST(dt_str AS DATE)) = 650;","handlingStrategy":"validation","validationCode":"if (!(value.dataType() instanceof DateType) && !(value.dataType() instanceof TimestampType) && !(value.dataType() instanceof TimestampNTZType)) {\n  throw new IllegalArgumentException(\"months() requires date or timestamp, got: \" + value.dataType());\n}","typeGuard":"boolean temporal(DataType t) {\n  return t instanceof DateType || t instanceof TimestampType || t instanceof TimestampNTZType;\n}","tryCatchPattern":"try {\n  bound = monthsFn.doBind(valueType);\n} catch (UnsupportedOperationException e) {\n  throw new IllegalArgumentException(\"Cast the expression to DATE or TIMESTAMP\", e);\n}","preventionTips":["Cast string/numeric values to date/timestamp before months()","Use years() for coarse partitioning on date columns","Validate transform argument types when building table specs or predicates"],"tags":["spark","sql-function","date-transform","type-mismatch"],"backgroundTag":"invalid-argument-value","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"}