Java Code Examples for org.apache.hadoop.hbase.filter.Filter

The following examples show how to use org.apache.hadoop.hbase.filter.Filter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hbase   Source File: PerformanceEvaluation.java    License: Apache License 2.0 6 votes vote down vote up
protected Scan constructScan(byte[] valuePrefix) throws IOException {
  FilterList list = new FilterList();
  Filter filter = new SingleColumnValueFilter(FAMILY_ZERO, COLUMN_ZERO,
    CompareOperator.EQUAL, new BinaryComparator(valuePrefix));
  list.addFilter(filter);
  if (opts.filterAll) {
    list.addFilter(new FilterAllFilter());
  }
  Scan scan = new Scan().setCaching(opts.caching).setCacheBlocks(opts.cacheBlocks)
      .setAsyncPrefetch(opts.asyncPrefetch).setReadType(opts.scanReadType)
      .setScanMetricsEnabled(true);
  if (opts.addColumns) {
    for (int column = 0; column < opts.columns; column++) {
      byte [] qualifier = column == 0? COLUMN_ZERO: Bytes.toBytes("" + column);
      scan.addColumn(FAMILY_ZERO, qualifier);
    }
  } else {
    scan.addFamily(FAMILY_ZERO);
  }
  scan.setFilter(list);
  return scan;
}
 
Example 2
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLikeNoOptKeyExpression() throws SQLException {
    String tenantId = "000000000000001";
    String keyPrefix = "002";
    String likeArg = "%001%" + keyPrefix + "%";
    String query = "select * from atable where organization_id = ? and entity_id  LIKE '" + likeArg + "'";
    List<Object> binds = Arrays.<Object>asList(tenantId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();

    Filter filter = scan.getFilter();
    assertNotNull(filter);
    assertEquals(
            rowKeyFilter(like(
                ENTITY_ID,
                likeArg)),
            filter);

    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId);
    assertArrayEquals(startRow, scan.getStartRow());
    assertArrayEquals(ByteUtil.nextKey(startRow), scan.getStopRow());
}
 
Example 3
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testNullAtEndOfRVC() throws SQLException {
    String tenantId = "000000000000001";
    String parentId = "000000000000002";
    Date createdDate = null;
    
    String query = "select * from entity_history where (organization_id, parent_id, created_date) >= (?,?,?)";
    List<Object> binds = Arrays.<Object>asList(tenantId, parentId, createdDate);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    byte[] expectedStartRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId), PVarchar.INSTANCE.toBytes(parentId));
    assertArrayEquals(expectedStartRow, scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 4
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testNullAtStartOfRVC() throws SQLException {
    String tenantId = null;
    String parentId = "000000000000002";
    Date createdDate = new Date(System.currentTimeMillis());
    
    String query = "select * from entity_history where (organization_id, parent_id, created_date) >= (?,?,?)";
    List<Object> binds = Arrays.<Object>asList(tenantId, parentId, createdDate);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    byte[] expectedStartRow = ByteUtil.concat(new byte[15], ByteUtil.previousKey(PChar.INSTANCE.toBytes(parentId)), PDate.INSTANCE.toBytes(createdDate));
    assertArrayEquals(expectedStartRow, scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 5
Source Project: phoenix   Source File: WhereCompilerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOrFalseFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and (a_integer=0 or 3!=3)";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertEquals(
        singleKVFilter(constantComparison(
            CompareOp.EQUAL,
            A_INTEGER,
            0)),
        filter);
    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = startRow;
    assertArrayEquals(ByteUtil.nextKey(stopRow), scan.getStopRow());
}
 
Example 6
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLikeOptKeyExpression() throws SQLException {
    String tenantId = "000000000000001";
    String keyPrefix = "002";
    String likeArg = keyPrefix + "%003%";
    String query = "select * from atable where organization_id = ? and entity_id  LIKE '" + likeArg + "'";
    List<Object> binds = Arrays.<Object>asList(tenantId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();

    Filter filter = scan.getFilter();
    assertNotNull(filter);
    assertEquals(
            rowKeyFilter(like(
                ENTITY_ID,
                likeArg)),
            filter);

    byte[] startRow = ByteUtil.concat(
        PVarchar.INSTANCE.toBytes(tenantId),StringUtil.padChar(PVarchar.INSTANCE.toBytes(keyPrefix),15));
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(
        PVarchar.INSTANCE.toBytes(tenantId),StringUtil.padChar(ByteUtil.nextKey(PVarchar.INSTANCE.toBytes(keyPrefix)),15));
    assertArrayEquals(stopRow, scan.getStopRow());
}
 
Example 7
@Test
public void testNotBetweenFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_integer not between 0 and 10";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), TEST_PROPERTIES).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertEquals(
            singleKVFilter(not(and(
                constantComparison(
                    CompareOp.GREATER_OR_EQUAL,
                    BaseConnectionlessQueryTest.A_INTEGER,
                    0),
                constantComparison(
                    CompareOp.LESS_OR_EQUAL,
                    BaseConnectionlessQueryTest.A_INTEGER,
                    10)))).toString(),
            filter.toString());
}
 
Example 8
Source Project: Eagle   Source File: GenericAggregateReader.java    License: Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param ed                Entity Definition
 * @param partitions        Partition values
 * @param startTime         Start time
 * @param endTime           End time
 * @param filter            HBase filter for scanning
 * @param lastScanKey       Last HBase scan row key in String
 * @param outputQualifiers  HBase output qualifiers in bytes
 * @param prefix            HBase prefix, not necessary except for GenericMetric query
 * @param condition         GroupAggregateCondition Object
 *
 * @see org.apache.eagle.query.aggregate.AggregateCondition
 */
public GenericAggregateReader(EntityDefinition ed,
                               List<String> partitions,
                               Date startTime,
                               Date endTime,
                               Filter filter,
                               String lastScanKey,
                               byte[][] outputQualifiers,
                               String prefix,
                               AggregateCondition condition) {
	super(ed, partitions, startTime, endTime, filter, lastScanKey, outputQualifiers, prefix);
	this.ed = ed;
	this.startTime = startTime.getTime();
	this.endTime = endTime.getTime();
	this.aggregateCondition = condition;
}
 
Example 9
Source Project: BigDataArchitect   Source File: LocationRunner.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Filter fetchHbaseFilter() {
    FilterList list = new FilterList();
    String[] columns = new String[] { EventLogConstants.LOG_COLUMN_NAME_PLATFORM, // 平台
            EventLogConstants.LOG_COLUMN_NAME_SERVER_TIME, // 服务器时间戳
            EventLogConstants.LOG_COLUMN_NAME_UUID, // 用户id
            EventLogConstants.LOG_COLUMN_NAME_SESSION_ID, // 会话id
            EventLogConstants.LOG_COLUMN_NAME_COUNTRY, // 国家
            EventLogConstants.LOG_COLUMN_NAME_PROVINCE, // 省份
            EventLogConstants.LOG_COLUMN_NAME_CITY, // 城市
            EventLogConstants.LOG_COLUMN_NAME_EVENT_NAME, // 事件名称
    };
    list.addFilter(this.getColumnFilter(columns));
    // 过滤只需要pageview事件
    list.addFilter(new SingleColumnValueFilter(LocationMapper.family, Bytes.toBytes(EventLogConstants.LOG_COLUMN_NAME_EVENT_NAME), CompareOp.EQUAL, Bytes.toBytes(EventEnum.PAGEVIEW.alias)));
    return list;
}
 
Example 10
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * With the leading row key col missing Phoenix won't be able to optimize
 * and provide the start row for the scan.
 * 
 * Table entity_history has the row key defined as (organization_id, parent_id, created_date, entity_history_id). 
 * This test uses (parent_id, entity_id) in RVC. Start row should be empty.
 * @throws SQLException
 */

@Test
public void testRVCExpressionWithoutLeadingColOfRowKey() throws SQLException {
    
    String parentId = "000000000000002";
    String entityHistId = "000000000000003";
    
    String query = "select * from entity_history where (parent_id, entity_history_id) >= (?,?)";
    List<Object> binds = Arrays.<Object>asList(parentId, entityHistId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNotNull(filter);
    assertTrue(filter instanceof RowKeyComparisonFilter);
    assertArrayEquals(HConstants.EMPTY_START_ROW, scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 11
Source Project: phoenix   Source File: WhereCompilerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInListFilter() throws SQLException {
    String tenantId1 = "000000000000001";
    String tenantId2 = "000000000000002";
    String tenantId3 = "000000000000003";
    String query = String.format("select * from %s where organization_id IN ('%s','%s','%s')",
            ATABLE_NAME, tenantId1, tenantId3, tenantId2);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId1);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = PVarchar.INSTANCE.toBytes(tenantId3);
    assertArrayEquals(ByteUtil.nextKey(stopRow), scan.getStopRow());

    Filter filter = scan.getFilter();
    assertEquals(
        new SkipScanFilter(
            ImmutableList.of(Arrays.asList(
                pointRange(tenantId1),
                pointRange(tenantId2),
                pointRange(tenantId3))),
            plan.getContext().getResolver().getTables().get(0).getTable().getRowKeySchema()),
        filter);
}
 
Example 12
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOrSameColRangeExpression() throws SQLException {
    String query = "select * from atable where substr(organization_id,1,3) = ? or organization_id LIKE 'foo%'";
    List<Object> binds = Arrays.<Object>asList("00D");
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();

    assertNotNull(filter);
    assertTrue(filter instanceof SkipScanFilter);
    ScanRanges scanRanges = context.getScanRanges();
    assertNotNull(scanRanges);
    List<List<KeyRange>> ranges = scanRanges.getRanges();
    assertEquals(1,ranges.size());
    List<List<KeyRange>> expectedRanges = Collections.singletonList(Arrays.asList(
            PChar.INSTANCE.getKeyRange(
                    StringUtil.padChar(PChar.INSTANCE.toBytes("00D"),15), true,
                    StringUtil.padChar(ByteUtil.nextKey(PChar.INSTANCE.toBytes("00D")),15), false),
            PChar.INSTANCE.getKeyRange(
                    StringUtil.padChar(PChar.INSTANCE.toBytes("foo"),15), true,
                    StringUtil.padChar(ByteUtil.nextKey(PChar.INSTANCE.toBytes("foo")),15), false)));
    assertEquals(expectedRanges, ranges);
}
 
Example 13
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTrailingIsNull() throws Exception {
    String baseTableDDL = "CREATE TABLE t(\n " + 
            "  a VARCHAR,\n" + 
            "  b VARCHAR,\n" + 
            "  CONSTRAINT pk PRIMARY KEY (a, b))";
    Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute(baseTableDDL);
    conn.close();
    
    String query = "SELECT * FROM t WHERE a = 'a' and b is null";
    StatementContext context = compileStatement(query, Collections.<Object>emptyList());
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    assertArrayEquals(Bytes.toBytes("a"), scan.getStartRow());
    assertArrayEquals(ByteUtil.concat(Bytes.toBytes("a"), QueryConstants.SEPARATOR_BYTE_ARRAY, QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow());
}
 
Example 14
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiRVCExpressionsCombinedWithAnd() throws SQLException {
    String lowerTenantId = "000000000000001";
    String lowerParentId = "000000000000002";
    Date lowerCreatedDate = new Date(System.currentTimeMillis());
    String upperTenantId = "000000000000008";
    String upperParentId = "000000000000009";
    
    String query = "select * from entity_history where (organization_id, parent_id, created_date) >= (?, ?, ?) AND (organization_id, parent_id) <= (?, ?)";
    List<Object> binds = Arrays.<Object>asList(lowerTenantId, lowerParentId, lowerCreatedDate, upperTenantId, upperParentId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    byte[] expectedStartRow = ByteUtil.concat(
        PVarchar.INSTANCE.toBytes(lowerTenantId), PVarchar.INSTANCE.toBytes(lowerParentId), PDate.INSTANCE.toBytes(lowerCreatedDate));
    byte[] expectedStopRow = ByteUtil.nextKey(ByteUtil.concat(PVarchar.INSTANCE.toBytes(upperTenantId), PVarchar.INSTANCE.toBytes(upperParentId)));
    assertArrayEquals(expectedStartRow, scan.getStartRow());
    assertArrayEquals(expectedStopRow, scan.getStopRow());
}
 
Example 15
@Test
public void testInListWithAnd1Filter() throws SQLException {
    String tenantId1 = "000000000000001";
    String tenantId2 = "000000000000002";
    String tenantId3 = "000000000000003";
    String entityId = "00000000000000X";
    String query = String.format("select * from %s where organization_id IN ('%s','%s','%s') AND entity_id='%s'",
            ATABLE_NAME, tenantId1, tenantId3, tenantId2, entityId);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), TEST_PROPERTIES).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertEquals(
        new SkipScanFilter(
            ImmutableList.of(
                Arrays.asList(
                    pointRange(tenantId1),
                    pointRange(tenantId2),
                    pointRange(tenantId3)),
                Arrays.asList(pointRange(entityId))),
            plan.getContext().getResolver().getTables().get(0).getTable().getRowKeySchema()),
        filter);
}
 
Example 16
Source Project: phoenix   Source File: WhereCompilerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testNotBetweenFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_integer not between 0 and 10";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertEquals(
            singleKVFilter(not(and(
                constantComparison(
                    CompareOp.GREATER_OR_EQUAL,
                    A_INTEGER,
                    0),
                constantComparison(
                    CompareOp.LESS_OR_EQUAL,
                    A_INTEGER,
                    10)))).toString(),
            filter.toString());
}
 
Example 17
Source Project: hbase   Source File: RegionCoprocessorHost.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Supports Coprocessor 'bypass'.
 * @param row row to check
 * @param filter filter
 * @param put data to put if check succeeds
 * @return true or false to return to client if default processing should be bypassed, or null
 * otherwise
 */
public Boolean preCheckAndPut(final byte [] row, final Filter filter, final Put put)
  throws IOException {
  boolean bypassable = true;
  boolean defaultResult = false;
  if (coprocEnvironments.isEmpty()) {
    return null;
  }
  return execOperationWithResult(
    new ObserverOperationWithResult<RegionObserver, Boolean>(regionObserverGetter,
      defaultResult, bypassable) {
      @Override
      public Boolean call(RegionObserver observer) throws IOException {
        return observer.preCheckAndPut(this, row, filter, put, getResult());
      }
    });
}
 
Example 18
Source Project: phoenix   Source File: WhereCompilerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiColumnEqualFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_string=b_string";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertEquals(
        multiEncodedKVFilter(columnComparison(
            CompareOp.EQUAL,
            A_STRING,
            B_STRING), TWO_BYTE_QUALIFIERS),
        filter);
}
 
Example 19
/**
 * Test filtering of KeyValues for in-progress and invalid transactions.
 * @throws Exception
 */
@Test
public void testFiltering() throws Exception {
  TxFilterFactory txFilterFactory = new TxFilterFactory() {
    @Override
    public Filter getTxFilter(Transaction tx, Map<byte[], Long> familyTTLs) {
      return new TransactionVisibilityFilter(tx, familyTTLs, false, ScanType.USER_SCAN);
    }
  };
  runFilteringTest(txFilterFactory,
                   ImmutableList.of(Filter.ReturnCode.INCLUDE_AND_NEXT_COL,
                                    Filter.ReturnCode.INCLUDE_AND_NEXT_COL,
                                    Filter.ReturnCode.SKIP,
                                    Filter.ReturnCode.SKIP,
                                    Filter.ReturnCode.INCLUDE_AND_NEXT_COL,
                                    Filter.ReturnCode.INCLUDE_AND_NEXT_COL));
}
 
Example 20
Source Project: Eagle   Source File: AbstractHBaseLogReader.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * <h2>History</h2>
 * <ul>
 * 	<li><b>Nov 19th, 2014</b>: Fix for out put all qualifiers</li>
 * </ul>
 * @param s1
 * @param filter
 */
protected void workaroundHBASE2198(Scan s1, Filter filter) {
	if (filter instanceof SingleColumnValueFilter) {
		if(this.qualifiers == null){
			s1.addFamily(((SingleColumnValueFilter) filter).getFamily());
		}else {
			s1.addColumn(((SingleColumnValueFilter) filter).getFamily(), ((SingleColumnValueFilter) filter).getQualifier());
		}
		return;
	}
	if (filter instanceof FilterList) {
		for (Filter f : ((FilterList)filter).getFilters()) {
			workaroundHBASE2198(s1, f);
		}
	}
}
 
Example 21
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOrDiffColExpression() throws SQLException {
    String tenantId1 = "000000000000001";
    String entityId1 = "002333333333331";
    String query = "select * from atable where organization_id = ? or entity_id  = ?";
    List<Object> binds = Arrays.<Object>asList(tenantId1,entityId1);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();

    assertNotNull(filter);
    assertTrue(filter instanceof RowKeyComparisonFilter);
    ScanRanges scanRanges = context.getScanRanges();
    assertEquals(ScanRanges.EVERYTHING,scanRanges);
    assertArrayEquals(HConstants.EMPTY_START_ROW, scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 22
Source Project: phoenix   Source File: WhereCompilerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRHSLiteral() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and 0 >= a_integer";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();

    Filter filter = scan.getFilter();
    assertEquals(
        singleKVFilter(constantComparison(
            CompareOp.LESS_OR_EQUAL,
            A_INTEGER,
            0)),
        filter);
}
 
Example 23
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTrailingIsNullWithOr() throws Exception {
    String baseTableDDL = "CREATE TABLE t(\n " + 
            "  a VARCHAR,\n" + 
            "  b VARCHAR,\n" + 
            "  CONSTRAINT pk PRIMARY KEY (a, b))";
    Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute(baseTableDDL);
    conn.close();
    
    String query = "SELECT * FROM t WHERE a = 'a' and (b is null or b = 'b')";
    StatementContext context = compileStatement(query, Collections.<Object>emptyList());
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertTrue(filter instanceof SkipScanFilter);
    SkipScanFilter skipScan = (SkipScanFilter)filter;
    List<List<KeyRange>>slots = skipScan.getSlots();
    assertEquals(2,slots.size());
    assertEquals(1,slots.get(0).size());
    assertEquals(2,slots.get(1).size());
    assertEquals(KeyRange.getKeyRange(Bytes.toBytes("a")), slots.get(0).get(0));
    assertTrue(KeyRange.IS_NULL_RANGE == slots.get(1).get(0));
    assertEquals(KeyRange.getKeyRange(Bytes.toBytes("b")), slots.get(1).get(1));
    assertArrayEquals(Bytes.toBytes("a"), scan.getStartRow());
    assertArrayEquals(ByteUtil.concat(Bytes.toBytes("a"), QueryConstants.SEPARATOR_BYTE_ARRAY, Bytes.toBytes("b"), QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow());
}
 
Example 24
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * With the leading row key col missing Phoenix won't be able to optimize
 * and provide the start row for the scan.
 * 
 * Table entity_history has the row key defined as (organization_id, parent_id, created_date, entity_history_id). 
 * This test uses (parent_id, entity_id) in RVC. Start row should be empty.
 * @throws SQLException
 */

@Test
public void testRVCExpressionWithoutLeadingColOfRowKey() throws SQLException {
    
    String parentId = "000000000000002";
    String entityHistId = "000000000000003";
    
    String query = "select * from entity_history where (parent_id, entity_history_id) >= (?,?)";
    List<Object> binds = Arrays.<Object>asList(parentId, entityHistId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNotNull(filter);
    assertTrue(filter instanceof RowKeyComparisonFilter);
    assertArrayEquals(HConstants.EMPTY_START_ROW, scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 25
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFullyQualifiedRVCWithNonTenantSpecificView() throws Exception {
	String baseTableDDL = "CREATE TABLE BASE_TABLE(\n " + 
            "  tenant_id VARCHAR(5) NOT NULL,\n" + 
            "  userid INTEGER NOT NULL,\n" + 
            "  username VARCHAR NOT NULL,\n" +
            "  col VARCHAR\n " + 
            "  CONSTRAINT pk PRIMARY KEY (tenant_id, userid, username))";
	Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute(baseTableDDL);
    conn.close();
    
    String viewDDL = "CREATE VIEW VIEWXYZ AS SELECT * FROM BASE_TABLE";
    conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute(viewDDL);
    
    String query = "SELECT * FROM VIEWXYZ WHERE (tenant_id, userid, username) IN ((?, ?, ?), (?, ?, ?))";
    List<Object> binds = Arrays.<Object>asList("tenantId", 1, "uname1", "tenantId", 2, "uname2");
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertEquals(SkipScanFilter.class, filter.getClass());
}
 
Example 26
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUseOfFunctionOnLHSInMiddleOfRVCForLTE() throws SQLException {
    String tenantId = "000000000000001";
    String parentId = "000000000000002";
    String subStringParentId = parentId.substring(0, 3);
    Date createdDate = new Date(System.currentTimeMillis());
    
    String query = "select * from entity_history where (organization_id, substr(parent_id, 1, 3), created_date) <= (?,?,?)";
    List<Object> binds = Arrays.<Object>asList(tenantId, subStringParentId, createdDate);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNotNull(filter);
    assertTrue(filter instanceof RowKeyComparisonFilter);
    byte[] expectedStopRow = ByteUtil.concat(
        PVarchar.INSTANCE.toBytes(tenantId), ByteUtil.nextKey(PVarchar.INSTANCE.toBytes(subStringParentId)));
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStartRow());
    assertArrayEquals(expectedStopRow, scan.getStopRow());
}
 
Example 27
Source Project: phoenix   Source File: WhereOptimizerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLikeExtractKeyExpression2() throws SQLException {
    String tenantId = "000000000000001";
    String keyPrefix = "002";
    String likeArg = keyPrefix + "_";
    String query = "select * from atable where organization_id = ? and entity_id  LIKE '" + likeArg + "'";
    List<Object> binds = Arrays.<Object>asList(tenantId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();

    Filter filter = scan.getFilter();
    assertNotNull(filter);
    assertEquals(
            rowKeyFilter(like(
                ENTITY_ID,
                likeArg,
                context)),
            filter);

    byte[] startRow = ByteUtil.concat(
        PVarchar.INSTANCE.toBytes(tenantId),StringUtil.padChar(PVarchar.INSTANCE.toBytes(keyPrefix),15));
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(
        PVarchar.INSTANCE.toBytes(tenantId),StringUtil.padChar(ByteUtil.nextKey(PVarchar.INSTANCE.toBytes(keyPrefix)),15));
    assertArrayEquals(stopRow, scan.getStopRow());
}
 
Example 28
Source Project: java-docs-samples   Source File: Filters.java    License: Apache License 2.0 5 votes vote down vote up
public static void filterLimitCellsPerRowOffset(
    String projectId, String instanceId, String tableId) {
  // A filter that skips the first 2 cells per row
  Filter filter = new ColumnPaginationFilter(Integer.MAX_VALUE, 2);
  Scan scan = new Scan().setFilter(filter);
  readWithFilter(projectId, instanceId, tableId, scan);
}
 
Example 29
Source Project: eagle   Source File: TestHBaseFilterBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Filter buildFilter(String query) throws EagleQueryParseException {
    ORExpression expression = new EagleQueryParser(query).parse();
    HBaseFilterBuilder builder = new HBaseFilterBuilder(ed, expression);
    Filter filterList = builder.buildFilters();
    LOG.info("\n" + expression + " \n=> " + filterList);
    return filterList;
}
 
Example 30
Source Project: hbase   Source File: MetaBrowser.java    License: Apache License 2.0 5 votes vote down vote up
private Scan buildScan() {
  final Scan metaScan = new Scan()
    .addFamily(HConstants.CATALOG_FAMILY)
    .readVersions(1)
    .setLimit((scanLimit != null ? scanLimit : SCAN_LIMIT_DEFAULT) + 1);
  if (scanStart != null) {
    metaScan.withStartRow(scanStart, false);
  }
  final Filter filter = buildScanFilter();
  if (filter != null) {
    metaScan.setFilter(filter);
  }
  return metaScan;
}