001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.filter;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import org.apache.hadoop.hbase.CompareOperator;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.hbase.client.Result;
027import org.apache.hadoop.hbase.client.ResultScanner;
028import org.apache.hadoop.hbase.client.Scan;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.filter.FilterList.Operator;
031import org.apache.hadoop.hbase.testclassification.FilterTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.jupiter.api.AfterAll;
035import org.junit.jupiter.api.BeforeAll;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038import org.junit.jupiter.api.TestInfo;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042/**
043 * Tests filter Lists in ways that rely on a MiniCluster. Where possible, favor tests in
044 * TestFilterList and TestFilterFromRegionSide instead.
045 */
046@Tag(MediumTests.TAG)
047@Tag(FilterTests.TAG)
048public class TestFilterListOnMini {
049
050  private static final Logger LOG = LoggerFactory.getLogger(TestFilterListOnMini.class);
051  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
052
053  @BeforeAll
054  public static void setUpBeforeClass() throws Exception {
055    TEST_UTIL.startMiniCluster(1);
056  }
057
058  @AfterAll
059  public static void tearDownAfterClass() throws Exception {
060    TEST_UTIL.shutdownMiniCluster();
061  }
062
063  @Test
064  public void testFiltersWithOR(TestInfo testInfo) throws Exception {
065    TableName tn = TableName.valueOf(testInfo.getTestMethod().get().getName());
066    Table table = TEST_UTIL.createTable(tn, new String[] { "cf1", "cf2" });
067    byte[] CF1 = Bytes.toBytes("cf1");
068    byte[] CF2 = Bytes.toBytes("cf2");
069    Put put1 = new Put(Bytes.toBytes("0"));
070    put1.addColumn(CF1, Bytes.toBytes("col_a"), Bytes.toBytes(0));
071    table.put(put1);
072    Put put2 = new Put(Bytes.toBytes("0"));
073    put2.addColumn(CF2, Bytes.toBytes("col_b"), Bytes.toBytes(0));
074    table.put(put2);
075    FamilyFilter filterCF1 = new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(CF1));
076    FamilyFilter filterCF2 = new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(CF2));
077    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
078    filterList.addFilter(filterCF1);
079    filterList.addFilter(filterCF2);
080    Scan scan = new Scan();
081    scan.setFilter(filterList);
082    ResultScanner scanner = table.getScanner(scan);
083    LOG.info("Filter list: " + filterList);
084    for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
085      assertEquals(2, rr.size());
086    }
087  }
088
089  /**
090   * Test case for HBASE-21620
091   */
092  @Test
093  public void testColumnPrefixFilterConcatWithOR(TestInfo testInfo) throws Exception {
094    TableName tn = TableName.valueOf(testInfo.getTestMethod().get().getName());
095    byte[] cf1 = Bytes.toBytes("f1");
096    byte[] row = Bytes.toBytes("row");
097    byte[] value = Bytes.toBytes("value");
098    String[] columns = new String[] { "1544768273917010001_lt", "1544768273917010001_w_1",
099      "1544768723910010001_ca_1", "1544768723910010001_lt", "1544768723910010001_ut_1",
100      "1544768723910010001_w_5", "1544769779710010001_lt", "1544769779710010001_w_5",
101      "1544769883529010001_lt", "1544769883529010001_w_5", "1544769915805010001_lt",
102      "1544769915805010001_w_5", "1544779883529010001_lt", "1544770422942010001_lt",
103      "1544770422942010001_w_5" };
104    Table table = TEST_UTIL.createTable(tn, cf1);
105    for (int i = 0; i < columns.length; i++) {
106      Put put = new Put(row).addColumn(cf1, Bytes.toBytes(columns[i]), value);
107      table.put(put);
108    }
109    Scan scan = new Scan();
110    scan.withStartRow(row).withStopRow(row, true)
111      .setFilter(new FilterList(Operator.MUST_PASS_ONE,
112        new ColumnPrefixFilter(Bytes.toBytes("1544770422942010001_")),
113        new ColumnPrefixFilter(Bytes.toBytes("1544769883529010001_"))));
114    ResultScanner scanner = table.getScanner(scan);
115    Result result;
116    int resultCount = 0;
117    int cellCount = 0;
118    while ((result = scanner.next()) != null) {
119      cellCount += result.listCells().size();
120      resultCount++;
121    }
122    assertEquals(resultCount, 1);
123    assertEquals(cellCount, 4);
124  }
125}