Index: hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestFilterOnSelectedColumns.java =================================================================== --- hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestFilterOnSelectedColumns.java (revision 0) +++ hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestFilterOnSelectedColumns.java (revision 0) @@ -0,0 +1,117 @@ +/** + * 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 + * + * 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. + */ +package org.apache.hadoop.hbase.filter; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; + +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.KeyValue; +import org.apache.hadoop.hbase.MediumTests; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.HTable; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Result; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +/** + * Verifies the way filter is applied when Get or Scan should return only selected columns + */ +@Category(MediumTests.class) +public class TestFilterOnSelectedColumns { + private static final HBaseTestingUtility TEST_UTIL = + new HBaseTestingUtility(); + + @BeforeClass + public static void setup() throws Exception { + TEST_UTIL.startMiniCluster(1, 1); + } + + @AfterClass + public static void teardown() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Test + public void testNonSelectedColumnsAreSkippedFromFilterApplying() throws IOException { + HTable hTable = TEST_UTIL.createTable(Bytes.toBytes("t"), + new byte[][] {Bytes.toBytes("cf")}); + + Put put = new Put(new byte[] {1}); + put.add(Bytes.toBytes("cf"), Bytes.toBytes("3"), Bytes.toBytes("abc")); + put.add(Bytes.toBytes("cf"), Bytes.toBytes("4"), Bytes.toBytes("abcd")); + hTable.put(put); + + put = new Put(new byte[] {2}); + put.add(Bytes.toBytes("cf"), Bytes.toBytes("3"), Bytes.toBytes("a")); + put.add(Bytes.toBytes("cf"), Bytes.toBytes("4"), Bytes.toBytes("ab")); + hTable.put(put); + + put = new Put(new byte[] {3}); + put.add(Bytes.toBytes("cf"), Bytes.toBytes("1"), Bytes.toBytes("a")); + put.add(Bytes.toBytes("cf"), Bytes.toBytes("2"), Bytes.toBytes("ab")); + put.add(Bytes.toBytes("cf"), Bytes.toBytes("3"), Bytes.toBytes("abc")); + put.add(Bytes.toBytes("cf"), Bytes.toBytes("4"), Bytes.toBytes("abcd")); + hTable.put(put); + + hTable.flushCommits(); + + Get get = new Get(new byte[] {1}); + get.setFilter(new ValueHasMoreThan2BytesFilter()); + Result res = hTable.get(get); + Assert.assertTrue(res != null && !res.isEmpty()); + + get = new Get(new byte[] {2}); + get.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("3")); + get.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("4")); + get.setFilter(new ValueHasMoreThan2BytesFilter()); + res = hTable.get(get); + Assert.assertTrue(res == null || res.isEmpty()); + + get = new Get(new byte[] {3}); + get.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("3")); + get.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("4")); + get.setFilter(new ValueHasMoreThan2BytesFilter()); + res = hTable.get(get); + Assert.assertTrue(res != null && !res.isEmpty()); + } + + private static class ValueHasMoreThan2BytesFilter extends FilterBase { + @Override + public ReturnCode filterKeyValue(KeyValue kv) { + byte[] val = kv.getValue(); + if (val.length > 2) { + return ReturnCode.INCLUDE; + } else { + return ReturnCode.NEXT_ROW; + } + } + + @Override + public void write(DataOutput dataOutput) throws IOException {} + + @Override + public void readFields(DataInput dataInput) throws IOException {} + } +}