Mazen-Ghanaym commented on code in PR #3000:
URL: https://github.com/apache/datafusion-comet/pull/3000#discussion_r2658551835


##########
native/spark-expr/src/string_funcs/starts_ends_with.rs:
##########
@@ -0,0 +1,270 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::array::{Array, BooleanArray, Scalar, StringArray};
+use arrow::buffer::BooleanBuffer;
+use arrow::compute;
+use arrow::datatypes::DataType;
+use datafusion::common::{Result, ScalarValue};
+use datafusion::logical_expr::ColumnarValue;
+use datafusion::physical_expr::PhysicalExpr;
+use std::any::Any;
+use std::fmt::{Debug, Display, Formatter};
+use std::hash::{Hash, Hasher};
+use std::sync::Arc;
+
+#[derive(Debug)]
+pub struct StartsWithExpr {
+    pub child: Arc<dyn PhysicalExpr>,
+    pub pattern_array: Arc<StringArray>, // Pre-allocated pattern
+}
+
+impl StartsWithExpr {
+    pub fn new(child: Arc<dyn PhysicalExpr>, pattern: String) -> Self {
+        // Optimization: Allocate the pattern array ONCE during construction
+        // This avoids creating a new StringArray for every single batch
+        let pattern_array = Arc::new(StringArray::from(vec![pattern]));
+        Self {
+            child,
+            pattern_array,
+        }
+    }
+}
+
+impl Hash for StartsWithExpr {
+    fn hash<H: Hasher>(&self, state: &mut H) {
+        self.child.hash(state);
+        self.pattern_array.value(0).hash(state);
+    }
+}
+
+impl PartialEq for StartsWithExpr {
+    fn eq(&self, other: &Self) -> bool {
+        self.child.eq(&other.child) && self.pattern_array.value(0) == 
other.pattern_array.value(0)
+    }
+}
+
+impl Eq for StartsWithExpr {}
+
+impl Display for StartsWithExpr {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        write!(
+            f,
+            "startsWith({}, \"{}\")",
+            self.child,
+            self.pattern_array.value(0)
+        )
+    }
+}
+
+impl PhysicalExpr for StartsWithExpr {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn fmt_sql(&self, _: &mut Formatter<'_>) -> std::fmt::Result {
+        unimplemented!()
+    }
+
+    fn data_type(&self, _input_schema: &arrow::datatypes::Schema) -> 
Result<DataType> {
+        Ok(DataType::Boolean)
+    }
+
+    fn nullable(&self, input_schema: &arrow::datatypes::Schema) -> 
Result<bool> {
+        self.child.nullable(input_schema)
+    }
+
+    fn evaluate(&self, batch: &arrow::record_batch::RecordBatch) -> 
Result<ColumnarValue> {
+        let arg = self.child.evaluate(batch)?;
+
+        match arg {
+            ColumnarValue::Array(array) => {
+                // Zero-Allocation here: We reuse the pre-allocated 
pattern_array
+                let scalar = Scalar::new(self.pattern_array.as_ref());
+
+                // Use Arrow's highly optimized SIMD kernel
+                let result = compute::starts_with(&array, &scalar)?;
+
+                Ok(ColumnarValue::Array(Arc::new(result)))
+            }
+            ColumnarValue::Scalar(ScalarValue::Utf8(Some(str_val))) => {
+                // Fallback for scalar inputs (rare in big data, but necessary)
+                let pattern_scalar = self.pattern_array.value(0);
+                Ok(ColumnarValue::Scalar(ScalarValue::Boolean(Some(
+                    str_val.starts_with(pattern_scalar),
+                ))))
+            }
+            ColumnarValue::Scalar(ScalarValue::Utf8(None)) => {
+                Ok(ColumnarValue::Scalar(ScalarValue::Boolean(None)))
+            }
+            _ => Err(datafusion::error::DataFusionError::Internal(
+                "StartsWith requires StringArray input".to_string(),
+            )),
+        }
+    }
+
+    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
+        vec![&self.child]
+    }
+
+    fn with_new_children(
+        self: Arc<Self>,
+        children: Vec<Arc<dyn PhysicalExpr>>,
+    ) -> Result<Arc<dyn PhysicalExpr>> {
+        Ok(Arc::new(StartsWithExpr::new(
+            Arc::clone(&children[0]),
+            self.pattern_array.value(0).to_string(),
+        )))
+    }
+}
+
+// ----------------------------------------------------------------------------
+// ENDS WITH IMPLEMENTATION
+// ----------------------------------------------------------------------------
+
+#[derive(Debug)]
+pub struct EndsWithExpr {
+    pub child: Arc<dyn PhysicalExpr>,
+    pub pattern: String,    // Keep pattern as String for raw byte access
+    pub pattern_len: usize, // Pre-calculate length
+}
+
+impl EndsWithExpr {
+    pub fn new(child: Arc<dyn PhysicalExpr>, pattern: String) -> Self {
+        let pattern_len = pattern.len();
+        Self {
+            child,
+            pattern,
+            pattern_len,
+        }
+    }
+}
+
+impl Hash for EndsWithExpr {
+    fn hash<H: Hasher>(&self, state: &mut H) {
+        self.child.hash(state);
+        self.pattern.hash(state);
+    }
+}
+
+impl PartialEq for EndsWithExpr {
+    fn eq(&self, other: &Self) -> bool {
+        self.child.eq(&other.child) && self.pattern == other.pattern
+    }
+}
+
+impl Eq for EndsWithExpr {}
+
+impl Display for EndsWithExpr {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        write!(f, "endsWith({}, \"{}\")", self.child, self.pattern)
+    }
+}
+
+impl PhysicalExpr for EndsWithExpr {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn fmt_sql(&self, _: &mut Formatter<'_>) -> std::fmt::Result {
+        unimplemented!()
+    }
+
+    fn data_type(&self, _input_schema: &arrow::datatypes::Schema) -> 
Result<DataType> {
+        Ok(DataType::Boolean)
+    }
+
+    fn nullable(&self, input_schema: &arrow::datatypes::Schema) -> 
Result<bool> {
+        self.child.nullable(input_schema)
+    }
+
+    fn evaluate(&self, batch: &arrow::record_batch::RecordBatch) -> 
Result<ColumnarValue> {
+        let arg = self.child.evaluate(batch)?;
+
+        match arg {
+            ColumnarValue::Array(array) => {
+                let string_array = 
array.as_any().downcast_ref::<StringArray>().unwrap();
+                let len = string_array.len();
+
+                let offsets = string_array.value_offsets();
+                let values = string_array.value_data();
+                let pattern_bytes = self.pattern.as_bytes();
+                let p_len = self.pattern_len;

Review Comment:
   I actually tried that first! Used `arrow::compute::ends_with` with the same 
`Scalar` approach as `starts_with`, but it was still ~0.9X vs Spark at the 
time. The direct buffer access is what got it to 1.0X parity.
   
   I saw the DataFusion PR you linked  (apache/datafusion#19516), looks like it 
adds the same Scalar optimization. Once Comet picks up that version, happy to 
switch this to the standard kernel and remove the custom code!



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to