{"record":{"id":"74a753a22b408701","repo":"apache/iceberg","slug":"expected-value-to-be-date-or-timestamp","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/DaysFunction.java","lineNumber":48,"sourceCode":"import org.apache.spark.sql.types.TimestampType;\n\n/**\n * A Spark function implementation for the Iceberg day transform.\n *\n * <p>Example usage: {@code SELECT system.days('source_col')}.\n */\npublic class DaysFunction extends UnaryUnboundFunction {\n\n  @Override\n  protected BoundFunction doBind(DataType valueType) {\n    if (valueType instanceof DateType) {\n      return new DateToDaysFunction();\n    } else if (valueType instanceof TimestampType) {\n      return new TimestampToDaysFunction();\n    } else if (valueType instanceof TimestampNTZType) {\n      return new TimestampNtzToDaysFunction();\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 day transform\\n\"\n        + \"  col :: source column (must be date or timestamp)\";\n  }\n\n  @Override\n  public String name() {\n    return \"days\";\n  }\n\n  protected abstract static class BaseToDaysFunction extends BaseScalarFunction<Integer>\n      implements ReducibleFunction<Integer, Integer> {","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/functions/DaysFunction.java#L30-L66","documentation":"Bind-time type validation in DaysFunction.doBind: the argument to system.days is neither DateType, TimestampType nor TimestampNTZType (the three temporal kinds the day transform accepts). The appended value is the offending type; the failure occurs when Spark binds the function, before any row is evaluated.","triggerScenarios":"Calling system.days(<expr>) where expr is a string, long, or other non-temporal type, e.g. system.days('2024-01-01') or system.days(id).","commonSituations":"Passing date strings instead of date literals; applying days() to bigint epoch columns; comparing partition columns with uncast values in SQL predicates.","solutions":["Cast to date/timestamp first: system.days(CAST(col AS DATE)) or system.days(CAST('2024-01-01' AS DATE))","Use to_date/to_timestamp to convert strings: system.days(to_date('2024-01-01'))","For epoch-second bigint columns, convert: system.days(timestamp_millis(col * 1000))"],"exampleFix":"// before\nSELECT * FROM t WHERE system.days(ts_col_string) = ...;  -- string\n// after\nSELECT * FROM t WHERE system.days(CAST(ts_col_string AS TIMESTAMP)) = ...;","handlingStrategy":"validation","validationCode":"if (!(value.dataType() instanceof DateType) && !(value.dataType() instanceof TimestampType) && !(value.dataType() instanceof TimestampNTZType)) {\n  throw new IllegalArgumentException(\"days() 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 = daysFn.doBind(valueType);\n} catch (UnsupportedOperationException e) {\n  throw new IllegalArgumentException(\"Cast the expression to DATE or TIMESTAMP\", e);\n}","preventionTips":["Cast strings/numbers to date/timestamp before temporal transforms","Never pass raw epoch bigints to days()/hours()/months()","Use to_date/to_timestamp for string values in partition 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"}