Skip to content

TryReflect

Overview

TryReflect is a runtime replaceable expression that provides error-safe reflection-based method calling in Spark SQL. It wraps the CallMethodViaReflection expression with failOnError = false, allowing method invocation via reflection to return null instead of throwing exceptions when errors occur.

Syntax

try_reflect(class_name, method_name, arg1, arg2, ...)

Arguments

Argument Type Description
class_name String The fully qualified class name containing the method
method_name String The name of the method to invoke
args Variable Variable number of arguments to pass to the method

Return Type

Returns the same data type as the underlying method being called, or NULL if an error occurs during reflection or method execution.

Supported Data Types

Supports all data types that can be passed as arguments to Java/Scala methods:

  • Primitive types (Integer, Long, Double, Boolean, etc.)
  • String types
  • Complex types (Arrays, Maps, Structs)
  • Any types supported by Spark's type conversion system

Algorithm

  • Accepts a sequence of expressions as parameters (class name, method name, and arguments)
  • Internally creates a CallMethodViaReflection expression with failOnError = false
  • Uses Java reflection to locate the specified class and method
  • Attempts to invoke the method with the provided arguments
  • Returns null instead of throwing exceptions when reflection or execution fails

Partitioning Behavior

This expression preserves partitioning behavior:

  • Does not require data shuffling
  • Can be evaluated independently on each partition
  • Does not affect the partitioning scheme of the dataset

Edge Cases

  • Returns NULL when the specified class cannot be found
  • Returns NULL when the specified method does not exist
  • Returns NULL when method arguments don't match the expected signature
  • Returns NULL when the method execution throws any exception
  • Handles null input arguments according to the underlying method's null handling

Code Generation

This expression supports code generation through its underlying CallMethodViaReflection replacement. The generated code includes proper null checking and exception handling to ensure null is returned instead of propagating errors.

Examples

-- Call a static method safely
SELECT try_reflect('java.lang.Math', 'abs', -42) as result;

-- Call with invalid class name (returns NULL)
SELECT try_reflect('invalid.Class', 'method', 'arg') as result;

-- Call with wrong argument types (returns NULL)
SELECT try_reflect('java.lang.String', 'substring', 'hello', 'invalid') as result;
// Example DataFrame API usage
import org.apache.spark.sql.functions._

df.select(expr("try_reflect('java.lang.Math', 'max', col1, col2)").as("max_value"))

// Using with error-prone reflection calls
df.select(expr("try_reflect('com.example.Utils', 'process', input_col)").as("processed"))

See Also

  • reflect - The standard reflection function that throws exceptions on errors
  • CallMethodViaReflection - The underlying implementation class
  • RuntimeReplaceable - The base trait for expressions that are replaced during analysis