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 name checking: duplicate definition and undefined
Date Thu, 23 Jun 2016 14:41:05 GMT
Repository: incubator-quickstep
Updated Branches:
  refs/heads/SQL-window-aggregation 614d17dee -> 9ac2fb0d7


added window name checking: duplicate definition and undefined


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

Branch: refs/heads/SQL-window-aggregation
Commit: 9ac2fb0d7544475a3c76e81e28ba59db4378aef1
Parents: 614d17d
Author: shixuan-fan <shixuan@apache.org>
Authored: Thu Jun 23 14:40:44 2016 +0000
Committer: shixuan-fan <shixuan@apache.org>
Committed: Thu Jun 23 14:40:44 2016 +0000

----------------------------------------------------------------------
 .../expressions/WindowAggregateFunction.cpp     | 109 +++++++++----------
 .../expressions/WindowAggregateFunction.hpp     |  45 ++------
 query_optimizer/resolver/Resolver.cpp           |  82 +++++++++-----
 query_optimizer/resolver/Resolver.hpp           |  10 +-
 4 files changed, 123 insertions(+), 123 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/9ac2fb0d/query_optimizer/expressions/WindowAggregateFunction.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/expressions/WindowAggregateFunction.cpp b/query_optimizer/expressions/WindowAggregateFunction.cpp
index bd813ec..0efa0f1 100644
--- a/query_optimizer/expressions/WindowAggregateFunction.cpp
+++ b/query_optimizer/expressions/WindowAggregateFunction.cpp
@@ -61,23 +61,7 @@ const Type& WindowAggregateFunction::getValueType() const {
 WindowAggregateFunctionPtr WindowAggregateFunction::Create(
     const ::quickstep::AggregateFunction &window_aggregate,
     const std::vector<ScalarPtr> &arguments,
-    WindowInfo *window_info,
-    const bool is_distinct) {
-#ifdef QUICKSTEP_DEBUG
-  std::vector<const Type*> argument_types;
-  for (const ScalarPtr &argument : arguments) {
-    argument_types.emplace_back(&argument->getValueType());
-  }
-  DCHECK(window_aggregate.canApplyToTypes(argument_types));
-#endif  // QUICKSTEP_DEBUG
-
-  return WindowAggregateFunctionPtr(
-      new WindowAggregateFunction(window_aggregate, arguments, window_info, "", is_distinct));
-}
-
-WindowAggregateFunctionPtr WindowAggregateFunction::Create(
-    const ::quickstep::AggregateFunction &window_aggregate,
-    const std::vector<ScalarPtr> &arguments,
+    const WindowInfo window_info,
     const std::string window_name,
     const bool is_distinct) {
 #ifdef QUICKSTEP_DEBUG
@@ -89,7 +73,7 @@ WindowAggregateFunctionPtr WindowAggregateFunction::Create(
 #endif  // QUICKSTEP_DEBUG
 
   return WindowAggregateFunctionPtr(
-      new WindowAggregateFunction(window_aggregate, arguments, nullptr, window_name, is_distinct));
+      new WindowAggregateFunction(window_aggregate, arguments, window_info, window_name,
is_distinct));
 }
 
 ExpressionPtr WindowAggregateFunction::copyWithNewChildren(
@@ -120,15 +104,13 @@ std::vector<AttributeReferencePtr> WindowAggregateFunction::getReferencedAttribu
                                  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_.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());
-  }
+  referenced_attributes.insert(referenced_attributes.end(),
+                               window_info_.order_by_attributes.begin(),
+                               window_info_.order_by_attributes.end());
   
   return referenced_attributes;
 }
@@ -149,40 +131,51 @@ void WindowAggregateFunction::getFieldStringItems(
   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,");
-      }
+  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,");
-      }
+  }
+  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);
+  }
+  nulls_first_flags.pop_back();
+  nulls_first_flags.append("]");
+  inline_field_values->push_back(nulls_first_flags);
+
+  if (window_info_.frame_info != nullptr) {
+    const WindowFrameInfo *frame_info = window_info_.frame_info;
+    
+    inline_field_names->push_back("frame mode");
+    inline_field_values->push_back(frame_info->is_row ? "row" : "range");
+
+    inline_field_names->push_back("num preceding");
+    inline_field_values->push_back(std::to_string(frame_info->num_preceding));
+
+    inline_field_names->push_back("num following");
+    inline_field_values->push_back(std::to_string(frame_info->num_following));
   }
 
   if (is_distinct_) {

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/9ac2fb0d/query_optimizer/expressions/WindowAggregateFunction.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/expressions/WindowAggregateFunction.hpp b/query_optimizer/expressions/WindowAggregateFunction.hpp
index 149214d..f12a00a 100644
--- a/query_optimizer/expressions/WindowAggregateFunction.hpp
+++ b/query_optimizer/expressions/WindowAggregateFunction.hpp
@@ -138,28 +138,28 @@ class WindowAggregateFunction : public Expression {
    * @return The WindowAggregateFunction singleton (from the expression system)
    *         for this node.
    **/
-  inline const ::quickstep::AggregateFunction& getWindowAggregate() const {
+  inline const ::quickstep::AggregateFunction& window_aggregate() const {
     return window_aggregate_;
   }
 
   /**
    * @return The list of scalar arguments to this aggregate.
    **/
-  inline const std::vector<ScalarPtr>& getArguments() const {
+  inline const std::vector<ScalarPtr>& arguments() const {
     return arguments_;
   }
 
   /**
    * @return The window info of this window aggregate function.
    **/
-  inline const WindowInfo* getWindowInfo() const {
+  inline const WindowInfo window_info() const {
     return window_info_;
   }
 
   /**
    * @return The name of the window.
    **/
-  inline const std::string getWindowName() const {
+  inline const std::string window_name() const {
     return window_name_;
   }
 
@@ -171,20 +171,6 @@ 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
@@ -199,24 +185,7 @@ class WindowAggregateFunction : public Expression {
    **/
   static WindowAggregateFunctionPtr Create(const ::quickstep::AggregateFunction &window_aggregate,
                                            const std::vector<ScalarPtr> &arguments,
-                                           WindowInfo *window_info,
-                                           const bool is_distinct);
-
-  /**
-   * @brief Create a new WindowAggregateFunction by window name.
-   *
-   * @warning It is an error to call this with arguments that the given
-   *          aggregate can not apply to.
-   *
-   * @param aggregate The underlying WindowAggregateFunction from the expression
-   *        system.
-   * @param arguments A list of arguments to the window aggregate function.
-   * @param window_name The window name of the window aggregate function.
-   * @param is_distinct Whether this is a DISTINCT aggregation.
-   * @return A new AggregateFunctionPtr.
-   **/
-  static WindowAggregateFunctionPtr Create(const ::quickstep::AggregateFunction &window_aggregate,
-                                           const std::vector<ScalarPtr> &arguments,
+                                           const WindowInfo window_info,
                                            const std::string window_name,
                                            const bool is_distinct);
 
@@ -240,7 +209,7 @@ class WindowAggregateFunction : public Expression {
    */
   WindowAggregateFunction(const ::quickstep::AggregateFunction &window_aggregate,
                           const std::vector<ScalarPtr> &arguments,
-                          WindowInfo *window_info,
+                          const WindowInfo window_info,
                           const std::string window_name,
                           const bool is_distinct)
       : window_aggregate_(window_aggregate),
@@ -259,7 +228,7 @@ class WindowAggregateFunction : public Expression {
   // created as quickstep::WindowAggregateFunction.
   const ::quickstep::AggregateFunction &window_aggregate_;
   std::vector<ScalarPtr> arguments_;
-  WindowInfo *window_info_;
+  const WindowInfo window_info_;
   const std::string window_name_;
   const bool is_distinct_;
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/9ac2fb0d/query_optimizer/resolver/Resolver.cpp
----------------------------------------------------------------------
diff --git a/query_optimizer/resolver/Resolver.cpp b/query_optimizer/resolver/Resolver.cpp
index 4488b10..24e4430 100644
--- a/query_optimizer/resolver/Resolver.cpp
+++ b/query_optimizer/resolver/Resolver.cpp
@@ -165,11 +165,11 @@ struct Resolver::ExpressionResolutionInfo {
    */
   ExpressionResolutionInfo(const NameResolver &name_resolver_in,
                            QueryAggregationInfo *query_aggregation_info_in,
-                           std::vector<E::AliasPtr> *window_aggregate_expressions_in,
+                           WindowAggregationInfo *window_aggregation_info_in,
                            SelectListInfo *select_list_info_in)
       : name_resolver(name_resolver_in),
         query_aggregation_info(query_aggregation_info_in),
-        window_aggregate_expressions(window_aggregate_expressions_in),
+        window_aggregation_info(window_aggregation_info_in),
         select_list_info(select_list_info_in) {}
 
   /**
@@ -183,7 +183,7 @@ struct Resolver::ExpressionResolutionInfo {
       : name_resolver(parent.name_resolver),
         not_allow_aggregate_here(parent.not_allow_aggregate_here),
         query_aggregation_info(parent.query_aggregation_info),
-        window_aggregate_expressions(parent.window_aggregate_expressions),
+        window_aggregation_info(parent.window_aggregation_info),
         select_list_info(parent.select_list_info) {}
 
   /**
@@ -206,7 +206,7 @@ struct Resolver::ExpressionResolutionInfo {
   QueryAggregationInfo *query_aggregation_info = nullptr;
 
   // Alias expressions that wraps window aggregate functions.
-  std::vector<E::AliasPtr> *window_aggregate_expressions = nullptr;
+  WindowAggregationInfo *window_aggregation_info = nullptr;
   
   // Can be NULL if alias references to SELECT-list expressions are not allowed.
   SelectListInfo *select_list_info = nullptr;
@@ -226,9 +226,9 @@ struct Resolver::QueryAggregationInfo {
   std::vector<E::AliasPtr> aggregate_expressions;
 };
 
-struct Resolver::WindowAggregationInfo {
-  explicit WindowAggregationInfo(E::WindowInfo window_info_in,
-                                 L::LogicalPtr logical_plan_in)
+struct Resolver::WindowPlan {
+  explicit WindowPlan(E::WindowInfo window_info_in,
+                      L::LogicalPtr logical_plan_in)
       : window_info(window_info_in),
         logical_plan(logical_plan_in) {}
 
@@ -236,6 +236,16 @@ struct Resolver::WindowAggregationInfo {
   const L::LogicalPtr logical_plan;
 };
 
+struct Resolver::WindowAggregationInfo {
+  explicit WindowAggregationInfo(const std::unordered_map<std::string, WindowPlan>
window_map_in)
+      : window_map(window_map_in) {}
+
+  // Whether the current query block has a GROUP BY.
+  const std::unordered_map<std::string, WindowPlan> window_map;
+  // Alias expressions that wraps window aggregate functions.
+  std::vector<E::AliasPtr> window_aggregate_expressions;
+};
+
 struct Resolver::SelectListInfo {
  public:
   /**
@@ -1001,7 +1011,7 @@ L::LogicalPtr Resolver::resolveSelect(
   }
 
   // Resolve WINDOW clause.
-  std::unordered_map<std::string, WindowAggregationInfo> sorted_window_table;
+  std::unordered_map<std::string, WindowPlan> sorted_window_map;
   if (select_query.window_list() != nullptr) {
     if (select_query.window_list()->size() > 1) {
       THROW_SQL_ERROR_AT(&(*select_query.window_list()->begin()))
@@ -1010,21 +1020,26 @@ L::LogicalPtr Resolver::resolveSelect(
 
     // Sort the table according to the window.
     for (const ParseWindow &window : *select_query.window_list()) {
+      // Check for duplicate definition.
+      if (sorted_window_map.find(window.name()->value()) != sorted_window_map.end()) {
+        THROW_SQL_ERROR_AT(window.name())
+            << "Duplicate definition of window " << window.name()->value();
+      }
+      
       E::WindowInfo resolved_window = resolveWindow(window, *name_resolver);
       L::LogicalPtr sorted_logical_plan = resolveSortInWindow(logical_plan,
                                                               resolved_window);
 
-      WindowAggregationInfo window_aggregation_info(resolved_window,
-                                                    sorted_logical_plan);
+      WindowPlan window_plan(resolved_window, sorted_logical_plan);
       
-      sorted_window_table.emplace(window.name()->value(), window_aggregation_info);
+      sorted_window_map.emplace(window.name()->value(), window_plan);
     }
   }
 
   QueryAggregationInfo query_aggregation_info(
       (select_query.group_by() != nullptr));
-  std::vector<E::AliasPtr> window_aggregate_expressions;
-
+  WindowAggregationInfo window_aggregation_info(sorted_window_map);    
+  
   // Resolve SELECT-list clause.
   std::vector<E::NamedExpressionPtr> select_list_expressions;
   std::vector<bool> has_aggregate_per_expression;
@@ -1033,7 +1048,7 @@ L::LogicalPtr Resolver::resolveSelect(
                       type_hints,
                       *name_resolver,
                       &query_aggregation_info,
-                      &window_aggregate_expressions,
+                      &window_aggregation_info,
                       &select_list_expressions,
                       &has_aggregate_per_expression);
   DCHECK_EQ(has_aggregate_per_expression.size(),
@@ -1043,10 +1058,13 @@ L::LogicalPtr Resolver::resolveSelect(
                                   has_aggregate_per_expression);
 
   // Create window aggregate node if needed
-  if (!window_aggregate_expressions.empty()) {
-    for (E::AliasPtr window_alias : window_aggregate_expressions) {
-      
-    }
+  for (E::AliasPtr alias : window_aggregation_info.window_aggregate_expressions) {
+    E::WindowAggregateFunctionPtr window_aggregate_function
+        = std::static_pointer_cast<const E::WindowAggregateFunction>(alias->expression());
+    L::LogicalPtr sorted_logical_plan;
+
+    // Get the sorted logical plan
+
   }
 
   // Resolve GROUP BY.
@@ -1096,7 +1114,7 @@ L::LogicalPtr Resolver::resolveSelect(
   E::PredicatePtr having_predicate;
   if (select_query.having() != nullptr) {
     ExpressionResolutionInfo expr_resolution_info(
-        *name_resolver, &query_aggregation_info, &window_aggregate_expressions, &select_list_info);
+        *name_resolver, &query_aggregation_info, &window_aggregation_info, &select_list_info);
     having_predicate = resolvePredicate(
         *select_query.having()->having_predicate(), &expr_resolution_info);
   }
@@ -1110,7 +1128,7 @@ L::LogicalPtr Resolver::resolveSelect(
     for (const ParseOrderByItem &order_by_item :
          *select_query.order_by()->order_by_items()) {
       ExpressionResolutionInfo expr_resolution_info(
-          *name_resolver, &query_aggregation_info, &window_aggregate_expressions,
&select_list_info);
+          *name_resolver, &query_aggregation_info, &window_aggregation_info, &select_list_info);
       E::ScalarPtr order_by_scalar = resolveExpression(
           *order_by_item.ordering_expression(),
           nullptr,  // No Type hint.
@@ -1862,7 +1880,7 @@ void Resolver::resolveSelectClause(
     const std::vector<const Type*> *type_hints,
     const NameResolver &name_resolver,
     QueryAggregationInfo *query_aggregation_info,
-    std::vector<E::AliasPtr> *window_aggregate_expressions,
+    WindowAggregationInfo *window_aggregation_info,
     std::vector<E::NamedExpressionPtr> *project_expressions,
     std::vector<bool> *has_aggregate_per_expression) {
   project_expressions->clear();
@@ -1893,7 +1911,7 @@ void Resolver::resolveSelectClause(
         ExpressionResolutionInfo expr_resolution_info(
             name_resolver,
             query_aggregation_info,
-            window_aggregate_expressions,
+            window_aggregation_info,
             nullptr /* select_list_info */);
         const E::ScalarPtr project_scalar =
             resolveExpression(*parse_project_expression,
@@ -2650,7 +2668,7 @@ 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()) {
+  if (!expression_resolution_info->window_aggregation_info->window_aggregate_expressions.empty())
{
     THROW_SQL_ERROR_AT(&parse_function_call)
        << "Currently we only support single window aggregate in the query";
   }
@@ -2752,9 +2770,21 @@ E::ScalarPtr Resolver::resolveWindowAggregateFunction(
   // A window aggregate function might be defined OVER a window name or a window.
   E::WindowAggregateFunctionPtr window_aggregate_function;
   if (parse_function_call.window_name() != nullptr) {
+    std::unordered_map<std::string, WindowPlan> window_map
+        = expression_resolution_info->window_aggregation_info->window_map;
+    std::string window_name = parse_function_call.window_name()->value();
+    std::unordered_map<std::string, WindowPlan>::const_iterator map_it
+        = window_map.find(window_name);
+        
+    if (map_it == window_map.end()) {
+      THROW_SQL_ERROR_AT(parse_function_call.window_name())
+        << "Undefined window " << window_name;
+    }
+    
     window_aggregate_function =
         E::WindowAggregateFunction::Create(*window_aggregate,
                                            resolved_arguments,
+                                           map_it->second.window_info,
                                            parse_function_call.window_name()->value(),
                                            parse_function_call.is_distinct());
   }
@@ -2765,7 +2795,8 @@ E::ScalarPtr Resolver::resolveWindowAggregateFunction(
     window_aggregate_function =
         E::WindowAggregateFunction::Create(*window_aggregate,
                                            resolved_arguments,
-                                           &resolved_window,
+                                           resolved_window,
+                                           "" /* window name */,
                                            parse_function_call.is_distinct());
   }
 
@@ -2777,7 +2808,8 @@ E::ScalarPtr Resolver::resolveWindowAggregateFunction(
                                                        internal_alias,
                                                        "$window_aggregate" /* relation_name
*/);
                                                        
-  expression_resolution_info->window_aggregate_expressions->emplace_back(aggregate_alias);
+  expression_resolution_info->window_aggregation_info
+      ->window_aggregate_expressions.emplace_back(aggregate_alias);
   expression_resolution_info->parse_window_aggregate_expression = &parse_function_call;
   return E::ToRef(aggregate_alias);
 }

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/9ac2fb0d/query_optimizer/resolver/Resolver.hpp
----------------------------------------------------------------------
diff --git a/query_optimizer/resolver/Resolver.hpp b/query_optimizer/resolver/Resolver.hpp
index 302b29d..7d597e9 100644
--- a/query_optimizer/resolver/Resolver.hpp
+++ b/query_optimizer/resolver/Resolver.hpp
@@ -126,11 +126,17 @@ class Resolver {
   struct QueryAggregationInfo;
 
   /**
-   * @brief A wrapper for resolved window and the corresponding sorted plan.
+   * @brief Query-scoped info that contains window aggregate expressions and a
+   *        window map.
    **/
   struct WindowAggregationInfo;
 
   /**
+   * @brief A wrapper for resolved window and the corresponding sorted plan.
+   **/
+  struct WindowPlan;
+
+  /**
    * @brief Query-scoped info that contains select-list expressions
    *        and whether an expression is referenced by an
    *        ordinal or alias reference.
@@ -292,7 +298,7 @@ class Resolver {
       const std::vector<const Type*> *type_hints,
       const NameResolver &name_resolver,
       QueryAggregationInfo *query_aggregation_info,
-      std::vector<expressions::AliasPtr> *window_aggregate_expressions,
+      WindowAggregationInfo *window_aggregation_info,
       std::vector<expressions::NamedExpressionPtr> *project_expressions,
       std::vector<bool> *has_aggregate_per_expression);
 


Mime
View raw message