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

kturner pushed a commit to branch 2.1
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/2.1 by this push:
     new 9846af574f fixes bug in seeking iterator w/ disjoint range and columns 
(#5631)
9846af574f is described below

commit 9846af574fce3fbd579443be58161e5d69ae4312
Author: Keith Turner <ktur...@apache.org>
AuthorDate: Tue Jun 10 14:37:08 2025 -0400

    fixes bug in seeking iterator w/ disjoint range and columns (#5631)
    
    In #5621 a bug was introduced where if an iterator seeks with range and
    columns families that are disjoint it will throw an exception.  This was
    caused by Range.bound() that had this behavior.
    
    To fix this copied the code in Range.bound() into RangeImpl and made a
    very slight modification.  This was done to preserve the existing
    behavior of the public API method Range.bound() and reuse 99% of its
    code and test for a slightly different behavior.  Also documented the
    behavior of Range.bound(), considered changing its behavior but this
    subtle change in runtime behavior seemed dicey w/ hard to predict
    outcomes (like it would change the runtime behavior of Scanner and
    BatchScanner which currently use Range.bound()).
    
    Now when the range and column families are disjoint
    ColumnFamilySkippingIterator will seek its underlying iterator with a
    new empty range.
    
    This bug was found by BigRootTabletIT.  After this change that test
    passes.
---
 .../java/org/apache/accumulo/core/data/Range.java  |  76 +--------------
 .../apache/accumulo/core/dataImpl/RangeImpl.java   | 108 +++++++++++++++++++++
 .../system/ColumnFamilySkippingIterator.java       |   5 +-
 .../org/apache/accumulo/core/data/RangeTest.java   |  39 ++++++++
 .../system/ColumnFamilySkippingIteratorTest.java   |  10 ++
 5 files changed, 163 insertions(+), 75 deletions(-)

diff --git a/core/src/main/java/org/apache/accumulo/core/data/Range.java 
b/core/src/main/java/org/apache/accumulo/core/data/Range.java
index 2c896b0f7f..3be2e70b07 100644
--- a/core/src/main/java/org/apache/accumulo/core/data/Range.java
+++ b/core/src/main/java/org/apache/accumulo/core/data/Range.java
@@ -27,6 +27,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.accumulo.core.dataImpl.RangeImpl;
 import org.apache.accumulo.core.dataImpl.thrift.TRange;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.WritableComparable;
@@ -562,81 +563,10 @@ public class Range implements WritableComparable<Range> {
    * @param max maximum column
    * @return a column bounded range
    * @throws IllegalArgumentException if the minimum column compares greater 
than the maximum column
+   *         OR if the columns and the range are disjoint
    */
   public Range bound(Column min, Column max) {
-
-    if (min.compareTo(max) > 0) {
-      throw new IllegalArgumentException("min column > max column " + min + " 
" + max);
-    }
-
-    Key sk = getStartKey();
-    boolean ski = isStartKeyInclusive();
-
-    if (sk != null) {
-
-      ByteSequence cf = sk.getColumnFamilyData();
-      ByteSequence cq = sk.getColumnQualifierData();
-
-      ByteSequence mincf = new ArrayByteSequence(min.columnFamily);
-      ByteSequence mincq;
-
-      if (min.columnQualifier != null) {
-        mincq = new ArrayByteSequence(min.columnQualifier);
-      } else {
-        mincq = new ArrayByteSequence(new byte[0]);
-      }
-
-      int cmp = cf.compareTo(mincf);
-
-      if (cmp < 0 || (cmp == 0 && cq.compareTo(mincq) < 0)) {
-        ski = true;
-        sk = new Key(sk.getRowData().toArray(), mincf.toArray(), 
mincq.toArray(), new byte[0],
-            Long.MAX_VALUE, true);
-      }
-    }
-
-    Key ek = getEndKey();
-    boolean eki = isEndKeyInclusive();
-
-    if (ek != null) {
-      ByteSequence row = ek.getRowData();
-      ByteSequence cf = ek.getColumnFamilyData();
-      ByteSequence cq = ek.getColumnQualifierData();
-      ByteSequence cv = ek.getColumnVisibilityData();
-
-      ByteSequence maxcf = new ArrayByteSequence(max.columnFamily);
-      ByteSequence maxcq = null;
-      if (max.columnQualifier != null) {
-        maxcq = new ArrayByteSequence(max.columnQualifier);
-      }
-
-      boolean set = false;
-
-      int comp = cf.compareTo(maxcf);
-
-      if (comp > 0) {
-        set = true;
-      } else if (comp == 0 && maxcq != null && cq.compareTo(maxcq) > 0) {
-        set = true;
-      } else if (!eki && row.length() > 0 && row.byteAt(row.length() - 1) == 0 
&& cf.length() == 0
-          && cq.length() == 0 && cv.length() == 0 && ek.getTimestamp() == 
Long.MAX_VALUE) {
-        row = row.subSequence(0, row.length() - 1);
-        set = true;
-      }
-
-      if (set) {
-        eki = false;
-        if (maxcq == null) {
-          ek = new Key(row.toArray(), maxcf.toArray(), new byte[0], new 
byte[0], 0, false)
-              .followingKey(PartialKey.ROW_COLFAM);
-        } else {
-          ek = new Key(row.toArray(), maxcf.toArray(), maxcq.toArray(), new 
byte[0], 0, false)
-              .followingKey(PartialKey.ROW_COLFAM_COLQUAL);
-        }
-      }
-    }
-
-    return new Range(sk, ski, ek, eki);
+    return RangeImpl.bound(this, min, max, false);
   }
 
   @Override
diff --git 
a/core/src/main/java/org/apache/accumulo/core/dataImpl/RangeImpl.java 
b/core/src/main/java/org/apache/accumulo/core/dataImpl/RangeImpl.java
new file mode 100644
index 0000000000..089f287f2e
--- /dev/null
+++ b/core/src/main/java/org/apache/accumulo/core/dataImpl/RangeImpl.java
@@ -0,0 +1,108 @@
+/*
+ * 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
+ *
+ *   https://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.
+ */
+package org.apache.accumulo.core.dataImpl;
+
+import org.apache.accumulo.core.data.ArrayByteSequence;
+import org.apache.accumulo.core.data.ByteSequence;
+import org.apache.accumulo.core.data.Column;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.PartialKey;
+import org.apache.accumulo.core.data.Range;
+
+public class RangeImpl {
+  public static Range bound(Range range, Column min, Column max,
+      boolean returnEmptyRangeWhenDisjoint) {
+
+    if (min.compareTo(max) > 0) {
+      throw new IllegalArgumentException("min column > max column " + min + " 
" + max);
+    }
+
+    Key sk = range.getStartKey();
+    boolean ski = range.isStartKeyInclusive();
+
+    if (sk != null) {
+
+      ByteSequence cf = sk.getColumnFamilyData();
+      ByteSequence cq = sk.getColumnQualifierData();
+
+      ByteSequence mincf = new ArrayByteSequence(min.columnFamily);
+      ByteSequence mincq;
+
+      if (min.columnQualifier != null) {
+        mincq = new ArrayByteSequence(min.columnQualifier);
+      } else {
+        mincq = new ArrayByteSequence(new byte[0]);
+      }
+
+      int cmp = cf.compareTo(mincf);
+
+      if (cmp < 0 || (cmp == 0 && cq.compareTo(mincq) < 0)) {
+        ski = true;
+        sk = new Key(sk.getRowData().toArray(), mincf.toArray(), 
mincq.toArray(), new byte[0],
+            Long.MAX_VALUE, true);
+      }
+    }
+
+    Key ek = range.getEndKey();
+    boolean eki = range.isEndKeyInclusive();
+
+    if (ek != null) {
+      ByteSequence row = ek.getRowData();
+      ByteSequence cf = ek.getColumnFamilyData();
+      ByteSequence cq = ek.getColumnQualifierData();
+      ByteSequence cv = ek.getColumnVisibilityData();
+
+      ByteSequence maxcf = new ArrayByteSequence(max.columnFamily);
+      ByteSequence maxcq = null;
+      if (max.columnQualifier != null) {
+        maxcq = new ArrayByteSequence(max.columnQualifier);
+      }
+
+      boolean set = false;
+
+      int comp = cf.compareTo(maxcf);
+
+      if (comp > 0) {
+        set = true;
+      } else if (comp == 0 && maxcq != null && cq.compareTo(maxcq) > 0) {
+        set = true;
+      } else if (!eki && row.length() > 0 && row.byteAt(row.length() - 1) == 0 
&& cf.length() == 0
+          && cq.length() == 0 && cv.length() == 0 && ek.getTimestamp() == 
Long.MAX_VALUE) {
+        row = row.subSequence(0, row.length() - 1);
+        set = true;
+      }
+
+      if (set) {
+        eki = false;
+        if (maxcq == null) {
+          ek = new Key(row.toArray(), maxcf.toArray(), new byte[0], new 
byte[0], 0, false)
+              .followingKey(PartialKey.ROW_COLFAM);
+        } else {
+          ek = new Key(row.toArray(), maxcf.toArray(), maxcq.toArray(), new 
byte[0], 0, false)
+              .followingKey(PartialKey.ROW_COLFAM_COLQUAL);
+        }
+      }
+    }
+
+    if (returnEmptyRangeWhenDisjoint && sk != null && ek != null && 
sk.compareTo(ek) > 0) {
+      return new Range(sk, true, sk, false);
+    }
+    return new Range(sk, ski, ek, eki);
+  }
+}
diff --git 
a/core/src/main/java/org/apache/accumulo/core/iteratorsImpl/system/ColumnFamilySkippingIterator.java
 
b/core/src/main/java/org/apache/accumulo/core/iteratorsImpl/system/ColumnFamilySkippingIterator.java
index bca148a42c..3745bf0fc3 100644
--- 
a/core/src/main/java/org/apache/accumulo/core/iteratorsImpl/system/ColumnFamilySkippingIterator.java
+++ 
b/core/src/main/java/org/apache/accumulo/core/iteratorsImpl/system/ColumnFamilySkippingIterator.java
@@ -31,6 +31,7 @@ import org.apache.accumulo.core.data.Key;
 import org.apache.accumulo.core.data.PartialKey;
 import org.apache.accumulo.core.data.Range;
 import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.dataImpl.RangeImpl;
 import org.apache.accumulo.core.iterators.IteratorEnvironment;
 import org.apache.accumulo.core.iterators.ServerSkippingIterator;
 import org.apache.accumulo.core.iterators.SortedKeyValueIterator;
@@ -126,8 +127,8 @@ public class ColumnFamilySkippingIterator extends 
ServerSkippingIterator
         this.range = range;
       } else {
         // Limit the range based on the min and max column families
-        this.range = range.bound(new Column(sortedColFams.first().toArray(), 
null, null),
-            new Column(sortedColFams.last().toArray(), null, null));
+        this.range = RangeImpl.bound(range, new 
Column(sortedColFams.first().toArray(), null, null),
+            new Column(sortedColFams.last().toArray(), null, null), true);
       }
     } else {
       sortedColFams = null;
diff --git a/core/src/test/java/org/apache/accumulo/core/data/RangeTest.java 
b/core/src/test/java/org/apache/accumulo/core/data/RangeTest.java
index c78fdace6e..5792c13e2e 100644
--- a/core/src/test/java/org/apache/accumulo/core/data/RangeTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/data/RangeTest.java
@@ -35,6 +35,7 @@ import java.util.HashSet;
 import java.util.List;
 
 import org.apache.accumulo.core.dataImpl.KeyExtent;
+import org.apache.accumulo.core.dataImpl.RangeImpl;
 import org.apache.accumulo.core.dataImpl.thrift.TRange;
 import org.apache.hadoop.io.Text;
 import org.junit.jupiter.api.Test;
@@ -728,6 +729,44 @@ public class RangeTest {
     assertTrue(range7.contains(newKey("row1", "b", "x")));
     assertTrue(range7.contains(newKey("row1", "f", "x")));
     assertFalse(range7.contains(newKey("row1", "f", "z")));
+
+    // These columns fall completely after the columns in range1, should fail
+    assertThrows(IllegalArgumentException.class,
+        () -> range1.bound(newColumn("g"), newColumn("x")));
+    // run the same test as above but produce empty range instead
+    Range range8 = RangeImpl.bound(range1, newColumn("g"), newColumn("x"), 
true);
+    assertFalse(range8.contains(range8.getStartKey()));
+    var expectedKey = newKey("row1", "g", "");
+    expectedKey.setDeleted(true);
+    assertEquals(new Range(expectedKey, true, expectedKey, false), range8);
+
+    // These columns fall completely before the columns in range1, should fail
+    assertThrows(IllegalArgumentException.class,
+        () -> range1.bound(newColumn("!"), newColumn("+")));
+    // run the same test as above but produce empty range instead
+    Range range9 = RangeImpl.bound(range1, newColumn("!"), newColumn("+"), 
true);
+    assertFalse(range9.contains(range9.getStartKey()));
+    assertEquals(range1.getStartKey(), range9.getStartKey());
+    assertTrue(range9.isStartKeyInclusive());
+    assertEquals(range1.getStartKey(), range9.getEndKey());
+    assertFalse(range9.isEndKeyInclusive());
+  }
+
+  @Test
+  public void testBoundEmpty() {
+    Text row = new Text(new byte[] {'!', '0', 0});
+    // BigRootTabletIT produced this exact range and it caused Range.bound to 
throw an exception
+    Range range = new Range(new Key(row), true, new Key(row), false);
+    assertThrows(IllegalArgumentException.class,
+        () -> range.bound(newColumn("loc"), newColumn("~tab")));
+
+    // this should produce an empty range
+    Range bounded = RangeImpl.bound(range, newColumn("loc"), 
newColumn("~tab"), true);
+    assertFalse(bounded.contains(bounded.getStartKey()));
+    var expectedKey = new Key(row, new Text("loc"));
+    expectedKey.setDeleted(true);
+    assertEquals(expectedKey, bounded.getStartKey());
+    assertTrue(bounded.isStartKeyInclusive());
   }
 
   @Test
diff --git 
a/core/src/test/java/org/apache/accumulo/core/iterators/system/ColumnFamilySkippingIteratorTest.java
 
b/core/src/test/java/org/apache/accumulo/core/iterators/system/ColumnFamilySkippingIteratorTest.java
index 2adbe3d282..1d3170dba8 100644
--- 
a/core/src/test/java/org/apache/accumulo/core/iterators/system/ColumnFamilySkippingIteratorTest.java
+++ 
b/core/src/test/java/org/apache/accumulo/core/iterators/system/ColumnFamilySkippingIteratorTest.java
@@ -364,5 +364,15 @@ public class ColumnFamilySkippingIteratorTest {
       }
     }
     assertFalse(cfi.hasTop());
+
+    // test the case where the columns and range are disjoint
+    seekRange =
+        new Range(new Key(format(1), format(9)), true, new Key(format(1), 
format(11)), false);
+    capturedRanges.clear();
+    cfi.seek(seekRange, Set.of(new ArrayByteSequence(format(7))), true);
+    assertFalse(cfi.hasTop());
+    assertEquals(new Range(seekRange.getStartKey(), true, 
seekRange.getStartKey(), false),
+        capturedRanges.get(0));
+
   }
 }

Reply via email to