quickstep-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zuyu <...@git.apache.org>
Subject [GitHub] incubator-quickstep pull request #171: QUICKSTEP-68 Reorder intermediate rel...
Date Mon, 30 Jan 2017 06:54:54 GMT
Github user zuyu commented on a diff in the pull request:

    https://github.com/apache/incubator-quickstep/pull/171#discussion_r98383922
  
    --- Diff: query_optimizer/rules/ReorderColumns.cpp ---
    @@ -0,0 +1,205 @@
    +/**
    + * 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/ReorderColumns.hpp"
    +
    +#include <algorithm>
    +#include <cstddef>
    +#include <limits>
    +#include <unordered_map>
    +#include <vector>
    +
    +#include "query_optimizer/expressions/AttributeReference.hpp"
    +#include "query_optimizer/expressions/ExprId.hpp"
    +#include "query_optimizer/expressions/NamedExpression.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/TableReference.hpp"
    +
    +#include "glog/logging.h"
    +
    +namespace quickstep {
    +namespace optimizer {
    +
    +namespace E = ::quickstep::optimizer::expressions;
    +namespace P = ::quickstep::optimizer::physical;
    +
    +P::PhysicalPtr ReorderColumns::apply(const P::PhysicalPtr &input) {
    +  DCHECK(input->getPhysicalType() == P::PhysicalType::kTopLevelPlan);
    +
    +  return applyInternal(input, true);
    +}
    +
    +P::PhysicalPtr ReorderColumns::applyInternal(const P::PhysicalPtr &input,
    +                                             bool lock_ordering) {
    +  // We have to guarantee that the top level ordering of the columns remain
    +  // unchanged so that the output columns are ordered as specified by the user.
    +  // So here we use the flag "lock_ordering" to skip the first transformable
    +  // node (i.e. the first Selection or HashJoin).
    +  const bool is_not_transformable = !IsTransformable(input);
    +  const bool skip_transform = lock_ordering || is_not_transformable;
    +  lock_ordering = lock_ordering && is_not_transformable;
    +
    +  if (skip_transform) {
    +    std::vector<P::PhysicalPtr> new_children;
    +    for (const P::PhysicalPtr &child : input->children()) {
    +      new_children.emplace_back(applyInternal(child, lock_ordering));
    +    }
    +
    +    if (new_children != input->children()) {
    +      return input->copyWithNewChildren(new_children);
    +    } else {
    +      return input;
    +    }
    +  }
    +
    +  // Collect the maximal chain of transformable nodes.
    +  std::vector<P::PhysicalPtr> nodes;
    +  for (P::PhysicalPtr node = input; IsTransformable(node); node = node->children().front())
{
    +    nodes.emplace_back(node);
    +  }
    +  std::reverse(nodes.begin(), nodes.end());
    +
    +  // A greedy algorithm that reorders the output attributes based on the GEN/KILL
    +  // intervals. This algorithm works well with SSB/TPCH queries and is not likely
    +  // to make the plans worse for whatever queries.
    +  std::unordered_map<E::ExprId, std::size_t> base, gen, kill;
    +
    +  const P::PhysicalPtr base_node =
    +      applyInternal(nodes.front()->children().front(), false);
    +  P::TableReferencePtr base_table;
    +  if (P::SomeTableReference::MatchesWithConditionalCast(base_node, &base_table))
{
    +  }
    +
    +  const std::vector<E::AttributeReferencePtr> base_attrs =
    +      nodes.front()->children().front()->getOutputAttributes();
    +  for (std::size_t i = 0; i < base_attrs.size(); ++i) {
    +    base.emplace(base_attrs[i]->id(), i);
    +  }
    +
    +  for (std::size_t i = 0; i < nodes.size(); ++i) {
    +    for (const auto &attr : nodes[i]->getOutputAttributes()) {
    +      const E::ExprId attr_id = attr->id();
    +      if (gen.find(attr_id) == gen.end()) {
    +        gen.emplace(attr_id, i);
    +      }
    +      kill[attr_id] = i;
    +    }
    +  }
    +
    +  const auto comparator = [&gen, &kill, &base](const E::NamedExpressionPtr
&lhs,
    +                                               const E::NamedExpressionPtr &rhs)
-> bool {
    +    const E::ExprId lhs_id = lhs->id();
    +    const E::ExprId rhs_id = rhs->id();
    +
    +    // Sort the attributes first by GEN location.
    +    const std::size_t lhs_gen = gen.at(lhs_id);
    +    const std::size_t rhs_gen = gen.at(rhs_id);
    +    if (lhs_gen != rhs_gen) {
    +      return lhs_gen < rhs_gen;
    +    }
    +
    +    // Then by KILL location.
    +    const std::size_t lhs_kill = kill.at(lhs_id);
    +    const std::size_t rhs_kill = kill.at(rhs_id);
    +    if (lhs_kill != rhs_kill) {
    +      return lhs_kill < rhs_kill;
    +    }
    +
    +    // Finally by the ordering in the base relaton.
    +    const auto lhs_base_it = base.find(lhs_id);
    +    const auto rhs_base_it = base.find(rhs_id);
    +    const std::size_t lhs_base =
    +        lhs_base_it == base.end() ? std::numeric_limits<std::size_t>::max()
    --- End diff --
    
    Optionally, if `lhs_base_it` is not found, we could simply return the size of `base`,
instead of the max value.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message