You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@auron.apache.org by "lyne7-sc (via GitHub)" <gi...@apache.org> on 2026/05/11 14:51:10 UTC

[PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

lyne7-sc opened a new pull request, #2262:
URL: https://github.com/apache/auron/pull/2262

   # Which issue does this PR close?
   
   Closes https://github.com/apache/auron/issues/1724
   
   # Rationale for this change
   
   Spark `substring` supports both string and binary inputs, while Auron previously mapped it to datafusion's `Substr`, which only handled string-compatible behavior and caused the Spark string/binary substring suite case to be excluded.
   
   # What changes are included in this PR?
   
     - Add native `Spark_Substring` ext function support for `Utf8` and `Binary` inputs.
     - Route Spark `Substring` conversion through `Spark_Substring` and preserve the input data type.
     - Add native unit coverage for string and binary substring.
     - Re-enable `string / binary substring function` test case.
   
   # Are there any user-facing changes?
   
   Yes. Spark SQL `substring` now supports binary input in native execution, matching Spark behavior.
   
   # How was this patch tested?
   
    - Added native unit tests for `spark_substring`.
    - Spark String Function Suite


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3227178755


##########
native-engine/datafusion-ext-functions/src/spark_strings.rs:
##########
@@ -395,6 +471,44 @@ mod test {
         }
     }
 
+    #[test]

Review Comment:
   Thanks, I’ve added the edge cases you mentioned.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on PR #2262:
URL: https://github.com/apache/auron/pull/2262#issuecomment-4431486695

   @ShreyeshArangath  Thanks for your review! I’ve updated the implementation accordingly.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "ShreyeshArangath (via GitHub)" <gi...@apache.org>.
ShreyeshArangath merged PR #2262:
URL: https://github.com/apache/auron/pull/2262


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on PR #2262:
URL: https://github.com/apache/auron/pull/2262#issuecomment-4586163289

   Thanks everyone for the reviews and feedback!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3227170883


##########
native-engine/datafusion-ext-functions/src/spark_strings.rs:
##########
@@ -114,6 +116,80 @@ pub fn string_split(args: &[ColumnarValue]) -> Result<ColumnarValue> {
     Ok(ColumnarValue::Array(Arc::new(splitted_builder.finish())))
 }
 
+pub fn spark_substring(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    let pos = match &args[1] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring pos only supports literal int64")?,
+    };
+    let len = match &args[2] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring len only supports literal int64")?,
+    };
+
+    match &args[0] {
+        ColumnarValue::Array(array) => match array.data_type() {
+            DataType::Utf8 => {
+                let result: StringArray = as_string_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let chars = value.chars().collect::<Vec<_>>();
+                            let (start, end) = substring_range(chars.len(), pos, len);
+                            chars[start..end].iter().collect::<String>()
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            DataType::Binary => {
+                let result: BinaryArray = as_binary_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let (start, end) = substring_range(value.len(), pos, len);
+                            &value[start..end]
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            other => df_execution_err!("substring only supports utf8 or binary, got {other:?}"),
+        },
+        ColumnarValue::Scalar(ScalarValue::Utf8(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Utf8(value.as_ref().map(|value| {
+                let chars = value.chars().collect::<Vec<_>>();
+                let (start, end) = substring_range(chars.len(), pos, len);
+                chars[start..end].iter().collect::<String>()
+            })),
+        )),
+        ColumnarValue::Scalar(ScalarValue::Binary(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Binary(value.as_ref().map(|value| {
+                let (start, end) = substring_range(value.len(), pos, len);
+                value[start..end].to_vec()
+            })),
+        )),
+        other => df_execution_err!("substring only supports utf8 or binary, got {:?}", other),
+    }
+}
+
+fn substring_range(total_len: usize, pos: i64, len: i64) -> (usize, usize) {
+    if len <= 0 {
+        return (0, 0);
+    }
+
+    let total_len_i64 = total_len as i64;
+    let start = if pos > 0 {
+        pos - 1
+    } else if pos < 0 {
+        total_len_i64 + pos

Review Comment:
   Same as above, switched to `satuating_add` here as well for safety.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3254848044


##########
spark-extension/src/main/scala/org/apache/spark/sql/auron/NativeConverters.scala:
##########
@@ -1025,10 +1025,10 @@ object NativeConverters extends Logging {
           if pos.asInstanceOf[Int] > 0 && len.asInstanceOf[Int] >= 0 =>
         val longPos = pos.asInstanceOf[Int].toLong

Review Comment:
   Good catch! That makes sense to me. I’ve relaxed the guard so these cases can go through the native path.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3227162821


##########
native-engine/datafusion-ext-functions/src/spark_strings.rs:
##########
@@ -114,6 +116,80 @@ pub fn string_split(args: &[ColumnarValue]) -> Result<ColumnarValue> {
     Ok(ColumnarValue::Array(Arc::new(splitted_builder.finish())))
 }
 
+pub fn spark_substring(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    let pos = match &args[1] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring pos only supports literal int64")?,
+    };
+    let len = match &args[2] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring len only supports literal int64")?,
+    };
+
+    match &args[0] {
+        ColumnarValue::Array(array) => match array.data_type() {
+            DataType::Utf8 => {
+                let result: StringArray = as_string_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let chars = value.chars().collect::<Vec<_>>();
+                            let (start, end) = substring_range(chars.len(), pos, len);
+                            chars[start..end].iter().collect::<String>()
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            DataType::Binary => {
+                let result: BinaryArray = as_binary_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let (start, end) = substring_range(value.len(), pos, len);
+                            &value[start..end]
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            other => df_execution_err!("substring only supports utf8 or binary, got {other:?}"),
+        },
+        ColumnarValue::Scalar(ScalarValue::Utf8(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Utf8(value.as_ref().map(|value| {
+                let chars = value.chars().collect::<Vec<_>>();
+                let (start, end) = substring_range(chars.len(), pos, len);
+                chars[start..end].iter().collect::<String>()
+            })),
+        )),
+        ColumnarValue::Scalar(ScalarValue::Binary(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Binary(value.as_ref().map(|value| {
+                let (start, end) = substring_range(value.len(), pos, len);
+                value[start..end].to_vec()
+            })),
+        )),
+        other => df_execution_err!("substring only supports utf8 or binary, got {:?}", other),
+    }
+}
+
+fn substring_range(total_len: usize, pos: i64, len: i64) -> (usize, usize) {
+    if len <= 0 {
+        return (0, 0);
+    }
+
+    let total_len_i64 = total_len as i64;
+    let start = if pos > 0 {
+        pos - 1
+    } else if pos < 0 {
+        total_len_i64 + pos
+    } else {
+        0
+    }
+    .clamp(0, total_len_i64) as usize;
+    let end = (start as i64 + len).clamp(0, total_len_i64) as usize;

Review Comment:
   In practice, `pos` and `len` come from Spark’s Int32 values cast to i64 (via `NativeConverters`), so overflow is likely unreachable here?
   
   Still, I switched to `saturating_add` for safety here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3227142976


##########
native-engine/datafusion-ext-functions/src/spark_strings.rs:
##########
@@ -114,6 +116,80 @@ pub fn string_split(args: &[ColumnarValue]) -> Result<ColumnarValue> {
     Ok(ColumnarValue::Array(Arc::new(splitted_builder.finish())))
 }
 
+pub fn spark_substring(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    let pos = match &args[1] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring pos only supports literal int64")?,
+    };
+    let len = match &args[2] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring len only supports literal int64")?,
+    };
+
+    match &args[0] {
+        ColumnarValue::Array(array) => match array.data_type() {
+            DataType::Utf8 => {
+                let result: StringArray = as_string_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let chars = value.chars().collect::<Vec<_>>();
+                            let (start, end) = substring_range(chars.len(), pos, len);
+                            chars[start..end].iter().collect::<String>()
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            DataType::Binary => {
+                let result: BinaryArray = as_binary_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let (start, end) = substring_range(value.len(), pos, len);
+                            &value[start..end]
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            other => df_execution_err!("substring only supports utf8 or binary, got {other:?}"),
+        },
+        ColumnarValue::Scalar(ScalarValue::Utf8(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Utf8(value.as_ref().map(|value| {
+                let chars = value.chars().collect::<Vec<_>>();
+                let (start, end) = substring_range(chars.len(), pos, len);
+                chars[start..end].iter().collect::<String>()
+            })),
+        )),
+        ColumnarValue::Scalar(ScalarValue::Binary(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Binary(value.as_ref().map(|value| {
+                let (start, end) = substring_range(value.len(), pos, len);
+                value[start..end].to_vec()
+            })),
+        )),
+        other => df_execution_err!("substring only supports utf8 or binary, got {:?}", other),
+    }
+}
+
+fn substring_range(total_len: usize, pos: i64, len: i64) -> (usize, usize) {
+    if len <= 0 {
+        return (0, 0);
+    }
+
+    let total_len_i64 = total_len as i64;
+    let start = if pos > 0 {
+        pos - 1
+    } else if pos < 0 {
+        total_len_i64 + pos
+    } else {
+        0
+    }
+    .clamp(0, total_len_i64) as usize;

Review Comment:
   Good point, updated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "yew1eb (via GitHub)" <gi...@apache.org>.
yew1eb commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3252438879


##########
spark-extension/src/main/scala/org/apache/spark/sql/auron/NativeConverters.scala:
##########
@@ -1025,10 +1025,10 @@ object NativeConverters extends Logging {
           if pos.asInstanceOf[Int] > 0 && len.asInstanceOf[Int] >= 0 =>
         val longPos = pos.asInstanceOf[Int].toLong

Review Comment:
   The guard `pos > 0 && len >= 0` was inherited from the old `Substr` mapping and is now too restrictive. `spark_substring` correctly handles `pos == 0` (treated as 1) and negative `pos` (counted from the end), as the Rust unit tests already cover. Cases with `pos <= 0` will still fall back to Spark instead of using the native path. Consider relaxing the guard to just `len >= 0`, or dropping it entirely since the implementation handles all integer values.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3251842100


##########
spark-extension/src/main/scala/org/apache/spark/sql/auron/NativeConverters.scala:
##########
@@ -1025,10 +1025,10 @@ object NativeConverters extends Logging {
           if pos.asInstanceOf[Int] > 0 && len.asInstanceOf[Int] >= 0 =>
         val longPos = pos.asInstanceOf[Int].toLong
         val longLen = len.asInstanceOf[Int].toLong
-        buildScalarFunction(
-          pb.ScalarFunction.Substr,
+        buildExtScalarFunction(
+          "Spark_Substring",
           str :: Literal(longPos) :: Literal(longLen) :: Nil,
-          StringType)
+          str.dataType)

Review Comment:
   since Substring now accepts both StringType and BinaryType inputs, the return type should match the input.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "ShreyeshArangath (via GitHub)" <gi...@apache.org>.
ShreyeshArangath commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3239026289


##########
spark-extension/src/main/scala/org/apache/spark/sql/auron/NativeConverters.scala:
##########
@@ -1025,10 +1025,10 @@ object NativeConverters extends Logging {
           if pos.asInstanceOf[Int] > 0 && len.asInstanceOf[Int] >= 0 =>
         val longPos = pos.asInstanceOf[Int].toLong
         val longLen = len.asInstanceOf[Int].toLong
-        buildScalarFunction(
-          pb.ScalarFunction.Substr,
+        buildExtScalarFunction(
+          "Spark_Substring",
           str :: Literal(longPos) :: Literal(longLen) :: Nil,
-          StringType)
+          str.dataType)

Review Comment:
   Why is this not StringType, similar to StringSpace and StringRepeat?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [AURON #1724] Support binary input for Spark `substring` function [auron]

Posted by "ShreyeshArangath (via GitHub)" <gi...@apache.org>.
ShreyeshArangath commented on code in PR #2262:
URL: https://github.com/apache/auron/pull/2262#discussion_r3220386795


##########
native-engine/datafusion-ext-functions/src/spark_strings.rs:
##########
@@ -395,6 +471,44 @@ mod test {
         }
     }
 
+    #[test]

Review Comment:
   let's add some more tests around the edge cases, examples: 
    - len == 0: expect empty string/bytes (not error).
   - pos > total_len: expect empty.
    - pos + len > total_len: expect clamp to end (e.g. substring("abc", 2, 100) == "bc").
   - Empty input string and empty binary input: expect empty
   



##########
native-engine/datafusion-ext-functions/src/spark_strings.rs:
##########
@@ -114,6 +116,80 @@ pub fn string_split(args: &[ColumnarValue]) -> Result<ColumnarValue> {
     Ok(ColumnarValue::Array(Arc::new(splitted_builder.finish())))
 }
 
+pub fn spark_substring(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    let pos = match &args[1] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring pos only supports literal int64")?,
+    };
+    let len = match &args[2] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring len only supports literal int64")?,
+    };
+
+    match &args[0] {
+        ColumnarValue::Array(array) => match array.data_type() {
+            DataType::Utf8 => {
+                let result: StringArray = as_string_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let chars = value.chars().collect::<Vec<_>>();
+                            let (start, end) = substring_range(chars.len(), pos, len);
+                            chars[start..end].iter().collect::<String>()
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            DataType::Binary => {
+                let result: BinaryArray = as_binary_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let (start, end) = substring_range(value.len(), pos, len);
+                            &value[start..end]
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            other => df_execution_err!("substring only supports utf8 or binary, got {other:?}"),
+        },
+        ColumnarValue::Scalar(ScalarValue::Utf8(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Utf8(value.as_ref().map(|value| {
+                let chars = value.chars().collect::<Vec<_>>();
+                let (start, end) = substring_range(chars.len(), pos, len);
+                chars[start..end].iter().collect::<String>()
+            })),
+        )),
+        ColumnarValue::Scalar(ScalarValue::Binary(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Binary(value.as_ref().map(|value| {
+                let (start, end) = substring_range(value.len(), pos, len);
+                value[start..end].to_vec()
+            })),
+        )),
+        other => df_execution_err!("substring only supports utf8 or binary, got {:?}", other),
+    }
+}
+
+fn substring_range(total_len: usize, pos: i64, len: i64) -> (usize, usize) {
+    if len <= 0 {
+        return (0, 0);
+    }
+
+    let total_len_i64 = total_len as i64;
+    let start = if pos > 0 {
+        pos - 1
+    } else if pos < 0 {
+        total_len_i64 + pos
+    } else {
+        0
+    }
+    .clamp(0, total_len_i64) as usize;
+    let end = (start as i64 + len).clamp(0, total_len_i64) as usize;

Review Comment:
   How are we planning to handle overflow for very large len? 



##########
native-engine/datafusion-ext-functions/src/spark_strings.rs:
##########
@@ -114,6 +116,80 @@ pub fn string_split(args: &[ColumnarValue]) -> Result<ColumnarValue> {
     Ok(ColumnarValue::Array(Arc::new(splitted_builder.finish())))
 }
 
+pub fn spark_substring(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    let pos = match &args[1] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring pos only supports literal int64")?,
+    };
+    let len = match &args[2] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring len only supports literal int64")?,
+    };
+
+    match &args[0] {
+        ColumnarValue::Array(array) => match array.data_type() {
+            DataType::Utf8 => {
+                let result: StringArray = as_string_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let chars = value.chars().collect::<Vec<_>>();
+                            let (start, end) = substring_range(chars.len(), pos, len);
+                            chars[start..end].iter().collect::<String>()
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            DataType::Binary => {
+                let result: BinaryArray = as_binary_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let (start, end) = substring_range(value.len(), pos, len);
+                            &value[start..end]
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            other => df_execution_err!("substring only supports utf8 or binary, got {other:?}"),
+        },
+        ColumnarValue::Scalar(ScalarValue::Utf8(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Utf8(value.as_ref().map(|value| {
+                let chars = value.chars().collect::<Vec<_>>();
+                let (start, end) = substring_range(chars.len(), pos, len);
+                chars[start..end].iter().collect::<String>()
+            })),
+        )),
+        ColumnarValue::Scalar(ScalarValue::Binary(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Binary(value.as_ref().map(|value| {
+                let (start, end) = substring_range(value.len(), pos, len);
+                value[start..end].to_vec()
+            })),
+        )),
+        other => df_execution_err!("substring only supports utf8 or binary, got {:?}", other),
+    }
+}
+
+fn substring_range(total_len: usize, pos: i64, len: i64) -> (usize, usize) {
+    if len <= 0 {
+        return (0, 0);
+    }
+
+    let total_len_i64 = total_len as i64;
+    let start = if pos > 0 {
+        pos - 1
+    } else if pos < 0 {
+        total_len_i64 + pos

Review Comment:
   what if pos == i64.MIN? will it cause any issues?



##########
native-engine/datafusion-ext-functions/src/spark_strings.rs:
##########
@@ -114,6 +116,80 @@ pub fn string_split(args: &[ColumnarValue]) -> Result<ColumnarValue> {
     Ok(ColumnarValue::Array(Arc::new(splitted_builder.finish())))
 }
 
+pub fn spark_substring(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    let pos = match &args[1] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring pos only supports literal int64")?,
+    };
+    let len = match &args[2] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => *value,
+        _ => df_execution_err!("substring len only supports literal int64")?,
+    };
+
+    match &args[0] {
+        ColumnarValue::Array(array) => match array.data_type() {
+            DataType::Utf8 => {
+                let result: StringArray = as_string_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let chars = value.chars().collect::<Vec<_>>();
+                            let (start, end) = substring_range(chars.len(), pos, len);
+                            chars[start..end].iter().collect::<String>()
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            DataType::Binary => {
+                let result: BinaryArray = as_binary_array(array)?
+                    .iter()
+                    .map(|value| {
+                        value.map(|value| {
+                            let (start, end) = substring_range(value.len(), pos, len);
+                            &value[start..end]
+                        })
+                    })
+                    .collect();
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            other => df_execution_err!("substring only supports utf8 or binary, got {other:?}"),
+        },
+        ColumnarValue::Scalar(ScalarValue::Utf8(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Utf8(value.as_ref().map(|value| {
+                let chars = value.chars().collect::<Vec<_>>();
+                let (start, end) = substring_range(chars.len(), pos, len);
+                chars[start..end].iter().collect::<String>()
+            })),
+        )),
+        ColumnarValue::Scalar(ScalarValue::Binary(value)) => Ok(ColumnarValue::Scalar(
+            ScalarValue::Binary(value.as_ref().map(|value| {
+                let (start, end) = substring_range(value.len(), pos, len);
+                value[start..end].to_vec()
+            })),
+        )),
+        other => df_execution_err!("substring only supports utf8 or binary, got {:?}", other),
+    }
+}
+
+fn substring_range(total_len: usize, pos: i64, len: i64) -> (usize, usize) {
+    if len <= 0 {
+        return (0, 0);
+    }
+
+    let total_len_i64 = total_len as i64;
+    let start = if pos > 0 {
+        pos - 1
+    } else if pos < 0 {
+        total_len_i64 + pos
+    } else {
+        0
+    }
+    .clamp(0, total_len_i64) as usize;

Review Comment:
   nit: we can split this up for better readability, right now it looked like it was only getting applied to the else block
   
   ```
    let raw_start = if pos > 0 { pos - 1 }
                        else if pos < 0 { total_len_i64 + pos }
                        else { 0 };
   let start = raw_start.clamp(0, total_len_i64) as usize;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@auron.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org