mbutrovich commented on code in PR #1954:
URL: https://github.com/apache/datafusion-comet/pull/1954#discussion_r2193077774


##########
native/spark-expr/src/bloom_filter/bloom_filter_might_contain.rs:
##########
@@ -0,0 +1,196 @@
+// 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, Int64Array, RecordBatch};
+use arrow::datatypes::{DataType, Schema};
+use datafusion::common::{internal_err, Result, ScalarValue};
+use datafusion::error::DataFusionError;
+use datafusion::logical_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature, 
Volatility};
+use datafusion::physical_expr::PhysicalExpr;
+use datafusion::physical_plan::ColumnarValue;
+use std::any::Any;
+use std::sync::Arc;
+
+use crate::bloom_filter::spark_bloom_filter::SparkBloomFilter;
+
+#[derive(Debug)]
+pub struct BloomFilterMightContain {
+    signature: Signature,
+    bloom_filter: Option<SparkBloomFilter>,
+}
+
+impl BloomFilterMightContain {
+    pub fn try_new(bloom_filter_expr: Arc<dyn PhysicalExpr>) -> Result<Self> {
+        // early evaluate the bloom_filter_expr to get the actual bloom filter
+        let bloom_filter = evaluate_bloom_filter(&bloom_filter_expr)?;
+        Ok(Self {
+            bloom_filter,
+            signature: Signature::exact(
+                vec![DataType::Binary, DataType::Int64],
+                Volatility::Immutable,
+            ),
+        })
+    }
+}
+
+fn evaluate_bloom_filter(
+    bloom_filter_expr: &Arc<dyn PhysicalExpr>,
+) -> Result<Option<SparkBloomFilter>> {
+    // bloom_filter_expr must be a literal/scalar subquery expression, so we 
can evaluate it
+    // with an empty batch with empty schema
+    let batch = RecordBatch::new_empty(Arc::new(Schema::empty()));
+    let bloom_filter_bytes = bloom_filter_expr.evaluate(&batch)?;
+    match bloom_filter_bytes {
+        ColumnarValue::Scalar(ScalarValue::Binary(v)) => {
+            Ok(v.map(|v| SparkBloomFilter::from(v.as_slice())))
+        }
+        _ => internal_err!("Bloom filter expression should be evaluated as a 
scalar binary value"),
+    }
+}
+
+impl ScalarUDFImpl for BloomFilterMightContain {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "might_contain"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+        Ok(DataType::Boolean)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        execute_might_contain(&self.bloom_filter, &args.args)
+    }
+}
+
+fn execute_might_contain(
+    bloom_filter: &Option<SparkBloomFilter>,
+    args: &[ColumnarValue],
+) -> Result<ColumnarValue> {
+    match &args[0] {
+        ColumnarValue::Scalar(ScalarValue::Int64(Some(item))) => {
+            let result = bloom_filter

Review Comment:
   Thank you for the fix and the relevant test case!



-- 
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: github-unsubscr...@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to