Skip to content

[Feature] Support Spark expression: time_add_interval #3121

Description

@andygrove

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_add_interval function, causing queries using this function to fall back to Spark's JVM execution instead of running natively on DataFusion.

TimeAddInterval is a Spark Catalyst expression that adds a day-time interval to a time value. It extends BinaryExpression and RuntimeReplaceable, meaning it is replaced with a StaticInvoke call to DateTimeUtils.timeAddInterval during expression optimization. The expression handles precision calculation to ensure the result maintains appropriate time precision based on both the input time and interval types.

Supporting this expression would allow more Spark workloads to benefit from Comet's native acceleration.

Describe the potential solution

Spark Specification

Syntax:

time_expression + interval_expression

Arguments:

Argument Type Description
time Expression The base time value to which the interval will be added
interval Expression The day-time interval to add to the time

Return Type: Returns a TimeType with precision calculated as the maximum of the input time precision and the interval precision. The interval precision is determined by the interval's end field - if less than SECOND, uses MIN_PRECISION, otherwise uses MICROS_PRECISION.

Supported Data Types:

  • time: AnyTimeType - accepts time values with any precision
  • interval: DayTimeIntervalType - accepts day-time intervals with any start and end fields

Edge Cases:

  • Null handling: The expression is null intolerant (nullIntolerant = true), meaning null inputs produce null outputs with proper null propagation
  • Type validation: Throws SparkException.internalError if unexpected input types are encountered during replacement
  • Precision handling: Automatically adjusts precision to accommodate both time and interval precision requirements
  • Overflow behavior: Relies on underlying DateTimeUtils.timeAddInterval implementation for overflow handling

Examples:

-- Add 2 hours to a time value
SELECT TIME '10:30:00' + INTERVAL '2' HOUR;

-- Add days and hours to a time (days component wraps around)
SELECT TIME '14:15:30' + INTERVAL '1 5' DAY TO HOUR;
// Example DataFrame API usage
import org.apache.spark.sql.functions._

df.select(col("time_col") + expr("INTERVAL '30' MINUTE"))

// Using interval literal
df.select(col("time_col") + lit(Duration.ofHours(3)))

Implementation Approach

See the Comet guide on adding new expressions 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.TimeAddInterval

Related:

  • TimeSubInterval - for subtracting intervals from time values
  • TimestampAddInterval - for adding intervals to timestamp values
  • DateTimeUtils.timeAddInterval - the underlying implementation method
  • DayTimeIntervalType - for day-time interval data type details

This issue was auto-generated from Spark reference documentation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions