quickstep-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shix...@apache.org
Subject incubator-quickstep git commit: Added window aggregation class in logical
Date Wed, 22 Jun 2016 20:19:43 GMT
Repository: incubator-quickstep
Updated Branches:
  refs/heads/SQL-window-aggregation 9434e6c73 -> 614d17dee


Added window aggregation class in logical


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

Branch: refs/heads/SQL-window-aggregation
Commit: 614d17deed54b26c250301fbb305cd2811c87909
Parents: 9434e6c
Author: shixuan-fan <shixuan@apache.org>
Authored: Wed Jun 22 20:19:28 2016 +0000
Committer: shixuan-fan <shixuan@apache.org>
Committed: Wed Jun 22 20:19:28 2016 +0000

----------------------------------------------------------------------
 .../expressions/WindowAggregateFunction.cpp     |  65 +++++++++-
 .../expressions/WindowAggregateFunction.hpp     |  29 ++++-
 query_optimizer/logical/CMakeLists.txt          |   4 +-
 query_optimizer/logical/LogicalType.hpp         |   3 +-
 query_optimizer/logical/WindowAggregate.cpp     |  86 +++++++++++++
 query_optimizer/logical/WindowAggregate.hpp     | 123 +++++++++++++++++++
 query_optimizer/resolver/CMakeLists.txt         |   1 +
 query_optimizer/resolver/Resolver.cpp           |  94 +++++++-------
 query_optimizer/resolver/Resolver.hpp           |  17 ++-
 9 files changed, 364 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/expressions/WindowAggregateFunction.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/expressions/WindowAggregateFunction.cpp b/query_optimizer/expressions/WindowAggregateFunction.cpp
index b980c99..bd813ec 100644
--- a/query_optimizer/expressions/WindowAggregateFunction.cpp
+++ b/query_optimizer/expressions/WindowAggregateFunction.cpp
@@ -61,7 +61,7 @@ const Type& WindowAggregateFunction::getValueType() const {
 WindowAggregateFunctionPtr WindowAggregateFunction::Create(
     const ::quickstep::AggregateFunction &window_aggregate,
     const std::vector<ScalarPtr> &arguments,
-    const WindowInfo *window_info,
+    WindowInfo *window_info,
     const bool is_distinct) {
 #ifdef QUICKSTEP_DEBUG
   std::vector<const Type*> argument_types;
@@ -102,7 +102,12 @@ ExpressionPtr WindowAggregateFunction::copyWithNewChildren(
     new_arguments.emplace_back(std::move(expr_as_scalar));
   }
 
-  return Create(window_aggregate_, new_arguments, window_info_, is_distinct_);
+  return WindowAggregateFunctionPtr(
+      new WindowAggregateFunction(window_aggregate_,
+                                  new_arguments,
+                                  window_info_,
+                                  window_name_,
+                                  is_distinct_));
 }
 
 std::vector<AttributeReferencePtr> WindowAggregateFunction::getReferencedAttributes()
const {
@@ -114,6 +119,17 @@ std::vector<AttributeReferencePtr> WindowAggregateFunction::getReferencedAttribu
                                  referenced_attributes_in_argument.begin(),
                                  referenced_attributes_in_argument.end());
   }
+
+  if (window_info_ != nullptr) {
+    referenced_attributes.insert(referenced_attributes.end(),
+                                 window_info_->partition_by_attributes.begin(),
+                                 window_info_->partition_by_attributes.end());
+
+    referenced_attributes.insert(referenced_attributes.end(),
+                                 window_info_->order_by_attributes.begin(),
+                                 window_info_->order_by_attributes.end());
+  }
+  
   return referenced_attributes;
 }
 
@@ -127,13 +143,52 @@ void WindowAggregateFunction::getFieldStringItems(
   inline_field_names->push_back("function");
   inline_field_values->push_back(window_aggregate_.getName());
 
+  container_child_field_names->push_back("arguments");
+  container_child_fields->emplace_back(CastSharedPtrVector<OptimizerTreeBase>(arguments_));
+
+  inline_field_names->push_back("window name");
+  inline_field_values->push_back(window_name_);
+
+  if (window_info_ != nullptr) {
+    container_child_field_names->push_back("partition by");
+    container_child_fields->emplace_back(
+        CastSharedPtrVector<OptimizerTreeBase>(window_info_->partition_by_attributes));
+
+    container_child_field_names->push_back("order by");
+    container_child_fields->emplace_back(
+        CastSharedPtrVector<OptimizerTreeBase>(window_info_->order_by_attributes));
+
+    inline_field_names->push_back("is_ascending");
+    std::string ascending_list("[");
+    for (bool is_ascending : window_info_->order_by_directions) {
+      if (is_ascending) {
+        ascending_list.append("true,");
+      } else {
+        ascending_list.append("false,");
+      }
+    }
+    ascending_list.pop_back();
+    ascending_list.append("]");
+    inline_field_values->push_back(ascending_list);
+
+    inline_field_names->push_back("nulls_first");
+    std::string nulls_first_flags("[");
+    for (bool nulls_first_flag : window_info_->nulls_first) {
+      if (nulls_first_flag) {
+        nulls_first_flags.append("true,");
+      } else {
+        nulls_first_flags.append("false,");
+      }
+    }
+    nulls_first_flags.pop_back();
+    nulls_first_flags.append("]");
+    inline_field_values->push_back(nulls_first_flags);
+  }
+
   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<OptimizerTreeBase>(arguments_));
 }
 
 }  // namespace expressions

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/expressions/WindowAggregateFunction.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/expressions/WindowAggregateFunction.hpp b/query_optimizer/expressions/WindowAggregateFunction.hpp
index ca7e449..149214d 100644
--- a/query_optimizer/expressions/WindowAggregateFunction.hpp
+++ b/query_optimizer/expressions/WindowAggregateFunction.hpp
@@ -157,6 +157,13 @@ class WindowAggregateFunction : public Expression {
   }
 
   /**
+   * @return The name of the window.
+   **/
+  inline const std::string getWindowName() const {
+    return window_name_;
+  }
+
+  /**
    * @return Whether this is a DISTINCT aggregation.
    **/
   inline bool is_distinct() const {
@@ -164,6 +171,20 @@ class WindowAggregateFunction : public Expression {
   }
 
   /**
+   * @brief Set the window info according to the window name
+   *
+   * @note This method should only be called when a window info is fetched for
+   *       a window function that is defined by window name.
+   *
+   * @param window_info The window info retrieved from the window name.
+   **/
+  void setWindowInfo(WindowInfo *window_info) {
+    if (window_info_ == nullptr) {
+      window_info_ = window_info;
+    }
+  }
+
+  /**
    * @brief Create a new WindowAggregateFunction by directly defined window.
    *
    * @warning It is an error to call this with arguments that the given
@@ -178,7 +199,7 @@ class WindowAggregateFunction : public Expression {
    **/
   static WindowAggregateFunctionPtr Create(const ::quickstep::AggregateFunction &window_aggregate,
                                            const std::vector<ScalarPtr> &arguments,
-                                           const WindowInfo *window_info,
+                                           WindowInfo *window_info,
                                            const bool is_distinct);
 
   /**
@@ -219,7 +240,7 @@ class WindowAggregateFunction : public Expression {
    */
   WindowAggregateFunction(const ::quickstep::AggregateFunction &window_aggregate,
                           const std::vector<ScalarPtr> &arguments,
-                          const WindowInfo *window_info,
+                          WindowInfo *window_info,
                           const std::string window_name,
                           const bool is_distinct)
       : window_aggregate_(window_aggregate),
@@ -238,7 +259,7 @@ class WindowAggregateFunction : public Expression {
   // created as quickstep::WindowAggregateFunction.
   const ::quickstep::AggregateFunction &window_aggregate_;
   std::vector<ScalarPtr> arguments_;
-  const WindowInfo *window_info_;
+  WindowInfo *window_info_;
   const std::string window_name_;
   const bool is_distinct_;
 
@@ -251,4 +272,4 @@ class WindowAggregateFunction : public Expression {
 }  // namespace optimizer
 }  // namespace quickstep
 
-#endif /* QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_AGGREGATE_FUNCTION_HPP_ */
+#endif /* QUICKSTEP_QUERY_OPTIMIZER_EXPRESSIONS_WINDOW_AGGREGATE_FUNCTION_HPP_ */

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/logical/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/query_optimizer/logical/CMakeLists.txt b/query_optimizer/logical/CMakeLists.txt
index 61c6234..e33e9fb 100644
--- a/query_optimizer/logical/CMakeLists.txt
+++ b/query_optimizer/logical/CMakeLists.txt
@@ -43,6 +43,7 @@ add_library(quickstep_queryoptimizer_logical_TableReference TableReference.cpp
T
 add_library(quickstep_queryoptimizer_logical_TableGenerator ../../empty_src.cpp TableGenerator.hpp)
 add_library(quickstep_queryoptimizer_logical_TopLevelPlan TopLevelPlan.cpp TopLevelPlan.hpp)
 add_library(quickstep_queryoptimizer_logical_UpdateTable UpdateTable.cpp UpdateTable.hpp)
+add_library(quickstep_queryoptimizer_logical_WindowAggregate WindowAggregate.cpp WindowAggregate.hpp)
 
 # Link dependencies:
 target_link_libraries(quickstep_queryoptimizer_logical_Aggregate
@@ -287,4 +288,5 @@ target_link_libraries(quickstep_queryoptimizer_logical
                       quickstep_queryoptimizer_logical_TableGenerator
                       quickstep_queryoptimizer_logical_TableReference
                       quickstep_queryoptimizer_logical_TopLevelPlan
-                      quickstep_queryoptimizer_logical_UpdateTable)
+                      quickstep_queryoptimizer_logical_UpdateTable
+                      quickstep_queryoptimizer_logical_WindowAggregate)

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/logical/LogicalType.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/logical/LogicalType.hpp b/query_optimizer/logical/LogicalType.hpp
index 1b9366e..c82fb47 100644
--- a/query_optimizer/logical/LogicalType.hpp
+++ b/query_optimizer/logical/LogicalType.hpp
@@ -49,7 +49,8 @@ enum class LogicalType {
   kTableGenerator,
   kTableReference,
   kTopLevelPlan,
-  kUpdateTable
+  kUpdateTable,
+  kWindowAggregate
 };
 
 /** @} */

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/logical/WindowAggregate.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/logical/WindowAggregate.cpp b/query_optimizer/logical/WindowAggregate.cpp
new file mode 100644
index 0000000..3e5c7b5
--- /dev/null
+++ b/query_optimizer/logical/WindowAggregate.cpp
@@ -0,0 +1,86 @@
+/**
+ *   Copyright 2011-2015 Quickstep Technologies LLC.
+ *   Copyright 2015 Pivotal Software, Inc.
+ *   Copyright 2016, Quickstep Research Group, Computer Sciences Department,
+ *     University of Wisconsin—Madison.
+ *
+ *   Licensed 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/logical/WindowAggregate.hpp"
+
+#include <string>
+#include <vector>
+
+#include "query_optimizer/OptimizerTree.hpp"
+#include "query_optimizer/expressions/Alias.hpp"
+#include "query_optimizer/expressions/AttributeReference.hpp"
+#include "query_optimizer/expressions/ExpressionUtil.hpp"
+#include "query_optimizer/expressions/NamedExpression.hpp"
+#include "query_optimizer/expressions/PatternMatcher.hpp"
+#include "utility/Cast.hpp"
+
+#include "glog/logging.h"
+
+namespace quickstep {
+namespace optimizer {
+namespace logical {
+
+namespace E = ::quickstep::optimizer::expressions;
+
+LogicalPtr WindowAggregate::copyWithNewChildren(
+    const std::vector<LogicalPtr> &new_children) const {
+  DCHECK_EQ(getNumChildren(), new_children.size());
+  return Create(new_children[0], window_aggregate_expression_);
+}
+
+std::vector<E::AttributeReferencePtr> WindowAggregate::getOutputAttributes() const
{
+  std::vector<E::AttributeReferencePtr> output_attributes;
+  output_attributes.push_back(E::ToRef(window_aggregate_expression_));
+  
+  return output_attributes;
+}
+
+std::vector<E::AttributeReferencePtr> WindowAggregate::getReferencedAttributes() const
{
+  return window_aggregate_expression_->getReferencedAttributes();
+}
+
+LogicalPtr WindowAggregate::copyWithNewInputExpressions(
+    const std::vector<E::ExpressionPtr> &input_expressions) const {
+  // Only one expression needed
+  DCHECK_EQ(input_expressions.size(), static_cast<std::uint32_t>(1));
+
+  E::AliasPtr window_aggregate_expression;
+  E::SomeAlias::MatchesWithConditionalCast(input_expressions[0],
+                                           &window_aggregate_expression);
+
+  return Create(input_, window_aggregate_expression);
+}
+
+void WindowAggregate::getFieldStringItems(
+    std::vector<std::string> *inline_field_names,
+    std::vector<std::string> *inline_field_values,
+    std::vector<std::string> *non_container_child_field_names,
+    std::vector<OptimizerTreeBaseNodePtr> *non_container_child_fields,
+    std::vector<std::string> *container_child_field_names,
+    std::vector<std::vector<OptimizerTreeBaseNodePtr>> *container_child_fields)
const {
+  non_container_child_field_names->push_back("input");
+  non_container_child_fields->push_back(input_);
+
+  non_container_child_field_names->push_back("window_aggregate_expression");
+  non_container_child_fields->push_back(window_aggregate_expression_);
+}
+
+}  // namespace logical
+}  // namespace optimizer
+}  // namespace quickstep

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/logical/WindowAggregate.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/logical/WindowAggregate.hpp b/query_optimizer/logical/WindowAggregate.hpp
new file mode 100644
index 0000000..7efecf4
--- /dev/null
+++ b/query_optimizer/logical/WindowAggregate.hpp
@@ -0,0 +1,123 @@
+/**
+ *   Copyright 2011-2015 Quickstep Technologies LLC.
+ *   Copyright 2015 Pivotal Software, Inc.
+ *   Copyright 2016, Quickstep Research Group, Computer Sciences Department,
+ *     University of Wisconsin—Madison.
+ *
+ *   Licensed 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_LOGICAL_WINDOW_AGGREGATE_HPP_
+#define QUICKSTEP_QUERY_OPTIMIZER_LOGICAL_WINDOW_AGGREGATE_HPP_
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "query_optimizer/OptimizerTree.hpp"
+#include "query_optimizer/expressions/Alias.hpp"
+#include "query_optimizer/expressions/AttributeReference.hpp"
+#include "query_optimizer/expressions/Expression.hpp"
+#include "query_optimizer/expressions/NamedExpression.hpp"
+#include "query_optimizer/logical/Logical.hpp"
+#include "query_optimizer/logical/LogicalType.hpp"
+#include "utility/Macros.hpp"
+
+namespace quickstep {
+namespace optimizer {
+namespace logical {
+
+/** \addtogroup OptimizerLogical
+ *  @{
+ */
+
+class WindowAggregate;
+typedef std::shared_ptr<const WindowAggregate> WindowAggregatePtr;
+
+/**
+ * @brief Window aggregate operator that computes window aggregate expressions.
+ */
+class WindowAggregate : public Logical {
+ public:
+  LogicalType getLogicalType() const override {
+    return LogicalType::kWindowAggregate;
+  }
+
+  std::string getName() const override { return "Window Aggregate"; }
+
+  /**
+   * @return The input logical node.
+   */
+  const LogicalPtr& input() const { return input_; }
+
+  /**
+   * @return PARTITION BY expressions.
+   */
+  const expressions::AliasPtr window_aggregate_expression() const {
+    return window_aggregate_expression_;
+  }
+
+  LogicalPtr copyWithNewChildren(
+      const std::vector<LogicalPtr> &new_children) const override;
+
+  LogicalPtr copyWithNewInputExpressions(
+      const std::vector<expressions::ExpressionPtr> &input_expressions) const override;
+
+  std::vector<expressions::AttributeReferencePtr> getOutputAttributes() const override;
+
+  std::vector<expressions::AttributeReferencePtr> getReferencedAttributes() const override;
+
+  /**
+   * @brief Creates an Aggregate logical node.
+   *
+   * @param input The input node.
+   * @param window_aggregate_expression The window aggregate expression.
+   * @return An immutable WindowAggregate node.
+   */
+  static WindowAggregatePtr Create(
+      LogicalPtr input,
+      const expressions::AliasPtr window_aggregate_expression) {
+    return WindowAggregatePtr(new WindowAggregate(input, window_aggregate_expression));
+  }
+
+ protected:
+  void getFieldStringItems(
+      std::vector<std::string> *inline_field_names,
+      std::vector<std::string> *inline_field_values,
+      std::vector<std::string> *non_container_child_field_names,
+      std::vector<OptimizerTreeBaseNodePtr> *non_container_child_fields,
+      std::vector<std::string> *container_child_field_names,
+      std::vector<std::vector<OptimizerTreeBaseNodePtr>> *container_child_fields)
const override;
+
+ private:
+  WindowAggregate(LogicalPtr input,
+                  const expressions::AliasPtr window_aggregate_expression)
+      : input_(input),
+        window_aggregate_expression_(window_aggregate_expression) {
+    addChild(input_);
+    addInputExpression(window_aggregate_expression_);
+  }
+
+  LogicalPtr input_;
+  const expressions::AliasPtr window_aggregate_expression_;
+
+  DISALLOW_COPY_AND_ASSIGN(WindowAggregate);
+};
+
+/** @} */
+
+}  // namespace logical
+}  // namespace optimizer
+}  // namespace quickstep
+
+#endif /* QUICKSTEP_QUERY_OPTIMIZER_LOGICAL_AGGREGATE_HPP_ */

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/resolver/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/query_optimizer/resolver/CMakeLists.txt b/query_optimizer/resolver/CMakeLists.txt
index dc7eac0..4c7a70b 100644
--- a/query_optimizer/resolver/CMakeLists.txt
+++ b/query_optimizer/resolver/CMakeLists.txt
@@ -89,6 +89,7 @@ target_link_libraries(quickstep_queryoptimizer_resolver_Resolver
                       quickstep_queryoptimizer_expressions_SimpleCase
                       quickstep_queryoptimizer_expressions_SubqueryExpression
                       quickstep_queryoptimizer_expressions_UnaryExpression
+                      quickstep_queryoptimizer_expressions_WindowAggregateFunction
                       quickstep_queryoptimizer_logical_Aggregate
                       quickstep_queryoptimizer_logical_CopyFrom
                       quickstep_queryoptimizer_logical_CreateIndex

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/resolver/Resolver.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/resolver/Resolver.cpp b/query_optimizer/resolver/Resolver.cpp
index a673ec1..4488b10 100644
--- a/query_optimizer/resolver/Resolver.cpp
+++ b/query_optimizer/resolver/Resolver.cpp
@@ -248,11 +248,9 @@ struct Resolver::SelectListInfo {
    */
   SelectListInfo(
       const std::vector<E::NamedExpressionPtr> &select_list_expressions_in,
-      const std::vector<bool> &has_aggregate_per_expression_in,
-      const std::vector<bool> &has_window_aggregate_per_expression_in)
+      const std::vector<bool> &has_aggregate_per_expression_in)
       : select_list_expressions(select_list_expressions_in),
         has_aggregate_per_expression(has_aggregate_per_expression_in),
-        has_window_aggregate_per_expression(has_window_aggregate_per_expression_in),
         is_referenced(select_list_expressions.size(), false) {
     for (std::vector<E::NamedExpressionPtr>::size_type idx = 0;
          idx < select_list_expressions.size();
@@ -285,10 +283,6 @@ struct Resolver::SelectListInfo {
   // True if the corresponding expression has an aggregate.
   const std::vector<bool> &has_aggregate_per_expression;
 
-  // Has 1:1 matchint with <select_list_expressions>.
-  // True if the corresponding expressions has a window aggregate.
-  const std::vector<bool> &has_window_aggregate_per_expression;
-
   // Has 1:1 matching with <select_list_expressions>.
   // True if the corresponding expression is referenced by an
   // ordinal number or an alias.
@@ -1013,34 +1007,12 @@ L::LogicalPtr Resolver::resolveSelect(
       THROW_SQL_ERROR_AT(&(*select_query.window_list()->begin()))
         << "Currently we don't support multiple window aggregation functions";
     }
-    
+
+    // Sort the table according to the window.
     for (const ParseWindow &window : *select_query.window_list()) {
       E::WindowInfo resolved_window = resolveWindow(window, *name_resolver);
-      
-      // Sort the table by (p_key, o_key)
-      std::vector<E::AttributeReferencePtr> sort_attributes(resolved_window.partition_by_attributes);
-      sort_attributes.insert(sort_attributes.end(),
-                             resolved_window.order_by_attributes.begin(),
-                             resolved_window.order_by_attributes.end());
-                      
-      std::vector<bool> sort_directions(
-          resolved_window.partition_by_attributes.size(), true);
-      sort_directions.insert(sort_directions.end(),
-                            resolved_window.order_by_directions.begin(),
-                            resolved_window.order_by_directions.end());
-                          
-      std::vector<bool> sort_nulls_first(
-          resolved_window.partition_by_attributes.size(), false);
-      sort_nulls_first.insert(sort_nulls_first.end(),
-                              resolved_window.nulls_first.begin(),
-                              resolved_window.nulls_first.end());
-                              
-      L::LogicalPtr sorted_logical_plan =
-          L::Sort::Create(logical_plan,
-                          sort_attributes,
-                          sort_directions,
-                          sort_nulls_first,
-                          -1 /* limit */);
+      L::LogicalPtr sorted_logical_plan = resolveSortInWindow(logical_plan,
+                                                              resolved_window);
 
       WindowAggregationInfo window_aggregation_info(resolved_window,
                                                     sorted_logical_plan);
@@ -1056,7 +1028,6 @@ L::LogicalPtr Resolver::resolveSelect(
   // Resolve SELECT-list clause.
   std::vector<E::NamedExpressionPtr> select_list_expressions;
   std::vector<bool> has_aggregate_per_expression;
-  std::vector<bool> has_window_aggregate_per_expression;
   resolveSelectClause(select_query.selection(),
                       select_name,
                       type_hints,
@@ -1064,14 +1035,19 @@ L::LogicalPtr Resolver::resolveSelect(
                       &query_aggregation_info,
                       &window_aggregate_expressions,
                       &select_list_expressions,
-                      &has_aggregate_per_expression,
-                      &has_window_aggregate_per_expression);
+                      &has_aggregate_per_expression);
   DCHECK_EQ(has_aggregate_per_expression.size(),
             select_list_expressions.size());
 
   SelectListInfo select_list_info(select_list_expressions,
-                                  has_aggregate_per_expression,
-                                  has_window_aggregate_per_expression);
+                                  has_aggregate_per_expression);
+
+  // Create window aggregate node if needed
+  if (!window_aggregate_expressions.empty()) {
+    for (E::AliasPtr window_alias : window_aggregate_expressions) {
+      
+    }
+  }
 
   // Resolve GROUP BY.
   std::vector<E::NamedExpressionPtr> group_by_expressions;
@@ -1848,6 +1824,38 @@ L::LogicalPtr Resolver::resolveJoinedTableReference(
   THROW_SQL_ERROR_AT(&joined_table_reference) << "Full outer join is not supported
yet";
 }
 
+L::LogicalPtr Resolver::resolveSortInWindow(
+    const L::LogicalPtr &logical_plan,
+    const E::WindowInfo &window_info) {
+  // Sort the table by (p_key, o_key)
+  std::vector<E::AttributeReferencePtr> sort_attributes(window_info.partition_by_attributes);
+  sort_attributes.insert(sort_attributes.end(),
+                         window_info.order_by_attributes.begin(),
+                         window_info.order_by_attributes.end());
+                  
+  std::vector<bool> sort_directions(
+      window_info.partition_by_attributes.size(), true);
+  sort_directions.insert(sort_directions.end(),
+                         window_info.order_by_directions.begin(),
+                         window_info.order_by_directions.end());
+                      
+  std::vector<bool> sort_nulls_first(
+      window_info.partition_by_attributes.size(), false);
+  sort_nulls_first.insert(sort_nulls_first.end(),
+                          window_info.nulls_first.begin(),
+                          window_info.nulls_first.end());
+                          
+  L::LogicalPtr sorted_logical_plan =
+      L::Sort::Create(logical_plan,
+                      sort_attributes,
+                      sort_directions,
+                      sort_nulls_first,
+                      -1 /* limit */);
+
+  return sorted_logical_plan;
+}
+    
+
 void Resolver::resolveSelectClause(
     const ParseSelectionClause &parse_selection,
     const std::string &select_name,
@@ -1856,8 +1864,7 @@ void Resolver::resolveSelectClause(
     QueryAggregationInfo *query_aggregation_info,
     std::vector<E::AliasPtr> *window_aggregate_expressions,
     std::vector<E::NamedExpressionPtr> *project_expressions,
-    std::vector<bool> *has_aggregate_per_expression,
-    std::vector<bool> *has_window_aggregate_per_expression) {
+    std::vector<bool> *has_aggregate_per_expression) {
   project_expressions->clear();
   switch (parse_selection.getSelectionType()) {
     case ParseSelectionClause::kStar: {
@@ -1936,8 +1943,6 @@ void Resolver::resolveSelectClause(
         project_expressions->push_back(project_named_expression);
         has_aggregate_per_expression->push_back(
             expr_resolution_info.hasAggregate());
-        has_window_aggregate_per_expression->push_back(
-            expr_resolution_info.hasWindowAggregate());
         ++tid;
       }
       break;
@@ -2645,6 +2650,11 @@ E::ScalarPtr Resolver::resolveFunctionCall(
 E::ScalarPtr Resolver::resolveWindowAggregateFunction(
     const ParseFunctionCall &parse_function_call,
     ExpressionResolutionInfo *expression_resolution_info) {
+  if (!expression_resolution_info->window_aggregate_expressions->empty()) {
+    THROW_SQL_ERROR_AT(&parse_function_call)
+       << "Currently we only support single window aggregate in the query";
+  }
+      
   std::string function_name = ToLower(parse_function_call.name()->value());
   
   // First check for the special case COUNT(*).

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/614d17de/query_optimizer/resolver/Resolver.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/resolver/Resolver.hpp b/query_optimizer/resolver/Resolver.hpp
index eee4d71..302b29d 100644
--- a/query_optimizer/resolver/Resolver.hpp
+++ b/query_optimizer/resolver/Resolver.hpp
@@ -285,9 +285,6 @@ class Resolver {
    * @param has_aggregate_per_expression For each SELECT-list expression,
    *                                     indicates whether it contains
    *                                     an aggregate.
-   * @param has_window_aggregate_per_expression For each SELECT-list expression,
-   *                                            indicates whether it contains a
-   *                                            window aggregate.
    */
   void resolveSelectClause(
       const ParseSelectionClause &parse_selection,
@@ -297,8 +294,7 @@ class Resolver {
       QueryAggregationInfo *query_aggregation_info,
       std::vector<expressions::AliasPtr> *window_aggregate_expressions,
       std::vector<expressions::NamedExpressionPtr> *project_expressions,
-      std::vector<bool> *has_aggregate_per_expression,
-      std::vector<bool> *has_window_aggregate_per_expression);
+      std::vector<bool> *has_aggregate_per_expression);
 
   /**
    * @brief Resolves FROM clause and converts it to a logical plan. If the FROM
@@ -374,6 +370,17 @@ class Resolver {
       const ParseTableReferenceSignature &table_signature);
 
   /**
+   * @brief Sort the input table in (p_key, o_key) order specified by the window.
+   *
+   * @param logical_plan The input logical node.
+   * @param window_info The window that the input table has to be sorted accordingly.
+   * @return A logical plan that sorts the table according to window_info.
+   **/
+  logical::LogicalPtr resolveSortInWindow(
+      const logical::LogicalPtr &logical_plan,
+      const expressions::WindowInfo &window_info);
+
+  /**
    * @brief Resolves a parse expression and converts it to a scalar expression
    *        in the query optimizer. A non-scalar parse expression is resolved
    *        to an AttributeReference to another optimizer expression.


Mime
View raw message