Search in sources :

Example 6 with RowKeyComparisonFilter

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());
    }
}
Also used : MultiCQKeyValueComparisonFilter(org.apache.phoenix.filter.MultiCQKeyValueComparisonFilter) SingleCFCQKeyValueComparisonFilter(org.apache.phoenix.filter.SingleCFCQKeyValueComparisonFilter) MultiEncodedCQKeyValueComparisonFilter(org.apache.phoenix.filter.MultiEncodedCQKeyValueComparisonFilter) KeyValueExpressionVisitor(org.apache.phoenix.expression.visitor.KeyValueExpressionVisitor) PTable(org.apache.phoenix.schema.PTable) QualifierEncodingScheme(org.apache.phoenix.schema.PTable.QualifierEncodingScheme) MultiCFCQKeyValueComparisonFilter(org.apache.phoenix.filter.MultiCFCQKeyValueComparisonFilter) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) SingleCFCQKeyValueComparisonFilter(org.apache.phoenix.filter.SingleCFCQKeyValueComparisonFilter) SingleCQKeyValueComparisonFilter(org.apache.phoenix.filter.SingleCQKeyValueComparisonFilter) Filter(org.apache.hadoop.hbase.filter.Filter) MultiCQKeyValueComparisonFilter(org.apache.phoenix.filter.MultiCQKeyValueComparisonFilter) EncodedColumnsUtil.isPossibleToUseEncodedCQFilter(org.apache.phoenix.util.EncodedColumnsUtil.isPossibleToUseEncodedCQFilter) MultiEncodedCQKeyValueComparisonFilter(org.apache.phoenix.filter.MultiEncodedCQKeyValueComparisonFilter) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) AndExpression(org.apache.phoenix.expression.AndExpression) SingleCQKeyValueComparisonFilter(org.apache.phoenix.filter.SingleCQKeyValueComparisonFilter) Scan(org.apache.hadoop.hbase.client.Scan) ImmutableStorageScheme(org.apache.phoenix.schema.PTable.ImmutableStorageScheme) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) MultiCFCQKeyValueComparisonFilter(org.apache.phoenix.filter.MultiCFCQKeyValueComparisonFilter) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter)

Example 7 with RowKeyComparisonFilter

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());
}
Also used : RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) SkipScanFilter(org.apache.phoenix.filter.SkipScanFilter) Filter(org.apache.hadoop.hbase.filter.Filter) SingleKeyValueComparisonFilter(org.apache.phoenix.filter.SingleKeyValueComparisonFilter) TestUtil.rowKeyFilter(org.apache.phoenix.util.TestUtil.rowKeyFilter) Scan(org.apache.hadoop.hbase.client.Scan) PDate(org.apache.phoenix.schema.types.PDate) Date(java.sql.Date) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 8 with RowKeyComparisonFilter

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());
}
Also used : RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) SkipScanFilter(org.apache.phoenix.filter.SkipScanFilter) Filter(org.apache.hadoop.hbase.filter.Filter) SingleKeyValueComparisonFilter(org.apache.phoenix.filter.SingleKeyValueComparisonFilter) TestUtil.rowKeyFilter(org.apache.phoenix.util.TestUtil.rowKeyFilter) Scan(org.apache.hadoop.hbase.client.Scan) PDate(org.apache.phoenix.schema.types.PDate) Date(java.sql.Date) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 9 with RowKeyComparisonFilter

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());
}
Also used : RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) SkipScanFilter(org.apache.phoenix.filter.SkipScanFilter) Filter(org.apache.hadoop.hbase.filter.Filter) SingleKeyValueComparisonFilter(org.apache.phoenix.filter.SingleKeyValueComparisonFilter) TestUtil.rowKeyFilter(org.apache.phoenix.util.TestUtil.rowKeyFilter) Scan(org.apache.hadoop.hbase.client.Scan) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 10 with RowKeyComparisonFilter

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());
}
Also used : RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) SkipScanFilter(org.apache.phoenix.filter.SkipScanFilter) Filter(org.apache.hadoop.hbase.filter.Filter) SingleKeyValueComparisonFilter(org.apache.phoenix.filter.SingleKeyValueComparisonFilter) TestUtil.rowKeyFilter(org.apache.phoenix.util.TestUtil.rowKeyFilter) Scan(org.apache.hadoop.hbase.client.Scan) PDate(org.apache.phoenix.schema.types.PDate) Date(java.sql.Date) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

Scan (org.apache.hadoop.hbase.client.Scan)11 Filter (org.apache.hadoop.hbase.filter.Filter)11 RowKeyComparisonFilter (org.apache.phoenix.filter.RowKeyComparisonFilter)11 SkipScanFilter (org.apache.phoenix.filter.SkipScanFilter)10 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)10 Test (org.junit.Test)10 SingleKeyValueComparisonFilter (org.apache.phoenix.filter.SingleKeyValueComparisonFilter)9 TestUtil.rowKeyFilter (org.apache.phoenix.util.TestUtil.rowKeyFilter)9 Date (java.sql.Date)4 PDate (org.apache.phoenix.schema.types.PDate)4 Expression (org.apache.phoenix.expression.Expression)2 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)2 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)2 AndExpression (org.apache.phoenix.expression.AndExpression)1 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)1 SubstrFunction (org.apache.phoenix.expression.function.SubstrFunction)1 KeyValueExpressionVisitor (org.apache.phoenix.expression.visitor.KeyValueExpressionVisitor)1 MultiCFCQKeyValueComparisonFilter (org.apache.phoenix.filter.MultiCFCQKeyValueComparisonFilter)1 MultiCQKeyValueComparisonFilter (org.apache.phoenix.filter.MultiCQKeyValueComparisonFilter)1 MultiEncodedCQKeyValueComparisonFilter (org.apache.phoenix.filter.MultiEncodedCQKeyValueComparisonFilter)1