From ff0a3108f3255506277d51197a01527247bcceba Mon Sep 17 00:00:00 2001 From: deepaknettem Date: Fri, 27 Nov 2026 15:51:44 -0800 Subject: [PATCH] fix(expression): use upper bound for StartsWith when lower bound is missing --- .../expression/inclusive_metrics_evaluator.cc | 33 +++++++------------ .../test/inclusive_metrics_evaluator_test.cc | 19 +++++++++++ 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/iceberg/expression/inclusive_metrics_evaluator.cc b/src/iceberg/expression/inclusive_metrics_evaluator.cc index cb0ea6139..b7165d1a0 100644 --- a/src/iceberg/expression/inclusive_metrics_evaluator.cc +++ b/src/iceberg/expression/inclusive_metrics_evaluator.cc @@ -20,6 +20,7 @@ #include "iceberg/expression/inclusive_metrics_evaluator.h" #include +#include #include #include "iceberg/expression/binder.h" @@ -315,32 +316,22 @@ class InclusiveMetricsVisitor : public BoundVisitor { if (lit.type()->type_id() != TypeId::kString) { return kRowCannotMatch; } - const auto& prefix = std::get(lit.value()); + std::string_view prefix = std::get(lit.value()); ICEBERG_ASSIGN_OR_RAISE(auto lower, LowerBound(expr)); - if (!lower.has_value() || lower->IsNull()) { - return kRowsMightMatch; - } - const auto& lower_str = std::get(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(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(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(upper->value()); + if (upper_str < prefix) { + return kRowCannotMatch; + } } return kRowsMightMatch; diff --git a/src/iceberg/test/inclusive_metrics_evaluator_test.cc b/src/iceberg/test/inclusive_metrics_evaluator_test.cc index e8c7b75ed..65fb62893 100644 --- a/src/iceberg/test/inclusive_metrics_evaluator_test.cc +++ b/src/iceberg/test/inclusive_metrics_evaluator_test.cc @@ -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(*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(*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_);