TryEval¶
Overview¶
TryEval is a Spark Catalyst unary expression that wraps another expression in exception handling logic. It evaluates its child expression and returns the result if successful, or null if any exception occurs during evaluation. This expression provides a safe way to handle potentially failing operations without causing query failures.
Syntax¶
This is an internal Catalyst expression primarily used within Spark's query engine. It may be generated automatically by the optimizer or used in DataFrame operations that require exception-safe evaluation.
Arguments¶
| Argument | Type | Description |
|---|---|---|
| child | Expression | The child expression to evaluate within a try-catch block |
Return Type¶
Returns the same data type as the child expression, but the result is always nullable regardless of the child's nullability.
Supported Data Types¶
Supports all data types that the child expression supports, including: - All primitive types (Boolean, Byte, Short, Integer, Long, Float, Double) - Complex types (String, Binary, Array, Map, Struct) - Date/Time types (Date, Timestamp, CalendarInterval) - Decimal types
Algorithm¶
- Initialize result variables with null flag set to true and default value for the data type
- Execute the child expression within a try-catch block
- If evaluation succeeds: copy the child's null flag and result value
- If any exception occurs: leave result as null (isNull = true)
- Return the final result with appropriate null flag
Partitioning Behavior¶
- Preserves partitioning: Yes, this expression does not change data distribution
- Requires shuffle: No, evaluation is performed locally on each partition
- Acts as a pass-through for partitioning schemes since it only affects individual row values
Edge Cases¶
- Null handling: If child expression returns null normally, TryEval preserves that null
- Exception handling: Any exception (RuntimeException, ArithmeticException, etc.) results in null output
- Nested exceptions: Catches all Exception types, including those from deeply nested expression trees
- Nullability override: Always returns nullable results even if child expression is non-nullable
- Code generation: Exception handling is preserved in generated code with proper try-catch blocks
Code Generation¶
This expression supports full code generation (Tungsten). The generated code includes: - Proper Java try-catch blocks around child expression evaluation - Initialization of null flags and default values - Exception-safe variable assignments - No fallback to interpreted mode required
Examples¶
-- TryEval is typically generated internally, not directly accessible in SQL
-- However, it might be used internally for operations like:
SELECT CASE WHEN some_condition THEN risky_operation(col) END FROM table
// Internal Catalyst usage example
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.types._
// Wrap a potentially failing expression
val riskyExpr = Divide(Literal(10), col("denominator"))
val safeExpr = TryEval(riskyExpr)
// The safe expression will return null instead of throwing division by zero
See Also¶
- Coalesce: For handling null values with fallback options
- IfNull: For null replacement logic
- CaseWhen: For conditional expression evaluation
- UnaryExpression: The base class that TryEval extends
- CodegenFallback: Alternative approach for expressions that don't support code generation