diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/filter/NullComparator.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/filter/NullComparator.java index 659729493a8..6f9ca110d27 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/filter/NullComparator.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/filter/NullComparator.java @@ -56,7 +56,7 @@ public class NullComparator extends ByteArrayComparable { @Override public int compareTo(byte[] value, int offset, int length) { - throw new UnsupportedOperationException(); + return compareTo(value); } /** diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestNullComparator.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestNullComparator.java new file mode 100644 index 00000000000..62639b8349e --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestNullComparator.java @@ -0,0 +1,73 @@ +/** + * + * Licensed 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 + * + * http://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. + * under the License. + */ + +package org.apache.hadoop.hbase.filter; + +import org.apache.hadoop.hbase.SmallTests; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(SmallTests.class) +public class TestNullComparator { + + @Test + public void testNullValue() + { + // given + byte[] value = null; + NullComparator comparator = new NullComparator(); + + // when + int comp1 = comparator.compareTo(value); + int comp2 = comparator.compareTo(value, 5, 15); + + // then + Assert.assertEquals(0, comp1); + Assert.assertEquals(0, comp2); + } + + @Test + public void testNonNullValue() { + // given + byte[] value = new byte[] { 0, 1, 2, 3, 4, 5 }; + NullComparator comparator = new NullComparator(); + + // when + int comp1 = comparator.compareTo(value); + int comp2 = comparator.compareTo(value, 1, 3); + + // then + Assert.assertEquals(1, comp1); + Assert.assertEquals(1, comp2); + } + + @Test + public void testEmptyValue() { + // given + byte[] value = new byte[] { 0 }; + NullComparator comparator = new NullComparator(); + + // when + int comp1 = comparator.compareTo(value); + int comp2 = comparator.compareTo(value, 1, 3); + + // then + Assert.assertEquals(1, comp1); + Assert.assertEquals(1, comp2); + } + +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestSingleColumnValueFilter.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestSingleColumnValueFilter.java index 1add7fb9c97..ceaff9ca2ac 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestSingleColumnValueFilter.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestSingleColumnValueFilter.java @@ -18,23 +18,19 @@ */ package org.apache.hadoop.hbase.filter; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.util.regex.Pattern; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.util.Bytes; - import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; -import static org.junit.Assert.*; - /** * Tests the value filter */ @@ -56,6 +52,7 @@ public class TestSingleColumnValueFilter { private static final Pattern QUICK_PATTERN = Pattern.compile("QuIcK", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Filter basicFilter; + Filter nullFilter; Filter substrFilter; Filter regexFilter; Filter regexPatternFilter; @@ -63,6 +60,7 @@ public class TestSingleColumnValueFilter { @Before public void setUp() throws Exception { basicFilter = basicFilterNew(); + nullFilter = nullFilterNew(); substrFilter = substrFilterNew(); regexFilter = regexFilterNew(); regexPatternFilter = regexFilterNew(QUICK_PATTERN); @@ -73,6 +71,11 @@ public class TestSingleColumnValueFilter { CompareOp.GREATER_OR_EQUAL, VAL_2); } + private Filter nullFilterNew() { + return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER, CompareOp.NOT_EQUAL, + new NullComparator()); + } + private Filter substrFilterNew() { return new SingleColumnValueFilter(COLUMN_FAMILY, COLUMN_QUALIFIER, CompareOp.EQUAL, @@ -116,6 +119,17 @@ public class TestSingleColumnValueFilter { assertFalse("basicFilterNotNull", filter.filterRow()); } + private void nullFilterTests(Filter filter) throws Exception { + ((SingleColumnValueFilter) filter).setFilterIfMissing(true); + KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, FULLSTRING_1); + assertTrue("null1", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); + assertFalse("null1FilterRow", filter.filterRow()); + filter.reset(); + kv = new KeyValue(ROW, COLUMN_FAMILY, Bytes.toBytes("qual2"), FULLSTRING_2); + assertTrue("null2", filter.filterKeyValue(kv) == Filter.ReturnCode.INCLUDE); + assertTrue("null2FilterRow", filter.filterRow()); + } + private void substrFilterTests(Filter filter) throws Exception { KeyValue kv = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, @@ -168,7 +182,8 @@ public class TestSingleColumnValueFilter { */ @Test public void testStop() throws Exception { - basicFilterTests((SingleColumnValueFilter)basicFilter); + basicFilterTests((SingleColumnValueFilter) basicFilter); + nullFilterTests(nullFilter); substrFilterTests(substrFilter); regexFilterTests(regexFilter); regexPatternFilterTests(regexPatternFilter); @@ -182,6 +197,8 @@ public class TestSingleColumnValueFilter { public void testSerialization() throws Exception { Filter newFilter = serializationTest(basicFilter); basicFilterTests((SingleColumnValueFilter)newFilter); + newFilter = serializationTest(nullFilter); + nullFilterTests(newFilter); newFilter = serializationTest(substrFilter); substrFilterTests(newFilter); newFilter = serializationTest(regexFilter);