advancedxy commented on code in PR #179: URL: https://github.com/apache/arrow-datafusion-comet/pull/179#discussion_r1522329641
########## core/src/execution/datafusion/util/spark_bloom_filter.rs: ########## @@ -0,0 +1,112 @@ +// 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 crate::{ + errors::CometResult, + execution::datafusion::{ + spark_hash::spark_compatible_murmur3_hash, util::spark_bit_array::SparkBitArray, + }, +}; +use arrow_array::{ArrowNativeTypeOp, BooleanArray, Int64Array}; + +const SPARK_BLOOM_FILTER_VERSION_1: i32 = 1; + +/// A Bloom filter implementation that simulates the behavior of Spark's BloomFilter. +/// It's not a complete implementation of Spark's BloomFilter, but just add the minimum +/// methods to support mightContainsLong in the native side. +#[derive(Debug, Hash)] +pub struct SparkBloomFilter { + bits: SparkBitArray, + num_hashes: u32, +} + +/// similar to the `read_num_be_bytes` macro in `bit.rs` but read nums from bytes in big-endian Review Comment: OK. Will move it and update the wrong wording in the comment doc. Thought it was not general enough to put in the `bit.rs`. ########## spark/src/test/spark-3.3-plus/org/apache/comet/CometExpressionPlusSuite.scala: ########## @@ -0,0 +1,102 @@ +/* + * 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. + */ + +package org.apache.comet + +import org.apache.spark.sql.{Column, CometTestBase, DataFrame, Row} +import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper +import org.apache.spark.sql.catalyst.FunctionIdentifier +import org.apache.spark.sql.catalyst.expressions.{BloomFilterMightContain, Expression, ExpressionInfo} +import org.apache.spark.sql.functions.{col, lit} +import org.apache.spark.util.sketch.BloomFilter + +import java.io.ByteArrayOutputStream +import scala.util.Random + +class CometExpressionPlusSuite extends CometTestBase with AdaptiveSparkPlanHelper { + import testImplicits._ + + val func_might_contain = new FunctionIdentifier("might_contain") + + override def beforeAll(): Unit = { + super.beforeAll() + // Register 'might_contain' to builtin. + spark.sessionState.functionRegistry.registerFunction(func_might_contain, + new ExpressionInfo(classOf[BloomFilterMightContain].getName, "might_contain"), + (children: Seq[Expression]) => BloomFilterMightContain(children.head, children(1))) + } + + override def afterAll(): Unit = { + spark.sessionState.functionRegistry.dropFunction(func_might_contain) + super.afterAll() + } + + test("test BloomFilterMightContain can take a constant value input") { + val table = "test" + withTable(table) { + sql(s"create table $table(col1 long, col2 int) using parquet") + sql(s"insert into $table values (201, 1)") + checkSparkAnswerAndOperator( + s""" + |SELECT might_contain( + |X'00000001000000050000000343A2EC6EA8C117E2D3CDB767296B144FC5BFBCED9737F267', col1) FROM $table + |""".stripMargin) + } + } + + test("test NULl inputs for BloomFilterMightContain") { Review Comment: Sure ########## spark/src/test/scala/org/apache/comet/CometCastSuite.scala: ########## @@ -90,13 +90,13 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper { Range(0, len).map(_ => chars.charAt(r.nextInt(chars.length))).mkString } - private def fuzzCastFromString(chars: String, maxLen: Int, toType: DataType) { + private def fuzzCastFromString(chars: String, maxLen: Int, toType: DataType): Unit = { Review Comment: This is not related, might be automatically formatted. Let me revert this. ########## spark/src/test/spark-3.3-plus/org/apache/comet/CometExpressionPlusSuite.scala: ########## @@ -0,0 +1,102 @@ +/* + * 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. + */ + +package org.apache.comet + +import org.apache.spark.sql.{Column, CometTestBase, DataFrame, Row} +import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper +import org.apache.spark.sql.catalyst.FunctionIdentifier +import org.apache.spark.sql.catalyst.expressions.{BloomFilterMightContain, Expression, ExpressionInfo} +import org.apache.spark.sql.functions.{col, lit} +import org.apache.spark.util.sketch.BloomFilter + +import java.io.ByteArrayOutputStream +import scala.util.Random + +class CometExpressionPlusSuite extends CometTestBase with AdaptiveSparkPlanHelper { + import testImplicits._ + + val func_might_contain = new FunctionIdentifier("might_contain") + + override def beforeAll(): Unit = { + super.beforeAll() + // Register 'might_contain' to builtin. + spark.sessionState.functionRegistry.registerFunction(func_might_contain, + new ExpressionInfo(classOf[BloomFilterMightContain].getName, "might_contain"), + (children: Seq[Expression]) => BloomFilterMightContain(children.head, children(1))) + } + + override def afterAll(): Unit = { + spark.sessionState.functionRegistry.dropFunction(func_might_contain) + super.afterAll() + } + + test("test BloomFilterMightContain can take a constant value input") { + val table = "test" + withTable(table) { + sql(s"create table $table(col1 long, col2 int) using parquet") + sql(s"insert into $table values (201, 1)") + checkSparkAnswerAndOperator( + s""" + |SELECT might_contain( + |X'00000001000000050000000343A2EC6EA8C117E2D3CDB767296B144FC5BFBCED9737F267', col1) FROM $table + |""".stripMargin) + } + } + + test("test NULl inputs for BloomFilterMightContain") { + val table = "test" + withTable(table) { + sql(s"create table $table(col1 long, col2 int) using parquet") + sql(s"insert into $table values (201, 1), (null, 2)") + checkSparkAnswerAndOperator( + s""" + |SELECT might_contain(null, null) both_null, + | might_contain(null, 1L) null_bf, + | might_contain( + | X'00000001000000050000000343A2EC6EA8C117E2D3CDB767296B144FC5BFBCED9737F267', col1) null_value + | FROM $table + |""".stripMargin) + } + } + + test("test BloomFilterMightContain from random input") { + val bf = BloomFilter.create(1000, 100) + val longs = (0 until 100).map(_ => Random.nextLong()) Review Comment: Of course ########## core/src/execution/datafusion/util/spark_bit_array.rs: ########## @@ -0,0 +1,130 @@ +// 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. + +#[derive(Debug, Hash)] +pub struct SparkBitArray { Review Comment: > Some minimal rustdoc would be useful. Will do. > Also, this is pretty general purpose, so perhaps we needn't have Spark in the name (I'm fine either way)? To be frankly, this `BitArray`'s impl strictly keeps the same semantics with Spark's, such as: choose a `Vec<u64>` instead of simply `Vec<u8>`. I would like to keep the Spark prefix. -- 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]
