use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.
the class WhereCompilerTest method testMultiFixedFullPkSalted.
@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);
}
use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.
the class WhereCompilerTest method testInListFilter.
@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.getTableRef().getTable().getRowKeySchema()), filter);
}
use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.
the class WhereCompilerTest method testInListWithAnd1FilterScankey.
@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
}
use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.
the class WhereCompilerTest method newPreparedStatement.
private PhoenixPreparedStatement newPreparedStatement(PhoenixConnection pconn, String query) throws SQLException {
PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
assertRoundtrip(query);
return pstmt;
}
use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.
the class WhereCompilerTest method testToDateFilter.
@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);
}
Aggregations