quickstep-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jianq...@apache.org
Subject incubator-quickstep git commit: Refactor Generators in Optimizer for constructing multiple generators in query_optimizer once for all queries. [Forced Update!]
Date Tue, 16 Aug 2016 18:27:11 GMT
Repository: incubator-quickstep
Updated Branches:
  refs/heads/opt-refactor 1cf99a426 -> 0d8f242fa (forced update)


Refactor Generators in Optimizer for constructing multiple generators in query_optimizer once
for all queries.


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

Branch: refs/heads/opt-refactor
Commit: 0d8f242fae1f06a22eb439d79669310cde7c00e4
Parents: 8ec99ed
Author: Zuyu Zhang <zuyuz@twitter.com>
Authored: Sat Aug 13 01:49:03 2016 -0700
Committer: Jianqiao Zhu <jianqiao@cs.wisc.edu>
Committed: Tue Aug 16 13:22:13 2016 -0500

----------------------------------------------------------------------
 cli/tests/CMakeLists.txt                        |  8 +---
 cli/tests/CommandExecutorTestRunner.cpp         | 35 ++++++------------
 cli/tests/CommandExecutorTestRunner.hpp         |  2 +
 query_optimizer/CMakeLists.txt                  |  2 +-
 query_optimizer/ExecutionGenerator.cpp          | 35 +++++++++---------
 query_optimizer/ExecutionGenerator.hpp          | 12 +++---
 query_optimizer/LogicalGenerator.cpp            |  3 +-
 query_optimizer/LogicalGenerator.hpp            |  5 ++-
 query_optimizer/Optimizer.cpp                   | 12 +++---
 query_optimizer/Optimizer.hpp                   | 26 +++++--------
 query_optimizer/OptimizerContext.hpp            | 39 +-------------------
 query_optimizer/QueryProcessor.cpp              |  7 ++--
 query_optimizer/QueryProcessor.hpp              |  3 ++
 query_optimizer/resolver/Resolver.cpp           |  4 +-
 query_optimizer/resolver/Resolver.hpp           |  8 +++-
 query_optimizer/tests/CMakeLists.txt            | 12 +-----
 .../tests/ExecutionGeneratorTestRunner.cpp      | 35 +++++-------------
 .../tests/ExecutionGeneratorTestRunner.hpp      |  3 +-
 query_optimizer/tests/OptimizerTest.cpp         |  3 +-
 .../tests/OptimizerTextTestRunner.cpp           |  9 ++---
 20 files changed, 93 insertions(+), 170 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/cli/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/cli/tests/CMakeLists.txt b/cli/tests/CMakeLists.txt
index 3bceba8..99fa3a3 100644
--- a/cli/tests/CMakeLists.txt
+++ b/cli/tests/CMakeLists.txt
@@ -36,19 +36,13 @@ target_link_libraries(quickstep_cli_tests_CommandExecutorTest
                       quickstep_parser_SqlParserWrapper
                       quickstep_queryexecution_AdmitRequestMessage
                       quickstep_queryexecution_ForemanSingleNode
-                      quickstep_queryexecution_QueryContext
                       quickstep_queryexecution_QueryExecutionTypedefs
                       quickstep_queryexecution_QueryExecutionUtil
                       quickstep_queryexecution_Worker
                       quickstep_queryexecution_WorkerDirectory
-                      quickstep_queryexecution_WorkerMessage
-                      quickstep_queryoptimizer_ExecutionGenerator
-                      quickstep_queryoptimizer_LogicalGenerator
+                      quickstep_queryoptimizer_Optimizer
                       quickstep_queryoptimizer_OptimizerContext
-                      quickstep_queryoptimizer_PhysicalGenerator
                       quickstep_queryoptimizer_QueryHandle
-                      quickstep_queryoptimizer_QueryPlan
-                      quickstep_queryoptimizer_physical_Physical
                       quickstep_queryoptimizer_tests_TestDatabaseLoader
                       quickstep_utility_Macros
                       quickstep_utility_MemStream

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/cli/tests/CommandExecutorTestRunner.cpp
----------------------------------------------------------------------
diff --git a/cli/tests/CommandExecutorTestRunner.cpp b/cli/tests/CommandExecutorTestRunner.cpp
index 9c701cd..41cc9da 100644
--- a/cli/tests/CommandExecutorTestRunner.cpp
+++ b/cli/tests/CommandExecutorTestRunner.cpp
@@ -31,17 +31,10 @@
 #include "query_execution/AdmitRequestMessage.hpp"
 #include "query_execution/ForemanSingleNode.hpp"
 #include "query_execution/QueryExecutionTypedefs.hpp"
-#include "query_execution/Worker.hpp"
-#include "query_optimizer/ExecutionGenerator.hpp"
-#include "query_optimizer/LogicalGenerator.hpp"
+#include "query_optimizer/Optimizer.hpp"
 #include "query_optimizer/OptimizerContext.hpp"
-#include "query_optimizer/PhysicalGenerator.hpp"
 #include "query_optimizer/QueryHandle.hpp"
-#include "query_optimizer/QueryPlan.hpp"
-#include "query_optimizer/physical/Physical.hpp"
-#include "utility/Macros.hpp"
 #include "utility/MemStream.hpp"
-#include "utility/PtrList.hpp"
 #include "utility/SqlError.hpp"
 
 #include "glog/logging.h"
@@ -53,8 +46,6 @@ namespace quickstep {
 class CatalogRelation;
 
 namespace O = ::quickstep::optimizer;
-namespace P = ::quickstep::optimizer::physical;
-namespace L = ::quickstep::optimizer::logical;
 
 const char CommandExecutorTestRunner::kResetOption[] =
     "reset_before_execution";
@@ -77,19 +68,16 @@ void CommandExecutorTestRunner::runTestCase(
 
   while (true) {
     ParseResult result = sql_parser_.getNextStatement();
-
-    O::OptimizerContext optimizer_context(test_database_loader_.catalog_database(),
-                                          test_database_loader_.storage_manager());
-
     if (result.condition != ParseResult::kSuccess) {
       if (result.condition == ParseResult::kError) {
         *output = result.error_message;
       }
       break;
     } else {
-      std::printf("%s\n", result.parsed_statement->toString().c_str());
+      const ParseStatement &parse_statement = *result.parsed_statement;
+      std::printf("%s\n", parse_statement.toString().c_str());
       try {
-        if (result.parsed_statement->getStatementType() == ParseStatement::kCommand) {
+        if (parse_statement.getStatementType() == ParseStatement::kCommand) {
           quickstep::cli::executeCommand(
               *result.parsed_statement,
               *(test_database_loader_.catalog_database()),
@@ -99,15 +87,14 @@ void CommandExecutorTestRunner::runTestCase(
               test_database_loader_.storage_manager(),
               nullptr,
               output_stream.file());
-        } else  {
+        } else {
           QueryHandle query_handle(0 /* query_id */, main_thread_client_id_);
-          O::LogicalGenerator logical_generator(&optimizer_context);
-          O::PhysicalGenerator physical_generator;
-          O::ExecutionGenerator execution_generator(&optimizer_context, &query_handle);
-          const P::PhysicalPtr physical_plan =
-              physical_generator.generatePlan(
-                  logical_generator.generatePlan(*result.parsed_statement));
-          execution_generator.generatePlan(physical_plan);
+          O::OptimizerContext optimizer_context;
+
+          optimizer_.generateQueryHandle(parse_statement,
+                                         test_database_loader_.catalog_database(),
+                                         &optimizer_context,
+                                         &query_handle);
 
           AdmitRequestMessage request_message(&query_handle);
           TaggedMessage admit_tagged_message(

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/cli/tests/CommandExecutorTestRunner.hpp
----------------------------------------------------------------------
diff --git a/cli/tests/CommandExecutorTestRunner.hpp b/cli/tests/CommandExecutorTestRunner.hpp
index 682dc81..83c5a9a 100644
--- a/cli/tests/CommandExecutorTestRunner.hpp
+++ b/cli/tests/CommandExecutorTestRunner.hpp
@@ -33,6 +33,7 @@
 #include "query_execution/Worker.hpp"
 #include "query_execution/WorkerDirectory.hpp"
 #include "query_execution/WorkerMessage.hpp"
+#include "query_optimizer/Optimizer.hpp"
 #include "query_optimizer/tests/TestDatabaseLoader.hpp"
 #include "utility/Macros.hpp"
 #include "utility/textbased_test/TextBasedTestDriver.hpp"
@@ -103,6 +104,7 @@ class CommandExecutorTestRunner : public TextBasedTestRunner {
  private:
   SqlParserWrapper sql_parser_;
   optimizer::TestDatabaseLoader test_database_loader_;
+  optimizer::Optimizer optimizer_;
 
   tmb::client_id main_thread_client_id_;
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/query_optimizer/CMakeLists.txt b/query_optimizer/CMakeLists.txt
index c24ee89..56ae52f 100644
--- a/query_optimizer/CMakeLists.txt
+++ b/query_optimizer/CMakeLists.txt
@@ -181,7 +181,6 @@ target_link_libraries(quickstep_queryoptimizer_LogicalToPhysicalMapper
 target_link_libraries(quickstep_queryoptimizer_Optimizer
                       quickstep_queryoptimizer_ExecutionGenerator
                       quickstep_queryoptimizer_LogicalGenerator
-                      quickstep_queryoptimizer_OptimizerContext
                       quickstep_queryoptimizer_PhysicalGenerator
                       quickstep_utility_Macros)
 target_link_libraries(quickstep_queryoptimizer_OptimizerContext
@@ -221,6 +220,7 @@ target_link_libraries(quickstep_queryoptimizer_QueryProcessor
                       quickstep_catalog_Catalog
                       quickstep_catalog_Catalog_proto
                       quickstep_queryoptimizer_Optimizer
+                      quickstep_queryoptimizer_OptimizerContext
                       quickstep_storage_StorageManager
                       quickstep_utility_Macros)
 target_link_libraries(quickstep_queryoptimizer_Validator

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/ExecutionGenerator.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/ExecutionGenerator.cpp b/query_optimizer/ExecutionGenerator.cpp
index a4b538e..2e03e09 100644
--- a/query_optimizer/ExecutionGenerator.cpp
+++ b/query_optimizer/ExecutionGenerator.cpp
@@ -195,14 +195,14 @@ void ExecutionGenerator::generatePlan(const P::PhysicalPtr &physical_plan)
{
     const CatalogRelation *temporary_relation = temporary_relation_info.relation;
     if (temporary_relation == result_relation) {
       query_handle_->setQueryResultRelation(
-          optimizer_context_->catalog_database()->getRelationByIdMutable(result_relation->getID()));
+          catalog_database_->getRelationByIdMutable(result_relation->getID()));
       continue;
     }
     const QueryPlan::DAGNodeIndex drop_table_index =
         execution_plan_->addRelationalOperator(
             new DropTableOperator(query_handle_->query_id(),
                                   *temporary_relation,
-                                  optimizer_context_->catalog_database(),
+                                  catalog_database_,
                                   false /* only_drop_blocks */));
     DCHECK(!temporary_relation_info.isStoredRelation());
     execution_plan_->addDependenciesForDropOperator(
@@ -216,12 +216,12 @@ void ExecutionGenerator::generatePlan(const P::PhysicalPtr &physical_plan)
{
   }
 
 #ifdef QUICKSTEP_DISTRIBUTED
-  catalog_database_cache_proto_->set_name(optimizer_context_->catalog_database()->getName());
+  catalog_database_cache_proto_->set_name(catalog_database_->getName());
 
   LOG(INFO) << "CatalogDatabaseCache proto has " << referenced_relation_ids_.size()
<< " relation(s)";
   for (const relation_id rel_id : referenced_relation_ids_) {
     const CatalogRelationSchema &relation =
-        optimizer_context_->catalog_database()->getRelationSchemaById(rel_id);
+        catalog_database_->getRelationSchemaById(rel_id);
     LOG(INFO) << "RelationSchema " << rel_id
               << ", name: " << relation.getName()
               << ", " << relation.size()  << " attribute(s)";
@@ -311,7 +311,7 @@ void ExecutionGenerator::createTemporaryCatalogRelation(
     const CatalogRelation **catalog_relation_output,
     S::InsertDestination *insert_destination_proto) {
   std::unique_ptr<CatalogRelation> catalog_relation(
-      new CatalogRelation(optimizer_context_->catalog_database(),
+      new CatalogRelation(catalog_database_,
                           getNewRelationName(),
                           -1 /* id */,
                           true /* is_temporary*/));
@@ -332,7 +332,7 @@ void ExecutionGenerator::createTemporaryCatalogRelation(
   }
 
   *catalog_relation_output = catalog_relation.get();
-  const relation_id output_rel_id = optimizer_context_->catalog_database()->addRelation(
+  const relation_id output_rel_id = catalog_database_->addRelation(
       catalog_relation.release());
 
 #ifdef QUICKSTEP_DISTRIBUTED
@@ -344,11 +344,10 @@ void ExecutionGenerator::createTemporaryCatalogRelation(
 }
 
 void ExecutionGenerator::dropAllTemporaryRelations() {
-  CatalogDatabase *catalog_database = optimizer_context_->catalog_database();
   for (const CatalogRelationInfo &temporary_relation_info :
        temporary_relation_info_vec_) {
     DCHECK_EQ(temporary_relation_info.relation->size_blocks(), 0u);
-    catalog_database->dropRelationById(temporary_relation_info.relation->getID());
+    catalog_database_->dropRelationById(temporary_relation_info.relation->getID());
   }
 }
 
@@ -618,7 +617,7 @@ void ExecutionGenerator::convertHashJoin(const P::HashJoinPtr &physical_plan)
{
   for (const E::AttributeReferencePtr &left_join_attribute : left_join_attributes) {
     // Try to determine the original stored relation referenced in the Hash Join.
     referenced_stored_probe_relation =
-        optimizer_context_->catalog_database()->getRelationByName(left_join_attribute->relation_name());
+        catalog_database_->getRelationByName(left_join_attribute->relation_name());
     if (referenced_stored_probe_relation == nullptr) {
       // Hash Join optimizations are not possible, if the referenced relation cannot be determined.
       skip_hash_join_optimization = true;
@@ -642,7 +641,7 @@ void ExecutionGenerator::convertHashJoin(const P::HashJoinPtr &physical_plan)
{
   for (const E::AttributeReferencePtr &right_join_attribute : right_join_attributes)
{
     // Try to determine the original stored relation referenced in the Hash Join.
     referenced_stored_build_relation =
-        optimizer_context_->catalog_database()->getRelationByName(right_join_attribute->relation_name());
+        catalog_database_->getRelationByName(right_join_attribute->relation_name());
     if (referenced_stored_build_relation == nullptr) {
       // Hash Join optimizations are not possible, if the referenced relation cannot be determined.
       skip_hash_join_optimization = true;
@@ -958,7 +957,7 @@ void ExecutionGenerator::convertCreateIndex(
   const CatalogRelationInfo *input_relation_info =
       findRelationInfoOutputByPhysical(physical_plan->input());
   CatalogRelation *input_relation =
-      optimizer_context_->catalog_database()->getRelationByIdMutable(
+      catalog_database_->getRelationByIdMutable(
             input_relation_info->relation->getID());
 
   // Check if any index with the specified name already exists.
@@ -1004,7 +1003,7 @@ void ExecutionGenerator::convertCreateTable(
   // CreateTable is converted to a CreateTable operator.
 
   std::unique_ptr<CatalogRelation> catalog_relation(new CatalogRelation(
-      optimizer_context_->catalog_database(),
+      catalog_database_,
       physical_plan->relation_name(),
       -1 /* id */,
       false /* is_temporary*/));
@@ -1038,7 +1037,7 @@ void ExecutionGenerator::convertCreateTable(
   execution_plan_->addRelationalOperator(
       new CreateTableOperator(query_handle_->query_id(),
                               catalog_relation.release(),
-                              optimizer_context_->catalog_database()));
+                              catalog_database_));
 }
 
 void ExecutionGenerator::convertDeleteTuples(
@@ -1065,7 +1064,7 @@ void ExecutionGenerator::convertDeleteTuples(
         execution_plan_->addRelationalOperator(
             new DropTableOperator(query_handle_->query_id(),
                                   *input_relation_info->relation,
-                                  optimizer_context_->catalog_database(),
+                                  catalog_database_,
                                   true /* only_drop_blocks */));
     if (!input_relation_info->isStoredRelation()) {
       execution_plan_->addDirectDependency(drop_table_index,
@@ -1110,7 +1109,7 @@ void ExecutionGenerator::convertDropTable(
   execution_plan_->addRelationalOperator(
       new DropTableOperator(query_handle_->query_id(),
                             catalog_relation,
-                            optimizer_context_->catalog_database()));
+                            catalog_database_));
 }
 
 void ExecutionGenerator::convertInsertTuple(
@@ -1120,7 +1119,7 @@ void ExecutionGenerator::convertInsertTuple(
   const CatalogRelationInfo *input_relation_info =
       findRelationInfoOutputByPhysical(physical_plan->input());
   const CatalogRelation &input_relation =
-      *optimizer_context_->catalog_database()->getRelationById(
+      *catalog_database_->getRelationById(
           input_relation_info->relation->getID());
 
   // Construct the tuple proto to be inserted.
@@ -1320,7 +1319,7 @@ void ExecutionGenerator::convertUpdateTable(
   const QueryPlan::DAGNodeIndex update_operator_index =
       execution_plan_->addRelationalOperator(new UpdateOperator(
           query_handle_->query_id(),
-          *optimizer_context_->catalog_database()->getRelationById(
+          *catalog_database_->getRelationById(
               input_rel_id),
           relocation_destination_index,
           execution_predicate_index,
@@ -1578,7 +1577,7 @@ void ExecutionGenerator::convertSort(const P::SortPtr &physical_sort)
{
           new DropTableOperator(
               query_handle_->query_id(),
               *merged_runs_relation,
-              optimizer_context_->catalog_database(),
+              catalog_database_,
               false /* only_drop_blocks */));
   execution_plan_->addDirectDependency(
       drop_merged_runs_index,

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/ExecutionGenerator.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/ExecutionGenerator.hpp b/query_optimizer/ExecutionGenerator.hpp
index d5c7b06..6017aa5 100644
--- a/query_optimizer/ExecutionGenerator.hpp
+++ b/query_optimizer/ExecutionGenerator.hpp
@@ -68,6 +68,7 @@
 namespace quickstep {
 
 class CatalogAttribute;
+class CatalogDatabase;
 class CatalogRelation;
 class Predicate;
 
@@ -82,8 +83,6 @@ class InsertDestination;
 
 namespace optimizer {
 
-class OptimizerContext;
-
 /** \addtogroup QueryOptimizer
  *  @{
  */
@@ -96,12 +95,12 @@ class ExecutionGenerator {
   /**
    * @brief Constructor. Does not take ownership of \p query_handle.
    *
-   * @param optimizer_context Optimizer context.
+   * @param catalog_database The catalog database where this query is executed.
    * @param query_handle The pointer to the output query handle.
    */
-  ExecutionGenerator(OptimizerContext *optimizer_context,
+  ExecutionGenerator(CatalogDatabase *catalog_database,
                      QueryHandle *query_handle)
-      : optimizer_context_(DCHECK_NOTNULL(optimizer_context)),
+      : catalog_database_(DCHECK_NOTNULL(catalog_database)),
         query_handle_(DCHECK_NOTNULL(query_handle)),
         execution_plan_(DCHECK_NOTNULL(query_handle->getQueryPlanMutable())),
         query_context_proto_(DCHECK_NOTNULL(query_handle->getQueryContextProtoMutable())),
@@ -383,7 +382,8 @@ class ExecutionGenerator {
    */
   void dropAllTemporaryRelations();
 
-  OptimizerContext *optimizer_context_;
+  CatalogDatabase *catalog_database_;
+
   QueryHandle *query_handle_;
   QueryPlan *execution_plan_;  // A part of QueryHandle.
   serialization::QueryContext *query_context_proto_;  // A part of QueryHandle.

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/LogicalGenerator.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/LogicalGenerator.cpp b/query_optimizer/LogicalGenerator.cpp
index 450c36a..abeca53 100644
--- a/query_optimizer/LogicalGenerator.cpp
+++ b/query_optimizer/LogicalGenerator.cpp
@@ -48,8 +48,9 @@ LogicalGenerator::LogicalGenerator(OptimizerContext *optimizer_context)
 LogicalGenerator::~LogicalGenerator() {}
 
 L::LogicalPtr LogicalGenerator::generatePlan(
+    const CatalogDatabase &catalog_database,
     const ParseStatement &parse_statement) {
-  resolver::Resolver resolver(optimizer_context_);
+  resolver::Resolver resolver(catalog_database, optimizer_context_);
   DVLOG(4) << "Parse tree:\n" << parse_statement.toString();
   logical_plan_ = resolver.resolve(parse_statement);
   DVLOG(4) << "Initial logical plan:\n" << logical_plan_->toString();

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/LogicalGenerator.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/LogicalGenerator.hpp b/query_optimizer/LogicalGenerator.hpp
index d4384c8..2898e6f 100644
--- a/query_optimizer/LogicalGenerator.hpp
+++ b/query_optimizer/LogicalGenerator.hpp
@@ -25,6 +25,7 @@
 
 namespace quickstep {
 
+class CatalogDatabase;
 class ParseStatement;
 
 namespace optimizer {
@@ -61,10 +62,12 @@ class LogicalGenerator {
    * @brief Converts the parse tree to a logical plan and optimizes the logical
    *        plan by using rules.
    *
+   * @param catalog_database The catalog database where this query is executed.
    * @param parse_statement The parse tree to be converted.
    * @return An optimized logical plan.
    */
-  logical::LogicalPtr generatePlan(const ParseStatement &parse_statement);
+  logical::LogicalPtr generatePlan(const CatalogDatabase &catalog_database,
+                                   const ParseStatement &parse_statement);
 
  private:
   /**

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/Optimizer.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/Optimizer.cpp b/query_optimizer/Optimizer.cpp
index b77e430..b14c938 100644
--- a/query_optimizer/Optimizer.cpp
+++ b/query_optimizer/Optimizer.cpp
@@ -21,20 +21,20 @@
 
 #include "query_optimizer/ExecutionGenerator.hpp"
 #include "query_optimizer/LogicalGenerator.hpp"
-#include "query_optimizer/PhysicalGenerator.hpp"
 
 namespace quickstep {
 namespace optimizer {
 
 void Optimizer::generateQueryHandle(const ParseStatement &parse_statement,
+                                    CatalogDatabase *catalog_database,
+                                    OptimizerContext *optimizer_context,
                                     QueryHandle *query_handle) {
-  LogicalGenerator logical_generator(&optimizer_context_);
-  PhysicalGenerator physical_generator;
-  ExecutionGenerator execution_generator(&optimizer_context_, query_handle);
+  LogicalGenerator logical_generator(optimizer_context);
+  ExecutionGenerator execution_generator(catalog_database, query_handle);
 
   execution_generator.generatePlan(
-      physical_generator.generatePlan(
-          logical_generator.generatePlan(parse_statement)));
+      physical_generator_.generatePlan(
+          logical_generator.generatePlan(*catalog_database, parse_statement)));
 }
 
 }  // namespace optimizer

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/Optimizer.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/Optimizer.hpp b/query_optimizer/Optimizer.hpp
index 132b0b8..36f956a 100644
--- a/query_optimizer/Optimizer.hpp
+++ b/query_optimizer/Optimizer.hpp
@@ -20,7 +20,7 @@
 #ifndef QUICKSTEP_QUERY_OPTIMIZER_OPTIMIZER_HPP_
 #define QUICKSTEP_QUERY_OPTIMIZER_OPTIMIZER_HPP_
 
-#include "query_optimizer/OptimizerContext.hpp"
+#include "query_optimizer/PhysicalGenerator.hpp"
 #include "utility/Macros.hpp"
 
 namespace quickstep {
@@ -28,10 +28,11 @@ namespace quickstep {
 class CatalogDatabase;
 class ParseStatement;
 class QueryHandle;
-class StorageManager;
 
 namespace optimizer {
 
+class OptimizerContext;
+
 /** \addtogroup QueryOptimizer
  *  @{
  */
@@ -44,13 +45,8 @@ class Optimizer {
  public:
   /**
    * @brief Constructor.
-   *
-   * @param database The database that the query is executed on.
-   * @param storage_manager The storage manager for the database.
    */
-  Optimizer(CatalogDatabase *database,
-            StorageManager *storage_manager)
-      : optimizer_context_(database, storage_manager) {}
+  Optimizer() {}
 
   /**
    * @brief Destructor.
@@ -64,21 +60,17 @@ class Optimizer {
    * @note Does not take ownership of \p query_handle.
    *
    * @param parse_statement The parse tree of the input query.
+   * @param catalog_database The database that the query is executed on.
+   * @param optimizer_context The optimizer context of the input query.
    * @param query_handle The generated query handle to output.
    */
   void generateQueryHandle(const ParseStatement &parse_statement,
+                           CatalogDatabase *catalog_database,
+                           OptimizerContext *optimizer_context,
                            QueryHandle *query_handle);
 
-  /**
-   * @return True if the catalog will be permanently changed after executing the
-   *         query and needs to be saved.
-   */
-  bool isCatalogChanged() const {
-    return optimizer_context_.is_catalog_changed();
-  }
-
  private:
-  OptimizerContext optimizer_context_;
+  PhysicalGenerator physical_generator_;
 
   DISALLOW_COPY_AND_ASSIGN(Optimizer);
 };

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/OptimizerContext.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/OptimizerContext.hpp b/query_optimizer/OptimizerContext.hpp
index 5be2a98..d52c96c 100644
--- a/query_optimizer/OptimizerContext.hpp
+++ b/query_optimizer/OptimizerContext.hpp
@@ -25,9 +25,6 @@
 
 namespace quickstep {
 
-class CatalogDatabase;
-class StorageManager;
-
 namespace optimizer {
 
 /** \addtogroup QueryOptimizer
@@ -44,16 +41,9 @@ class OptimizerContext {
 
   /**
    * @brief Constructor.
-   *
-   * @param catalog_database The catalog database where this query is executed.
-   * @param storage_manager The storage manager to use for allocating storage
-   *        blocks.
    */
-  OptimizerContext(CatalogDatabase *catalog_database,
-                   StorageManager *storage_manager)
+  OptimizerContext()
       : current_expr_id_(-1),
-        catalog_database_(catalog_database),
-        storage_manager_(storage_manager),
         has_nested_queries_(false),
         is_catalog_changed_(false) {}
 
@@ -63,30 +53,6 @@ class OptimizerContext {
   ~OptimizerContext() {}
 
   /**
-   * @return Const pointer to the catalog database
-   *         where the query is executed.
-   */
-  const CatalogDatabase* catalog_database() const { return catalog_database_; }
-
-  /**
-   * @return Mutable pointer to the catalog database
-   *         where this query is executed.
-   */
-  CatalogDatabase* catalog_database() { return catalog_database_; }
-
-  /**
-   * @return Const pointer to the storage manager to use for
-   *         allocating storage blocks.
-   */
-  const StorageManager* storage_manager() const { return storage_manager_; }
-
-  /**
-   * @return Mutable pointer to the storage manager to use for
-   *         allocating storage blocks.
-   */
-  StorageManager* storage_manager() { return storage_manager_; }
-
-  /**
    * @brief Gets the next ExprId.
    *
    * @return A new ExprId.
@@ -123,9 +89,6 @@ class OptimizerContext {
  private:
   expressions::ExprId current_expr_id_;
 
-  CatalogDatabase *catalog_database_;
-  StorageManager *storage_manager_;
-
   bool has_nested_queries_;
 
   bool is_catalog_changed_;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/QueryProcessor.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/QueryProcessor.cpp b/query_optimizer/QueryProcessor.cpp
index ea9f126..d74eeba 100644
--- a/query_optimizer/QueryProcessor.cpp
+++ b/query_optimizer/QueryProcessor.cpp
@@ -26,6 +26,7 @@
 #include "catalog/Catalog.hpp"
 #include "catalog/Catalog.pb.h"
 #include "query_optimizer/Optimizer.hpp"
+#include "query_optimizer/OptimizerContext.hpp"
 
 using std::ifstream;
 using std::ofstream;
@@ -34,11 +35,11 @@ namespace quickstep {
 
 void QueryProcessor::generateQueryHandle(const ParseStatement &statement,
                                          QueryHandle *query_handle) {
-  optimizer::Optimizer optimizer(getDefaultDatabase(), storage_manager_.get());
+  optimizer::OptimizerContext optimizer_context;
 
-  optimizer.generateQueryHandle(statement, query_handle);
+  optimizer_.generateQueryHandle(statement, getDefaultDatabase(), &optimizer_context,
query_handle);
 
-  if (optimizer.isCatalogChanged() && !catalog_altered_) {
+  if (optimizer_context.is_catalog_changed() && !catalog_altered_) {
     catalog_altered_ = true;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/QueryProcessor.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/QueryProcessor.hpp b/query_optimizer/QueryProcessor.hpp
index 2a7e942..5d3818e 100644
--- a/query_optimizer/QueryProcessor.hpp
+++ b/query_optimizer/QueryProcessor.hpp
@@ -26,6 +26,7 @@
 #include <string>
 
 #include "catalog/Catalog.hpp"
+#include "query_optimizer/Optimizer.hpp"
 #include "storage/StorageManager.hpp"
 #include "utility/Macros.hpp"
 
@@ -194,6 +195,8 @@ class QueryProcessor {
  private:
   void loadCatalog();  // If it exists, free catalog_ before calling this
 
+  optimizer::Optimizer optimizer_;
+
   std::string catalog_filename_;
 
   std::unique_ptr<Catalog> catalog_;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/resolver/Resolver.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/resolver/Resolver.cpp b/query_optimizer/resolver/Resolver.cpp
index 4931629..38ec24a 100644
--- a/query_optimizer/resolver/Resolver.cpp
+++ b/query_optimizer/resolver/Resolver.cpp
@@ -439,7 +439,7 @@ L::LogicalPtr Resolver::resolveCreateTable(
   // Resolve relation name.
   const std::string relation_name =
       create_table_statement.relation_name()->value();
-  if (context_->catalog_database()->hasRelationWithName(relation_name)) {
+  if (catalog_database_.hasRelationWithName(relation_name)) {
     THROW_SQL_ERROR_AT(create_table_statement.relation_name())
         << "Relation " << create_table_statement.relation_name()->value()
         << " already exists";
@@ -1719,7 +1719,7 @@ E::WindowInfo Resolver::resolveWindow(const ParseWindow &parse_window,
 const CatalogRelation* Resolver::resolveRelationName(
     const ParseString *relation_name) {
   const CatalogRelation *relation =
-      context_->catalog_database()->getRelationByName(
+      catalog_database_.getRelationByName(
           ToLower(relation_name->value()));
   if (relation == nullptr) {
     THROW_SQL_ERROR_AT(relation_name) << "Unrecognized relation "

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/resolver/Resolver.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/resolver/Resolver.hpp b/query_optimizer/resolver/Resolver.hpp
index 7bfeed2..855e6ba 100644
--- a/query_optimizer/resolver/Resolver.hpp
+++ b/query_optimizer/resolver/Resolver.hpp
@@ -98,10 +98,12 @@ class Resolver {
   /**
    * @brief Constructor. Does not take ownership of \p context.
    *
+   * @param catalog_database The database that the query is executed on.
    * @param context The context of this query.
    */
-  explicit Resolver(OptimizerContext *context)
-      : context_(context) {}
+  Resolver(const CatalogDatabase &catalog_database, OptimizerContext *context)
+      : catalog_database_(catalog_database),
+        context_(context) {}
 
   /**
    * @brief Validates the query is semantically correct and converts the parse
@@ -673,6 +675,8 @@ class Resolver {
       std::vector<expressions::NamedExpressionPtr> *select_list_expressions,
       logical::LogicalPtr *logical_plan);
 
+  const CatalogDatabase &catalog_database_;
+
   OptimizerContext *context_;
   WithQueriesInfo with_queries_info_;
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/query_optimizer/tests/CMakeLists.txt b/query_optimizer/tests/CMakeLists.txt
index 1453291..4969ada 100644
--- a/query_optimizer/tests/CMakeLists.txt
+++ b/query_optimizer/tests/CMakeLists.txt
@@ -110,7 +110,6 @@ add_executable(quickstep_queryoptimizer_tests_OptimizerTextTest
                "${PROJECT_SOURCE_DIR}/utility/textbased_test/TextBasedTest.hpp")
 
 target_link_libraries(quickstep_queryoptimizer_tests_ExecutionGeneratorTest
-                      ${GFLAGS_LIB_NAME}
                       glog
                       gtest
                       quickstep_catalog_CatalogDatabase
@@ -118,21 +117,14 @@ target_link_libraries(quickstep_queryoptimizer_tests_ExecutionGeneratorTest
                       quickstep_cli_PrintToScreen
                       quickstep_parser_ParseStatement
                       quickstep_parser_SqlParserWrapper
-                      quickstep_queryexecution_AdmitRequestMessage
                       quickstep_queryexecution_ForemanSingleNode
-                      quickstep_queryexecution_QueryContext
                       quickstep_queryexecution_QueryExecutionTypedefs
                       quickstep_queryexecution_QueryExecutionUtil
                       quickstep_queryexecution_Worker
                       quickstep_queryexecution_WorkerDirectory
-                      quickstep_queryexecution_WorkerMessage
-                      quickstep_queryoptimizer_ExecutionGenerator
-                      quickstep_queryoptimizer_LogicalGenerator
+                      quickstep_queryoptimizer_Optimizer
                       quickstep_queryoptimizer_OptimizerContext
-                      quickstep_queryoptimizer_PhysicalGenerator
                       quickstep_queryoptimizer_QueryHandle
-                      quickstep_queryoptimizer_QueryPlan
-                      quickstep_queryoptimizer_physical_Physical
                       quickstep_queryoptimizer_tests_TestDatabaseLoader
                       quickstep_threading_ThreadIDBasedMap
                       quickstep_utility_Macros
@@ -140,13 +132,13 @@ target_link_libraries(quickstep_queryoptimizer_tests_ExecutionGeneratorTest
                       quickstep_utility_SqlError
                       quickstep_utility_TextBasedTestDriver
                       tmb
+                      ${GFLAGS_LIB_NAME}
                       ${LIBS})
 target_link_libraries(quickstep_queryoptimizer_tests_OptimizerTextTest
                       ${GFLAGS_LIB_NAME}
                       glog
                       gtest
                       gtest_main
-                      quickstep_parser_ParseStatement
                       quickstep_parser_SqlParserWrapper
                       quickstep_queryoptimizer_LogicalGenerator
                       quickstep_queryoptimizer_OptimizerContext

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/tests/ExecutionGeneratorTestRunner.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/tests/ExecutionGeneratorTestRunner.cpp b/query_optimizer/tests/ExecutionGeneratorTestRunner.cpp
index 2e01c7b..06397d4 100644
--- a/query_optimizer/tests/ExecutionGeneratorTestRunner.cpp
+++ b/query_optimizer/tests/ExecutionGeneratorTestRunner.cpp
@@ -26,27 +26,16 @@
 #include "cli/DropRelation.hpp"
 #include "cli/PrintToScreen.hpp"
 #include "parser/ParseStatement.hpp"
-#include "query_execution/AdmitRequestMessage.hpp"
 #include "query_execution/ForemanSingleNode.hpp"
 #include "query_execution/QueryExecutionUtil.hpp"
-#include "query_execution/Worker.hpp"
-#include "query_optimizer/ExecutionGenerator.hpp"
-#include "query_optimizer/LogicalGenerator.hpp"
+#include "query_optimizer/Optimizer.hpp"
 #include "query_optimizer/OptimizerContext.hpp"
-#include "query_optimizer/PhysicalGenerator.hpp"
 #include "query_optimizer/QueryHandle.hpp"
-#include "query_optimizer/QueryPlan.hpp"
-#include "query_optimizer/physical/Physical.hpp"
-#include "utility/Macros.hpp"
 #include "utility/MemStream.hpp"
-#include "utility/PtrList.hpp"
 #include "utility/SqlError.hpp"
 
 #include "glog/logging.h"
 
-#include "tmb/id_typedefs.h"
-#include "tmb/message_bus.h"
-
 namespace quickstep {
 
 class CatalogRelation;
@@ -74,28 +63,22 @@ void ExecutionGeneratorTestRunner::runTestCase(
 
   while (true) {
     ParseResult result = sql_parser_.getNextStatement();
-
-    OptimizerContext optimizer_context(test_database_loader_.catalog_database(),
-                                       test_database_loader_.storage_manager());
-
     if (result.condition != ParseResult::kSuccess) {
       if (result.condition == ParseResult::kError) {
         *output = result.error_message;
       }
       break;
     } else {
-      std::printf("%s\n", result.parsed_statement->toString().c_str());
+      const ParseStatement &parse_statement = *result.parsed_statement;
+      std::printf("%s\n", parse_statement.toString().c_str());
       try {
         QueryHandle query_handle(0 /* query_id */, main_thread_client_id_);
-        LogicalGenerator logical_generator(&optimizer_context);
-        PhysicalGenerator physical_generator;
-        ExecutionGenerator execution_generator(&optimizer_context,
-                                               &query_handle);
-
-        const physical::PhysicalPtr physical_plan =
-            physical_generator.generatePlan(
-                logical_generator.generatePlan(*result.parsed_statement));
-        execution_generator.generatePlan(physical_plan);
+        OptimizerContext optimizer_context;
+
+        optimizer_.generateQueryHandle(parse_statement,
+                                       test_database_loader_.catalog_database(),
+                                       &optimizer_context,
+                                       &query_handle);
 
         QueryExecutionUtil::ConstructAndSendAdmitRequestMessage(
             main_thread_client_id_,

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/tests/ExecutionGeneratorTestRunner.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/tests/ExecutionGeneratorTestRunner.hpp b/query_optimizer/tests/ExecutionGeneratorTestRunner.hpp
index b8cd02a..f4e11ea 100644
--- a/query_optimizer/tests/ExecutionGeneratorTestRunner.hpp
+++ b/query_optimizer/tests/ExecutionGeneratorTestRunner.hpp
@@ -32,6 +32,7 @@
 #include "query_execution/Worker.hpp"
 #include "query_execution/WorkerDirectory.hpp"
 #include "query_execution/WorkerMessage.hpp"
+#include "query_optimizer/Optimizer.hpp"
 #include "query_optimizer/tests/TestDatabaseLoader.hpp"
 #include "threading/ThreadIDBasedMap.hpp"
 #include "utility/Macros.hpp"
@@ -41,7 +42,6 @@
 #include "tmb/message_bus.h"
 
 namespace quickstep {
-
 namespace optimizer {
 
 /**
@@ -106,6 +106,7 @@ class ExecutionGeneratorTestRunner : public TextBasedTestRunner {
  private:
   SqlParserWrapper sql_parser_;
   TestDatabaseLoader test_database_loader_;
+  Optimizer optimizer_;
 
   MessageBusImpl bus_;
   std::unique_ptr<ForemanSingleNode> foreman_;

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/tests/OptimizerTest.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/tests/OptimizerTest.cpp b/query_optimizer/tests/OptimizerTest.cpp
index a93db3e..3838638 100644
--- a/query_optimizer/tests/OptimizerTest.cpp
+++ b/query_optimizer/tests/OptimizerTest.cpp
@@ -61,8 +61,7 @@ OptimizerTest::OptimizerTest()
     : catalog_(new Catalog),
       catalog_database_(
           new CatalogDatabase(catalog_.get(), "TestDatabase" /* name */, 0)),
-      optimizer_context_(new OptimizerContext(catalog_database_.get(),
-                                              nullptr /* storage_manager */)),
+      optimizer_context_(new OptimizerContext),
       physical_generator_(new PhysicalGenerator()) {}
 
 E::AliasPtr OptimizerTest::createAlias(const E::ExpressionPtr &expression,

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/0d8f242f/query_optimizer/tests/OptimizerTextTestRunner.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/tests/OptimizerTextTestRunner.cpp b/query_optimizer/tests/OptimizerTextTestRunner.cpp
index 93f3123..b9238c9 100644
--- a/query_optimizer/tests/OptimizerTextTestRunner.cpp
+++ b/query_optimizer/tests/OptimizerTextTestRunner.cpp
@@ -23,7 +23,6 @@
 #include <string>
 #include <vector>
 
-#include "parser/ParseStatement.hpp"
 #include "query_optimizer/LogicalGenerator.hpp"
 #include "query_optimizer/OptimizerContext.hpp"
 #include "query_optimizer/PhysicalGenerator.hpp"
@@ -49,8 +48,7 @@ void OptimizerTextTestRunner::runTestCase(const std::string &input,
   sql_parser_.feedNextBuffer(new std::string(input));
   ParseResult result = sql_parser_.getNextStatement();
 
-  OptimizerContext optimizer_context(test_database_loader_.catalog_database(),
-                                     nullptr /* storage_manager */);
+  OptimizerContext optimizer_context;
   if (result.condition != ParseResult::kSuccess) {
     *output = result.error_message;
   } else {
@@ -115,7 +113,7 @@ void OptimizerTextTestRunner::runTestCase(const std::string &input,
 logical::LogicalPtr OptimizerTextTestRunner::resolveParseTree(
     const ParseStatement &parse_statement,
     OptimizerContext *optimizer_context) {
-  resolver::Resolver resolver(optimizer_context);
+  resolver::Resolver resolver(*test_database_loader_.catalog_database(), optimizer_context);
   return resolver.resolve(parse_statement);
 }
 
@@ -123,7 +121,8 @@ logical::LogicalPtr OptimizerTextTestRunner::generateLogicalPlan(
     const ParseStatement &parse_statement,
     OptimizerContext *optimizer_context) {
   LogicalGenerator logical_generator(optimizer_context);
-  return logical_generator.generatePlan(parse_statement);
+  return logical_generator.generatePlan(*test_database_loader_.catalog_database(),
+                                        parse_statement);
 }
 
 physical::PhysicalPtr OptimizerTextTestRunner::generatePhysicalPlan(



Mime
View raw message