You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by ab...@apache.org on 2026/06/15 14:31:29 UTC

(kudu) 01/02: KUDU-3782 avoid unsigned underflow in MaterializingIterator::Init

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 9abc5a5e3c3d3c3a9f1807817fbdbecbd9125129
Author: Gabriella Lotz <lo...@gmail.com>
AuthorDate: Thu Jun 11 16:15:10 2026 +0200

    KUDU-3782 avoid unsigned underflow in MaterializingIterator::Init
    
    A "counting" scan (e.g. SELECT count(...)) projects no columns, so the
    MaterializingIterator's schema can have fewer columns than the number of
    predicates in the ScanSpec. This happens because a lower-level iterator,
    CFileSet::Iterator::OptimizePKPredicates(), can lift additional predicates
    from a rowset's primary key bounds onto key columns that are not part of
    the scan projection. It is most pronounced for counting scans whose
    projection is empty or tiny.
    
    In that case MaterializingIterator::Init() computed:
    
      non_predicate_column_indexes_.reserve(
          num_columns - spec->predicates().size());
    
    where num_columns is int32_t and predicates().size() is size_t. When
    num_columns < predicates().size() the subtraction is evaluated in unsigned
    arithmetic and wraps to a value near SIZE_MAX, so vector::reserve() throws
    std::length_error. The exception is not caught, so the tablet server
    aborts with SIGABRT. The preceding DCHECK_GE(num_columns,
    predicates().size()) encodes an invariant that does not actually hold and
    is compiled out in release builds.
    
    This patch:
     - removes the invalid DCHECK_GE;
     - reserves an upper bound (num_columns) instead of the difference, which
       cannot underflow;
     - skips predicates whose column is not in the projection rather than
       returning InvalidArgument. Such predicates are derived from the
       rowset's primary key bounds and are already enforced by the key range,
       so skipping them here is safe.
    
    Also adds a regression test that initializes a MaterializingIterator with
    more predicates than projected columns (including a predicate on a column
    outside the projection) and verifies that Init() succeeds and that the
    in-projection predicate still filters correctly.
    
    Change-Id: I646ab64c2139b37c7e29152695805bf28ef1dd20
    Reviewed-on: http://gerrit.cloudera.org:8080/24439
    Tested-by: Kudu Jenkins
    Reviewed-by: Marton Greber <gr...@gmail.com>
    Reviewed-by: Zoltan Chovan <zc...@cloudera.com>
    Reviewed-by: Abhishek Chennaka <ac...@cloudera.com>
    Reviewed-by: Alexey Serbin <al...@apache.org>
---
 src/kudu/common/generic_iterators-test.cc | 58 +++++++++++++++++++++++++++++++
 src/kudu/common/generic_iterators.cc      | 21 +++++++++--
 2 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/src/kudu/common/generic_iterators-test.cc b/src/kudu/common/generic_iterators-test.cc
index f2353c655..1bef29255 100644
--- a/src/kudu/common/generic_iterators-test.cc
+++ b/src/kudu/common/generic_iterators-test.cc
@@ -543,6 +543,64 @@ TEST(TestMaterializingIterator, TestMaterializingPredicatePushdown) {
   ASSERT_FALSE(dst.selection_vector()->IsRowSelected(30));
 }
 
+// Regression test for KUDU-3782: a tablet server crash (SIGABRT via
+// std::length_error in release builds, or a failed DCHECK_GE in debug builds)
+// observed for "counting" scans (e.g. SELECT count(...)) that also carry
+// predicates.
+//
+// In that scenario the projection materialized by the MaterializingIterator is
+// tiny (or empty), while a lower-level iterator (CFileSet::Iterator) can lift
+// extra predicates from the rowset's primary key bounds onto key columns that
+// are not part of the projection. As a result spec->predicates().size() can
+// exceed schema().num_columns(), and the expression
+//   non_predicate_column_indexes_.reserve(num_columns - predicates().size())
+// underflowed (unsigned arithmetic) to a huge value, throwing std::length_error
+// out of vector::reserve() and aborting the process.
+//
+// This reproduces that state directly: the iterator's projection has a single
+// column, but the scan spec carries two predicates, one of which is on a column
+// absent from the projection. Init() must not crash, must skip the
+// out-of-projection predicate, and must still evaluate the in-projection one.
+TEST(TestMaterializingIterator, TestMorePredicatesThanProjectedColumns) {
+  ScanSpec spec;
+  // In-projection predicate on 'val': selects rows 20-29.
+  TestIntRangePredicate pred_in_proj(20, 30);
+  spec.AddPredicate(pred_in_proj.pred_);
+  // Out-of-projection predicate on a key column that the MaterializingIterator
+  // does not materialize, emulating a predicate lifted from a rowset's primary
+  // key bounds. This pushes predicates().size() (2) above the projection's
+  // column count (1), which previously triggered the unsigned underflow.
+  const ColumnSchema kMissingKeyCol("missing_key", INT64);
+  TestIntRangePredicate pred_out_of_proj(0, 1000, kMissingKeyCol);
+  spec.AddPredicate(pred_out_of_proj.pred_);
+  ASSERT_GT(spec.predicates().size(), kIntSchema.num_columns());
+
+  vector<int64_t> ints(100);
+  for (int i = 0; i < 100; i++) {
+    ints[i] = i;
+  }
+
+  unique_ptr<VectorIterator> colwise(new VectorIterator(ints));
+  unique_ptr<RowwiseIterator> materializing(NewMaterializingIterator(std::move(colwise)));
+  // Must not crash, and must not return an error for the out-of-projection
+  // predicate.
+  ASSERT_OK(materializing->Init(&spec));
+  ASSERT_EQ(0, spec.predicates().size()) << "Iterator should have pushed down predicates";
+
+  RowBlockMemory mem(1024);
+  RowBlock dst(&kIntSchema, 100, &mem);
+  ASSERT_OK(materializing->NextBlock(&dst));
+  ASSERT_EQ(dst.nrows(), 100);
+
+  // Only the in-projection predicate (rows 20-29) should have been applied.
+  ASSERT_EQ(10, dst.selection_vector()->CountSelected());
+  ASSERT_FALSE(dst.selection_vector()->IsRowSelected(0));
+  ASSERT_FALSE(dst.selection_vector()->IsRowSelected(19));
+  ASSERT_TRUE(dst.selection_vector()->IsRowSelected(20));
+  ASSERT_TRUE(dst.selection_vector()->IsRowSelected(29));
+  ASSERT_FALSE(dst.selection_vector()->IsRowSelected(30));
+}
+
 // Test that PredicateEvaluatingIterator will properly evaluate predicates on its
 // input.
 TEST(TestPredicateEvaluatingIterator, TestPredicateEvaluation) {
diff --git a/src/kudu/common/generic_iterators.cc b/src/kudu/common/generic_iterators.cc
index 2c8c9e2ec..66293f687 100644
--- a/src/kudu/common/generic_iterators.cc
+++ b/src/kudu/common/generic_iterators.cc
@@ -1180,14 +1180,29 @@ Status MaterializingIterator::Init(ScanSpec *spec) {
   if (PREDICT_TRUE(!disallow_pushdown_for_tests_) && spec != nullptr &&
       !spec->predicates().empty()) {
     col_idx_predicates_.reserve(spec->predicates().size());
-    DCHECK_GE(num_columns, spec->predicates().size());
-    non_predicate_column_indexes_.reserve(num_columns - spec->predicates().size());
+    // Reserve an upper bound: this list holds at most one entry per projected
+    // column. Reserving 'num_columns - spec->predicates().size()' would
+    // underflow (the operands are int32_t and size_t) when the spec has more
+    // predicates than there are projected columns, e.g. a counting scan whose
+    // projection is empty but whose spec carries predicates lifted from the
+    // rowset's primary key bounds. That wrapped to a huge size_t and aborted
+    // the server with std::length_error out of vector::reserve().
+    non_predicate_column_indexes_.reserve(num_columns);
 
     for (const auto& col_pred : spec->predicates()) {
       const ColumnPredicate& pred = col_pred.second;
       int col_idx = schema().find_column(pred.column().name());
       if (col_idx == Schema::kColumnNotFound) {
-        return Status::InvalidArgument("No such column", col_pred.first);
+        // This predicate is on a column outside the projection. On the server
+        // scan path that only happens for predicates lifted from the rowset's
+        // primary key bounds (redundant, already enforced by the key range),
+        // since TabletServiceImpl projects all predicate columns via
+        // ScanSpec::GetMissingColumns(). Skip it rather than failing the scan.
+        // TODO(KUDU-3784): honor predicates on non-projected columns generally
+        // instead of skipping; see the disabled TestPushdownIntValueRange in
+        // tablet-pushdown-test.cc.
+        VLOG(1) << "Skipping out-of-projection predicate " << pred.ToString();
+        continue;
       }
       VLOG(1) << "Pushing down predicate " << pred.ToString();
       col_idx_predicates_.emplace_back(col_idx, col_pred.second);