Skip to content
Merged
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
33 changes: 12 additions & 21 deletions src/iceberg/expression/inclusive_metrics_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "iceberg/expression/inclusive_metrics_evaluator.h"

#include <ranges>
#include <string_view>
#include <utility>

#include "iceberg/expression/binder.h"
Expand Down Expand Up @@ -315,32 +316,22 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
if (lit.type()->type_id() != TypeId::kString) {
return kRowCannotMatch;
}
const auto& prefix = std::get<std::string>(lit.value());
std::string_view prefix = std::get<std::string>(lit.value());

ICEBERG_ASSIGN_OR_RAISE(auto lower, LowerBound(expr));
if (!lower.has_value() || lower->IsNull()) {
return kRowsMightMatch;
}
const auto& lower_str = std::get<std::string>(lower->value());
// truncate lower bound so that its length in bytes is not greater than the length of
// prefix
size_t length = std::min(prefix.size(), lower_str.size());
// if prefix of lower bound is greater than prefix, rows cannot match
if (lower_str.substr(0, length) > prefix) {
return kRowCannotMatch;
if (lower.has_value() && !lower->IsNull()) {
std::string_view lower_str = std::get<std::string>(lower->value());
if (lower_str.compare(0, prefix.size(), prefix) > 0) {
return kRowCannotMatch;
}
}

ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr));
if (!upper.has_value() || upper->IsNull()) {
return kRowsMightMatch;
}
const auto& upper_str = std::get<std::string>(upper->value());
// truncate upper bound so that its length in bytes is not greater than the length of
// prefix
length = std::min(prefix.size(), upper_str.size());
// if prefix of upper bound is less than prefix, rows cannot match
if (upper_str.substr(0, length) < prefix) {
return kRowCannotMatch;
if (upper.has_value() && !upper->IsNull()) {
std::string_view upper_str = std::get<std::string>(upper->value());
if (upper_str < prefix) {
return kRowCannotMatch;
}
}

return kRowsMightMatch;
Expand Down
19 changes: 19 additions & 0 deletions src/iceberg/test/inclusive_metrics_evaluator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,25 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest, StringStartsWithTest) {
RunTest(Expressions::StartsWith("required", above_max), kRowCannotMatch, file4_);
}

TEST_F(InclusiveMetricsEvaluatorMigratedTest, StringStartsWithSingleBoundTest) {
// file2_ has required column 3 with bounds ["aa", "dC"].
auto upper_only = std::make_shared<DataFile>(*file2_);
upper_only->lower_bounds.clear();
RunTest(Expressions::StartsWith("required", "dWX"), kRowCannotMatch, upper_only);
RunTest(Expressions::StartsWith("required", "e"), kRowCannotMatch, upper_only);
RunTest(Expressions::StartsWith("required", "dCa"), kRowCannotMatch, upper_only);
RunTest(Expressions::StartsWith("required", "dC"), kRowsMightMatch, upper_only);
RunTest(Expressions::StartsWith("required", "d"), kRowsMightMatch, upper_only);
RunTest(Expressions::StartsWith("required", "a"), kRowsMightMatch, upper_only);
RunTest(Expressions::StartsWith("required", ""), kRowsMightMatch, upper_only);

auto lower_only = std::make_shared<DataFile>(*file2_);
lower_only->upper_bounds.clear();
RunTest(Expressions::StartsWith("required", "aB"), kRowCannotMatch, lower_only);
RunTest(Expressions::StartsWith("required", "aa"), kRowsMightMatch, lower_only);
RunTest(Expressions::StartsWith("required", "z"), kRowsMightMatch, lower_only);
}

TEST_F(InclusiveMetricsEvaluatorMigratedTest, StringNotStartsWithTest) {
RunTest(Expressions::NotStartsWith("required", "a"), kRowsMightMatch, file1_);
RunTest(Expressions::NotStartsWith("required", "a"), kRowsMightMatch, file2_);
Expand Down
Loading