incubator-quickstep git commit: Fix to a bug in InjectJoinFilters

2018-02-12 Thread jianqiao
Repository: incubator-quickstep
Updated Branches:
  refs/heads/trace 0bb163a7e -> 26c04441c


Fix to a bug in InjectJoinFilters


Project: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-quickstep/commit/26c04441
Tree: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/tree/26c04441
Diff: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/diff/26c04441

Branch: refs/heads/trace
Commit: 26c04441c5ed1f54f2a5716312ee6e3cfc3982f3
Parents: 0bb163a
Author: Jianqiao Zhu 
Authored: Mon Feb 12 22:01:56 2018 -0600
Committer: Jianqiao Zhu 
Committed: Mon Feb 12 22:01:56 2018 -0600

--
 query_optimizer/PhysicalGenerator.cpp   |  2 +-
 query_optimizer/rules/InjectJoinFilters.cpp | 42 +---
 query_optimizer/rules/InjectJoinFilters.hpp |  2 ++
 3 files changed, 34 insertions(+), 12 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/26c04441/query_optimizer/PhysicalGenerator.cpp
--
diff --git a/query_optimizer/PhysicalGenerator.cpp 
b/query_optimizer/PhysicalGenerator.cpp
index 865cd11..b7b0db0 100644
--- a/query_optimizer/PhysicalGenerator.cpp
+++ b/query_optimizer/PhysicalGenerator.cpp
@@ -194,7 +194,7 @@ P::PhysicalPtr PhysicalGenerator::optimizePlan() {
  << physical_plan_->toString();
   }
 
-  DVLOG(4) << "Optimized physical plan:\n" << physical_plan_->toString();
+  std::cerr << "Optimized physical plan:\n" << physical_plan_->toString();
 
   if (FLAGS_visualize_plan) {
 quickstep::PlanVisualizer plan_visualizer;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/26c04441/query_optimizer/rules/InjectJoinFilters.cpp
--
diff --git a/query_optimizer/rules/InjectJoinFilters.cpp 
b/query_optimizer/rules/InjectJoinFilters.cpp
index 0aa2f5b..b85b8d8 100644
--- a/query_optimizer/rules/InjectJoinFilters.cpp
+++ b/query_optimizer/rules/InjectJoinFilters.cpp
@@ -62,17 +62,24 @@ P::PhysicalPtr InjectJoinFilters::apply(const 
P::PhysicalPtr ) {
   // Step 1. Transform applicable HashJoin nodes to FilterJoin nodes.
   P::PhysicalPtr output = transformHashJoinToFilters(input);
 
-  // Step 2. Push down FilterJoin nodes to be evaluated early.
+  if (output == input) {
+return input;
+  }
+
+  // Step 2. Wrap the top level plan with a Selection to stabilize output 
columns.
+  output = wrapSelection(output);
+
+  // Step 3. Push down FilterJoin nodes to be evaluated early.
   output = pushDownFilters(output);
 
-  // Step 3. Add Selection nodes for attaching the LIPFilters, if necessary.
+  // Step 4. Add Selection nodes for attaching the LIPFilters, if necessary.
   output = addFilterAnchors(output, false);
 
-  // Step 4. Because of the pushdown of FilterJoin nodes, there are 
optimization
+  // Step 5. Because of the pushdown of FilterJoin nodes, there are 
optimization
   // opportunities for projecting columns early.
   output = PruneColumns().apply(output);
 
-  // Step 5. For each FilterJoin node, attach its corresponding LIPFilter to
+  // Step 6. For each FilterJoin node, attach its corresponding LIPFilter to
   // proper nodes.
   concretizeAsLIPFilters(output, nullptr);
 
@@ -146,13 +153,8 @@ bool InjectJoinFilters::isTransformable(
 P::PhysicalPtr InjectJoinFilters::transformHashJoinToFilters(
 const P::PhysicalPtr ) const {
   std::vector new_children;
-  bool has_changed_children = false;
   for (const P::PhysicalPtr  : input->children()) {
-const P::PhysicalPtr new_child = transformHashJoinToFilters(child);
-if (child != new_child && !has_changed_children) {
-  has_changed_children = true;
-}
-new_children.push_back(new_child);
+new_children.emplace_back(transformHashJoinToFilters(child));
   }
 
   P::HashJoinPtr hash_join;
@@ -182,7 +184,7 @@ P::PhysicalPtr 
InjectJoinFilters::transformHashJoinToFilters(
  
hash_join->cloneOutputPartitionSchemeHeader());
   }
 
-  if (has_changed_children) {
+  if (input->children() != new_children) {
 return input->copyWithNewChildren(new_children);
   } else {
 return input;
@@ -437,5 +439,23 @@ bool 
InjectJoinFilters::findExactMinMaxValuesForAttributeHelper(
   }
 }
 
+P::PhysicalPtr InjectJoinFilters::wrapSelection(
+const P::PhysicalPtr ) const {
+  DCHECK(input->getPhysicalType() == P::PhysicalType::kTopLevelPlan);
+  const P::TopLevelPlanPtr top_level_plan =
+  std::static_pointer_cast(input);
+
+  const P::SelectionPtr selection =
+  P::Selection::Create(
+  top_level_plan->plan(),
+  E::ToNamedExpressions(top_level_plan->plan()->getOutputAttributes()),
+  nullptr /* filter_predicate 

incubator-quickstep git commit: Fix a bug in InjectJoinFilters [Forced Update!]

2018-02-12 Thread jianqiao
Repository: incubator-quickstep
Updated Branches:
  refs/heads/trace 26c04441c -> ba16c5aef (forced update)


Fix a bug in InjectJoinFilters


Project: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-quickstep/commit/ba16c5ae
Tree: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/tree/ba16c5ae
Diff: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/diff/ba16c5ae

Branch: refs/heads/trace
Commit: ba16c5aefa25f1d83e0e30e6a2a6a44c5eb984e0
Parents: 0bb163a
Author: Jianqiao Zhu 
Authored: Mon Feb 12 22:01:56 2018 -0600
Committer: Jianqiao Zhu 
Committed: Mon Feb 12 22:55:34 2018 -0600

--
 query_optimizer/rules/InjectJoinFilters.cpp | 47 ++--
 query_optimizer/rules/InjectJoinFilters.hpp |  2 +
 2 files changed, 38 insertions(+), 11 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/ba16c5ae/query_optimizer/rules/InjectJoinFilters.cpp
--
diff --git a/query_optimizer/rules/InjectJoinFilters.cpp 
b/query_optimizer/rules/InjectJoinFilters.cpp
index 0aa2f5b..a92e433 100644
--- a/query_optimizer/rules/InjectJoinFilters.cpp
+++ b/query_optimizer/rules/InjectJoinFilters.cpp
@@ -62,17 +62,25 @@ P::PhysicalPtr InjectJoinFilters::apply(const 
P::PhysicalPtr ) {
   // Step 1. Transform applicable HashJoin nodes to FilterJoin nodes.
   P::PhysicalPtr output = transformHashJoinToFilters(input);
 
-  // Step 2. Push down FilterJoin nodes to be evaluated early.
+  if (output == input) {
+return input;
+  }
+
+  // Step 2. If the top level plan is a filter join, wrap it with a Selection
+  // to stabilize output columns.
+  output = wrapSelection(output);
+
+  // Step 3. Push down FilterJoin nodes to be evaluated early.
   output = pushDownFilters(output);
 
-  // Step 3. Add Selection nodes for attaching the LIPFilters, if necessary.
+  // Step 4. Add Selection nodes for attaching the LIPFilters, if necessary.
   output = addFilterAnchors(output, false);
 
-  // Step 4. Because of the pushdown of FilterJoin nodes, there are 
optimization
+  // Step 5. Because of the pushdown of FilterJoin nodes, there are 
optimization
   // opportunities for projecting columns early.
   output = PruneColumns().apply(output);
 
-  // Step 5. For each FilterJoin node, attach its corresponding LIPFilter to
+  // Step 6. For each FilterJoin node, attach its corresponding LIPFilter to
   // proper nodes.
   concretizeAsLIPFilters(output, nullptr);
 
@@ -146,13 +154,8 @@ bool InjectJoinFilters::isTransformable(
 P::PhysicalPtr InjectJoinFilters::transformHashJoinToFilters(
 const P::PhysicalPtr ) const {
   std::vector new_children;
-  bool has_changed_children = false;
   for (const P::PhysicalPtr  : input->children()) {
-const P::PhysicalPtr new_child = transformHashJoinToFilters(child);
-if (child != new_child && !has_changed_children) {
-  has_changed_children = true;
-}
-new_children.push_back(new_child);
+new_children.emplace_back(transformHashJoinToFilters(child));
   }
 
   P::HashJoinPtr hash_join;
@@ -182,7 +185,7 @@ P::PhysicalPtr 
InjectJoinFilters::transformHashJoinToFilters(
  
hash_join->cloneOutputPartitionSchemeHeader());
   }
 
-  if (has_changed_children) {
+  if (input->children() != new_children) {
 return input->copyWithNewChildren(new_children);
   } else {
 return input;
@@ -437,5 +440,27 @@ bool 
InjectJoinFilters::findExactMinMaxValuesForAttributeHelper(
   }
 }
 
+P::PhysicalPtr InjectJoinFilters::wrapSelection(
+const P::PhysicalPtr ) const {
+  DCHECK(input->getPhysicalType() == P::PhysicalType::kTopLevelPlan);
+  const P::TopLevelPlanPtr top_level_plan =
+  std::static_pointer_cast(input);
+
+  if (top_level_plan->plan()->getPhysicalType() != 
P::PhysicalType::kFilterJoin) {
+return input;
+  }
+
+  const P::SelectionPtr selection =
+  P::Selection::Create(
+  top_level_plan->plan(),
+  E::ToNamedExpressions(top_level_plan->plan()->getOutputAttributes()),
+  nullptr /* filter_predicate */);
+
+  return P::TopLevelPlan::Create(selection,
+ top_level_plan->shared_subplans(),
+ top_level_plan->uncorrelated_subquery_map());
+}
+
+
 }  // namespace optimizer
 }  // namespace quickstep

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/ba16c5ae/query_optimizer/rules/InjectJoinFilters.hpp
--
diff --git a/query_optimizer/rules/InjectJoinFilters.hpp 
b/query_optimizer/rules/InjectJoinFilters.hpp
index c5250b3..71a6c99 100644
--- a/query_optimizer/rules/InjectJoinFilters.hpp
+++