szaszm commented on a change in pull request #732: MINIFICPP-1013
URL: https://github.com/apache/nifi-minifi-cpp/pull/732#discussion_r377579639
 
 

 ##########
 File path: extensions/sql/data/MaxCollector.h
 ##########
 @@ -0,0 +1,172 @@
+/**
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <unordered_map>
+#include <tuple>
+
+#include "SQLRowSubscriber.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace sql {
+
+class MaxCollector: public SQLRowSubscriber {
+  void beginProcessRow() override {}
+
+  void endProcessRow() override {
+    if (columnsVerified_) {
+      return;
+    }
+
+    if (countColumns_ != mapState_.size())
+      throw minifi::Exception(PROCESSOR_EXCEPTION, "MaxCollector: Column(s) '" 
+ maxValueColumnNames_ + "' are not found in the columns of '" + selectQuery_ + 
"' result.");
+
+    columnsVerified_ = true;
+  }
+
+  void processColumnName(const std::string& name) override {
+    if (columnsVerified_) {
+      return;
+    }
+
+    if (mapState_.count(name)) {
+      countColumns_++;
+    }
+  }
+
+  void processColumn(const std::string& name, const std::string& value)  
override {
+    updateMaxValue(name, '\'' + value + '\'');
+  }
+
+  void processColumn(const std::string& name, double value) override {
+    updateMaxValue(name, value);
+  }
+
+  void processColumn(const std::string& name, int value) override {
+    updateMaxValue(name, value);
+  }
+
+  void processColumn(const std::string& name, long long value) override {
+    updateMaxValue(name, value);
+  }
+
+  void processColumn(const std::string& name, unsigned long long value) 
override {
+    updateMaxValue(name, value);
+  }
+
+  void processColumn(const std::string& name, const char* value) override {}
+
+  template <typename T>
+  struct MaxValue {
+    void updateMaxValue(const std::string& name, const T& value) {
+      const auto it = mapColumnNameValue_.find(name);
+      if (it == mapColumnNameValue_.end()) {
+        mapColumnNameValue_.insert({ name, value });
+      } else {
+        if (value > it->second) {
+          it->second = value;
+        }
+      }
+    }
+
+    std::unordered_map<std::string, T> mapColumnNameValue_;
+  };
+
+  template <typename T, typename Tuple, int Index, bool>
+  struct TupleIndexByType {
+    constexpr static int index() {
+      using tupleElType = typename std::decay<decltype(std::get<Index + 
1>(Tuple()))>::type;
+
+      return TupleIndexByType<T, Tuple, Index + 1, std::is_same<tupleElType, 
MaxValue<T>>::value>::index();
+    }
+  };
+
+  template <typename T, typename Tuple, int Index>
+  struct TupleIndexByType<T, Tuple, Index, true> {
+    constexpr static int index() {
+      return Index;
+    }
+  };
 
 Review comment:
   `std::get(std::tuple)` supports indexing by type if the type is present in 
the tuple exactly once, but only since C++14. Can we/do we want to raise the 
requirements of this extension to avoid reimplementing this?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to