Java Code Examples for org.apache.phoenix.jdbc.PhoenixPreparedStatement#optimizeQuery()

The following examples show how to use org.apache.phoenix.jdbc.PhoenixPreparedStatement#optimizeQuery() . 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 testSingleEqualFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_integer=0";
    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);
}
 
Example 2
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testBetweenFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_integer 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(and(
                constantComparison(
                    CompareOp.GREATER_OR_EQUAL,
                    A_INTEGER,
                    0),
                constantComparison(
                    CompareOp.LESS_OR_EQUAL,
                    A_INTEGER,
                    10))),
            filter);
}
 
Example 3
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testInListWithAnd2Filter() throws SQLException {
    String tenantId1 = "000000000000001";
    String tenantId2 = "000000000000002";
    String entityId1 = "00000000000000X";
    String entityId2 = "00000000000000Y";
    String query = String.format("select * from %s where organization_id IN ('%s','%s') AND entity_id IN ('%s', '%s')",
            ATABLE_NAME, tenantId1, tenantId2, entityId1, entityId2);
    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(
        new SkipScanFilter(
                ImmutableList.<List<KeyRange>>of(ImmutableList.of(
                    pointRange(tenantId1, entityId1),
                    pointRange(tenantId1, entityId2),
                    pointRange(tenantId2, entityId1),
                    pointRange(tenantId2, entityId2))),
            SchemaUtil.VAR_BINARY_SCHEMA),
        filter);
}
 
Example 4
Source File: WhereCompilerTest.java    From phoenix with 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 5
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testInListWithAnd2FilterScanKey() throws SQLException {
    String tenantId1 = "000000000000001";
    String tenantId2 = "000000000000002";
    String tenantId3 = "000000000000003";
    String entityId1 = "00000000000000X";
    String entityId2 = "00000000000000Y";
    String query = String.format("select * from %s where organization_id IN ('%s','%s','%s') AND entity_id IN ('%s', '%s')",
            ATABLE_NAME, tenantId1, tenantId3, tenantId2, entityId1, entityId2);
    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 = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId1), PVarchar.INSTANCE.toBytes(entityId1));
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId3), PVarchar.INSTANCE.toBytes(entityId2));
    assertArrayEquals(ByteUtil.concat(stopRow, QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow());
    // TODO: validate scan ranges
}
 
Example 6
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleVariableFullPkSalted() throws SQLException {
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    pconn.createStatement().execute("CREATE TABLE t (k varchar(10) primary key, v varchar) SALT_BUCKETS=20");
    String query = "select * from t where k='a'";
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    byte[] key = new byte[2];
    PVarchar.INSTANCE.toBytes("a", key, 1);
    key[0] = SaltingUtil.getSaltingByte(key, 1, 1, 20);
    byte[] expectedStartKey = key;
    //lexicographically this is the next PK
    byte[] expectedEndKey = ByteUtil.concat(key,new byte[]{0});
    byte[] startKey = scan.getStartRow();
    byte[] stopKey = scan.getStopRow();
    assertTrue(Bytes.compareTo(expectedStartKey, startKey) == 0);
    assertTrue(Bytes.compareTo(expectedEndKey, stopKey) == 0);
}
 
Example 7
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void helpTestToNumberFilter(String toNumberClause, BigDecimal expectedDecimal) throws Exception {
        String tenantId = "000000000000001";
        String query = "select * from atable where organization_id='" + tenantId + "' and x_decimal >= " + toNumberClause;
        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.GREATER_OR_EQUAL,
                X_DECIMAL,
                expectedDecimal)),
            filter);
}
 
Example 8
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testInListWithAnd1FilterScankey() 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(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId1), PVarchar.INSTANCE.toBytes(entityId));
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId3), PVarchar.INSTANCE.toBytes(entityId));
    assertArrayEquals(ByteUtil.concat(stopRow, QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow());
    // TODO: validate scan ranges
}
 
Example 9
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleVariableFullPkSalted() throws SQLException {
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    pconn.createStatement().execute("CREATE TABLE t (k varchar primary key, v varchar) SALT_BUCKETS=20");
    String query = "select * from t where k='a'";
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertNull(filter);
    byte[] key = new byte[2];
    PVarchar.INSTANCE.toBytes("a", key, 1);
    key[0] = SaltingUtil.getSaltingByte(key, 1, 1, 20);
    byte[] expectedStartKey = key;
    byte[] expectedEndKey = ByteUtil.nextKey(ByteUtil.concat(key, QueryConstants.SEPARATOR_BYTE_ARRAY));
    byte[] startKey = scan.getStartRow();
    byte[] stopKey = scan.getStopRow();
    assertTrue(Bytes.compareTo(expectedStartKey, startKey) == 0);
    assertTrue(Bytes.compareTo(expectedEndKey, stopKey) == 0);
}
 
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: WhereCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testOrTrueFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and (a_integer=0 or 3>2)";
    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();
    assertNull(filter);
    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = startRow;
    assertArrayEquals(ByteUtil.nextKey(stopRow), scan.getStopRow());
}
 
Example 12
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testTenantConstraintsAddedToScanWithNullTenantTypeId() throws SQLException {
    String tenantId = "000000000000123";
    createTestTable(getUrl(), "create table base_table_for_tenant_filter_test (tenant_id char(15) not null, " +
            "id char(5) not null, a_integer integer, a_string varchar(100) constraint pk primary key (tenant_id, id)) multi_tenant=true");
    createTestTable(getUrl(tenantId), "create view tenant_filter_test (tenant_col integer) AS SELECT * FROM BASE_TABLE_FOR_TENANT_FILTER_TEST");
    
    String query = "select * from tenant_filter_test where a_integer=0 and a_string='foo'";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(tenantId), 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(
        multiKVFilter(and(
            constantComparison(
                CompareOp.EQUAL,
                A_INTEGER,
                0),
            constantComparison(
                CompareOp.EQUAL,
                A_STRING,
                "foo"))),
        filter);
    
    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = startRow;
    assertArrayEquals(ByteUtil.nextKey(stopRow), scan.getStopRow());
}
 
Example 13
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDegenerateRowKeyFilter() throws SQLException {
    String keyPrefix = "foobar";
    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();
    // Degenerate b/c "foobar" is more than 3 characters
    assertDegenerate(plan.getContext());
}
 
Example 14
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDegenerateBiggerThanMaxLengthVarchar() throws SQLException {
    byte[] tooBigValue = new byte[101];
    Arrays.fill(tooBigValue, (byte)50);
    String aString = (String) PVarchar.INSTANCE.toObject(tooBigValue);
    String query = "select * from atable where a_string=?";
    List<Object> binds = Arrays.<Object>asList(aString);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    bindParams(pstmt, binds);
    QueryPlan plan = pstmt.optimizeQuery();
    // Degenerate b/c a_string length is 100
    assertDegenerate(plan.getContext());
}
 
Example 15
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testInListWithAnd1GTEFilter() throws SQLException {
    String tenantId1 = "000000000000001";
    String tenantId2 = "000000000000002";
    String tenantId3 = "000000000000003";
    String entityId1 = "00000000000000X";
    String entityId2 = "00000000000000Y";
    String query = String.format("select * from %s where organization_id IN ('%s','%s','%s') AND entity_id>='%s' AND entity_id<='%s'",
            ATABLE_NAME, tenantId1, tenantId3, tenantId2, entityId1, entityId2);
    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(
        new SkipScanFilter(
            ImmutableList.of(
                Arrays.asList(
                    pointRange(tenantId1),
                    pointRange(tenantId2),
                    pointRange(tenantId3)),
                Arrays.asList(PChar.INSTANCE.getKeyRange(
                    Bytes.toBytes(entityId1),
                    true,
                    Bytes.toBytes(entityId2),
                    true))),
            plan.getTableRef().getTable().getRowKeySchema()),
        filter);
}
 
Example 16
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testSecondPkColInListFilter() throws SQLException {
    String tenantId = "000000000000001";
    String entityId1 = "00000000000000X";
    String entityId2 = "00000000000000Y";
    String query = String.format("select * from %s where organization_id='%s' AND entity_id IN ('%s','%s')",
            ATABLE_NAME, tenantId, entityId1, entityId2);
    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(tenantId + entityId1);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = PVarchar.INSTANCE.toBytes(tenantId + entityId2);
    assertArrayEquals(ByteUtil.concat(stopRow, QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow());

    Filter filter = scan.getFilter();

    assertEquals(
        new SkipScanFilter(
            ImmutableList.of(
                Arrays.asList(
                    pointRange(tenantId,entityId1),
                    pointRange(tenantId,entityId2))),
            SchemaUtil.VAR_BINARY_SCHEMA),
        filter);
}
 
Example 17
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testTenantConstraintsAddedToScan() throws SQLException {
    String tenantTypeId = "5678";
    String tenantId = "000000000000123";
    String url = getUrl(tenantId);
    createTestTable(getUrl(), "create table base_table_for_tenant_filter_test (tenant_id char(15) not null, type_id char(4) not null, " +
    		"id char(5) not null, a_integer integer, a_string varchar(100) constraint pk primary key (tenant_id, type_id, id)) multi_tenant=true");
    createTestTable(url, "create view tenant_filter_test (tenant_col integer) AS SELECT * FROM BASE_TABLE_FOR_TENANT_FILTER_TEST WHERE type_id= '" + tenantTypeId + "'");

    String query = "select * from tenant_filter_test where a_integer=0 and a_string='foo'";
    PhoenixConnection pconn = DriverManager.getConnection(url, 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();
    PTable table = plan.getTableRef().getTable();
    Expression aInteger = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_INTEGER").getPosition()).newColumnExpression();
    Expression aString = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_STRING").getPosition()).newColumnExpression();
    assertEquals(
        multiEncodedKVFilter(and(
            constantComparison(
                CompareOp.EQUAL,
                aInteger,
                0),
            constantComparison(
                CompareOp.EQUAL,
                aString,
                "foo")), TWO_BYTE_QUALIFIERS),
        filter);

    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId + tenantTypeId);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = startRow;
    assertArrayEquals(ByteUtil.nextKey(stopRow), scan.getStopRow());
}
 
Example 18
Source File: TestUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static QueryPlan getOptimizeQueryPlan(Connection conn, String sql) throws SQLException {
    PhoenixPreparedStatement statement = conn.prepareStatement(sql).unwrap(PhoenixPreparedStatement.class);
    QueryPlan queryPlan = statement.optimizeQuery(sql);
    queryPlan.iterator();
    return queryPlan;
}
 
Example 19
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiFixedFullPkSalted() 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 in (1,3)";
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    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[] startKey1 = key;
    
    key = new byte[PLong.INSTANCE.getByteSize() + 1];
    PLong.INSTANCE.toBytes(3L, key, 1);
    key[0] = SaltingUtil.getSaltingByte(key, 1, PLong.INSTANCE.getByteSize(), 20);
    byte[] startKey2 = key;
    
    byte[] startKey = scan.getStartRow();
    byte[] stopKey = scan.getStopRow();
    
    // Due to salting byte, the 1 key may be after the 3 key
    byte[] expectedStartKey;
    byte[] expectedEndKey;
    List<List<KeyRange>> expectedRanges = Collections.singletonList(
            Arrays.asList(KeyRange.getKeyRange(startKey1),
                          KeyRange.getKeyRange(startKey2)));
    if (Bytes.compareTo(startKey1, startKey2) > 0) {
        expectedStartKey = startKey2;
        expectedEndKey = startKey1;
        Collections.reverse(expectedRanges.get(0));
    } else {
        expectedStartKey = startKey1;
        expectedEndKey = startKey2;
    }
    assertEquals(0,startKey.length);
    assertEquals(0,stopKey.length);

    assertNotNull(filter);
    assertTrue(filter instanceof SkipScanFilter);
    SkipScanFilter skipScanFilter = (SkipScanFilter)filter;
    assertEquals(1,skipScanFilter.getSlots().size());
    assertEquals(2,skipScanFilter.getSlots().get(0).size());
    assertArrayEquals(expectedStartKey, skipScanFilter.getSlots().get(0).get(0).getLowerRange());
    assertArrayEquals(expectedEndKey, skipScanFilter.getSlots().get(0).get(1).getLowerRange());
    StatementContext context = plan.getContext();
    ScanRanges scanRanges = context.getScanRanges();
    List<List<KeyRange>> ranges = scanRanges.getRanges();
    assertEquals(expectedRanges, ranges);
}
 
Example 20
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiFixedFullPkSalted() 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 in (1,3)";
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    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[] startKey1 = key;

    key = new byte[PLong.INSTANCE.getByteSize() + 1];
    PLong.INSTANCE.toBytes(3L, key, 1);
    key[0] = SaltingUtil.getSaltingByte(key, 1, PLong.INSTANCE.getByteSize(), 20);
    byte[] startKey2 = key;

    byte[] startKey = scan.getStartRow();
    byte[] stopKey = scan.getStopRow();

    // Due to salting byte, the 1 key may be after the 3 key
    byte[] expectedStartKey;
    byte[] expectedEndKey;
    List<List<KeyRange>> expectedRanges = Collections.singletonList(
            Arrays.asList(KeyRange.getKeyRange(startKey1),
                          KeyRange.getKeyRange(startKey2)));
    if (Bytes.compareTo(startKey1, startKey2) > 0) {
        expectedStartKey = startKey2;
        expectedEndKey = startKey1;
        Collections.reverse(expectedRanges.get(0));
    } else {
        expectedStartKey = startKey1;
        expectedEndKey = startKey2;
    }
    assertEquals(0,startKey.length);
    assertEquals(0,stopKey.length);

    assertNotNull(filter);
    assertTrue(filter instanceof SkipScanFilter);
    SkipScanFilter skipScanFilter = (SkipScanFilter)filter;
    assertEquals(1,skipScanFilter.getSlots().size());
    assertEquals(2,skipScanFilter.getSlots().get(0).size());
    assertArrayEquals(expectedStartKey, skipScanFilter.getSlots().get(0).get(0).getLowerRange());
    assertArrayEquals(expectedEndKey, skipScanFilter.getSlots().get(0).get(1).getLowerRange());
    StatementContext context = plan.getContext();
    ScanRanges scanRanges = context.getScanRanges();
    List<List<KeyRange>> ranges = scanRanges.getRanges();
    assertEquals(expectedRanges, ranges);
}