TryMakeTimestampFromDateTime¶
Overview¶
TryMakeTimestampFromDateTime is a runtime-replaceable Catalyst expression that attempts to construct a timestamp from date and optional time components. Unlike its non-try counterpart, this expression returns null instead of throwing an exception when the input values cannot be converted to a valid timestamp.
Syntax¶
// DataFrame API usage would depend on the function registry
col("date_col").expr("try_make_timestamp(date_col)")
Arguments¶
| Argument | Type | Description |
|---|---|---|
| date | Expression | The date component for the timestamp |
| time | Expression (optional) | The time component to combine with the date |
| timezone | Expression (optional) | The timezone for the resulting timestamp |
| replacement | Expression | Internal replacement expression used during runtime |
Return Type¶
Returns TimestampType on successful conversion, or null if the conversion fails.
Supported Data Types¶
- date: Date types, string representations of dates
- time: Time types, string representations of time
- timezone: String representations of valid timezone identifiers
Algorithm¶
- Delegates evaluation to the underlying
MakeTimestampFromDateTimeexpression through runtime replacement - Catches any exceptions thrown during timestamp construction and returns null instead
- Validates that the date component is not null before attempting conversion
- Applies timezone conversion if a timezone parameter is provided
- Returns the constructed timestamp as a long value representing microseconds since epoch
Partitioning Behavior¶
This expression preserves partitioning behavior:
- Does not require data shuffle as it operates row-by-row
- Maintains existing partition boundaries
- Can be safely pushed down in query optimization
Edge Cases¶
- Null date input: Returns null without attempting conversion
- Invalid date formats: Returns null instead of throwing parse exceptions
- Invalid timezone: Returns null for unrecognized timezone identifiers
- Leap second handling: Behavior depends on the underlying datetime implementation
- Daylight saving transitions: May return null for ambiguous times during DST changes
Code Generation¶
As a RuntimeReplaceable expression, this delegates code generation to its replacement expression (MakeTimestampFromDateTime). The actual code generation support depends on the underlying replacement implementation.
Examples¶
-- Basic usage with date only
SELECT try_make_timestamp('2023-12-25') as christmas_timestamp;
-- With date and time components
SELECT try_make_timestamp('2023-12-25', '15:30:00') as afternoon_timestamp;
-- With timezone specification
SELECT try_make_timestamp('2023-12-25', '15:30:00', 'UTC') as utc_timestamp;
-- Handling invalid input (returns null)
SELECT try_make_timestamp('invalid-date') as null_result;
// DataFrame API usage (conceptual)
import org.apache.spark.sql.functions._
df.select(expr("try_make_timestamp(date_col)").as("timestamp_col"))
df.select(expr("try_make_timestamp(date_col, time_col, 'PST')").as("pst_timestamp"))
See Also¶
MakeTimestampFromDateTime- The non-try version that throws exceptions on invalid inputMakeTimestamp- Creates timestamps from individual year/month/day/hour/minute/second componentsToTimestamp- Converts string representations to timestamps with format patterns