DateFromUnixDate¶
Overview¶
The DateFromUnixDate expression converts a Unix date integer (days since epoch) to a SQL DATE value. It takes an integer representing the number of days since January 1, 1970 (Unix epoch) and returns the corresponding date.
Syntax¶
Arguments¶
| Argument | Type | Description |
|---|---|---|
| unix_date | INTEGER | Number of days since Unix epoch (1970-01-01) |
Return Type¶
DATE - Returns a date value representing the calculated date.
Supported Data Types¶
- Input:
IntegerTypeonly - The expression uses implicit casting to convert compatible numeric types to integers
Algorithm¶
- Takes the input integer value representing days since Unix epoch
- Performs null-safety check on the input value
- Returns the integer value directly as the internal representation (Spark internally stores dates as days since epoch)
- No actual date conversion computation is needed since Spark's internal date representation matches the input format
- Code generation creates optimized bytecode that simply passes through the input value
Partitioning Behavior¶
- Preserves partitioning as this is a deterministic unary transformation
- Does not require shuffle operations
- Can be pushed down to individual partitions independently
Edge Cases¶
- Null handling: Returns null for null input values (nullIntolerant = true means null inputs produce null outputs)
- Integer overflow: Subject to standard integer overflow limitations of the input type
- Negative values: Accepts negative integers representing dates before Unix epoch
- Out of range dates: May produce invalid dates for extremely large positive or negative integers
Code Generation¶
This expression supports full code generation (Tungsten). The doGenCode method uses defineCodeGen with a simple pass-through function, generating optimized bytecode that directly copies the input value without interpretation overhead.
Examples¶
-- Convert Unix date to actual date
SELECT date_from_unix_date(1);
-- Result: 1970-01-02
-- Convert multiple Unix dates
SELECT date_from_unix_date(0), date_from_unix_date(365), date_from_unix_date(-1);
-- Result: 1970-01-01, 1971-01-01, 1969-12-31
-- Handle null input
SELECT date_from_unix_date(NULL);
-- Result: NULL
// DataFrame API usage
import org.apache.spark.sql.functions.expr
df.select(expr("date_from_unix_date(unix_day_column)"))
// Using with literal values
df.select(expr("date_from_unix_date(1)").as("epoch_plus_one"))
See Also¶
unix_date()- Converts date to Unix date integer (inverse operation)date_add()- Adds days to a datefrom_unixtime()- Converts Unix timestamp to timestamp valueto_date()- Converts string to date value