Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add might_contain SparkSql function #4029

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion velox/common/base/BloomFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BloomFilter {
void merge(const char* serialized) {
common::InputByteStream stream(serialized);
auto version = stream.read<int8_t>();
VELOX_CHECK_EQ(kBloomFilterV1, version);
VELOX_USER_CHECK_EQ(kBloomFilterV1, version);
auto size = stream.read<int32_t>();
bits_.resize(size);
auto bitsdata =
Expand Down
7 changes: 7 additions & 0 deletions velox/docs/functions/spark/binary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ Binary Functions

Computes the md5 of x.

.. spark:function:: might_contain(bloomFilter, value) -> boolean

Returns TRUE if ``bloomFilter`` might contain ``value``.

``bloomFilter`` is a VARBINARY computed using ::spark::function::`bloom_filter_agg` aggregate function.
``value`` is a BIGINT.

.. spark:function:: sha1(x) -> varchar

Computes SHA-1 digest of x and convert the result to a hex string.
Expand Down
1 change: 1 addition & 0 deletions velox/functions/sparksql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_library(
In.cpp
LeastGreatest.cpp
Map.cpp
MightContain.cpp
RegexFunctions.cpp
Register.cpp
RegisterArithmetic.cpp
Expand Down
70 changes: 70 additions & 0 deletions velox/functions/sparksql/MightContain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed 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.
*/
#include "velox/functions/sparksql/MightContain.h"

#include "velox/common/base/BloomFilter.h"
#include "velox/common/memory/HashStringAllocator.h"
#include "velox/expression/DecodedArgs.h"
#include "velox/vector/FlatVector.h"

namespace facebook::velox::functions::sparksql {
namespace {
class BloomFilterMightContainFunction final : public exec::VectorFunction {
void apply(
const SelectivityVector& rows,
std::vector<VectorPtr>& args, // Not using const ref so we can reuse args
const TypePtr& outputType,
exec::EvalCtx& context,
VectorPtr& resultRef) const final {
VELOX_CHECK_EQ(args.size(), 2);
context.ensureWritable(rows, BOOLEAN(), resultRef);
auto& result = *resultRef->as<FlatVector<bool>>();
exec::DecodedArgs decodedArgs(rows, args, context);
auto serialized = decodedArgs.at(0);
auto value = decodedArgs.at(1);

HashStringAllocator allocator{context.pool()};
VELOX_USER_CHECK(serialized->isConstantMapping())
BloomFilter output{StlAllocator<uint64_t>(&allocator)};
try {
output.merge(serialized->valueAt<StringView>(0).str().c_str());
} catch (const std::exception& e) {
context.setErrors(rows, std::current_exception());
return;
}

rows.applyToSelected([&](int row) {
auto contain = output.mayContain(
folly::hasher<int64_t>()(value->valueAt<int64_t>(row)));
mbasmanova marked this conversation as resolved.
Show resolved Hide resolved
result.set(row, contain);
});
}
};
} // namespace

std::vector<std::shared_ptr<exec::FunctionSignature>> mightContainSignatures() {
return {exec::FunctionSignatureBuilder()
.returnType("boolean")
.constantArgumentType("varbinary")
.argumentType("bigint")
.build()};
}

std::unique_ptr<exec::VectorFunction> makeMightContain() {
return std::make_unique<BloomFilterMightContainFunction>();
}

} // namespace facebook::velox::functions::sparksql
24 changes: 24 additions & 0 deletions velox/functions/sparksql/MightContain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed 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.
*/
#include "velox/expression/VectorFunction.h"

namespace facebook::velox::functions::sparksql {

std::vector<std::shared_ptr<exec::FunctionSignature>> mightContainSignatures();

std::unique_ptr<exec::VectorFunction> makeMightContain();

} // namespace facebook::velox::functions::sparksql
5 changes: 5 additions & 0 deletions velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "velox/functions/sparksql/Hash.h"
#include "velox/functions/sparksql/In.h"
#include "velox/functions/sparksql/LeastGreatest.h"
#include "velox/functions/sparksql/MightContain.h"
#include "velox/functions/sparksql/RegexFunctions.h"
#include "velox/functions/sparksql/RegisterArithmetic.h"
#include "velox/functions/sparksql/RegisterCompare.h"
Expand Down Expand Up @@ -165,6 +166,10 @@ void registerFunctions(const std::string& prefix) {
int64_t,
Varchar,
Varchar>({prefix + "unix_timestamp", prefix + "to_unix_timestamp"});

// Register bloom filter function
exec::registerVectorFunction(
prefix + "might_contain", mightContainSignatures(), makeMightContain());
}

} // namespace sparksql
Expand Down
1 change: 1 addition & 0 deletions velox/functions/sparksql/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_executable(
InTest.cpp
LeastGreatestTest.cpp
MapTest.cpp
MightContainTest.cpp
RegexFunctionsTest.cpp
SizeTest.cpp
SortArrayTest.cpp
Expand Down
78 changes: 78 additions & 0 deletions velox/functions/sparksql/tests/MightContainTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed 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.
*/

#include "velox/functions/sparksql/MightContain.h"
#include "velox/common/base/BloomFilter.h"
#include "velox/common/memory/HashStringAllocator.h"
#include "velox/functions/sparksql/tests/SparkFunctionBaseTest.h"

namespace facebook::velox::functions::sparksql::test {
namespace {

class MightContainTest : public SparkFunctionBaseTest {
protected:
void testMightContain(
const VectorPtr& bloom,
const VectorPtr& value,
const VectorPtr& expected) {
auto result = evaluate(
"might_contain(cast(c0 as varbinary), c1)",
makeRowVector({bloom, value}));
velox::test::assertEqualVectors(expected, result);
}

std::string getSerializedBloomFilter() {
constexpr int64_t kSize = 10;
BloomFilter bloomFilter;
bloomFilter.reset(kSize);
for (auto i = 0; i < kSize; ++i) {
bloomFilter.insert(folly::hasher<int64_t>()(i));
}
std::string data;
data.resize(bloomFilter.serializedSize());
bloomFilter.serialize(data.data());
return data;
}
};

TEST_F(MightContainTest, basic) {
auto serialized = getSerializedBloomFilter();
auto bloomFilter = makeConstant<StringView>(StringView(serialized), 10);
auto value =
makeFlatVector<int64_t>(10, [](vector_size_t row) { return row; });
auto expectedContain = makeConstant(true, 10);
testMightContain(bloomFilter, value, expectedContain);

auto valueNotContain = makeFlatVector<int64_t>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you add a test case where some rows return true and others false?

10, [](vector_size_t row) { return row + 123451; });
auto expectedNotContain = makeConstant(false, 10);
testMightContain(bloomFilter, valueNotContain, expectedNotContain);

auto values = makeNullableFlatVector<int64_t>(
{1, 2, 3, 4, 5, std::nullopt, 123451, 23456, 4, 5});
auto expected = makeNullableFlatVector<bool>(
{true, true, true, true, true, std::nullopt, false, false, true, true});
testMightContain(bloomFilter, values, expected);
}

TEST_F(MightContainTest, nullBloomFilter) {
auto bloomFilter = makeConstant<StringView>(std::nullopt, 2);
auto value = makeFlatVector<int64_t>({2, 4});
auto expected = makeNullConstant(TypeKind::BOOLEAN, 2);
testMightContain(bloomFilter, value, expected);
}
} // namespace
} // namespace facebook::velox::functions::sparksql::test