andygrove commented on code in PR #3000:
URL: https://github.com/apache/datafusion-comet/pull/3000#discussion_r2658398772


##########
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)?;

Review Comment:
   👍 



-- 
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