Repository navigation
[Feature] Support Spark expression: timestamp_add #3113
Description
What is the problem the feature request solves?
Note: This issue was generated with AI assistance. The specification details have been extracted from Spark documentation and may need verification.
Comet does not currently support the Spark timestamp_add function, causing queries using this function to fall back to Spark's JVM execution instead of running natively on DataFusion.
The TimestampAdd expression adds a specified quantity of time units to a timestamp value. It supports various time units (like days, hours, minutes, seconds) and is timezone-aware, returning a timestamp of the same type as the input timestamp.
Supporting this expression would allow more Spark workloads to benefit from Comet's native acceleration.
Describe the potential solution
Spark Specification
Syntax:
TIMESTAMPADD(unit, quantity, timestamp)Arguments:
| Argument | Type | Description |
|---|---|---|
| unit | String | The time unit to add (e.g., "YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND") |
| quantity | Long | The number of units to add to the timestamp |
| timestamp | AnyTimestampType | The base timestamp to which the quantity will be added |
| timeZoneId | Option[String] | Optional timezone identifier for timezone-aware calculations |
Return Type: Returns the same data type as the input timestamp parameter (preserves whether it's TimestampType or TimestampNTZType).
Supported Data Types:
- quantity:
LongTypeonly - timestamp: Any timestamp type (
TimestampTypeorTimestampNTZType) - unit: String literal representing valid time units
Edge Cases:
- Null handling: Returns null if any input parameter (quantity or timestamp) is null (
nullIntolerant = true) - Timezone handling: Automatically selects appropriate timezone based on timestamp data type
- Unit validation: Invalid unit strings are handled during expression conversion phase
- Overflow: Large quantity values may cause timestamp overflow, behavior depends on underlying
DateTimeUtilsimplementation
Examples:
-- Add 5 days to a timestamp
SELECT TIMESTAMPADD('DAY', 5, TIMESTAMP '2010-01-01 01:02:03.123456');
-- Add 3 hours to current timestamp
SELECT TIMESTAMPADD('HOUR', 3, current_timestamp());
-- Subtract time by using negative quantity
SELECT TIMESTAMPADD('MINUTE', -30, TIMESTAMP '2010-01-01 12:00:00');// DataFrame API usage
import org.apache.spark.sql.functions._
// Add 7 days to a timestamp column
df.select(expr("TIMESTAMPADD('DAY', 7, timestamp_col)"))
// Using column references for quantity
df.select(expr("TIMESTAMPADD('HOUR', quantity_col, timestamp_col)"))Implementation Approach
See the Comet guide on adding new expressions for detailed instructions.
- Scala Serde: Add expression handler in
spark/src/main/scala/org/apache/comet/serde/ - Register: Add to appropriate map in
QueryPlanSerde.scala - Protobuf: Add message type in
native/proto/src/proto/expr.protoif needed - Rust: Implement in
native/spark-expr/src/(check if DataFusion has built-in support first)
Additional context
Difficulty: Medium
Spark Expression Class: org.apache.spark.sql.catalyst.expressions.TimestampAdd
Related:
DateAdd- For date-only arithmeticDateSub- For subtracting from datesIntervalexpressions for duration-based calculationsTimeZoneAwareExpressiontrait for timezone handling
This issue was auto-generated from Spark reference documentation.
Activity
dwsmith1983 commentedon Sep 11, 2026
timestampaddis handled since #5030 (0fe9b38):CometTimestampAddin spark/src/main/scala/org/apache/comet/serde/datetime.scala routes the expression through the codegen dispatcher, so it executes inside the Comet pipeline instead of forcing a Spark fallback. It is covered by spark/src/test/resources/sql-tests/expressions/datetime/timestampadd.sql for bothtimestampandtimestamp_ntzinputs. Suggest closing.andygrove commentedon Sep 28, 2026
timestampadd has run through the codegen dispatcher since #5030, and timestampadd.sql covers both timestamp and timestamp_ntz inputs, so it no longer falls back to Spark.