-
Notifications
You must be signed in to change notification settings - Fork 582
[GLUTEN-11708][VL] Translate might_contain as a subfield filter for scan-level bloom filter pushdown #11711
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
Open
acvictor
wants to merge
3
commits into
apache:main
Choose a base branch
from
acvictor:acvictor/mightContain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−0
Open
[GLUTEN-11708][VL] Translate might_contain as a subfield filter for scan-level bloom filter pushdown #11711
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "operators/functions/SparkExprToSubfieldFilterParser.h" | ||
| #include "velox/common/base/BloomFilter.h" | ||
| #include "velox/core/QueryCtx.h" | ||
| #include "velox/expression/Expr.h" | ||
| #include "velox/vector/tests/utils/VectorTestBase.h" | ||
|
|
||
| using namespace facebook::velox; | ||
| using namespace facebook::velox::common; | ||
|
|
||
| namespace gluten { | ||
| namespace { | ||
|
|
||
| class SparkExprToSubfieldFilterParserTest : public ::testing::Test, | ||
| public test::VectorTestBase { | ||
| protected: | ||
| static void SetUpTestCase() { | ||
| memory::MemoryManager::testingSetInstance(memory::MemoryManager::Options{}); | ||
| } | ||
|
|
||
| /// Builds a serialized Velox BloomFilter containing the given int64 values. | ||
| std::vector<char> makeSerializedBloomFilter( | ||
| const std::vector<int64_t>& values) { | ||
| BloomFilter<> bf; | ||
| bf.reset(std::max<int32_t>(100, values.size() * 4)); | ||
| for (auto v : values) { | ||
| bf.insert(folly::hasher<int64_t>()(v)); | ||
| } | ||
| std::vector<char> data(bf.serializedSize()); | ||
| bf.serialize(data.data()); | ||
| return data; | ||
| } | ||
|
|
||
| /// Creates a ConstantTypedExpr wrapping serialized bloom filter bytes. | ||
| core::TypedExprPtr makeVarbinaryConstant(const std::vector<char>& data) { | ||
| auto vector = makeFlatVector<StringView>( | ||
| std::vector<StringView>{StringView(data.data(), data.size())}, | ||
| VARBINARY()); | ||
| return std::make_shared<const core::ConstantTypedExpr>(vector); | ||
| } | ||
|
|
||
| /// Creates a null VARBINARY constant expression. | ||
| core::TypedExprPtr makeNullVarbinaryConstant() { | ||
| auto vector = BaseVector::createNullConstant(VARBINARY(), 1, pool()); | ||
| return std::make_shared<const core::ConstantTypedExpr>(vector); | ||
| } | ||
|
|
||
| /// Constructs a might_contain(bloomFilter, value) CallTypedExpr. | ||
| core::CallTypedExprPtr makeMightContainCall( | ||
| const core::TypedExprPtr& bloomFilterExpr, | ||
| const core::TypedExprPtr& valueExpr) { | ||
| return std::make_shared<core::CallTypedExpr>( | ||
| BOOLEAN(), "might_contain", bloomFilterExpr, valueExpr); | ||
| } | ||
|
|
||
| /// Calls leafCallToSubfieldFilter on the parser. Returns (subfield, nullptr) | ||
| /// when the parser cannot translate the expression. | ||
| std::pair<Subfield, std::unique_ptr<Filter>> parse( | ||
| const core::CallTypedExprPtr& call, | ||
| bool negated = false) { | ||
| if (auto result = | ||
| parser_.leafCallToSubfieldFilter(*call, &evaluator_, negated)) { | ||
| return std::move(result.value()); | ||
| } | ||
| return std::make_pair(Subfield(), nullptr); | ||
| } | ||
|
|
||
| private: | ||
| std::shared_ptr<core::QueryCtx> queryCtx_{core::QueryCtx::create()}; | ||
| exec::SimpleExpressionEvaluator evaluator_{queryCtx_.get(), pool()}; | ||
| SparkExprToSubfieldFilterParser parser_; | ||
| }; | ||
|
|
||
| TEST_F(SparkExprToSubfieldFilterParserTest, mightContainBasic) { | ||
| std::vector<int64_t> inserted = {42, 100, 200, 300}; | ||
| auto serialized = makeSerializedBloomFilter(inserted); | ||
|
|
||
| auto bloomExpr = makeVarbinaryConstant(serialized); | ||
| auto columnExpr = | ||
| std::make_shared<core::FieldAccessTypedExpr>(BIGINT(), "a"); | ||
| auto call = makeMightContainCall(bloomExpr, columnExpr); | ||
|
|
||
| auto [subfield, filter] = parse(call); | ||
|
|
||
| ASSERT_TRUE(filter); | ||
|
|
||
| // Verify the subfield points to column "a". | ||
| ASSERT_EQ(subfield.path().size(), 1); | ||
| EXPECT_EQ(*subfield.path()[0], Subfield::NestedField("a")); | ||
|
|
||
| // All inserted values must pass the bloom filter. | ||
| for (auto v : inserted) { | ||
| EXPECT_TRUE(filter->testInt64(v)) << "Value " << v << " should pass"; | ||
| } | ||
|
|
||
| // Most non-inserted values should be rejected. | ||
| int falsePositives = 0; | ||
| for (int64_t v = 1000; v < 2000; v++) { | ||
| if (filter->testInt64(v)) { | ||
| ++falsePositives; | ||
| } | ||
| } | ||
| EXPECT_LT(falsePositives, 100) << "Too many false positives"; | ||
| } | ||
|
|
||
| TEST_F(SparkExprToSubfieldFilterParserTest, mightContainNullBloomFilter) { | ||
| auto nullExpr = makeNullVarbinaryConstant(); | ||
| auto columnExpr = | ||
| std::make_shared<core::FieldAccessTypedExpr>(BIGINT(), "a"); | ||
| auto call = makeMightContainCall(nullExpr, columnExpr); | ||
|
|
||
| auto [subfield, filter] = parse(call); | ||
| EXPECT_FALSE(filter); | ||
| } | ||
|
|
||
| TEST_F(SparkExprToSubfieldFilterParserTest, mightContainNegated) { | ||
| auto serialized = makeSerializedBloomFilter({42}); | ||
| auto bloomExpr = makeVarbinaryConstant(serialized); | ||
| auto columnExpr = | ||
| std::make_shared<core::FieldAccessTypedExpr>(BIGINT(), "a"); | ||
| auto call = makeMightContainCall(bloomExpr, columnExpr); | ||
|
|
||
| auto [subfield, filter] = parse(call, /*negated=*/true); | ||
| EXPECT_FALSE(filter); | ||
| } | ||
|
|
||
| TEST_F(SparkExprToSubfieldFilterParserTest, mightContainNonColumnValue) { | ||
| auto serialized = makeSerializedBloomFilter({42}); | ||
| auto bloomExpr = makeVarbinaryConstant(serialized); | ||
| // Use a constant (not a column reference) as the value argument. | ||
| auto constValue = makeVarbinaryConstant(serialized); // type doesn't matter | ||
| auto call = makeMightContainCall(bloomExpr, constValue); | ||
|
|
||
| auto [subfield, filter] = parse(call); | ||
| EXPECT_FALSE(filter); | ||
| } | ||
|
|
||
| TEST_F(SparkExprToSubfieldFilterParserTest, mightContainInt64Range) { | ||
| auto serialized = makeSerializedBloomFilter({42}); | ||
| auto bloomExpr = makeVarbinaryConstant(serialized); | ||
| auto columnExpr = | ||
| std::make_shared<core::FieldAccessTypedExpr>(BIGINT(), "a"); | ||
| auto call = makeMightContainCall(bloomExpr, columnExpr); | ||
|
|
||
| auto [subfield, filter] = parse(call); | ||
| ASSERT_TRUE(filter); | ||
|
|
||
| // Bloom filters cannot efficiently prune integer ranges. | ||
| EXPECT_TRUE(filter->testInt64Range(0, 1000, false)); | ||
| EXPECT_TRUE(filter->testInt64Range(0, 1000, true)); | ||
| } | ||
|
|
||
| TEST_F(SparkExprToSubfieldFilterParserTest, mightContainClone) { | ||
| std::vector<int64_t> inserted = {42, 100}; | ||
| auto serialized = makeSerializedBloomFilter(inserted); | ||
| auto bloomExpr = makeVarbinaryConstant(serialized); | ||
| auto columnExpr = | ||
| std::make_shared<core::FieldAccessTypedExpr>(BIGINT(), "a"); | ||
| auto call = makeMightContainCall(bloomExpr, columnExpr); | ||
|
|
||
| auto [subfield, filter] = parse(call); | ||
| ASSERT_TRUE(filter); | ||
|
|
||
| auto cloned = filter->clone(std::nullopt); | ||
| ASSERT_TRUE(cloned); | ||
| EXPECT_TRUE(filter->testingEquals(*cloned)); | ||
|
|
||
| for (auto v : inserted) { | ||
| EXPECT_TRUE(cloned->testInt64(v)); | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace gluten |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why error is swallowed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Errors are intentionally swallowed because a non-evaluable expression simply means the filter cannot be pushed down. So if the filter cannot be pushed down,
leafCallToSubfieldFilterreturnsstd::nullopt, and Velox will evaluatemight_containas a regular post-scan expression which is the same behavior as before. Does this flow make sense? I have updated the comment as well.