WZhuo commented on code in PR #1177:
URL: https://github.com/apache/iceberg-cpp/pull/1177#discussion_r4703135727
##########
src/iceberg/expression/strict_metrics_evaluator.cc:
##########
@@ -477,9 +477,51 @@ class StrictMetricsVisitor : public BoundVisitor<bool> {
Result<bool> NotStartsWith(const std::shared_ptr<Bound>& expr,
const Literal& lit) override {
- // TODO(xiao.dong) Handle cases that definitely cannot match,
- // such as notStartsWith("x") when
- // the bounds are ["a", "b"].
+ RETURN_IF_NOT_REFERENCE(expr);
+
+ int32_t id = expr->reference()->field().field_id();
+
+ ICEBERG_ASSIGN_OR_RAISE(auto is_nested, IsNestedColumn(id));
+ if (is_nested) {
+ return kRowsMightNotMatch;
+ }
+
+ if (expr->reference()->type()->type_id() != TypeId::kString ||
+ lit.type()->type_id() != TypeId::kString ||
+ !std::holds_alternative<std::string>(lit.value())) {
+ return InvalidExpression(
+ "NotStartsWith requires a string column and string literal");
+ }
+
+ if (ContainsNullsOnly(id)) {
+ return kRowsMustMatch;
+ }
+
+ std::string_view prefix = std::get<std::string>(lit.value());
+
+ // All non-null values lie in [lower, upper] in UTF-8 byte order, so every
row
+ // satisfies NotStartsWith(prefix) if the range falls completely outside
the
+ // prefix range:
+ // 1. lower > prefix and does not start with prefix (all values are past
it), or
+ // 2. upper < prefix (all values are strictly before it).
+ auto lower_it = data_file_.lower_bounds.find(id);
+ if (lower_it != data_file_.lower_bounds.cend()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto lower, ParseBound(expr, lower_it->second));
+ std::string_view lower_str = std::get<std::string>(lower.value());
+ if (lower_str > prefix && !lower_str.starts_with(prefix)) {
Review Comment:
nit: The Java implementation truncates the lower bound to the prefix length
and does a single comparison, while here we do `>` and then `starts_with`,
which may scan the prefix twice. `std::string_view::compare(pos, count, str)`
already clamps `count` to the string size, so we can get the same result with
one call and stay closer to the Java logic:
```suggestion
if (lower_str.compare(0, prefix.size(), prefix) > 0) {
```
The upper bound check is fine as is, since `upper_str < prefix` is already a
single comparison and equivalent to Java's truncated check.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]