DivideDTInterval¶
Overview¶
The DivideDTInterval expression divides a day-time interval by a numeric value, returning a new day-time interval. This operation performs division with HALF_UP rounding mode and includes comprehensive overflow and divide-by-zero checks to ensure safe arithmetic operations.
Syntax¶
Arguments¶
| Argument | Type | Description |
|---|---|---|
interval |
DayTimeIntervalType |
The day-time interval to be divided |
num |
NumericType |
The numeric divisor (integral, decimal, or fractional types) |
Return Type¶
DayTimeIntervalType() - Returns a day-time interval representing the result of the division operation.
Supported Data Types¶
- Left operand (interval):
DayTimeIntervalTypeonly - Right operand (num): All numeric types including:
IntegralType(Byte, Short, Int, Long)DecimalTypeFractionalType(Float, Double)
Algorithm¶
- Converts the day-time interval to microseconds (Long representation)
- Performs type-specific division based on the divisor's data type:
- Integral types: Uses
LongMath.divide()withHALF_UProunding - Decimal types: Converts to Decimal, performs division, then rounds to Long with
HALF_UP - Fractional types: Uses
DoubleMath.roundToLong()withHALF_UProunding - Validates for divide-by-zero and overflow conditions before computation
- Returns the result as a day-time interval in microseconds
Partitioning Behavior¶
- Preserves partitioning: No, this is a value-transforming operation
- Requires shuffle: No, operates on individual rows independently
- Can be applied within existing partitions without data movement
Edge Cases¶
- Null handling: Expression is null-intolerant (
nullIntolerant = true) - returns null if either operand is null - Divide by zero: Throws
QueryExecutionErrorswhen divisor is zero - Overflow behavior:
- Checks for
Long.MinValue / -1overflow condition - Throws
QueryExecutionErrors.overflowInIntegralDivideErroron overflow - Uses
longValueExact()for decimal operations to detect overflow - Precision: All division operations use
HALF_UProunding mode for consistent behavior
Code Generation¶
This expression supports full Tungsten code generation with type-specific optimized paths:
- Integral types: Generates inline overflow checks and LongMath.divide() calls
- Decimal types: Generates Decimal arithmetic with precision handling
- Fractional types: Generates optimized double division with DoubleMath.roundToLong()
- Includes generated divide-by-zero validation for all numeric types
Examples¶
-- Divide a day-time interval by an integer
SELECT INTERVAL '2 12:30:45.123' DAY TO SECOND / 2;
-- Result: INTERVAL '1 06:15:22.561500' DAY TO SECOND
-- Divide by decimal value
SELECT INTERVAL '5 00:00:00' DAY TO SECOND / 2.5;
-- Result: INTERVAL '2 00:00:00' DAY TO SECOND
// Example DataFrame API usage
import org.apache.spark.sql.functions._
// Divide interval column by numeric literal
df.select(col("day_time_interval") / lit(3))
// Divide interval by another numeric column
df.select(col("day_time_interval") / col("divisor"))
See Also¶
DivideYMInterval- Division operation for year-month intervalsMultiplyDTInterval- Multiplication of day-time intervalsIntervalDivide- Base trait for interval division operationsBinaryExpression- Parent class for binary operations