Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A62E8200BD7 for ; Sun, 11 Dec 2016 18:45:45 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id A4CC8160B35; Sun, 11 Dec 2016 17:45:45 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 87498160B20 for ; Sun, 11 Dec 2016 18:45:43 +0100 (CET) Received: (qmail 12876 invoked by uid 500); 11 Dec 2016 17:45:42 -0000 Mailing-List: contact commits-help@quickstep.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@quickstep.incubator.apache.org Delivered-To: mailing list commits@quickstep.incubator.apache.org Received: (qmail 12867 invoked by uid 99); 11 Dec 2016 17:45:42 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 11 Dec 2016 17:45:42 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 26DF7C1EE6 for ; Sun, 11 Dec 2016 17:45:42 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -6.219 X-Spam-Level: X-Spam-Status: No, score=-6.219 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id Ax7Ph1Kah8oN for ; Sun, 11 Dec 2016 17:45:36 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id EF15B5FC2A for ; Sun, 11 Dec 2016 17:45:25 +0000 (UTC) Received: (qmail 12290 invoked by uid 99); 11 Dec 2016 17:45:25 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 11 Dec 2016 17:45:25 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D9929E97DD; Sun, 11 Dec 2016 17:45:24 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: spehl@apache.org To: commits@quickstep.incubator.apache.org Date: Sun, 11 Dec 2016 17:45:35 -0000 Message-Id: <32a6828ccfcb4ab3958fe8689f539e0b@git.apache.org> In-Reply-To: <3731e3d9799c476f8fcd2a0cd4619eeb@git.apache.org> References: <3731e3d9799c476f8fcd2a0cd4619eeb@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [12/51] [abbrv] [partial] incubator-quickstep git commit: remove c++ files archived-at: Sun, 11 Dec 2016 17:45:45 -0000 http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/QueryProcessor.cpp ---------------------------------------------------------------------- diff --git a/query_optimizer/QueryProcessor.cpp b/query_optimizer/QueryProcessor.cpp deleted file mode 100644 index d74eeba..0000000 --- a/query_optimizer/QueryProcessor.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#include "query_optimizer/QueryProcessor.hpp" - -#include -#include -#include - -#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; - -namespace quickstep { - -void QueryProcessor::generateQueryHandle(const ParseStatement &statement, - QueryHandle *query_handle) { - optimizer::OptimizerContext optimizer_context; - - optimizer_.generateQueryHandle(statement, getDefaultDatabase(), &optimizer_context, query_handle); - - if (optimizer_context.is_catalog_changed() && !catalog_altered_) { - catalog_altered_ = true; - } - - ++query_id_; -} - -void QueryProcessor::saveCatalog() { - if (catalog_altered_) { - ofstream catalog_file(catalog_filename_.c_str()); - - if (!catalog_->getProto().SerializeToOstream(&catalog_file)) { - throw UnableToWriteCatalog(catalog_filename_); - } - - catalog_file.close(); - - catalog_altered_ = false; - } -} - -void QueryProcessor::loadCatalog() { - ifstream catalog_file(catalog_filename_.c_str()); - if (!catalog_file.good()) { - throw UnableToReadCatalog(catalog_filename_); - } - - serialization::Catalog catalog_proto; - if (!catalog_proto.ParseFromIstream(&catalog_file)) { - throw CatalogNotProto(catalog_filename_); - } - catalog_file.close(); - catalog_ = std::make_unique(catalog_proto); - - catalog_altered_ = false; -} - -} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/QueryProcessor.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/QueryProcessor.hpp b/query_optimizer/QueryProcessor.hpp deleted file mode 100644 index 2b39b84..0000000 --- a/query_optimizer/QueryProcessor.hpp +++ /dev/null @@ -1,204 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#ifndef QUICKSTEP_QUERY_OPTIMIZER_QUERY_PROCESSOR_HPP_ -#define QUICKSTEP_QUERY_OPTIMIZER_QUERY_PROCESSOR_HPP_ - -#include -#include -#include -#include -#include - -#include "catalog/Catalog.hpp" -#include "query_optimizer/Optimizer.hpp" -#include "utility/Macros.hpp" - -namespace quickstep { - -class CatalogDatabase; -class ParseStatement; -class QueryHandle; - -/** \addtogroup QueryOptimizer - * @{ - */ - -/** - * @brief Exception thrown when unable to read the catalog from disk. - **/ -class UnableToReadCatalog : public std::exception { - public: - /** - * @brief Constructor. - * - * @param filename The catalog filename provided. - **/ - explicit UnableToReadCatalog(const std::string &filename) - : message_("UnableToReadCatalog: Unable to read catalog from file ") { - message_.append(filename); - } - - ~UnableToReadCatalog() throw() { - } - - virtual const char* what() const throw() { - return message_.c_str(); - } - - private: - std::string message_; -}; - -/** - * @brief Exception thrown when unable to write the catalog to disk. - **/ -class UnableToWriteCatalog : public std::exception { - public: - /** - * @brief Constructor. - * - * @param filename The catalog filename provided. - **/ - explicit UnableToWriteCatalog(const std::string &filename) - : message_("UnableToWriteCatalog: Unable to write catalog to file ") { - message_.append(filename); - } - - ~UnableToWriteCatalog() throw() { - } - - virtual const char* what() const throw() { - return message_.c_str(); - } - - private: - std::string message_; -}; - -/** - * @brief Exception thrown when there is a Proto parse error when loading the - * catalog. - **/ -class CatalogNotProto : public std::exception { - public: - /** - * @brief Constructor. - * - * @param filename The catalog filename provided. - **/ - explicit CatalogNotProto(const std::string &filename) - : message_("CatalogNotProto: The file ") { - message_.append(filename).append(" is not valid Proto"); - } - - ~CatalogNotProto() throw() { - } - - virtual const char* what() const throw() { - return message_.c_str(); - } - - private: - std::string message_; -}; - - -/** - * @brief An object which manages the state of quickstep and the execution of - * queries. - **/ -class QueryProcessor { - public: - /** - * @brief Constructor. - * - * @param catalog_filename The file to read the serialized catalog from. - **/ - explicit QueryProcessor(std::string &&catalog_filename) // NOLINT(whitespace/operators) - : catalog_filename_(std::move(catalog_filename)), - catalog_altered_(false), - query_id_(0) { - loadCatalog(); - } - - /** - * @brief Destructor. - **/ - ~QueryProcessor() {} - - /** - * @brief Get the next query id. - **/ - std::size_t query_id() const { - return query_id_; - } - - /** - * @brief Fill a query handle for the given parsed SQL statement. This - * includes that the optimizer creates a QueryPlan inside the handle. - * - * @param statement The parsed SQL statement to generate a query handle for. - * @param query_handle The generated query handle to output. - **/ - void generateQueryHandle(const ParseStatement &statement, - QueryHandle *query_handle); - - /** - * @brief Save the catalog back to disk. - **/ - void saveCatalog(); - - /** - * @brief Set \p catalog_altered_ to true to indicate that the catalog - * has been altered. - */ - void markCatalogAltered() { - catalog_altered_ = true; - } - - /** - * @brief Get the default database in the Catalog held by this - * QueryProcessor. - **/ - CatalogDatabase* getDefaultDatabase() const { - return catalog_->getDatabaseByNameMutable("default"); - } - - private: - void loadCatalog(); // If it exists, free catalog_ before calling this - - optimizer::Optimizer optimizer_; - - const std::string catalog_filename_; - - std::unique_ptr catalog_; - - bool catalog_altered_; - - std::size_t query_id_; - - DISALLOW_COPY_AND_ASSIGN(QueryProcessor); -}; - -/** @} */ - -} // namespace quickstep - -#endif // QUICKSTEP_QUERY_OPTIMIZER_QUERY_PROCESSOR_HPP_ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/Validator.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/Validator.hpp b/query_optimizer/Validator.hpp deleted file mode 100644 index 9f8b6fd..0000000 --- a/query_optimizer/Validator.hpp +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#ifndef QUICKSTEP_QUERY_OPTIMIZER_VALIDATOR_HPP_ -#define QUICKSTEP_QUERY_OPTIMIZER_VALIDATOR_HPP_ - -#include -#include - -#include "query_optimizer/expressions/AttributeReference.hpp" -#include "query_optimizer/expressions/ExpressionUtil.hpp" - -#include "glog/logging.h" - -namespace quickstep { -namespace optimizer { - -/** \addtogroup QueryOptimizer - * @{ - */ - -/** - * @brief Validate that the plan does not have a dangling attribute reference. - * - * @param plan The plan to be validated. - */ -template -static void Validate(const std::shared_ptr &plan) { - if (plan->getNumChildren() > 0) { - const std::vector - referenced_attributes = plan->getReferencedAttributes(); - std::vector visible_attributes; - for (const std::shared_ptr &child : plan->children()) { - Validate(child); - const std::vector input_attributes = - child->getOutputAttributes(); - visible_attributes.insert(visible_attributes.end(), - input_attributes.begin(), - input_attributes.end()); - } - for (const expressions::AttributeReferencePtr &referenced_attribute : - referenced_attributes) { - CHECK(expressions::ContainsExpression(visible_attributes, - referenced_attribute)) - << "\nDangling attribute reference " - << referenced_attribute->getShortString() << " in the plan:\n" - << plan->toString(); - } - } -} - -/** @} */ - -} // namespace optimizer -} // namespace quickstep - -#endif /* QUICKSTEP_QUERY_OPTIMIZER_VALIDATOR_HPP_ */ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/cost_model/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/query_optimizer/cost_model/CMakeLists.txt b/query_optimizer/cost_model/CMakeLists.txt deleted file mode 100644 index 032e34c..0000000 --- a/query_optimizer/cost_model/CMakeLists.txt +++ /dev/null @@ -1,81 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -# Declare micro-libs: -add_library(quickstep_queryoptimizer_costmodel_CostModel ../../empty_src.cpp CostModel.hpp) -add_library(quickstep_queryoptimizer_costmodel_SimpleCostModel SimpleCostModel.cpp SimpleCostModel.hpp) -add_library(quickstep_queryoptimizer_costmodel_StarSchemaSimpleCostModel - StarSchemaSimpleCostModel.cpp - StarSchemaSimpleCostModel.hpp) - -# Link dependencies: -target_link_libraries(quickstep_queryoptimizer_costmodel_CostModel - quickstep_queryoptimizer_physical_Aggregate - quickstep_queryoptimizer_physical_Physical - quickstep_utility_Macros) -target_link_libraries(quickstep_queryoptimizer_costmodel_SimpleCostModel - glog - quickstep_catalog_CatalogRelation - quickstep_catalog_CatalogRelationStatistics - quickstep_queryoptimizer_costmodel_CostModel - quickstep_queryoptimizer_physical_Aggregate - quickstep_queryoptimizer_physical_HashJoin - quickstep_queryoptimizer_physical_NestedLoopsJoin - quickstep_queryoptimizer_physical_Physical - quickstep_queryoptimizer_physical_PhysicalType - quickstep_queryoptimizer_physical_Selection - quickstep_queryoptimizer_physical_SharedSubplanReference - quickstep_queryoptimizer_physical_Sort - quickstep_queryoptimizer_physical_TableGenerator - quickstep_queryoptimizer_physical_TableReference - quickstep_queryoptimizer_physical_TopLevelPlan - quickstep_queryoptimizer_physical_WindowAggregate - quickstep_utility_Macros) -target_link_libraries(quickstep_queryoptimizer_costmodel_StarSchemaSimpleCostModel - glog - quickstep_catalog_CatalogRelation - quickstep_queryoptimizer_costmodel_CostModel - quickstep_queryoptimizer_expressions_AttributeReference - quickstep_queryoptimizer_expressions_ComparisonExpression - quickstep_queryoptimizer_expressions_ExprId - quickstep_queryoptimizer_expressions_ExpressionType - quickstep_queryoptimizer_expressions_ExpressionUtil - quickstep_queryoptimizer_expressions_LogicalAnd - quickstep_queryoptimizer_expressions_LogicalOr - quickstep_queryoptimizer_expressions_PatternMatcher - quickstep_queryoptimizer_expressions_Predicate - quickstep_queryoptimizer_physical_Aggregate - quickstep_queryoptimizer_physical_HashJoin - quickstep_queryoptimizer_physical_NestedLoopsJoin - quickstep_queryoptimizer_physical_PatternMatcher - quickstep_queryoptimizer_physical_Physical - quickstep_queryoptimizer_physical_PhysicalType - quickstep_queryoptimizer_physical_Selection - quickstep_queryoptimizer_physical_SharedSubplanReference - quickstep_queryoptimizer_physical_Sort - quickstep_queryoptimizer_physical_TableGenerator - quickstep_queryoptimizer_physical_TableReference - quickstep_queryoptimizer_physical_TopLevelPlan - quickstep_queryoptimizer_physical_WindowAggregate - quickstep_utility_Macros) - -# Module all-in-one library: -add_library(quickstep_queryoptimizer_costmodel ../../empty_src.cpp CostModelModule.hpp) -target_link_libraries(quickstep_queryoptimizer_costmodel - quickstep_queryoptimizer_costmodel_CostModel - quickstep_queryoptimizer_costmodel_SimpleCostModel - quickstep_queryoptimizer_costmodel_StarSchemaSimpleCostModel) http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/cost_model/CostModel.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/cost_model/CostModel.hpp b/query_optimizer/cost_model/CostModel.hpp deleted file mode 100644 index 20bc208..0000000 --- a/query_optimizer/cost_model/CostModel.hpp +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#ifndef QUERY_OPTIMIZER_COST_MODEL_COST_MODEL_HPP_ -#define QUERY_OPTIMIZER_COST_MODEL_COST_MODEL_HPP_ - -#include - -#include "query_optimizer/physical/Aggregate.hpp" -#include "query_optimizer/physical/Physical.hpp" -#include "utility/Macros.hpp" - -namespace quickstep { -namespace optimizer { -namespace cost { - -/** \addtogroup CostModel - * @{ - */ - -/** - * @brief Interface to a cost model of physical plans. - */ -class CostModel { - public: - /** - * @brief Destructor. - */ - virtual ~CostModel() {} - - /** - * @brief Estimate the cardinality of the output relation of \p physical_plan. - * - * @param physical_plan The physical plan for which the cardinality of the - * output relation is estimated. - * @return The estimated cardinality. - */ - virtual std::size_t estimateCardinality( - const physical::PhysicalPtr &physical_plan) = 0; - - /** - * @brief Estimate the number of groups of an aggregation. - * - * @param aggregate The physical plan of the aggregation. - * @return The estimated number of groups. - */ - virtual std::size_t estimateNumGroupsForAggregate(const physical::AggregatePtr &aggregate) { - return estimateCardinality(aggregate); - } - - protected: - /** - * @brief Constructor. - */ - CostModel() {} - - private: - DISALLOW_COPY_AND_ASSIGN(CostModel); -}; - -/** @} */ - -} // namespace cost -} // namespace optimizer -} // namespace quickstep - -#endif /* QUERY_OPTIMIZER_COST_MODEL_COST_MODEL_HPP_ */ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/cost_model/CostModelModule.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/cost_model/CostModelModule.hpp b/query_optimizer/cost_model/CostModelModule.hpp deleted file mode 100644 index 41bc053..0000000 --- a/query_optimizer/cost_model/CostModelModule.hpp +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -/** @defgroup CostModel - * @ingroup QueryOptimizer - * - * Models that estimate the costs of physical plans. - **/ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/cost_model/SimpleCostModel.cpp ---------------------------------------------------------------------- diff --git a/query_optimizer/cost_model/SimpleCostModel.cpp b/query_optimizer/cost_model/SimpleCostModel.cpp deleted file mode 100644 index a803c67..0000000 --- a/query_optimizer/cost_model/SimpleCostModel.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#include "query_optimizer/cost_model/SimpleCostModel.hpp" - -#include -#include - -#include "catalog/CatalogRelation.hpp" -#include "catalog/CatalogRelationStatistics.hpp" -#include "query_optimizer/physical/Aggregate.hpp" -#include "query_optimizer/physical/NestedLoopsJoin.hpp" -#include "query_optimizer/physical/HashJoin.hpp" -#include "query_optimizer/physical/Physical.hpp" -#include "query_optimizer/physical/PhysicalType.hpp" -#include "query_optimizer/physical/Selection.hpp" -#include "query_optimizer/physical/SharedSubplanReference.hpp" -#include "query_optimizer/physical/Sort.hpp" -#include "query_optimizer/physical/TableGenerator.hpp" -#include "query_optimizer/physical/TableReference.hpp" -#include "query_optimizer/physical/TopLevelPlan.hpp" -#include "query_optimizer/physical/WindowAggregate.hpp" - -#include "glog/logging.h" - -namespace quickstep { -namespace optimizer { -namespace cost { - -namespace P = ::quickstep::optimizer::physical; - -std::size_t SimpleCostModel::estimateCardinality( - const P::PhysicalPtr &physical_plan) { - switch (physical_plan->getPhysicalType()) { - case P::PhysicalType::kTopLevelPlan: - return estimateCardinalityForTopLevelPlan( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kTableReference: - return estimateCardinalityForTableReference( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kSelection: - return estimateCardinalityForSelection( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kTableGenerator: - return estimateCardinalityForTableGenerator( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kHashJoin: - return estimateCardinalityForHashJoin( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kNestedLoopsJoin: - return estimateCardinalityForNestedLoopsJoin( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kAggregate: - return estimateCardinalityForAggregate( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kSharedSubplanReference: { - const P::SharedSubplanReferencePtr shared_subplan_reference = - std::static_pointer_cast(physical_plan); - return estimateCardinality( - shared_subplans_[shared_subplan_reference->subplan_id()]); - } - case P::PhysicalType::kSort: - return estimateCardinalityForSort( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kWindowAggregate: - return estimateCardinalityForWindowAggregate( - std::static_pointer_cast(physical_plan)); - default: - LOG(FATAL) << "Unsupported physical plan:" << physical_plan->toString(); - } -} - -std::size_t SimpleCostModel::estimateCardinalityForTopLevelPlan( - const P::TopLevelPlanPtr &physical_plan) { - return estimateCardinality(physical_plan->plan()); -} - -std::size_t SimpleCostModel::estimateCardinalityForTableReference( - const P::TableReferencePtr &physical_plan) { - const std::size_t num_tuples_in_relation = - physical_plan->relation()->getStatistics().getNumTuples(); - if (num_tuples_in_relation == 0) { - return physical_plan->relation()->estimateTupleCardinality(); - } else { - return num_tuples_in_relation; - } -} - -std::size_t SimpleCostModel::estimateCardinalityForSelection( - const P::SelectionPtr &physical_plan) { - return estimateCardinality(physical_plan->input()); -} - -std::size_t SimpleCostModel::estimateCardinalityForSort( - const physical::SortPtr &physical_plan) { - std::size_t cardinality = estimateCardinality( - std::static_pointer_cast(physical_plan)->input()); - return std::min(cardinality, static_cast(physical_plan->limit())); -} - -std::size_t SimpleCostModel::estimateCardinalityForTableGenerator( - const P::TableGeneratorPtr &physical_plan) { - return physical_plan->generator_function_handle()->getEstimatedCardinality(); -} - -std::size_t SimpleCostModel::estimateCardinalityForHashJoin( - const P::HashJoinPtr &physical_plan) { - return std::max(estimateCardinality(physical_plan->left()), - estimateCardinality(physical_plan->right())); -} - -std::size_t SimpleCostModel::estimateCardinalityForNestedLoopsJoin( - const P::NestedLoopsJoinPtr &physical_plan) { - return std::max(estimateCardinality(physical_plan->left()), - estimateCardinality(physical_plan->right())); -} - -std::size_t SimpleCostModel::estimateCardinalityForAggregate( - const physical::AggregatePtr &physical_plan) { - if (physical_plan->grouping_expressions().empty()) { - return 1; - } - return std::max(static_cast(1), - estimateCardinality(physical_plan->input()) / 10); -} - -std::size_t SimpleCostModel::estimateCardinalityForWindowAggregate( - const physical::WindowAggregatePtr &physical_plan) { - return estimateCardinality(physical_plan->input()); -} - -} // namespace cost -} // namespace optimizer -} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/cost_model/SimpleCostModel.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/cost_model/SimpleCostModel.hpp b/query_optimizer/cost_model/SimpleCostModel.hpp deleted file mode 100644 index 16366cd..0000000 --- a/query_optimizer/cost_model/SimpleCostModel.hpp +++ /dev/null @@ -1,113 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#ifndef QUERY_OPTIMIZER_COST_MODEL_SIMPLE_COST_MODEL_HPP_ -#define QUERY_OPTIMIZER_COST_MODEL_SIMPLE_COST_MODEL_HPP_ - -#include -#include - -#include "query_optimizer/cost_model/CostModel.hpp" -#include "query_optimizer/physical/Aggregate.hpp" -#include "query_optimizer/physical/NestedLoopsJoin.hpp" -#include "query_optimizer/physical/HashJoin.hpp" -#include "query_optimizer/physical/Physical.hpp" -#include "query_optimizer/physical/Selection.hpp" -#include "query_optimizer/physical/Sort.hpp" -#include "query_optimizer/physical/TableGenerator.hpp" -#include "query_optimizer/physical/TableReference.hpp" -#include "query_optimizer/physical/TopLevelPlan.hpp" -#include "query_optimizer/physical/WindowAggregate.hpp" -#include "utility/Macros.hpp" - -namespace quickstep { -namespace optimizer { -namespace cost { - -/** \addtogroup CostModel - * @{ - */ - -/** - * @brief A simple cost model. - */ -class SimpleCostModel : public CostModel { - public: - /** - * @brief Constructor. - */ - explicit SimpleCostModel(const std::vector &shared_subplans) - : shared_subplans_(shared_subplans) {} - - std::size_t estimateCardinality( - const physical::PhysicalPtr &physical_plan) override; - - private: - // Returns the estimated cardinality of the input plan. - std::size_t estimateCardinalityForTopLevelPlan( - const physical::TopLevelPlanPtr &physical_plan); - - // Returns the estimated cardinality of the relation. - std::size_t estimateCardinalityForTableReference( - const physical::TableReferencePtr &physical_plan); - - // Returns the estimated cardinality of the input plan. - std::size_t estimateCardinalityForSelection( - const physical::SelectionPtr &physical_plan); - - // Returns the estimated cardinality of the input plan. - std::size_t estimateCardinalityForTableGenerator( - const physical::TableGeneratorPtr &physical_plan); - - // Returns the estimated cardinality as K if there is a LIMIT K clause, - // otherwise returns the estimated cardinality of the input plan. - std::size_t estimateCardinalityForSort( - const physical::SortPtr &physical_plan); - - // Returns the larger value of the estimated cardinalities of two - // input plans. - std::size_t estimateCardinalityForHashJoin( - const physical::HashJoinPtr &physical_plan); - - // Returns the larger value of the estimated cardinalities of two - // input plans. - std::size_t estimateCardinalityForNestedLoopsJoin( - const physical::NestedLoopsJoinPtr &physical_plan); - - // Returns 1 for a scalar aggregation; otherwise, returns the estimated - // cardinality of the input plan divided by 10. - std::size_t estimateCardinalityForAggregate( - const physical::AggregatePtr &physical_plan); - - // Return the estimated cardinality of the input plan. - std::size_t estimateCardinalityForWindowAggregate( - const physical::WindowAggregatePtr &physical_plan); - - const std::vector &shared_subplans_; - - DISALLOW_COPY_AND_ASSIGN(SimpleCostModel); -}; - -/** @} */ - -} // namespace cost -} // namespace optimizer -} // namespace quickstep - -#endif /* QUERY_OPTIMIZER_COST_MODEL_SIMPLE_COST_MODEL_HPP_ */ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/cost_model/StarSchemaSimpleCostModel.cpp ---------------------------------------------------------------------- diff --git a/query_optimizer/cost_model/StarSchemaSimpleCostModel.cpp b/query_optimizer/cost_model/StarSchemaSimpleCostModel.cpp deleted file mode 100644 index 1075739..0000000 --- a/query_optimizer/cost_model/StarSchemaSimpleCostModel.cpp +++ /dev/null @@ -1,399 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#include "query_optimizer/cost_model/StarSchemaSimpleCostModel.hpp" - -#include -#include -#include -#include - -#include "catalog/CatalogRelation.hpp" -#include "query_optimizer/expressions/AttributeReference.hpp" -#include "query_optimizer/expressions/ComparisonExpression.hpp" -#include "query_optimizer/expressions/ExprId.hpp" -#include "query_optimizer/expressions/ExpressionType.hpp" -#include "query_optimizer/expressions/ExpressionUtil.hpp" -#include "query_optimizer/expressions/LogicalAnd.hpp" -#include "query_optimizer/expressions/LogicalOr.hpp" -#include "query_optimizer/expressions/Predicate.hpp" -#include "query_optimizer/expressions/PatternMatcher.hpp" -#include "query_optimizer/physical/Aggregate.hpp" -#include "query_optimizer/physical/NestedLoopsJoin.hpp" -#include "query_optimizer/physical/HashJoin.hpp" -#include "query_optimizer/physical/PatternMatcher.hpp" -#include "query_optimizer/physical/Physical.hpp" -#include "query_optimizer/physical/PhysicalType.hpp" -#include "query_optimizer/physical/Selection.hpp" -#include "query_optimizer/physical/SharedSubplanReference.hpp" -#include "query_optimizer/physical/Sort.hpp" -#include "query_optimizer/physical/TableGenerator.hpp" -#include "query_optimizer/physical/TableReference.hpp" -#include "query_optimizer/physical/TopLevelPlan.hpp" - -#include "glog/logging.h" - -namespace quickstep { -namespace optimizer { -namespace cost { - -namespace E = ::quickstep::optimizer::expressions; -namespace P = ::quickstep::optimizer::physical; - -std::size_t StarSchemaSimpleCostModel::estimateCardinality( - const P::PhysicalPtr &physical_plan) { - switch (physical_plan->getPhysicalType()) { - case P::PhysicalType::kTopLevelPlan: - return estimateCardinalityForTopLevelPlan( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kTableReference: - return estimateCardinalityForTableReference( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kSelection: - return estimateCardinalityForSelection( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kTableGenerator: - return estimateCardinalityForTableGenerator( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kHashJoin: - return estimateCardinalityForHashJoin( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kNestedLoopsJoin: - return estimateCardinalityForNestedLoopsJoin( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kAggregate: - return estimateCardinalityForAggregate( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kSharedSubplanReference: { - const P::SharedSubplanReferencePtr shared_subplan_reference = - std::static_pointer_cast(physical_plan); - return estimateCardinality( - shared_subplans_[shared_subplan_reference->subplan_id()]); - } - case P::PhysicalType::kSort: - return estimateCardinalityForSort( - std::static_pointer_cast(physical_plan)); - case P::PhysicalType::kWindowAggregate: - return estimateCardinalityForWindowAggregate( - std::static_pointer_cast(physical_plan)); - default: - LOG(FATAL) << "Unsupported physical plan:" << physical_plan->toString(); - } -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForTopLevelPlan( - const P::TopLevelPlanPtr &physical_plan) { - return estimateCardinality(physical_plan->plan()); -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForTableReference( - const P::TableReferencePtr &physical_plan) { - std::size_t num_tuples = physical_plan->relation()->getStatistics().getNumTuples(); - if (num_tuples == 0) { - num_tuples = physical_plan->relation()->estimateTupleCardinality(); - } - return num_tuples; -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForSelection( - const P::SelectionPtr &physical_plan) { - double selectivity = - estimateSelectivityForPredicate(physical_plan->filter_predicate(), physical_plan); - return static_cast( - estimateCardinality(physical_plan->input()) * selectivity); -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForSort( - const physical::SortPtr &physical_plan) { - std::size_t cardinality = estimateCardinality( - std::static_pointer_cast(physical_plan)->input()); - return std::min(cardinality, static_cast(physical_plan->limit())); -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForTableGenerator( - const P::TableGeneratorPtr &physical_plan) { - return physical_plan->generator_function_handle()->getEstimatedCardinality(); -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForHashJoin( - const P::HashJoinPtr &physical_plan) { - std::size_t left_cardinality = estimateCardinality(physical_plan->left()); - std::size_t right_cardinality = estimateCardinality(physical_plan->right()); - double left_selectivity = estimateSelectivity(physical_plan->left()); - double right_selectivity = estimateSelectivity(physical_plan->right()); - return std::max(static_cast(left_cardinality * right_selectivity + 0.5), - static_cast(right_cardinality * left_selectivity + 0.5)); -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForNestedLoopsJoin( - const P::NestedLoopsJoinPtr &physical_plan) { - return std::max(estimateCardinality(physical_plan->left()), - estimateCardinality(physical_plan->right())); -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForAggregate( - const P::AggregatePtr &physical_plan) { - double filter_selectivity = - estimateSelectivityForPredicate(physical_plan->filter_predicate(), physical_plan); - return static_cast( - estimateNumGroupsForAggregate(physical_plan) * filter_selectivity); -} - -std::size_t StarSchemaSimpleCostModel::estimateCardinalityForWindowAggregate( - const P::WindowAggregatePtr &physical_plan) { - return estimateCardinality(physical_plan->input()); -} - - -std::size_t StarSchemaSimpleCostModel::estimateNumGroupsForAggregate( - const physical::AggregatePtr &aggregate) { - if (aggregate->grouping_expressions().empty()) { - return 1uL; - } - - std::size_t estimated_child_cardinality = estimateCardinality(aggregate->input()); - std::size_t estimated_num_groups = 1; - std::size_t max_attr_num_distinct_values = 0; - for (const auto &expr : aggregate->grouping_expressions()) { - E::AttributeReferencePtr attr; - if (E::SomeAttributeReference::MatchesWithConditionalCast(expr, &attr)) { - std::size_t attr_num_distinct_values = - estimateNumDistinctValues(attr->id(), aggregate->input()); - estimated_num_groups *= std::max(1uL, attr_num_distinct_values); - max_attr_num_distinct_values = - std::max(max_attr_num_distinct_values, attr_num_distinct_values); - } else { - // TODO(jianqiao): implement estimateNumDistinctValues() for expressions. - estimated_num_groups *= 64uL; - } - } - estimated_num_groups = std::max( - std::min(estimated_num_groups, estimated_child_cardinality / 10), - max_attr_num_distinct_values); - return estimated_num_groups; -} - - -std::size_t StarSchemaSimpleCostModel::estimateNumDistinctValues( - const expressions::ExprId attribute_id, - const physical::PhysicalPtr &physical_plan) { - DCHECK(E::ContainsExprId(physical_plan->getOutputAttributes(), attribute_id)); - - P::TableReferencePtr table_reference; - if (P::SomeTableReference::MatchesWithConditionalCast(physical_plan, &table_reference)) { - return getNumDistinctValues(attribute_id, table_reference); - } - - double filter_selectivity = estimateSelectivityForFilterPredicate(physical_plan); - switch (physical_plan->getPhysicalType()) { - case P::PhysicalType::kSelection: // Fall through - case P::PhysicalType::kAggregate: { - const P::PhysicalPtr &child = physical_plan->children()[0]; - if (E::ContainsExprId(child->getOutputAttributes(), attribute_id)) { - std::size_t child_num_distinct_values = - estimateNumDistinctValues(attribute_id, child); - return static_cast( - child_num_distinct_values * filter_selectivity + 0.5); - } - break; - } - case P::PhysicalType::kHashJoin: { - const P::HashJoinPtr &hash_join = - std::static_pointer_cast(physical_plan); - if (E::ContainsExprId(hash_join->left()->getOutputAttributes(), attribute_id)) { - std::size_t left_child_num_distinct_values = - estimateNumDistinctValues(attribute_id, hash_join->left()); - double right_child_selectivity = - estimateSelectivity(hash_join->right()); - return static_cast( - left_child_num_distinct_values * right_child_selectivity * filter_selectivity + 0.5); - } - if (E::ContainsExprId(hash_join->right()->getOutputAttributes(), attribute_id)) { - std::size_t right_child_num_distinct_values = - estimateNumDistinctValues(attribute_id, hash_join->right()); - double left_child_selectivity = - estimateSelectivity(hash_join->left()); - return static_cast( - right_child_num_distinct_values * left_child_selectivity * filter_selectivity + 0.5); - } - } - default: - break; - } - - return 16uL; -} - -double StarSchemaSimpleCostModel::estimateSelectivity( - const physical::PhysicalPtr &physical_plan) { - switch (physical_plan->getPhysicalType()) { - case P::PhysicalType::kSelection: { - const P::SelectionPtr &selection = - std::static_pointer_cast(physical_plan); - double filter_selectivity = - estimateSelectivityForPredicate(selection->filter_predicate(), selection); - double child_selectivity = estimateSelectivity(selection->input()); - return filter_selectivity * child_selectivity; - } - case P::PhysicalType::kHashJoin: { - const P::HashJoinPtr &hash_join = - std::static_pointer_cast(physical_plan); - double filter_selectivity = - estimateSelectivityForPredicate(hash_join->residual_predicate(), hash_join); - double child_selectivity = - estimateSelectivity(hash_join->left()) * estimateSelectivity(hash_join->right()); - return filter_selectivity * child_selectivity; - } - case P::PhysicalType::kNestedLoopsJoin: { - const P::NestedLoopsJoinPtr &nested_loop_join = - std::static_pointer_cast(physical_plan); - double filter_selectivity = - estimateSelectivityForPredicate(nested_loop_join->join_predicate(), nested_loop_join); - double child_selectivity = std::min( - estimateSelectivity(nested_loop_join->left()), - estimateSelectivity(nested_loop_join->right())); - return filter_selectivity * child_selectivity; - } - case P::PhysicalType::kSharedSubplanReference: { - const P::SharedSubplanReferencePtr shared_subplan_reference = - std::static_pointer_cast(physical_plan); - return estimateSelectivity( - shared_subplans_[shared_subplan_reference->subplan_id()]); - } - default: - break; - } - - if (physical_plan->getNumChildren() == 1) { - return estimateSelectivity(physical_plan->children()[0]); - } - - return 1.0; -} - -double StarSchemaSimpleCostModel::estimateSelectivityForFilterPredicate( - const physical::PhysicalPtr &physical_plan) { - E::PredicatePtr filter_predicate = nullptr; - switch (physical_plan->getPhysicalType()) { - case P::PhysicalType::kSelection: - filter_predicate = - std::static_pointer_cast(physical_plan)->filter_predicate(); - break; - case P::PhysicalType::kAggregate: - filter_predicate = - std::static_pointer_cast(physical_plan)->filter_predicate(); - break; - case P::PhysicalType::kHashJoin: - filter_predicate = - std::static_pointer_cast(physical_plan)->residual_predicate(); - break; - case P::PhysicalType::kNestedLoopsJoin: - filter_predicate = - std::static_pointer_cast(physical_plan)->join_predicate(); - break; - default: - break; - } - - if (filter_predicate == nullptr) { - return 1.0; - } else { - return estimateSelectivityForPredicate(filter_predicate, physical_plan); - } -} - - -double StarSchemaSimpleCostModel::estimateSelectivityForPredicate( - const expressions::PredicatePtr &filter_predicate, - const P::PhysicalPtr &physical_plan) { - if (filter_predicate == nullptr) { - return 1.0; - } - - switch (filter_predicate->getExpressionType()) { - case E::ExpressionType::kComparisonExpression: { - // Case 1 - Number of distinct values statistics available - // Case 1.1 - Equality comparison: 1.0 / num_distinct_values - // Case 1.2 - Otherwise: 0.1 - // Case 2 - Otherwise: 0.5 - const E::ComparisonExpressionPtr &comparison_expression = - std::static_pointer_cast(filter_predicate); - E::AttributeReferencePtr attr; - if ((E::SomeAttributeReference::MatchesWithConditionalCast(comparison_expression->left(), &attr) && - E::SomeScalarLiteral::Matches(comparison_expression->right())) || - (E::SomeAttributeReference::MatchesWithConditionalCast(comparison_expression->right(), &attr) && - E::SomeScalarLiteral::Matches(comparison_expression->left()))) { - for (const auto &child : physical_plan->children()) { - if (E::ContainsExprId(child->getOutputAttributes(), attr->id())) { - const std::size_t child_num_distinct_values = estimateNumDistinctValues(attr->id(), child); - if (comparison_expression->isEqualityComparisonPredicate()) { - return 1.0 / child_num_distinct_values; - } else { - return 1.0 / std::max(std::min(child_num_distinct_values / 100.0, 10.0), 2.0); - } - } - } - return 0.1; - } - return 0.5; - } - case E::ExpressionType::kLogicalAnd: { - const E::LogicalAndPtr &logical_and = - std::static_pointer_cast(filter_predicate); - double selectivity = 1.0; - for (const auto &predicate : logical_and->operands()) { - selectivity = std::min(selectivity, estimateSelectivityForPredicate(predicate, physical_plan)); - } - return selectivity; - } - case E::ExpressionType::kLogicalOr: { - const E::LogicalOrPtr &logical_or = - std::static_pointer_cast(filter_predicate); - double selectivity = 0; - for (const auto &predicate : logical_or->operands()) { - selectivity += estimateSelectivityForPredicate(predicate, physical_plan); - } - return std::min(selectivity, 1.0); - } - default: - break; - } - return 1.0; -} - -std::size_t StarSchemaSimpleCostModel::getNumDistinctValues( - const E::ExprId attribute_id, - const P::TableReferencePtr &table_reference) { - const CatalogRelation &relation = *table_reference->relation(); - const std::vector &attributes = table_reference->attribute_list(); - for (std::size_t i = 0; i < attributes.size(); ++i) { - if (attributes[i]->id() == attribute_id) { - std::size_t num_distinct_values = relation.getStatistics().getNumDistinctValues(i); - if (num_distinct_values > 0) { - return num_distinct_values; - } - break; - } - } - return estimateCardinalityForTableReference(table_reference); -} - -} // namespace cost -} // namespace optimizer -} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/cost_model/StarSchemaSimpleCostModel.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/cost_model/StarSchemaSimpleCostModel.hpp b/query_optimizer/cost_model/StarSchemaSimpleCostModel.hpp deleted file mode 100644 index 6f6aa29..0000000 --- a/query_optimizer/cost_model/StarSchemaSimpleCostModel.hpp +++ /dev/null @@ -1,157 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#ifndef QUERY_OPTIMIZER_COST_MODEL_STAR_SCHEMA_SIMPLE_COST_MODEL_HPP_ -#define QUERY_OPTIMIZER_COST_MODEL_STAR_SCHEMA_SIMPLE_COST_MODEL_HPP_ - -#include -#include - -#include "query_optimizer/cost_model/CostModel.hpp" -#include "query_optimizer/expressions/ExprId.hpp" -#include "query_optimizer/expressions/Predicate.hpp" -#include "query_optimizer/physical/Aggregate.hpp" -#include "query_optimizer/physical/NestedLoopsJoin.hpp" -#include "query_optimizer/physical/HashJoin.hpp" -#include "query_optimizer/physical/Physical.hpp" -#include "query_optimizer/physical/Selection.hpp" -#include "query_optimizer/physical/Sort.hpp" -#include "query_optimizer/physical/TableGenerator.hpp" -#include "query_optimizer/physical/TableReference.hpp" -#include "query_optimizer/physical/TopLevelPlan.hpp" -#include "query_optimizer/physical/WindowAggregate.hpp" -#include "utility/Macros.hpp" - -namespace quickstep { -namespace optimizer { -namespace cost { - -/** \addtogroup CostModel - * @{ - */ - -/** - * @brief A simple cost model for hash join planning. - */ -class StarSchemaSimpleCostModel : public CostModel { - public: - /** - * @brief Constructor. - */ - explicit StarSchemaSimpleCostModel(const std::vector &shared_subplans) - : shared_subplans_(shared_subplans) {} - - /** - * @brief Estimate the cardinality of a physical plan. - * - * @param physical_plan The physical plan. - * @return The estimated cardinality. - */ - std::size_t estimateCardinality( - const physical::PhysicalPtr &physical_plan) override; - - /** - * @brief Estimate the number of groups in an aggregation. - * - * @param aggregate The physical plan of the aggregation. - * @return The estimated number of groups. - */ - std::size_t estimateNumGroupsForAggregate( - const physical::AggregatePtr &aggregate) override; - - /** - * @brief Estimate the number of distinct values of an attribute in a relation. - * - * @param attribute_id The expression id of the target attribute. - * @param physical_plan The physical plan of the attribute's relation. - * @return The estimated number of distinct values for the attribute. - */ - std::size_t estimateNumDistinctValues(const expressions::ExprId attribute_id, - const physical::PhysicalPtr &physical_plan); - - /** - * @brief Estimate the "selectivity" of a physical plan under the assumption - * that it acts as a filtered dimension table in a hash join. - * - * @param phyiscal_plan The physical plan. - * @return The estimated selectivity. - */ - double estimateSelectivity(const physical::PhysicalPtr &physical_plan); - - /** - * @brief Estimate the filter predicate's selectivity if it is present in - * the input plan's root node. - * - * @param physical_plan The input physical plan. - * @return The estimated selectivity of the filter predicate if physical_plan - * has such a filter predicate; 1.0 otherwise. - */ - double estimateSelectivityForFilterPredicate( - const physical::PhysicalPtr &physical_plan); - - private: - std::size_t estimateCardinalityForAggregate( - const physical::AggregatePtr &physical_plan); - - std::size_t estimateCardinalityForHashJoin( - const physical::HashJoinPtr &physical_plan); - - std::size_t estimateCardinalityForNestedLoopsJoin( - const physical::NestedLoopsJoinPtr &physical_plan); - - std::size_t estimateCardinalityForSelection( - const physical::SelectionPtr &physical_plan); - - std::size_t estimateCardinalityForSort( - const physical::SortPtr &physical_plan); - - std::size_t estimateCardinalityForTableGenerator( - const physical::TableGeneratorPtr &physical_plan); - - std::size_t estimateCardinalityForTableReference( - const physical::TableReferencePtr &physical_plan); - - std::size_t estimateCardinalityForTopLevelPlan( - const physical::TopLevelPlanPtr &physical_plan); - - std::size_t estimateCardinalityForWindowAggregate( - const physical::WindowAggregatePtr &physical_plan); - - double estimateSelectivityForPredicate( - const expressions::PredicatePtr &filter_predicate, - const physical::PhysicalPtr &physical_plan); - - const std::vector &shared_subplans_; - - // Get the number of distinct values of an attribute in the table reference. - // If the stat is not avaiable, simply returns the table's cardinality. - std::size_t getNumDistinctValues(const expressions::ExprId attribute_id, - const physical::TableReferencePtr &table_reference); - - - DISALLOW_COPY_AND_ASSIGN(StarSchemaSimpleCostModel); -}; - -/** @} */ - -} // namespace cost -} // namespace optimizer -} // namespace quickstep - -#endif /* QUERY_OPTIMIZER_COST_MODEL_STAR_SCHEMA_SIMPLE_COST_MODEL_HPP_ */ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/expressions/AggregateFunction.cpp ---------------------------------------------------------------------- diff --git a/query_optimizer/expressions/AggregateFunction.cpp b/query_optimizer/expressions/AggregateFunction.cpp deleted file mode 100644 index cf2cb00..0000000 --- a/query_optimizer/expressions/AggregateFunction.cpp +++ /dev/null @@ -1,125 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#include "query_optimizer/expressions/AggregateFunction.hpp" - -#include -#include -#include - -#include "expressions/aggregation/AggregateFunction.hpp" -#include "query_optimizer/expressions/AttributeReference.hpp" -#include "query_optimizer/expressions/Expression.hpp" -#include "query_optimizer/expressions/PatternMatcher.hpp" -#include "query_optimizer/expressions/Scalar.hpp" -#include "types/Type.hpp" -#include "utility/Cast.hpp" - -#include "glog/logging.h" - -namespace quickstep { -namespace optimizer { -namespace expressions { - -bool AggregateFunction::isNullable() const { - std::vector argument_types; - for (const ScalarPtr &argument : arguments_) { - argument_types.emplace_back(&argument->getValueType()); - } - - const Type *return_type = aggregate_.resultTypeForArgumentTypes(argument_types); - DCHECK(return_type != nullptr); - return return_type->isNullable(); -} - -const Type& AggregateFunction::getValueType() const { - std::vector argument_types; - for (const ScalarPtr &argument : arguments_) { - argument_types.emplace_back(&argument->getValueType()); - } - - const Type *return_type = aggregate_.resultTypeForArgumentTypes(argument_types); - DCHECK(return_type != nullptr); - return *return_type; -} - -AggregateFunctionPtr AggregateFunction::Create( - const ::quickstep::AggregateFunction &aggregate, - const std::vector &arguments, - const bool is_vector_aggregate, - const bool is_distinct) { -#ifdef QUICKSTEP_DEBUG - std::vector argument_types; - for (const ScalarPtr &argument : arguments) { - argument_types.emplace_back(&argument->getValueType()); - } - DCHECK(aggregate.canApplyToTypes(argument_types)); -#endif // QUICKSTEP_DEBUG - - return AggregateFunctionPtr( - new AggregateFunction(aggregate, arguments, is_vector_aggregate, is_distinct)); -} - -ExpressionPtr AggregateFunction::copyWithNewChildren( - const std::vector &new_children) const { - std::vector new_arguments; - for (const ExpressionPtr &expression_ptr : new_children) { - ScalarPtr expr_as_scalar; - CHECK(SomeScalar::MatchesWithConditionalCast(expression_ptr, &expr_as_scalar)) - << expression_ptr->toString(); - new_arguments.emplace_back(std::move(expr_as_scalar)); - } - - return Create(aggregate_, new_arguments, is_vector_aggregate_, is_distinct_); -} - -std::vector AggregateFunction::getReferencedAttributes() const { - std::vector referenced_attributes; - for (const ScalarPtr &argument : arguments_) { - const std::vector referenced_attributes_in_argument = - argument->getReferencedAttributes(); - referenced_attributes.insert(referenced_attributes.end(), - referenced_attributes_in_argument.begin(), - referenced_attributes_in_argument.end()); - } - return referenced_attributes; -} - -void AggregateFunction::getFieldStringItems( - std::vector *inline_field_names, - std::vector *inline_field_values, - std::vector *non_container_child_field_names, - std::vector *non_container_child_fields, - std::vector *container_child_field_names, - std::vector> *container_child_fields) const { - inline_field_names->push_back("function"); - inline_field_values->push_back(aggregate_.getName()); - - if (is_distinct_) { - inline_field_names->push_back("is_distinct"); - inline_field_values->push_back("true"); - } - - container_child_field_names->push_back(""); - container_child_fields->emplace_back(CastSharedPtrVector(arguments_)); -} - -} // namespace expressions -} // namespace optimizer -} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/expressions/AggregateFunction.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/expressions/AggregateFunction.hpp b/query_optimizer/expressions/AggregateFunction.hpp deleted file mode 100644 index 108c661..0000000 --- a/query_optimizer/expressions/AggregateFunction.hpp +++ /dev/null @@ -1,180 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#ifndef QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_AGGREGATE_FUNCTION_HPP_ -#define QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_AGGREGATE_FUNCTION_HPP_ - -#include -#include -#include - -#include "query_optimizer/OptimizerTree.hpp" -#include "query_optimizer/expressions/AttributeReference.hpp" -#include "query_optimizer/expressions/Expression.hpp" -#include "query_optimizer/expressions/ExpressionType.hpp" -#include "query_optimizer/expressions/Scalar.hpp" -#include "utility/Macros.hpp" - -namespace quickstep { - -class AggregateFunction; -class Type; - -namespace optimizer { -namespace expressions { - -/** \addtogroup OptimizerExpressions - * @{ - */ - -class AggregateFunction; -typedef std::shared_ptr AggregateFunctionPtr; - -/** - * @brief Represents an aggregate and its arguments in the optimizer. This - * class wraps some of the functionality from - * quickstep::AggregateFunction and represents a particular instance - * of an aggregate during query optimization. - **/ -class AggregateFunction : public Expression { - public: - /** - * @brief Destructor. - */ - ~AggregateFunction() override {} - - ExpressionType getExpressionType() const override { - return ExpressionType::kAggregateFunction; - } - - std::string getName() const override { - return "AggregateFunction"; - } - - const Type& getValueType() const override; - - bool isConstant() const override { - // Aggregate function is never considered as a constant expression. - return false; - } - - ExpressionPtr copyWithNewChildren( - const std::vector &new_children) const override; - - std::vector getReferencedAttributes() const override; - - /** - * @return Whether the type of the return value is nullable. - **/ - bool isNullable() const; - - /** - * @return True if this aggregate is a vector aggregate (i.e. with GROUP BY). - **/ - inline bool is_vector_aggregate() const { - return is_vector_aggregate_; - } - - /** - * @return The AggregateFunction singleton (from the expression system) - * for this node. - **/ - inline const ::quickstep::AggregateFunction& getAggregate() const { - return aggregate_; - } - - /** - * @return The list of scalar arguments to this aggregate. - **/ - inline const std::vector& getArguments() const { - return arguments_; - } - - /* - * @return Whether this is a DISTINCT aggregation. - */ - inline bool is_distinct() const { - return is_distinct_; - } - - /** - * @brief Create a new AggregateFunction. - * - * @warning It is an error to call this with arguments that the given - * aggregate can not apply to. - * - * @param aggregate The underlying AggregateFunction from the expression - * system. - * @param arguments A list of arguments to the aggregate function. - * @param is_vector_aggregate Whether the aggregate has a GROUP BY clause. - * @param is_distinct Whether this is a DISTINCT aggregation. - * @return A new AggregateFunctionPtr. - **/ - static AggregateFunctionPtr Create(const ::quickstep::AggregateFunction &aggregate, - const std::vector &arguments, - const bool is_vector_aggregate, - const bool is_distinct); - - protected: - void getFieldStringItems( - std::vector *inline_field_names, - std::vector *inline_field_values, - std::vector *non_container_child_field_names, - std::vector *non_container_child_fields, - std::vector *container_child_field_names, - std::vector> *container_child_fields) const override; - - private: - /** - * @brief Constructor. - * - * @param aggregate The actual AggregateFunction to use. - * @param arguments The scalar arguments to the aggregate function. - * @param is_vector_aggregate Indicates whether this aggregation is a vector - * aggregation (i.e. GROUP BY exists). - * @param is_distinct Indicates whether this is a DISTINCT aggregation. - */ - AggregateFunction(const ::quickstep::AggregateFunction &aggregate, - const std::vector &arguments, - const bool is_vector_aggregate, - const bool is_distinct) - : aggregate_(aggregate), - arguments_(arguments), - is_vector_aggregate_(is_vector_aggregate), - is_distinct_(is_distinct) { - for (const ScalarPtr &child : arguments_) { - addChild(child); - } - } - - const ::quickstep::AggregateFunction &aggregate_; - std::vector arguments_; - const bool is_vector_aggregate_; - const bool is_distinct_; - - DISALLOW_COPY_AND_ASSIGN(AggregateFunction); -}; - -/** @} */ - -} // namespace expressions -} // namespace optimizer -} // namespace quickstep - -#endif /* QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_AGGREGATE_FUNCTION_HPP_ */ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/expressions/Alias.cpp ---------------------------------------------------------------------- diff --git a/query_optimizer/expressions/Alias.cpp b/query_optimizer/expressions/Alias.cpp deleted file mode 100644 index 30dade7..0000000 --- a/query_optimizer/expressions/Alias.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#include "query_optimizer/expressions/Alias.hpp" - -#include -#include -#include - -#include "catalog/CatalogTypedefs.hpp" -#include "query_optimizer/OptimizerTree.hpp" -#include "query_optimizer/expressions/Expression.hpp" -#include "query_optimizer/expressions/NamedExpression.hpp" -#include "query_optimizer/expressions/PatternMatcher.hpp" - -#include "glog/logging.h" - -namespace quickstep { - -class Scalar; - -namespace optimizer { -namespace expressions { - -Alias::Alias(attribute_id id, - const ExpressionPtr &expression, - const std::string &attribute_name, - const std::string &attribute_alias, - const std::string &table_name) - : NamedExpression(id, attribute_name, attribute_alias, table_name) { - AliasPtr attribute_child; - // Do not add an Alias as the child of another Alias. - if (SomeAlias::MatchesWithConditionalCast(expression, &attribute_child)) { - expression_ = attribute_child->expression(); - } else { - expression_ = expression; - } - addChild(expression_); -} - -ExpressionPtr Alias::copyWithNewChildren( - const std::vector &new_children) const { - DCHECK_EQ(new_children.size(), children().size()); - return Alias::Create(id(), - new_children[0], - attribute_name(), - attribute_alias(), - relation_name()); -} - -::quickstep::Scalar *Alias::concretize( - const std::unordered_map &substitution_map) const { - // Alias should be converted to a CatalogAttribute. - LOG(FATAL) << "Cannot concretize Alias to a scalar for evaluation"; -} - -void Alias::getFieldStringItems( - std::vector *inline_field_names, - std::vector *inline_field_values, - std::vector *non_container_child_field_names, - std::vector *non_container_child_fields, - std::vector *container_child_field_names, - std::vector> *container_child_fields) - const { - NamedExpression::getFieldStringItems(inline_field_names, - inline_field_values, - non_container_child_field_names, - non_container_child_fields, - container_child_field_names, - container_child_fields); - - non_container_child_field_names->push_back(""); - non_container_child_fields->push_back(expression_); -} - -} // namespace expressions -} // namespace optimizer -} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/expressions/Alias.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/expressions/Alias.hpp b/query_optimizer/expressions/Alias.hpp deleted file mode 100644 index e07f9fe..0000000 --- a/query_optimizer/expressions/Alias.hpp +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#ifndef QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_ALIAS_HPP -#define QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_ALIAS_HPP - -#include -#include -#include -#include - -#include "query_optimizer/OptimizerTree.hpp" -#include "query_optimizer/expressions/AttributeReference.hpp" -#include "query_optimizer/expressions/ExprId.hpp" -#include "query_optimizer/expressions/Expression.hpp" -#include "query_optimizer/expressions/ExpressionType.hpp" -#include "query_optimizer/expressions/NamedExpression.hpp" -#include "query_optimizer/expressions/Scalar.hpp" -#include "utility/Macros.hpp" - -namespace quickstep { - -class CatalogAttribute; -class Scalar; -class Type; - -namespace optimizer { -namespace expressions { - -/** \addtogroup OptimizerExpressions - * @{ - */ - -class Alias; -typedef std::shared_ptr AliasPtr; - -/** - * @brief Assigns an ID and a name to an expression so that the expression can be - * referenced by others. It corresponds to a new CatalogAttribute. - */ -class Alias : public NamedExpression { - public: - ExpressionType getExpressionType() const override { return ExpressionType::kAlias; } - - std::string getName() const override { return "Alias"; } - - const Type& getValueType() const override { - return expression_->getValueType(); - } - - bool isConstant() const override { return expression_->isConstant(); } - - /** - * @brief Returns the named expression. - * - * @return The expression named by this Alias. - */ - inline const ExpressionPtr& expression() const { return expression_; } - - ExpressionPtr copyWithNewChildren( - const std::vector &new_children) const override; - - std::vector getReferencedAttributes() const override { - return expression_->getReferencedAttributes(); - } - - ::quickstep::Scalar *concretize( - const std::unordered_map &substitution_map) const override; - - /** - * @brief Creates an immutable Alias. If \p expression is also an Alias, - * its child expression rather than the Alias is the child expression - * of the new Alias. - * - * @param id Expression ID. - * @param expression Expression that the alias is applied on. - * @param attribute_name The internal name (corresponding to the attribute - * name if being concretized). - * @param attribute_alias The display name (corresponding to the display name - * if being concretized). - * @param relation_name The name of the source relation. - * @return An immutable Alias. - */ - static AliasPtr Create(ExprId id, - const ExpressionPtr &expression, - const std::string &attribute_name, - const std::string &attribute_alias, - const std::string &relation_name = "") { - return AliasPtr(new Alias(id, - expression, - attribute_name, - attribute_alias, - relation_name)); - } - - protected: - void getFieldStringItems( - std::vector *inline_field_names, - std::vector *inline_field_values, - std::vector *non_container_child_field_names, - std::vector *non_container_child_fields, - std::vector *container_child_field_names, - std::vector> *container_child_fields) const override; - - private: - Alias(ExprId id, - const ExpressionPtr &expression, - const std::string &attribute_name, - const std::string &attribute_alais, - const std::string &relation_name); - - ExpressionPtr expression_; - - DISALLOW_COPY_AND_ASSIGN(Alias); -}; - -/** @} */ - -} // namespace expressions -} // namespace optimizer -} // namespace quickstep - -#endif /* QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_ALIAS_HPP */ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/expressions/AttributeReference.cpp ---------------------------------------------------------------------- diff --git a/query_optimizer/expressions/AttributeReference.cpp b/query_optimizer/expressions/AttributeReference.cpp deleted file mode 100644 index f0e49d4..0000000 --- a/query_optimizer/expressions/AttributeReference.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#include "query_optimizer/expressions/AttributeReference.hpp" - -#include -#include -#include - -#include "expressions/scalar/ScalarAttribute.hpp" -#include "query_optimizer/expressions/ExprId.hpp" -#include "query_optimizer/expressions/Expression.hpp" - -#include "glog/logging.h" - -namespace quickstep { -namespace optimizer { -namespace expressions { - -ExpressionPtr AttributeReference::copyWithNewChildren( - const std::vector &new_children) const { - DCHECK_EQ(new_children.size(), children().size()); - return AttributeReference::Create(id(), - attribute_name(), - attribute_alias(), - relation_name(), - getValueType(), - scope()); -} - -std::vector AttributeReference::getReferencedAttributes() - const { - return { Create(id(), attribute_name(), attribute_alias(), relation_name(), getValueType(), scope()) }; -} - -::quickstep::Scalar *AttributeReference::concretize( - const std::unordered_map &substitution_map) const { - const std::unordered_map::const_iterator found_it = - substitution_map.find(id()); - DCHECK(found_it != substitution_map.end()) << toString(); - return new ::quickstep::ScalarAttribute(*found_it->second); -} - -void AttributeReference::getFieldStringItems( - std::vector *inline_field_names, - std::vector *inline_field_values, - std::vector *non_container_child_field_names, - std::vector *non_container_child_fields, - std::vector *container_child_field_names, - std::vector> *container_child_fields) const { - NamedExpression::getFieldStringItems( - inline_field_names, - inline_field_values, - non_container_child_field_names, - non_container_child_fields, - container_child_field_names, - container_child_fields); - - if (scope_ == AttributeReferenceScope::kOuter) { - inline_field_names->push_back("is_outer_reference"); - inline_field_values->push_back("true"); - } -} - -} // namespace expressions -} // namespace optimizer -} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/156290a4/query_optimizer/expressions/AttributeReference.hpp ---------------------------------------------------------------------- diff --git a/query_optimizer/expressions/AttributeReference.hpp b/query_optimizer/expressions/AttributeReference.hpp deleted file mode 100644 index f5207b1..0000000 --- a/query_optimizer/expressions/AttributeReference.hpp +++ /dev/null @@ -1,151 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#ifndef QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_ATTRIBUTE_REFERENCE_HPP_ -#define QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_ATTRIBUTE_REFERENCE_HPP_ - -#include -#include -#include -#include - -#include "catalog/CatalogTypedefs.hpp" -#include "query_optimizer/expressions/ExprId.hpp" -#include "query_optimizer/expressions/Expression.hpp" -#include "query_optimizer/expressions/ExpressionType.hpp" -#include "query_optimizer/expressions/NamedExpression.hpp" -#include "utility/Macros.hpp" - -namespace quickstep { - -class CatalogAttribute; -class Type; - -namespace optimizer { -namespace expressions { - -/** \addtogroup OptimizerExpressions - * @{ - */ - -/** - * @brief The scope of the referenced attribute. I.e. whether the referenced - * attribute belongs to a local TableReference or comes from an outside - * TableReference. The outside case implies a correlated subquery. - */ -enum class AttributeReferenceScope { - kLocal = 0, - kOuter -}; - -class AttributeReference; -typedef std::shared_ptr AttributeReferencePtr; - -/** - * @brief Reference to the attribute created by an Alias or to an attribute in a - * base relation. - */ -class AttributeReference : public NamedExpression { - public: - ExpressionType getExpressionType() const override { - return ExpressionType::kAttributeReference; - } - - std::string getName() const override { return "AttributeReference"; } - - const Type& getValueType() const override { return type_; } - - bool isConstant() const override { return false; } - - /** - * @return The scope of the referenced attribute. - */ - const AttributeReferenceScope scope() const { - return scope_; - } - - ExpressionPtr copyWithNewChildren( - const std::vector &new_children) const override; - - std::vector getReferencedAttributes() const override; - - ::quickstep::Scalar* concretize( - const std::unordered_map &substitution_map) const override; - - /** - * @brief Creates an immutable AttributReference. - * - * @param attribute_id The ExprId of the expression this AttributeReference - * references to. - * @param attribute_name The attribute name. This is only for printing purpose. - * @param attribute_alias The attribute alias (or display name). This is only - * for printing purpose. - * @param relation_name The name of the relation where the reference - * expression comes from. This is only for - * printing purpose. - * @param type The type of the value of the referenced expression. - * @param scope The scope of the referenced attribute. - * @return An immutable AttributeReference. - */ - static AttributeReferencePtr Create(ExprId attribute_id, - const std::string &attribute_name, - const std::string &attribute_alias, - const std::string &relation_name, - const Type &type, - const AttributeReferenceScope scope) { - return AttributeReferencePtr(new AttributeReference( - attribute_id, attribute_name, attribute_alias, relation_name, type, scope)); - } - - protected: - void getFieldStringItems( - std::vector *inline_field_names, - std::vector *inline_field_values, - std::vector *non_container_child_field_names, - std::vector *non_container_child_fields, - std::vector *container_child_field_names, - std::vector> *container_child_fields) const override; - - private: - AttributeReference(attribute_id attribute_id, - const std::string &attribute_name, - const std::string &attribute_alias, - const std::string &relation_name, - const Type &type, - const AttributeReferenceScope scope) - : NamedExpression(attribute_id, - attribute_name, - attribute_alias, - relation_name), - type_(type), - scope_(scope) {} - - const Type &type_; - const AttributeReferenceScope scope_; - - DISALLOW_COPY_AND_ASSIGN(AttributeReference); -}; - -/** @} */ - -} // namespace expressions -} // namespace optimizer -} // namespace quickstep - -#endif /* QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_ATTRIBUTE_REFERENCE_HPP_ */