Repository: drill Updated Branches: refs/heads/master 9b351c945 -> 66a30f12b
DRILL-1651: Push filter past project with ITEM operator into HBase scan. Project: http://git-wip-us.apache.org/repos/asf/drill/repo Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/66a30f12 Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/66a30f12 Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/66a30f12 Branch: refs/heads/master Commit: 66a30f12bfa033200e5ac9ca5c05c1c5a31c720b Parents: 9b351c9 Author: Jinfeng Ni <[email protected]> Authored: Fri Jul 17 14:53:13 2015 -0700 Committer: Jinfeng Ni <[email protected]> Committed: Tue Jul 21 13:00:16 2015 -0700 ---------------------------------------------------------------------- .../store/hbase/HBasePushFilterIntoScan.java | 126 +++++++---- .../exec/store/hbase/HBaseStoragePlugin.java | 2 +- .../drill/hbase/TestHBaseFilterPushDown.java | 215 ++++++++++++++++--- 3 files changed, 278 insertions(+), 65 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/drill/blob/66a30f12/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBasePushFilterIntoScan.java ---------------------------------------------------------------------- diff --git a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBasePushFilterIntoScan.java b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBasePushFilterIntoScan.java index cd97b59..a1cd9c9 100644 --- a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBasePushFilterIntoScan.java +++ b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBasePushFilterIntoScan.java @@ -18,12 +18,16 @@ package org.apache.drill.exec.store.hbase; +import org.apache.calcite.plan.RelOptRuleOperand; +import org.apache.calcite.plan.RelOptUtil; import org.apache.drill.common.expression.LogicalExpression; import org.apache.drill.exec.planner.logical.DrillOptiq; import org.apache.drill.exec.planner.logical.DrillParseContext; import org.apache.drill.exec.planner.logical.RelOptHelper; import org.apache.drill.exec.planner.physical.FilterPrel; +import org.apache.drill.exec.planner.physical.Prel; import org.apache.drill.exec.planner.physical.PrelUtil; +import org.apache.drill.exec.planner.physical.ProjectPrel; import org.apache.drill.exec.planner.physical.ScanPrel; import org.apache.drill.exec.store.StoragePluginOptimizerRule; import org.apache.calcite.rel.RelNode; @@ -32,33 +36,86 @@ import org.apache.calcite.rex.RexNode; import com.google.common.collect.ImmutableList; -public class HBasePushFilterIntoScan extends StoragePluginOptimizerRule { - public static final StoragePluginOptimizerRule INSTANCE = new HBasePushFilterIntoScan(); +public abstract class HBasePushFilterIntoScan extends StoragePluginOptimizerRule { - private HBasePushFilterIntoScan() { - super(RelOptHelper.some(FilterPrel.class, RelOptHelper.any(ScanPrel.class)), "HBasePushFilterIntoScan"); + private HBasePushFilterIntoScan(RelOptRuleOperand operand, String description) { + super(operand, description); } - @Override - public void onMatch(RelOptRuleCall call) { - final ScanPrel scan = (ScanPrel) call.rel(1); - final FilterPrel filter = (FilterPrel) call.rel(0); - final RexNode condition = filter.getCondition(); - - HBaseGroupScan groupScan = (HBaseGroupScan)scan.getGroupScan(); - if (groupScan.isFilterPushedDown()) { - /* - * The rule can get triggered again due to the transformed "scan => filter" sequence - * created by the earlier execution of this rule when we could not do a complete - * conversion of Optiq Filter's condition to HBase Filter. In such cases, we rely upon - * this flag to not do a re-processing of the rule on the already transformed call. - */ - return; + public static final StoragePluginOptimizerRule FILTER_ON_SCAN = new HBasePushFilterIntoScan(RelOptHelper.some(FilterPrel.class, RelOptHelper.any(ScanPrel.class)), "HBasePushFilterIntoScan:Filter_On_Scan") { + + @Override + public void onMatch(RelOptRuleCall call) { + final ScanPrel scan = (ScanPrel) call.rel(1); + final FilterPrel filter = (FilterPrel) call.rel(0); + final RexNode condition = filter.getCondition(); + + HBaseGroupScan groupScan = (HBaseGroupScan)scan.getGroupScan(); + if (groupScan.isFilterPushedDown()) { + /* + * The rule can get triggered again due to the transformed "scan => filter" sequence + * created by the earlier execution of this rule when we could not do a complete + * conversion of Optiq Filter's condition to HBase Filter. In such cases, we rely upon + * this flag to not do a re-processing of the rule on the already transformed call. + */ + return; + } + + doPushFilterToScan(call, filter, null, scan, groupScan, condition); } - LogicalExpression conditionExp = DrillOptiq.toDrill(new DrillParseContext(PrelUtil.getPlannerSettings(call.getPlanner())), scan, condition); - HBaseFilterBuilder hbaseFilterBuilder = new HBaseFilterBuilder(groupScan, conditionExp); - HBaseScanSpec newScanSpec = hbaseFilterBuilder.parseTree(); + @Override + public boolean matches(RelOptRuleCall call) { + final ScanPrel scan = (ScanPrel) call.rel(1); + if (scan.getGroupScan() instanceof HBaseGroupScan) { + return super.matches(call); + } + return false; + } + }; + + + public static final StoragePluginOptimizerRule FILTER_ON_PROJECT = new HBasePushFilterIntoScan(RelOptHelper.some(FilterPrel.class, RelOptHelper.some(ProjectPrel.class, RelOptHelper.any(ScanPrel.class))), "HBasePushFilterIntoScan:Filter_On_Project") { + + @Override + public void onMatch(RelOptRuleCall call) { + final ScanPrel scan = (ScanPrel) call.rel(2); + final ProjectPrel project = (ProjectPrel) call.rel(1); + final FilterPrel filter = (FilterPrel) call.rel(0); + + HBaseGroupScan groupScan = (HBaseGroupScan)scan.getGroupScan(); + if (groupScan.isFilterPushedDown()) { + /* + * The rule can get triggered again due to the transformed "scan => filter" sequence + * created by the earlier execution of this rule when we could not do a complete + * conversion of Optiq Filter's condition to HBase Filter. In such cases, we rely upon + * this flag to not do a re-processing of the rule on the already transformed call. + */ + return; + } + + // convert the filter to one that references the child of the project + final RexNode condition = RelOptUtil.pushFilterPastProject(filter.getCondition(), project); + + doPushFilterToScan(call, filter, project, scan, groupScan, condition); + } + + @Override + public boolean matches(RelOptRuleCall call) { + final ScanPrel scan = (ScanPrel) call.rel(2); + if (scan.getGroupScan() instanceof HBaseGroupScan) { + return super.matches(call); + } + return false; + } + }; + + + protected void doPushFilterToScan(final RelOptRuleCall call, final FilterPrel filter, final ProjectPrel project, final ScanPrel scan, final HBaseGroupScan groupScan, final RexNode condition) { + + final LogicalExpression conditionExp = DrillOptiq.toDrill(new DrillParseContext(PrelUtil.getPlannerSettings(call.getPlanner())), scan, condition); + final HBaseFilterBuilder hbaseFilterBuilder = new HBaseFilterBuilder(groupScan, conditionExp); + final HBaseScanSpec newScanSpec = hbaseFilterBuilder.parseTree(); if (newScanSpec == null) { return; //no filter pushdown ==> No transformation. } @@ -68,24 +125,19 @@ public class HBasePushFilterIntoScan extends StoragePluginOptimizerRule { newGroupsScan.setFilterPushedDown(true); final ScanPrel newScanPrel = ScanPrel.create(scan, filter.getTraitSet(), newGroupsScan, scan.getRowType()); + + // Depending on whether is a project in the middle, assign either scan or copy of project to childRel. + final RelNode childRel = project == null ? newScanPrel : project.copy(project.getTraitSet(), ImmutableList.of((RelNode)newScanPrel));; + if (hbaseFilterBuilder.isAllExpressionsConverted()) { - /* - * Since we could convert the entire filter condition expression into an HBase filter, - * we can eliminate the filter operator altogether. - */ - call.transformTo(newScanPrel); + /* + * Since we could convert the entire filter condition expression into an HBase filter, + * we can eliminate the filter operator altogether. + */ + call.transformTo(childRel); } else { - call.transformTo(filter.copy(filter.getTraitSet(), ImmutableList.of((RelNode)newScanPrel))); - } - } - - @Override - public boolean matches(RelOptRuleCall call) { - final ScanPrel scan = (ScanPrel) call.rel(1); - if (scan.getGroupScan() instanceof HBaseGroupScan) { - return super.matches(call); + call.transformTo(filter.copy(filter.getTraitSet(), ImmutableList.of(childRel))); } - return false; } } http://git-wip-us.apache.org/repos/asf/drill/blob/66a30f12/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java ---------------------------------------------------------------------- diff --git a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java index 7737f69..e0f2eae 100644 --- a/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java +++ b/contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseStoragePlugin.java @@ -77,7 +77,7 @@ public class HBaseStoragePlugin extends AbstractStoragePlugin { @Override public Set<StoragePluginOptimizerRule> getOptimizerRules() { - return ImmutableSet.of(HBasePushFilterIntoScan.INSTANCE); + return ImmutableSet.of(HBasePushFilterIntoScan.FILTER_ON_SCAN, HBasePushFilterIntoScan.FILTER_ON_PROJECT); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/drill/blob/66a30f12/contrib/storage-hbase/src/test/java/org/apache/drill/hbase/TestHBaseFilterPushDown.java ---------------------------------------------------------------------- diff --git a/contrib/storage-hbase/src/test/java/org/apache/drill/hbase/TestHBaseFilterPushDown.java b/contrib/storage-hbase/src/test/java/org/apache/drill/hbase/TestHBaseFilterPushDown.java index ca4c07c..7abcb29 100644 --- a/contrib/storage-hbase/src/test/java/org/apache/drill/hbase/TestHBaseFilterPushDown.java +++ b/contrib/storage-hbase/src/test/java/org/apache/drill/hbase/TestHBaseFilterPushDown.java @@ -17,6 +17,7 @@ */ package org.apache.drill.hbase; +import org.apache.drill.PlanTestBase; import org.junit.Test; public class TestHBaseFilterPushDown extends BaseHBaseTest { @@ -24,97 +25,233 @@ public class TestHBaseFilterPushDown extends BaseHBaseTest { @Test public void testFilterPushDownRowKeyEqual() throws Exception { setColumnWidths(new int[] {8, 38, 38}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " *\n" + "FROM\n" + " hbase.`[TABLE_NAME]` tableName\n" + "WHERE\n" - + " row_key = 'b4'" - , 1); + + " row_key = 'b4'"; + + runHBaseSQLVerifyCount(sql, 1); + + final String[] expectedPlan = {".*startRow=b4, stopRow=b4.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); + } + + @Test + public void testFilterPushDownRowKeyEqualWithItem() throws Exception { + setColumnWidths(new int[] {20, 30}); + final String sql = "SELECT\n" + + " cast(tableName.row_key as varchar(20)), cast(tableName.f.c1 as varchar(30))\n" + + "FROM\n" + + " hbase.`[TABLE_NAME]` tableName\n" + + "WHERE\n" + + " row_key = 'b4'"; + + runHBaseSQLVerifyCount(sql, 1); + + final String[] expectedPlan = {".*startRow=b4, stopRow=b4.*"}; + final String[] excludedPlan ={".*startRow=null, stopRow=null.*"}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } @Test public void testFilterPushDownRowKeyLike() throws Exception { setColumnWidths(new int[] {8, 22}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " row_key, convert_from(tableName.f.c, 'UTF8') `f.c`\n" + "FROM\n" + " hbase.`TestTable3` tableName\n" + "WHERE\n" - + " row_key LIKE '08%0' OR row_key LIKE '%70'" - , 21); + + " row_key LIKE '08%0' OR row_key LIKE '%70'"; + + runHBaseSQLVerifyCount(sql, 21); + + final String[] expectedPlan = {".*filter=FilterList OR.*EQUAL.*EQUAL.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } @Test public void testFilterPushDownRowKeyLikeWithEscape() throws Exception { setColumnWidths(new int[] {8, 22}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " row_key, convert_from(tableName.f.c, 'UTF8') `f.c`\n" + "FROM\n" + " hbase.`TestTable3` tableName\n" + "WHERE\n" - + " row_key LIKE '!%!_AS!_PREFIX!_%' ESCAPE '!'" - , 2); + + " row_key LIKE '!%!_AS!_PREFIX!_%' ESCAPE '!'"; + + runHBaseSQLVerifyCount(sql, 2); + + final String[] expectedPlan = {".*startRow=\\%_AS_PREFIX_, stopRow=\\%_AS_PREFIX`, filter=RowFilter.*EQUAL.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } @Test public void testFilterPushDownRowKeyRangeAndColumnValueLike() throws Exception { setColumnWidths(new int[] {8, 22}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " row_key, convert_from(tableName.f.c, 'UTF8') `f.c`\n" + "FROM\n" + " hbase.`TestTable3` tableName\n" + "WHERE\n" - + " row_key >= '07' AND row_key < '09' AND tableName.f.c LIKE 'value 0%9'" - , 22); + + " row_key >= '07' AND row_key < '09' AND tableName.f.c LIKE 'value 0%9'"; + + runHBaseSQLVerifyCount(sql, 22); + + final String[] expectedPlan = {".*startRow=07, stopRow=09, filter=FilterList AND.*RowFilter \\(GREATER_OR_EQUAL, 07\\), RowFilter \\(LESS, 09\\), SingleColumnValueFilter \\(f, c, EQUAL.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } @Test public void testFilterPushDownRowKeyGreaterThan() throws Exception { setColumnWidths(new int[] {8, 38, 38}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " *\n" + "FROM\n" + " hbase.`[TABLE_NAME]` tableName\n" + "WHERE\n" - + " row_key > 'b4'" - , 2); + + " row_key > 'b4'"; + + runHBaseSQLVerifyCount(sql, 2); + + final String[] expectedPlan = {".*startRow=b4.*stopRow=,.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); + } + + @Test + public void testFilterPushDownRowKeyGreaterThanWithItem() throws Exception { + setColumnWidths(new int[] {8, 38}); + final String sql = "SELECT\n" + + " row_key, tableName.f2.c3\n" + + "FROM\n" + + " hbase.`[TABLE_NAME]` tableName\n" + + "WHERE\n" + + " row_key > 'b4'"; + + runHBaseSQLVerifyCount(sql, 2); + + final String[] expectedPlan = {".*startRow=b4.*stopRow=,.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } @Test public void testFilterPushDownRowKeyBetween() throws Exception { setColumnWidths(new int[] {8, 74, 38}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " *\n" + "FROM\n" + " hbase.`[TABLE_NAME]` tableName\n" + "WHERE\n" - + " row_key BETWEEN 'a2' AND 'b4'" - , 3); + + " row_key BETWEEN 'a2' AND 'b4'"; + + runHBaseSQLVerifyCount(sql, 3); + + final String[] expectedPlan = {".*startRow=a2, stopRow=b4\\\\x00, filter=FilterList AND.*GREATER_OR_EQUAL, a2.*LESS_OR_EQUAL, b4.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); + } + + @Test + public void testFilterPushDownRowKeyBetweenWithItem() throws Exception { + setColumnWidths(new int[] {8, 12}); + final String sql = "SELECT\n" + + " row_key, tableName.f.c1\n" + + "FROM\n" + + " hbase.`[TABLE_NAME]` tableName\n" + + "WHERE\n" + + " row_key BETWEEN 'a2' AND 'b4'"; + + runHBaseSQLVerifyCount(sql, 3); + + final String[] expectedPlan = {".*startRow=a2, stopRow=b4\\\\x00, filter=FilterList AND.*GREATER_OR_EQUAL, a2.*LESS_OR_EQUAL, b4.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } @Test public void testFilterPushDownMultiColumns() throws Exception { setColumnWidths(new int[] {8, 74, 38}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " *\n" + "FROM\n" + " hbase.`[TABLE_NAME]` t\n" + "WHERE\n" - + " (row_key >= 'b5' OR row_key <= 'a2') AND (t.f.c1 >= '1' OR t.f.c1 is null)" - , 4); + + " (row_key >= 'b5' OR row_key <= 'a2') AND (t.f.c1 >= '1' OR t.f.c1 is null)"; + + runHBaseSQLVerifyCount(sql, 4); + + final String[] expectedPlan = {".*startRow=, stopRow=, filter=FilterList OR.*GREATER_OR_EQUAL, b5.*LESS_OR_EQUAL, a2.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); + } + + @Test + public void testFilterPushDownMultiColumnsWithItem() throws Exception { + setColumnWidths(new int[] {8, 8}); + final String sql = "SELECT\n" + + " row_key, t.f.c1\n" + + "FROM\n" + + " hbase.`[TABLE_NAME]` t\n" + + "WHERE\n" + + " (row_key >= 'b5' OR row_key <= 'a2') AND (t.f.c1 >= '1' OR t.f.c1 is null)"; + + final String[] expectedPlan = {".*startRow=, stopRow=, filter=FilterList OR.*GREATER_OR_EQUAL, b5.*LESS_OR_EQUAL, a2.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } @Test public void testFilterPushDownConvertExpression() throws Exception { setColumnWidths(new int[] {8, 38, 38}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " *\n" + "FROM\n" + " hbase.`[TABLE_NAME]` tableName\n" + "WHERE\n" - + " convert_from(row_key, 'UTF8') > 'b4'" - , 2); + + " convert_from(row_key, 'UTF8') > 'b4'"; + + runHBaseSQLVerifyCount(sql, 2); + + final String[] expectedPlan = {".*startRow=b4\\\\x00, stopRow=,.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); + } + + @Test + public void testFilterPushDownConvertExpressionWithItem() throws Exception { + setColumnWidths(new int[] {8, 38}); + final String sql = "SELECT\n" + + " row_key, tableName.f2.c3\n" + + "FROM\n" + + " hbase.`[TABLE_NAME]` tableName\n" + + "WHERE\n" + + " convert_from(row_key, 'UTF8') > 'b4'"; + + runHBaseSQLVerifyCount(sql, 2); + + final String[] expectedPlan = {".*startRow=b4\\\\x00, stopRow=,.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } @Test @@ -133,13 +270,37 @@ public class TestHBaseFilterPushDown extends BaseHBaseTest { @Test public void testFilterPushDownRowKeyLessThanOrEqualTo() throws Exception { setColumnWidths(new int[] {8, 74, 38}); - runHBaseSQLVerifyCount("SELECT\n" + final String sql = "SELECT\n" + " *\n" + "FROM\n" + " hbase.`[TABLE_NAME]` tableName\n" + "WHERE\n" - + " 'b4' >= row_key" - , 4); + + " 'b4' >= row_key"; + + runHBaseSQLVerifyCount(sql, 4); + + final String[] expectedPlan = {".*startRow=, stopRow=b4\\\\x00, filter=null.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); + } + + @Test + public void testFilterPushDownRowKeyLessThanOrEqualToWithItem() throws Exception { + setColumnWidths(new int[] {8, 12}); + final String sql = "SELECT\n" + + " row_key, tableName.f.c1\n" + + "FROM\n" + + " hbase.`[TABLE_NAME]` tableName\n" + + "WHERE\n" + + " 'b4' >= row_key"; + + runHBaseSQLVerifyCount(sql, 4); + + final String[] expectedPlan = {".*startRow=, stopRow=b4\\\\x00, filter=null.*"}; + final String[] excludedPlan ={}; + final String sqlHBase = canonizeHBaseSQL(sql); + PlanTestBase.testPlanMatchingPatterns(sqlHBase, expectedPlan, excludedPlan); } }
