use of org.apache.phoenix.filter.RowKeyComparisonFilter in project phoenix by apache.
the class WhereCompiler method setScanFilter.
/**
* Sets the start/stop key range based on the whereClause expression.
* @param context the shared context during query compilation
* @param whereClause the final where clause expression.
*/
private static void setScanFilter(StatementContext context, FilterableStatement statement, Expression whereClause, boolean disambiguateWithFamily, boolean hashJoinOptimization) {
Scan scan = context.getScan();
if (LiteralExpression.isBooleanFalseOrNull(whereClause)) {
context.setScanRanges(ScanRanges.NOTHING);
} else if (whereClause != null && !ExpressionUtil.evaluatesToTrue(whereClause) && !hashJoinOptimization) {
Filter filter = null;
final Counter counter = new Counter();
whereClause.accept(new KeyValueExpressionVisitor() {
@Override
public Iterator<Expression> defaultIterator(Expression node) {
// Stop traversal once we've found multiple KeyValue columns
if (counter.getCount() == Counter.Count.MULTIPLE) {
return Iterators.emptyIterator();
}
return super.defaultIterator(node);
}
@Override
public Void visit(KeyValueColumnExpression expression) {
counter.increment(expression);
return null;
}
});
PTable table = context.getCurrentTable().getTable();
QualifierEncodingScheme encodingScheme = table.getEncodingScheme();
ImmutableStorageScheme storageScheme = table.getImmutableStorageScheme();
Counter.Count count = counter.getCount();
boolean allCFs = false;
byte[] essentialCF = null;
if (counter.getCount() == Counter.Count.SINGLE && whereClause.requiresFinalEvaluation()) {
if (table.getViewType() == ViewType.MAPPED) {
allCFs = true;
} else {
byte[] emptyCF = SchemaUtil.getEmptyColumnFamily(table);
if (Bytes.compareTo(emptyCF, counter.getColumn().getColumnFamily()) != 0) {
essentialCF = emptyCF;
count = Counter.Count.MULTIPLE;
}
}
}
switch(count) {
case NONE:
essentialCF = table.getType() == PTableType.VIEW ? ByteUtil.EMPTY_BYTE_ARRAY : SchemaUtil.getEmptyColumnFamily(table);
filter = new RowKeyComparisonFilter(whereClause, essentialCF);
break;
case SINGLE:
filter = disambiguateWithFamily ? new SingleCFCQKeyValueComparisonFilter(whereClause) : new SingleCQKeyValueComparisonFilter(whereClause);
break;
case MULTIPLE:
filter = isPossibleToUseEncodedCQFilter(encodingScheme, storageScheme) ? new MultiEncodedCQKeyValueComparisonFilter(whereClause, encodingScheme, allCFs, essentialCF) : (disambiguateWithFamily ? new MultiCFCQKeyValueComparisonFilter(whereClause, allCFs, essentialCF) : new MultiCQKeyValueComparisonFilter(whereClause, allCFs, essentialCF));
break;
}
scan.setFilter(filter);
}
ScanRanges scanRanges = context.getScanRanges();
if (scanRanges.useSkipScanFilter()) {
ScanUtil.andFilterAtBeginning(scan, scanRanges.getSkipScanFilter());
}
}
use of org.apache.phoenix.filter.RowKeyComparisonFilter in project phoenix by apache.
the class WhereOptimizerTest method testUseOfFunctionOnLHSInMiddleOfRVC.
@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());
}
use of org.apache.phoenix.filter.RowKeyComparisonFilter in project phoenix by apache.
the class WhereOptimizerTest method testUseOfFunctionOnLHSInMiddleOfRVCForLTE.
@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());
}
use of org.apache.phoenix.filter.RowKeyComparisonFilter in project phoenix by apache.
the class WhereOptimizerTest method testCombiningRVCWithNonRVCUsingOr3.
@Test
public void testCombiningRVCWithNonRVCUsingOr3() throws SQLException {
String firstTenantId = "000000000000005";
String secondTenantId = "000000000000001";
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();
assertTrue(filter instanceof RowKeyComparisonFilter);
assertArrayEquals(HConstants.EMPTY_START_ROW, scan.getStartRow());
assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow());
}
use of org.apache.phoenix.filter.RowKeyComparisonFilter in project phoenix by apache.
the class WhereOptimizerTest method testForceRangeScanKeepsFilters.
@Test
public void testForceRangeScanKeepsFilters() throws SQLException {
ensureTableCreated(getUrl(), TestUtil.ENTITY_HISTORY_TABLE_NAME, TestUtil.ENTITY_HISTORY_TABLE_NAME);
String tenantId = "000000000000001";
String keyPrefix = "002";
String query = "select /*+ RANGE_SCAN */ ORGANIZATION_ID, PARENT_ID, CREATED_DATE, ENTITY_HISTORY_ID from " + TestUtil.ENTITY_HISTORY_TABLE_NAME + " where ORGANIZATION_ID=? and SUBSTR(PARENT_ID, 1, 3) = ? and CREATED_DATE >= ? and CREATED_DATE < ? order by ORGANIZATION_ID, PARENT_ID, CREATED_DATE, ENTITY_HISTORY_ID limit 6";
Date startTime = new Date(System.currentTimeMillis());
Date stopTime = new Date(startTime.getTime() + MILLIS_IN_DAY);
List<Object> binds = Arrays.<Object>asList(tenantId, keyPrefix, startTime, stopTime);
StatementContext context = compileStatement(query, binds, 6);
Scan scan = context.getScan();
Filter filter = scan.getFilter();
assertNotNull(filter);
assertTrue(filter instanceof RowKeyComparisonFilter);
byte[] expectedStartRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId), StringUtil.padChar(PVarchar.INSTANCE.toBytes(keyPrefix), 15), PDate.INSTANCE.toBytes(startTime));
assertArrayEquals(expectedStartRow, scan.getStartRow());
byte[] expectedStopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId), StringUtil.padChar(ByteUtil.nextKey(PVarchar.INSTANCE.toBytes(keyPrefix)), 15));
assertArrayEquals(expectedStopRow, scan.getStopRow());
}
Aggregations