Github user jianqiao commented on a diff in the pull request: https://github.com/apache/incubator-quickstep/pull/177#discussion_r99865239 --- Diff: query_optimizer/rules/ReduceGroupByAttributes.cpp --- @@ -0,0 +1,217 @@ +/** + * 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/rules/ReduceGroupByAttributes.hpp" + +#include <algorithm> +#include <map> +#include <vector> +#include <unordered_set> +#include <utility> + +#include "catalog/CatalogRelation.hpp" +#include "query_optimizer/OptimizerContext.hpp" +#include "query_optimizer/cost_model/StarSchemaSimpleCostModel.hpp" +#include "query_optimizer/expressions/AttributeReference.hpp" +#include "query_optimizer/expressions/ExprId.hpp" +#include "query_optimizer/expressions/ExpressionUtil.hpp" +#include "query_optimizer/expressions/NamedExpression.hpp" +#include "query_optimizer/physical/Aggregate.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/TableReference.hpp" +#include "query_optimizer/physical/TopLevelPlan.hpp" +#include "query_optimizer/rules/PruneColumns.hpp" + +#include "gflags/gflags.h" + +#include "glog/logging.h" + +namespace quickstep { +namespace optimizer { + +DEFINE_uint64(reduce_group_by_attributes_threshold, 3u, + "The threshold for a stored relation's number of attributes in a " + "group-by clause for the ReduceGroupByAttributes optimization " + "rule to pull the stored relation up the aggregation"); + +namespace E = ::quickstep::optimizer::expressions; +namespace P = ::quickstep::optimizer::physical; + +P::PhysicalPtr ReduceGroupByAttributes::apply(const P::PhysicalPtr &input) { + DCHECK(input->getPhysicalType() == P::PhysicalType::kTopLevelPlan); + cost_model_.reset(new cost::StarSchemaSimpleCostModel( + std::static_pointer_cast<const P::TopLevelPlan>(input)->shared_subplans())); + + P::PhysicalPtr output = applyInternal(input); + if (output != input) { + output = PruneColumns().apply(output); + } + return output; +} + +P::PhysicalPtr ReduceGroupByAttributes::applyInternal(const P::PhysicalPtr &input) { + std::vector<P::PhysicalPtr> new_children; + for (const P::PhysicalPtr &child : input->children()) { + new_children.push_back(applyInternal(child)); + } + + if (new_children != input->children()) { + return applyToNode(input->copyWithNewChildren(new_children)); + } else { + return applyToNode(input); + } +} + +P::PhysicalPtr ReduceGroupByAttributes::applyToNode(const P::PhysicalPtr &input) { + P::TableReferencePtr table_reference; + if (P::SomeTableReference::MatchesWithConditionalCast(input, &table_reference)) { + // Collect the attributes-to-TableReference mapping info. + for (const auto &attr : table_reference->attribute_list()) { + source_.emplace(attr->id(), std::make_pair(table_reference, attr)); + } + return input; + } + + P::AggregatePtr aggregate; + if (!P::SomeAggregate::MatchesWithConditionalCast(input, &aggregate) || + aggregate->grouping_expressions().size() <= 1u) { + return input; + } + + // Divide the group-by attributes into groups based on their source table. + std::map<P::TableReferencePtr, std::vector<E::AttributeReferencePtr>> table_attributes; --- End diff -- We would expect tables attributes to contain a small number of entries (# of storage tables for the attributes in one group-by clause, typically 1 to 3). Also consider that there an iteration of all the map elements below. It might be better to use a `std::map` here. Anyway it won't make even a tiny performance difference to use either data structure here.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---