Java Code Examples for org.apache.hadoop.hbase.client.Scan#getFilter()

The following examples show how to use org.apache.hadoop.hbase.client.Scan#getFilter() . 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 File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleFixedFullPkSalted() throws SQLException {
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    pconn.createStatement().execute("CREATE TABLE t (k bigint not null primary key, v varchar) SALT_BUCKETS=20");
    String query = "select * from t where k=" + 1;
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    byte[] key = new byte[PLong.INSTANCE.getByteSize() + 1];
    PLong.INSTANCE.toBytes(1L, key, 1);
    key[0] = SaltingUtil.getSaltingByte(key, 1, PLong.INSTANCE.getByteSize(), 20);
    byte[] expectedStartKey = key;
    byte[] expectedEndKey = ByteUtil.nextKey(key);
    byte[] startKey = scan.getStartRow();
    byte[] stopKey = scan.getStopRow();
    assertArrayEquals(expectedStartKey, startKey);
    assertArrayEquals(expectedEndKey, stopKey);
}
 
Example 2
Source File: WhereOptimizerTest.java    From phoenix with 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 3
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testRowKeyFilter() throws SQLException {
    String keyPrefix = "foo";
    String query = "select * from atable where substr(entity_id,1,3)=?";
    List<Object> binds = Arrays.<Object>asList(keyPrefix);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    bindParams(pstmt, binds);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();

    assertEquals(
        new RowKeyComparisonFilter(
            constantComparison(CompareOp.EQUAL,
                new SubstrFunction(
                    Arrays.<Expression>asList(
                        new RowKeyColumnExpression(ENTITY_ID,new RowKeyValueAccessor(ATABLE.getPKColumns(),1)),
                        LiteralExpression.newConstant(1),
                        LiteralExpression.newConstant(3))
                    ),
                keyPrefix),
            QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES),
        filter);
}
 
Example 4
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testUseOfFunctionOnLHSInMiddleOfRVC() 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[] expectedStartRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId), PVarchar.INSTANCE.toBytes(subStringParentId));
    assertArrayEquals(expectedStartRow, scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 5
Source File: WhereOptimizerTest.java    From phoenix with 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 6
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiRVCExpressionsCombinedUsingLiteralExpressions() throws SQLException {
    String lowerTenantId = "000000000000001";
    String lowerParentId = "000000000000002";
    Date lowerCreatedDate = new Date(System.currentTimeMillis());
    
    String query = "select * from entity_history where (organization_id, parent_id, created_date) >= (?, ?, ?) AND (organization_id, parent_id) <= ('7', '7')";
    List<Object> binds = Arrays.<Object>asList(lowerTenantId, lowerParentId, lowerCreatedDate);
    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(StringUtil.padChar(PVarchar.INSTANCE.toBytes("7"),15), StringUtil.padChar(
        PVarchar.INSTANCE.toBytes("7"), 15)));
    assertArrayEquals(expectedStartRow, scan.getStartRow());
    assertArrayEquals(expectedStopRow, scan.getStopRow());
}
 
Example 7
Source File: WhereOptimizerTest.java    From phoenix with 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 8
Source File: WhereClauseCompileTest.java    From phoenix with BSD 3-Clause "New" or "Revised" License 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(), 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(constantComparison(
            CompareOp.EQUAL,
            BaseConnectionlessQueryTest.A_INTEGER,
            0)),
        filter);
    byte[] startRow = PDataType.VARCHAR.toBytes(tenantId);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = startRow;
    assertArrayEquals(ByteUtil.nextKey(stopRow), scan.getStopRow());
}
 
Example 9
Source File: WhereClauseCompileTest.java    From phoenix with BSD 3-Clause "New" or "Revised" License 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(), 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 10
Source File: WhereCompilerTest.java    From phoenix with 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 11
Source File: WhereClauseCompileTest.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testToDateFilter() throws Exception {
    String tenantId = "000000000000001";
    String dateStr = "2012-01-01 12:00:00";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_date >= to_date('" + dateStr + "')";
    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();

    Format format = DateUtil.getDateParser(DateUtil.DEFAULT_DATE_FORMAT);
    Object date = format.parseObject(dateStr);

    assertEquals(
        singleKVFilter(constantComparison(
            CompareOp.GREATER_OR_EQUAL,
            BaseConnectionlessQueryTest.A_DATE,
            date)),
        filter);
}
 
Example 12
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testToDateFilter() throws Exception {
    String tenantId = "000000000000001";
    String dateStr = "2012-01-01 12:00:00";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_date >= to_date('" + dateStr + "')";
    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();

    Date date = DateUtil.parseDate(dateStr);

    assertEquals(
        singleKVFilter(constantComparison(
            CompareOp.GREATER_OR_EQUAL,
            A_DATE,
            date)),
        filter);
}
 
Example 13
Source File: WhereOptimizerTest.java    From phoenix with 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 14
Source File: WhereOptimizerTest.java    From phoenix with 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 15
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCombiningRVCWithNonRVCUsingOr2() throws SQLException {
    String firstTenantId = "000000000000001";
    String secondTenantId = "000000000000005";
    String firstParentId = "000000000000011";
    
    String query = "select * from entity_history where (organization_id, parent_id) >= (?,?) OR organization_id  <= ?";
    List<Object> binds = Arrays.<Object>asList(firstTenantId, firstParentId, secondTenantId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    assertArrayEquals(HConstants.EMPTY_START_ROW, scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 16
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testGreaterThanEqualTo_NonRVCOnLHSAndRVCOnRHS_WithNonNullBindParams() throws SQLException {
    String tenantId = "000000000000001";
    String parentId = "000000000000008";
    
    String query = "select * from entity_history where organization_id >= (?,?)";
    List<Object> binds = Arrays.<Object>asList(tenantId, parentId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    assertArrayEquals(ByteUtil.nextKey(PVarchar.INSTANCE.toBytes(tenantId)), scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 17
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsingRVCNonFullyQualifiedInClause() throws Exception {
    String firstOrgId = "000000000000001";
    String secondOrgId = "000000000000009";
    String firstParentId = "000000000000011";
    String secondParentId = "000000000000021";
    String query = "select * from entity_history where (organization_id, parent_id) IN ((?, ?), (?, ?))";
    List<Object> binds = Arrays.<Object>asList(firstOrgId, firstParentId, secondOrgId, secondParentId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertTrue(filter instanceof SkipScanFilter);
    assertArrayEquals(ByteUtil.concat(PVarchar.INSTANCE.toBytes(firstOrgId), PVarchar.INSTANCE.toBytes(firstParentId)), scan.getStartRow());
    assertArrayEquals(ByteUtil.nextKey(ByteUtil.concat(PVarchar.INSTANCE.toBytes(secondOrgId), PVarchar.INSTANCE.toBytes(secondParentId))), scan.getStopRow());
}
 
Example 18
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testGreaterThanEqualTo_NonRVCOnLHSAndRVCOnRHS_WithNonNullBindParams() throws SQLException {
    String tenantId = "000000000000001";
    String parentId = "000000000000008";
    
    String query = "select * from entity_history where organization_id >= (?,?)";
    List<Object> binds = Arrays.<Object>asList(tenantId, parentId);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    assertArrayEquals(ByteUtil.nextKey(PVarchar.INSTANCE.toBytes(tenantId)), scan.getStartRow());
    assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
 
Example 19
Source File: StatementHintsCompilationTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static boolean usingSkipScan(Scan scan) {
    Filter filter = scan.getFilter();
    if (filter instanceof FilterList) {
        FilterList filterList = (FilterList) filter;
        for (Filter childFilter : filterList.getFilters()) {
            if (childFilter instanceof SkipScanFilter) {
                return true;
            }
        }
        return false;
    }
    return filter instanceof SkipScanFilter;
}
 
Example 20
Source File: ScannerModel.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * @param scan the scan specification
 * @throws Exception
 */
public static ScannerModel fromScan(Scan scan) throws Exception {
  ScannerModel model = new ScannerModel();
  model.setStartRow(scan.getStartRow());
  model.setEndRow(scan.getStopRow());
  Map<byte [], NavigableSet<byte []>> families = scan.getFamilyMap();
  if (families != null) {
    for (Map.Entry<byte [], NavigableSet<byte []>> entry : families.entrySet()) {
      if (entry.getValue() != null) {
        for (byte[] qualifier: entry.getValue()) {
          model.addColumn(Bytes.add(entry.getKey(), COLUMN_DIVIDER, qualifier));
        }
      } else {
        model.addColumn(entry.getKey());
      }
    }
  }
  model.setStartTime(scan.getTimeRange().getMin());
  model.setEndTime(scan.getTimeRange().getMax());
  int caching = scan.getCaching();
  if (caching > 0) {
    model.setCaching(caching);
  }
  int batch = scan.getBatch();
  if (batch > 0) {
    model.setBatch(batch);
  }
  int maxVersions = scan.getMaxVersions();
  if (maxVersions > 0) {
    model.setMaxVersions(maxVersions);
  }
  if (scan.getLimit() > 0) {
    model.setLimit(scan.getLimit());
  }
  Filter filter = scan.getFilter();
  if (filter != null) {
    model.setFilter(stringifyFilter(filter));
  }
  // Add the visbility labels if found in the attributes
  Authorizations authorizations = scan.getAuthorizations();
  if (authorizations != null) {
    List<String> labels = authorizations.getLabels();
    for (String label : labels) {
      model.addLabel(label);
    }
  }
  return model;
}