Spark Native Operator Optimization
What Is the Native Engine?
The Spark Native engine is a core component of Apache Spark designed to improve SparkSQL compute performance. By using the vectorized C++ acceleration library (Velox), this engine accelerates Spark operators. Enabling the Spark Native engine improves SparkSQL job performance, reducing CPU and memory usage.
How It Works
The Native engine creates a connection between Spark's execution layer and native code. The overall workflow is as follows:
- Query planning: Spark's Catalyst optimizer still generates the query plan, ensuring the integrity of the existing optimization logic.
- Plan conversion: After the execution plan is generated, the Native engine checks whether it can run natively. If it can, the engine converts the Spark plan into a Substrait plan, which is a cross-language representation of the query plan.
- Native execution: The converted subplan is sent to the C++ runtime for execution via the Java Native Interface (JNI). This runtime processes data using columnar batch processing and vectorized computation (such as SIMD instructions).
- Hybrid mode and fallback: If the query contains operators or expressions that the engine does not support, the system will automatically fall back to the standard Spark JVM for execution, ensuring that the query completes successfully.
This architecture improves performance in several areas:
- Elimination of JVM overhead: This avoids garbage collection (GC) pauses and object memory overhead.
- Modern CPU features: The columnar data layout and batch processing significantly improve CPU cache hit rates and memory bandwidth utilization, while supporting SIMD instruction acceleration.
- Efficient memory management: Native engines typically use more precise and predictable memory management policies, reducing memory fluctuations.
- Transparent acceleration: Existing SparkSQL code can run faster without any modifications.
Notes and Constraints
- Transparent fallback for unsupported functions: If an unsupported operator or expression is encountered, the query falls back to standard Spark execution, ensuring correct results but without acceleration.
- User-defined functions (UDFs): Expressions using UDFs, UDAFs, or UDTFs cannot be accelerated and will fall back to standard execution.
- RDD/DataSet API: Currently, acceleration is mainly available for SparkSQL and DataFrame APIs. Jobs that directly use RDD or DataSet APIs cannot benefit from this feature.
- Stream processing: Support for Structured Streaming is evolving. Currently, support for complex streaming aggregations (stateful streams) is limited.
- Short queries: For queries that run in a very short time (for example, within seconds), the overhead of task scheduling and plan generation may dominate, resulting in minimal performance improvement.
Enabling Spark Native Operator Optimization
- Spark Native acceleration is enabled by default.
- You can use EXPLAIN to view the execution plan. Operators with the ^ prefix indicate that native acceleration is enabled. For unsupported operators, standard Spark operators are used.
Disabling Spark Native Operator Optimization
Spark Native can be disabled at the job level. If compatibility issues, class conflicts, or executor startup errors occur, you can set spark.gluten.enabled=false in the SQL job settings to disable Spark Native at the job level.
Support for ANSI Syntax
Apache Spark 4.0 significantly improves SQL standard compliance by enabling ANSI SQL mode by default. This change aligns Spark's behavior more closely with mainstream SQL standards, improving query portability across platforms and the accuracy of data processing. Currently, the Spark Native engine supports ANSI mode by default. In the following scenarios, the Spark Native engine processes data as follows:
- Arithmetic operations: Spark Native throws a runtime exception for invalid operations.
- Data type conversions: Spark Native follows strict conversion rules and throws a runtime exception for invalid conversions.
- Data insertion into a table: Spark Native strictly checks data types and throws a runtime exception for numeric overflow.
- Implicit conversions: Spark Native blocks unsupported implicit conversions.
- Fallback to Spark: For unsupported functions, Spark Native falls back to standard Spark execution.
After ANSI is enabled, the following behaviors change compared to when ANSI is disabled:
- Cast type conversion
In ANSI mode (spark.sql.ansi.enabled set to true), explicit casting using the CAST syntax throws a runtime exception for illegal cast patterns defined in the standard (for example, casts from a string to an integer).
Spark Native (Velox) currently supports the majority of type conversion combinations natively. For unsupported conversion combinations, the system falls back to standard Spark for execution. The current fallback conversion combinations in Spark Native will be supported in future versions. The following table lists the conversion combinations that Spark Native currently falls back to standard Spark to process:
Table 1 Spark Native fallback conversion combinations SRC/TGT
TinyInt
SmallInt
Int
BigInt
Float
Double
Decimal
String
Date
Timestamp
TimestampNTZ
Interval
Boolean
Binary
Array
Map
Struct
TinyInt
OK
OK
OK
OK
OK
OK
OK
OK
N
OK
N
FB
OK
N
N
N
N
SmallInt
OK
OK
OK
OK
OK
OK
OK
OK
N
OK
N
FB
OK
N
N
N
N
Int
OK
OK
OK
OK
OK
OK
OK
OK
N
OK
N
FB
OK
N
N
N
N
BigInt
OK
OK
OK
OK
OK
OK
OK
OK
N
OK
N
FB
OK
N
N
N
N
Float
OK
OK
OK
OK
OK
OK
OK
OK
N
OK
N
N
OK
N
N
N
N
Double
OK
OK
OK
OK
OK
OK
OK
OK
N
OK
N
N
OK
N
N
N
N
Decimal
OK
OK
OK
OK
OK
OK
OK
OK
N
FB
N
FB
OK
N
N
N
N
String
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
FB
FB
OK
OK
N
N
N
Date
N
N
N
N
N
N
N
OK
OK
OK
FB
N
N
N
N
N
N
Timestamp
OK
OK
OK
OK
OK
OK
FB
OK
OK
OK
FB
N
N
N
N
N
N
TimestampNTZ
N
N
N
N
N
N
N
FB
FB
FB
FB
N
N
N
N
N
N
Interval
FB
FB
FB
FB
N
N
FB
FB
N
N
N
FB
N
N
N
N
N
Boolean
OK
OK
OK
OK
OK
OK
OK
OK
N
N
N
N
OK
N
N
N
N
Binary
N
N
N
N
N
N
N
OK
N
N
N
N
N
OK
N
N
N
Array
N
N
N
N
N
N
N
OK
N
N
N
N
N
N
OK
N
N
Map
N
N
N
N
N
N
N
FB
N
N
N
N
N
N
N
OK
N
Struct
N
N
N
N
N
N
N
FB
N
N
N
N
N
N
N
N
OK
There are three different states during data type conversion:
- OK: Indicates that the conversion is natively supported in Velox.
- N: Indicates that the conversion is invalid or unsupported in Spark.
- FB: Indicates that the conversion falls back to standard Spark for processing when it cannot be executed in Velox.
The TimestampNTZ data type is not supported in Velox. Therefore, when conversion of this data type is involved, processing falls back to standard Spark.
Table 2 Data type conversion matrix Source\Target
Numeric
String
Date
Timestamp
Timestamp_NTZ
Interval
Boolean
Binary
Array
Map
Struct
Numeric
Y
Y
N
Y
N
Y
Y
N
N
N
N
String
Y
Y
Y
Y
Y
Y
Y
Y
N
N
N
Date
N
Y
Y
Y
Y
N
N
N
N
N
N
Timestamp
Y
Y
Y
Y
Y
N
N
N
N
N
N
Timestamp_NTZ
N
Y
Y
Y
Y
N
N
N
N
N
N
Interval
Y
Y
N
N
N
Y
N
N
N
N
N
Boolean
Y
Y
N
N
N
N
Y
N
N
N
N
Binary
N
Y
N
N
N
N
N
Y
N
N
N
Array
N
Y
N
N
N
N
N
N
Y
N
N
Map
N
Y
N
N
N
N
N
N
N
Y
N
Struct
N
Y
N
N
N
N
N
N
N
N
Y
The following table lists the valid combinations of source and target data types in the Spark CAST expressions. Y indicates that the combination is syntactically valid without restriction, and N indicates that the combination is not valid.
In the preceding table, all CAST operations that use the new syntax are marked with a red Y.
- CAST(Numeric AS Numeric): If the value is out of the target data type's range, an overflow exception is thrown.
- CAST(String AS Numeric/Date/Timestamp/Timestamp_NTZ/Interval/Boolean): If the value cannot be parsed as the target data type, a runtime exception is thrown.
- CAST(Timestamp AS Numeric): If the number of seconds since epoch is out of the target data type's range, an overflow exception is thrown.
- CAST(Numeric AS Timestamp): If the result of multiplying the numeric value by 1,000,000 (microseconds per second) is out of the range of the Long type, an overflow exception is thrown.
- CAST(Array AS Array): If the conversion of any element fails, an exception is thrown.
- CAST(Map AS Map): If the conversion of any key or value fails, an exception is thrown.
- CAST(Struct AS Struct): If the conversion of any struct field fails, an exception is thrown.
- CAST(Numeric AS String): Always uses plain string representation on casting decimal values to strings, instead of using scientific notation if an exponent is needed.
- CAST(Interval AS Numeric): If the number of microseconds in a day-time interval or the number of months in a year-month interval is out of the target data type's range, an overflow exception is thrown.
- CAST(Numeric AS Interval): If the numeric value multiplied by the end unit of the target interval is out of the range of the Int type for year-month intervals or the range of the Long type for day-time intervals, an overflow exception is thrown.
- Data insertion
When spark.sql.storeAssignmentPolicy is set to ANSI (which is the default value), SparkSQL complies with the ANSI store assignment rules on table insertions. The following table lists the valid combinations of source and target data types in table insertions.

The store assignment rules are processed by standard Spark, so no modification is required in Spark Native.
- * indicates that Spark does not support the Interval type as a column type in a table.
- ** indicates that for the Array, Map, and Struct types, the data type check rules apply recursively to their component elements.
- Functions with different behaviors
The behavior of some SQL functions can be different in ANSI mode (spark.sql.ansi.enabled=true).
Function
Description
Spark Native (Velox) Status
size
This function returns null for null input.
Supported
element_at
This function throws ArrayIndexOutOfBoundsException if you use an invalid index.
Supported
elt
This function throws ArrayIndexOutOfBoundsException if you use an invalid index.
Fallback to Spark
parse_url
This function throws an IllegalArgumentException error if the input string is not a valid URL.
Fallback to Spark
to_date
This function should fail with an exception if the input string cannot be parsed or if the pattern string is invalid.
Supported
to_timestamp
This function should fail with an exception if the input string cannot be parsed or if the pattern string is invalid.
Supported
unix_timestamp
This function should fail with an exception if the input string cannot be parsed or if the pattern string is invalid.
Supported
to_unix_timestamp
This function should fail with an exception if the input string cannot be parsed or if the pattern string is invalid.
Supported
make_date
This function should fail with an exception if the result date is invalid.
Supported
make_timestamp
This function should fail with an exception if the result timestamp is invalid.
Supported
make_interval
This function should fail with an exception if the result interval is invalid.
Fallback to Spark
next_day
This function throws an IllegalArgumentException error if the input is not a valid day of the week.
Supported
- SQL operators
The behavior of some SQL operators can be different under ANSI mode (spark.sql.ansi.enabled=true).
Operator
Description
Spark Native (Velox) Status
array_col[index]
This operator throws ArrayIndexOutOfBoundsException if you use an invalid index.
Fallback to Spark
- Useful functions for ANSI mode
When ANSI mode is enabled, it throws exceptions for invalid operations. You can use the following SQL functions to suppress these exceptions.
Function
Description
Spark Native (Velox) Status
try_cast
Identical to CAST, except that it returns a NULL result instead of throwing an exception if a runtime error occurs.
Supported
try_add
Identical to the addition operator (+), except that it returns a NULL result instead of throwing an exception if an integer value overflow occurs.
Supported
try_subtract
Identical to the subtraction operator (-), except that it returns a NULL result instead of throwing an exception if an integer value overflow occurs.
Supported
try_multiply
Identical to the multiplication operator (*), except that it returns a NULL result instead of throwing an exception if an integer value overflow occurs.
Supported
try_divide
Identical to the division operator (/), except that it returns a NULL result instead of throwing an exception if divided by zero.
Supported
try_mod
Identical to the remainder operator (%), except that it returns a NULL result instead of throwing an exception if divided by zero.
Supported
try_sum
Identical to the sum function, except that it returns a NULL result instead of throwing an exception if an integer, decimal, or interval value overflow occurs.
Fallback to Spark
try_avg
Identical to the avg function, except that it returns a NULL result instead of throwing an exception if a decimal or interval value overflow occurs.
Fallback to Spark
try_element_at
Identical to the element_at function, except that it returns a NULL result instead of throwing an exception if an array index is out of bounds.
Supported
try_to_timestamp
Identical to the to_timestamp function, except that it returns a NULL result instead of throwing an exception if a string parsing error occurs.
Supported
try_parse_url
Identical to the parse_url function, except that it returns a NULL result instead of throwing an exception if a URL parsing error occurs.
Fallback to Spark
try_make_timestamp
Identical to the make_timestamp function, except that it returns a NULL result instead of throwing an exception if an error occurs.
Supported
try_make_timestamp_ltz
Identical to the make_timestamp_ltz function, except that it returns a NULL result instead of throwing an exception if an error occurs.
Supported
try_make_timestamp_ntz
Identical to the make_timestamp_ntz function, except that it returns a NULL result instead of throwing an exception if an error occurs.
Fallback to Spark
try_make_interval
Identical to the make_interval function, except that it returns a NULL result instead of throwing an exception if an invalid interval is provided.
Fallback to Spark
Support for Remote Shuffle Service
For shuffle-intensive jobs, the Native engine can connect to a Remote Shuffle Service (RSS) to offload shuffle data from local disks to an external cluster, which improves stability and performance. Currently, Spark Native integrates Fabric Store as its remote shuffle service solution.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot