You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2026/01/14 19:47:21 UTC
[I] [Feature] Support Spark expression: time_diff [datafusion-comet]
andygrove opened a new issue, #3122:
URL: https://github.com/apache/datafusion-comet/issues/3122
## 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 `time_diff` function, causing queries using this function to fall back to Spark's JVM execution instead of running natively on DataFusion.
The `TimeDiff` expression calculates the difference between two time-based values (timestamps, dates, or time intervals) in a specified unit. It is a ternary expression that takes a unit string and two temporal expressions, returning the numeric difference as a long integer.
Supporting this expression would allow more Spark workloads to benefit from Comet's native acceleration.
## Describe the potential solution
### Spark Specification
**Syntax:**
```sql
TIME_DIFF(unit, start_time, end_time)
```
```scala
// DataFrame API usage
import org.apache.spark.sql.functions._
df.select(expr("time_diff('SECOND', start_col, end_col)"))
```
**Arguments:**
| Argument | Type | Description |
|----------|------|-------------|
| unit | StringType | The unit of time difference to calculate (e.g., 'SECOND', 'MINUTE', 'HOUR', 'DAY') |
| start | AnyTimeType | The starting timestamp, date, or time value |
| end | AnyTimeType | The ending timestamp, date, or time value |
**Return Type:** `LongType` - Returns a long integer representing the time difference in the specified unit.
**Supported Data Types:**
- **unit**: String type with collation support (trim collation supported)
- **start**: Any time-related type (TimestampType, DateType, etc.)
- **end**: Any time-related type (TimestampType, DateType, etc.)
**Edge Cases:**
- **Null handling**: Returns null if any of the three arguments (unit, start, end) is null
- **Invalid unit**: Throws runtime exception for unrecognized time unit strings
- **Type mismatch**: Implicit casting is applied to make start and end compatible time types
- **Overflow**: May overflow for extremely large time differences that exceed Long.MAX_VALUE
- **Timezone handling**: Results depend on session timezone settings for timestamp calculations
**Examples:**
```sql
-- Calculate difference in seconds
SELECT TIME_DIFF('SECOND', '2023-01-01 10:00:00', '2023-01-01 10:05:30') AS diff_seconds;
-- Returns: 330
-- Calculate difference in days
SELECT TIME_DIFF('DAY', '2023-01-01', '2023-01-15') AS diff_days;
-- Returns: 14
```
```scala
// DataFrame API usage
import org.apache.spark.sql.functions._
val df = spark.sql("SELECT '2023-01-01 10:00:00' as start_time, '2023-01-01 12:00:00' as end_time")
df.select(expr("time_diff('HOUR', start_time, end_time)").alias("hour_diff")).show()
// Using with column references
df.withColumn("minute_diff", expr("time_diff('MINUTE', start_time, end_time)"))
```
### Implementation Approach
See the [Comet guide on adding new expressions](https://datafusion.apache.org/comet/contributor-guide/adding_a_new_expression.html) for detailed instructions.
1. **Scala Serde**: Add expression handler in `spark/src/main/scala/org/apache/comet/serde/`
2. **Register**: Add to appropriate map in `QueryPlanSerde.scala`
3. **Protobuf**: Add message type in `native/proto/src/proto/expr.proto` if needed
4. **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.TimeDiff`
**Related:**
- `DateDiff` - For date-only differences
- `DateAdd` - For adding time intervals
- `DateSub` - For subtracting time intervals
- `Extract` - For extracting specific time components
---
*This issue was auto-generated from Spark reference documentation.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] [Feature] Support Spark expression: time_diff [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove closed issue #3122: [Feature] Support Spark expression: time_diff
URL: https://github.com/apache/datafusion-comet/issues/3122
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] [Feature] Support Spark expression: time_diff [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on issue #3122:
URL: https://github.com/apache/datafusion-comet/issues/3122#issuecomment-5113040333
`TimeDiff` is a `RuntimeReplaceable` expression. The `ReplaceExpressions` rule (Finish Analysis batch) substitutes it with its `replacement` before physical planning, so the class never appears in a physical plan and Comet's expression serde never sees it. Registering a `CometExpressionSerde` for it would be dead code.
Its replacement is a `StaticInvoke` into `DateTimeUtils.timeDiff`, so this function is never gated on a serde for `TimeDiff`. Native support for `time_diff` would have to come from supporting that replacement, which is a different piece of work than this issue describes.
Closing as not actionable as specified. If the fallback matters in practice, a fresh issue should track the replacement path.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] [Feature] Support Spark expression: time_diff [datafusion-comet]
Posted by "subkanthi (via GitHub)" <gi...@apache.org>.
subkanthi commented on issue #3122:
URL: https://github.com/apache/datafusion-comet/issues/3122#issuecomment-3765417535
take
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org