quickstep-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jianq...@apache.org
Subject [48/55] [abbrv] incubator-quickstep git commit: Initial commit
Date Thu, 09 Mar 2017 21:32:27 GMT
http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/6941f903/types/operations/unary_operations/UnaryOperationWrapper.hpp
----------------------------------------------------------------------
diff --git a/types/operations/unary_operations/UnaryOperationWrapper.hpp b/types/operations/unary_operations/UnaryOperationWrapper.hpp
new file mode 100644
index 0000000..417c752
--- /dev/null
+++ b/types/operations/unary_operations/UnaryOperationWrapper.hpp
@@ -0,0 +1,441 @@
+/**
+ * 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.
+ **/
+
+#ifndef QUICKSTEP_TYPES_OPERATIONS_UNARY_OPERATIONS_UNARY_OPERATION_WRAPPER_HPP_
+#define QUICKSTEP_TYPES_OPERATIONS_UNARY_OPERATIONS_UNARY_OPERATION_WRAPPER_HPP_
+
+#include <cstddef>
+#include <string>
+#include <type_traits>
+
+#include "catalog/CatalogTypedefs.hpp"
+#include "storage/ValueAccessor.hpp"
+#include "storage/ValueAccessorUtil.hpp"
+#include "types/CharType.hpp"
+#include "types/IntType.hpp"
+#include "types/LongType.hpp"
+#include "types/Type.hpp"
+#include "types/TypeFactory.hpp"
+#include "types/TypeID.hpp"
+#include "types/TypedValue.hpp"
+#include "types/VarCharType.hpp"
+#include "types/containers/ColumnVector.hpp"
+#include "types/operations/Operation.hpp"
+#include "types/operations/OperationSignature.hpp"
+#include "types/operations/Operation.pb.h"
+#include "utility/Macros.hpp"
+
+namespace quickstep {
+
+class ColumnVector;
+class Type;
+class ValueAccessor;
+
+/** \addtogroup Types
+ *  @{
+ */
+
+template <typename ArgumentT, typename ResultT>
+struct UnaryFunctor {
+  typedef ArgumentT ArgumentType;
+  typedef ResultT ResultType;
+};
+
+template <typename FunctorT, typename ...SpecArgs>
+class UncheckedUnaryOperatorWrapperCodegen : public UncheckedUnaryOperator {
+ public:
+  template <typename ...ConstructorArgs>
+  UncheckedUnaryOperatorWrapperCodegen(const Type &argument_type,
+                                       const Type &result_type,
+                                       ConstructorArgs &&...args)
+      : functor_(std::forward<ConstructorArgs>(args)...),
+        impl_(argument_type, result_type) {}
+
+  TypedValue applyToTypedValue(const TypedValue &argument) const override {
+    return impl_.applyToTypedValue(argument, functor_);
+  }
+
+  ColumnVector* applyToColumnVector(const ColumnVector &argument) const override {
+    return impl_.applyToColumnVector(argument, functor_);
+  }
+
+  ColumnVector* applyToValueAccessor(ValueAccessor *accessor,
+                                     const attribute_id argument_attr_id) const override
{
+    return impl_.applyToValueAccessor(accessor, argument_attr_id, functor_);
+  }
+
+ private:
+  using ArgumentType = typename FunctorT::ArgumentType;
+  using ResultType = typename FunctorT::ResultType;
+
+  template <bool specialize, typename EnableT = void>
+  struct FunctorSpecializer;
+
+  template <typename T, typename EnableT = void>
+  struct Codegen;
+
+  template <bool argument_nullable,
+            typename ArgumentGen, typename ResultGen>
+  struct Implementation;
+
+  using FuncSpec = FunctorSpecializer<sizeof...(SpecArgs) != 0>;
+  using Impl = Implementation<false,
+                              Codegen<ArgumentType>,
+                              Codegen<ResultType>>;
+
+  const FunctorT functor_;
+  const Impl impl_;
+};
+
+template <typename FunctorT>
+class UnaryOperationWrapper : public UnaryOperation {
+ public:
+  UnaryOperationWrapper()
+      : UnaryOperation(),
+        operation_name_(FunctorT().getName()) {}
+
+  std::string getName() const override {
+    return operation_name_;
+  }
+
+  std::string getShortName() const override {
+    return getName();
+  }
+
+  std::vector<OperationSignaturePtr> getSignatures() const override {
+    return {
+      OperationSignature::Create(getName(), {ArgumentType::kStaticTypeID}, 0)
+    };
+  }
+
+  bool canApplyTo(const Type &argument_type,
+                  const std::vector<TypedValue> &static_arguments,
+                  std::string *message) const override {
+    DCHECK(argument_type.getTypeID() == ArgumentType::kStaticTypeID);
+    DCHECK(static_arguments.empty());
+    return true;
+  }
+
+  const Type* getResultType(
+      const Type &argument_type,
+      const std::vector<TypedValue> &static_arguments) const override {
+    DCHECK(argument_type.getTypeID() == ArgumentType::kStaticTypeID);
+    DCHECK(static_arguments.empty());
+    return &TypeFactory::GetType(ResultType::kStaticTypeID);
+  }
+
+  UncheckedUnaryOperator* makeUncheckedUnaryOperator(
+      const Type &argument_type,
+      const std::vector<TypedValue> &static_arguments) const override {
+    DCHECK(argument_type.getTypeID() == ArgumentType::kStaticTypeID);
+    DCHECK(static_arguments.empty());
+    return new UncheckedUnaryOperatorWrapperCodegen<FunctorT>(
+        argument_type, *getResultType(argument_type, static_arguments));
+  }
+
+ private:
+  using ArgumentType = typename FunctorT::ArgumentType;
+  using ResultType = typename FunctorT::ResultType;
+
+  // TODO
+  static constexpr bool kCanAutoDerive = !ResultType::kParameterized;
+
+  const std::string operation_name_;
+};
+
+
+template <typename FunctorT, typename ...SpecArgs>
+template <bool specialize>
+struct UncheckedUnaryOperatorWrapperCodegen<FunctorT, SpecArgs...>
+    ::FunctorSpecializer<specialize, std::enable_if_t<specialize>> {
+  template <typename ...FuncArgs>
+  inline static auto Invoke(const FunctorT &functor, FuncArgs &&...args) {
+    return functor.template apply<SpecArgs...>(std::forward<FuncArgs>(args)...);
+  }
+};
+
+template <typename FunctorT, typename ...SpecArgs>
+template <bool specialize>
+struct UncheckedUnaryOperatorWrapperCodegen<FunctorT, SpecArgs...>
+    ::FunctorSpecializer<specialize, std::enable_if_t<!specialize>> {
+  template <typename ...FuncArgs>
+  inline static auto Invoke(const FunctorT &functor, FuncArgs &&...args) {
+    return functor.apply(std::forward<FuncArgs>(args)...);
+  }
+};
+
+template <typename FunctorT, typename ...SpecArgs>
+template <typename T>
+struct UncheckedUnaryOperatorWrapperCodegen<FunctorT, SpecArgs...>
+    ::Codegen<T, std::enable_if_t<T::kLayout == kNativeEmbedded>> {
+  using NativeType = typename T::cpptype;
+  using NativeTypeConstRef = const NativeType&;
+  using NativeTypeConstPtr = const NativeType*;
+  using ColumnVectorType = NativeColumnVector;
+
+  template <typename ArgumentGen>
+  inline static TypedValue ApplyGenericTypedValue(
+      typename ArgumentGen::NativeTypeConstRef argument,
+      const Type &result_type,
+      const FunctorT &functor) {
+    return TypedValue(FuncSpec::Invoke(functor, argument));
+  }
+
+  template <typename ArgumentGen>
+  inline static void ApplyGenericColumnVector(
+      const typename ArgumentGen::NativeTypeConstPtr &argument,
+      const FunctorT &functor,
+      ColumnVectorType *cv) {
+    *static_cast<NativeType *>(cv->getPtrForDirectWrite()) =
+        FuncSpec::Invoke(functor, ArgumentGen::Dereference(argument));
+  }
+
+  template <bool nullable>
+  inline static NativeTypeConstPtr GetValuePtrColumnVector(
+      const ColumnVectorType &cv,
+      const std::size_t pos) {
+    return static_cast<NativeTypeConstPtr>(
+        cv.template getUntypedValue<nullable>(pos));
+  }
+
+  template <bool nullable, typename ValueAccessorT>
+  inline static NativeTypeConstPtr GetValuePtrValueAccessor(
+      ValueAccessorT *va,
+      const attribute_id argument_attr_id) {
+    return static_cast<NativeTypeConstPtr>(
+        va->template getUntypedValue<nullable>(argument_attr_id));
+  }
+
+  // Dereference: NativeTypeConstPtr& -> bool
+  inline static bool IsNull(const NativeType *value) {
+    return value == nullptr;
+  }
+
+  // Dereference: NativeTypeConstPtr& -> const NativeType&
+  inline static const NativeType& Dereference(const NativeType *value) {
+    return *value;
+  }
+
+  inline static const NativeType ToNativeValue(const TypedValue &value) {
+    return value.getLiteral<NativeType>();
+  }
+};
+
+template <typename FunctorT, typename ...SpecArgs>
+template <typename T>
+struct UncheckedUnaryOperatorWrapperCodegen<FunctorT, SpecArgs...>
+    ::Codegen<T, std::enable_if_t<T::kLayout == kNonNativeInline>> {
+  using NativeType = void*;
+  using NativeTypeConstRef = const void*;
+  using NativeTypeConstPtr = const void*;
+  using ColumnVectorType = NativeColumnVector;
+
+  template <typename ArgumentGen>
+  inline static TypedValue ApplyGenericTypedValue(
+      typename ArgumentGen::NativeTypeConstRef argument,
+      const Type &result_type,
+      const FunctorT &functor) {
+    void *result = std::malloc(result_type.maximumByteLength());
+    FuncSpec::Invoke(functor, argument, result);
+    return TypedValue::CreateWithOwnedData(T::kStaticTypeID,
+                                           result,
+                                           result_type.maximumByteLength());
+  }
+
+  template <typename ArgumentGen>
+  inline static void ApplyGenericColumnVector(
+      const typename ArgumentGen::NativeTypeConstPtr &argument,
+      const FunctorT &functor,
+      ColumnVectorType *cv) {
+    FuncSpec::Invoke(functor,
+                     ArgumentGen::Dereference(argument),
+                     cv->getPtrForDirectWrite());
+  }
+
+  template <bool nullable>
+  inline static NativeTypeConstPtr GetValuePtrColumnVector(
+      const ColumnVectorType &cv,
+      const std::size_t pos) {
+    return cv.template getUntypedValue<nullable>(pos);
+  }
+
+  template <bool nullable, typename ValueAccessorT>
+  inline static NativeTypeConstPtr GetValuePtrValueAccessor(
+      ValueAccessorT *va,
+      const attribute_id argument_attr_id) {
+    return va->template getUntypedValue<nullable>(argument_attr_id);
+  }
+
+  inline static bool IsNull(const void *value) {
+    return value == nullptr;
+  }
+
+  // Dereference: NativeTypeConstPtr& -> const NativeType&
+  inline static const void* Dereference(const void *value) {
+    return value;
+  }
+
+  inline static const void* ToNativeValue(const TypedValue &value) {
+    return value.getDataPtr();
+  }
+};
+
+template <typename FunctorT, typename ...SpecArgs>
+template <typename T>
+struct UncheckedUnaryOperatorWrapperCodegen<FunctorT, SpecArgs...>
+    ::Codegen<T, std::enable_if_t<T::kLayout == kOutOfLine>> {
+  using NativeType = TypedValue;
+  using NativeTypeConstRef = const TypedValue&;
+  using NativeTypeConstPtr = const TypedValue;
+  using ColumnVectorType = IndirectColumnVector;
+
+  template <typename ArgumentGen>
+  inline static TypedValue ApplyGenericTypedValue(
+      typename ArgumentGen::NativeTypeConstRef argument,
+      const Type &result_type,
+      const FunctorT &functor) {
+    return FuncSpec::Invoke(functor, argument);
+  }
+
+  template <typename ArgumentGen>
+  inline static void ApplyGenericColumnVector(
+      const typename ArgumentGen::NativeTypeConstPtr &argument,
+      const FunctorT &functor,
+      ColumnVectorType *cv) {
+    cv->appendTypedValue(
+        FuncSpec::Invoke(functor, ArgumentGen::Dereference(argument)));
+  }
+
+  template <bool nullable>
+  inline static NativeTypeConstPtr GetValuePtrColumnVector(
+      const ColumnVectorType &cv,
+      const std::size_t pos) {
+    return cv.getTypedValue(pos);
+  }
+
+  template <bool nullable, typename ValueAccessorT>
+  inline static NativeTypeConstPtr GetValuePtrValueAccessor(
+      ValueAccessorT *va,
+      const attribute_id argument_attr_id) {
+    return va->getTypedValue(argument_attr_id);
+  }
+
+  inline static bool IsNull(NativeTypeConstPtr &value) {
+    return value.isNull();
+  }
+
+  // Dereference: NativeTypeConstPtr& -> const NativeType&
+  inline static const NativeType& Dereference(NativeTypeConstPtr &value) {
+    return value;
+  }
+
+  inline static const NativeType& ToNativeValue(const TypedValue &value) {
+    return value;
+  }
+};
+
+template <typename FunctorT, typename ...SpecArgs>
+template <bool argument_nullable,
+          typename ArgumentGen, typename ResultGen>
+struct UncheckedUnaryOperatorWrapperCodegen<FunctorT, SpecArgs...>
+    ::Implementation {
+  Implementation(const Type &argument_type_in, const Type &result_type_in)
+      : argument_type(argument_type_in),
+        result_type(result_type_in) {}
+
+  inline TypedValue applyToTypedValue(const TypedValue &argument,
+                                      const FunctorT &functor) const {
+    if (argument_nullable && argument.isNull()) {
+      return TypedValue(result_type.getTypeID());
+    }
+
+    return ResultGen::template ApplyGenericTypedValue<ArgumentGen>(
+        ArgumentGen::ToNativeValue(argument),
+        result_type,
+        functor);
+  }
+
+  inline ColumnVector* applyToColumnVector(const ColumnVector &argument,
+                                           const FunctorT &functor) const {
+    using ArgumentCVT = typename ArgumentGen::ColumnVectorType;
+    using ResultCVT = typename ResultGen::ColumnVectorType;
+
+    const ArgumentCVT &argument_cv = static_cast<const ArgumentCVT&>(argument);
+    ResultCVT *result_cv = new ResultCVT(result_type, argument_cv.size());
+
+    for (std::size_t pos = 0; pos < argument_cv.size(); ++pos) {
+      typename ArgumentGen::NativeTypeConstPtr scalar_arg =
+          ArgumentGen::template GetValuePtrColumnVector<argument_nullable>(
+              argument_cv, pos);
+
+      if (argument_nullable && ArgumentGen::IsNull(scalar_arg)) {
+        result_cv->appendNullValue();
+      } else {
+        ResultGen::template ApplyGenericColumnVector<ArgumentGen>(
+            scalar_arg, functor, result_cv);
+      }
+    }
+    return result_cv;
+  }
+
+  inline ColumnVector* applyToValueAccessor(ValueAccessor *accessor,
+                                            const attribute_id argument_attr_id,
+                                            const FunctorT &functor) const {
+    using ResultCVT = typename ResultGen::ColumnVectorType;
+
+    return InvokeOnValueAccessorMaybeTupleIdSequenceAdapter(
+        accessor,
+        [&](auto *accessor) -> ColumnVector* {  // NOLINT(build/c++11)
+      ResultCVT *result_cv = new ResultCVT(result_type, accessor->getNumTuples());
+
+      accessor->beginIteration();
+      while (accessor->next()) {
+        typename ArgumentGen::NativeTypeConstPtr scalar_arg =
+            ArgumentGen::template GetValuePtrValueAccessor<argument_nullable>(
+                accessor, argument_attr_id);
+
+        if (argument_nullable && ArgumentGen::IsNull(scalar_arg)) {
+          result_cv->appendNullValue();
+        } else {
+          ResultGen::template ApplyGenericColumnVector<ArgumentGen>(
+              scalar_arg, functor, result_cv);
+        }
+      }
+      return result_cv;
+    });
+  }
+
+  const Type &argument_type;
+  const Type &result_type;
+};
+
+template <typename ...FunctorTypes>
+struct UnaryFunctorPack {
+  static std::vector<UnaryOperationPtr> GenerateAll() {
+    return {
+        UnaryOperationPtr(new UnaryOperationWrapper<FunctorTypes>())...
+    };
+  }
+};
+
+/** @} */
+
+}  // namespace quickstep
+
+#endif  // QUICKSTEP_TYPES_OPERATIONS_UNARY_OPERATIONS_UNARY_OPERATION_WRAPPER_HPP_

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/6941f903/utility/StringUtil.cpp
----------------------------------------------------------------------
diff --git a/utility/StringUtil.cpp b/utility/StringUtil.cpp
index 2745457..9fca695 100644
--- a/utility/StringUtil.cpp
+++ b/utility/StringUtil.cpp
@@ -52,6 +52,12 @@ std::string ToLower(const std::string& str) {
   return lower_str;
 }
 
+std::string ToUpper(const std::string& str) {
+  std::string upper_str(str.size(), ' ');
+  std::transform(str.begin(), str.end(), upper_str.begin(), toupper);
+  return upper_str;
+}
+
 std::string EscapeSpecialChars(const std::string& text) {
   std::string new_text;
   for (const char& c : text) {

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/6941f903/utility/StringUtil.hpp
----------------------------------------------------------------------
diff --git a/utility/StringUtil.hpp b/utility/StringUtil.hpp
index abda8f3..e928225 100644
--- a/utility/StringUtil.hpp
+++ b/utility/StringUtil.hpp
@@ -35,11 +35,21 @@ namespace quickstep {
  * @brief Convert a string \p str to lower case.
  *
  * @param str The string to be converted.
- * @return The converted string with all lower case characters bing converted to upper case
characters.
+ * @return The converted string with all lower case characters being converted
+ *         to upper case characters.
  */
 extern std::string ToLower(const std::string &str);
 
 /**
+ * @brief Convert a string \p str to upper case.
+ *
+ * @param str The string to be converted.
+ * @return The converted string with all upper case characters being converted
+ *         to lower case characters.
+ */
+extern std::string ToUpper(const std::string &str);
+
+/**
  * @brief Converts special characters to escape characters.
  *
  * @param text The string to be unescaped.

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/6941f903/utility/TemplateUtil.hpp
----------------------------------------------------------------------
diff --git a/utility/TemplateUtil.hpp b/utility/TemplateUtil.hpp
index dfae8e4..f6baec2 100644
--- a/utility/TemplateUtil.hpp
+++ b/utility/TemplateUtil.hpp
@@ -30,8 +30,6 @@ namespace quickstep {
  *  @{
  */
 
-namespace template_util_inner {
-
 /**
  * @brief Represents a compile-time sequence of integers.
  *
@@ -60,6 +58,20 @@ struct MakeSequence<0, S...> {
   typedef Sequence<S...> type;
 };
 
+template<class...> struct Disjunction : std::false_type {};
+template<class B1> struct Disjunction<B1> : B1 {};
+template<class B1, class... Bn>
+struct Disjunction<B1, Bn...>
+    : std::conditional_t<bool(B1::value), B1, Disjunction<Bn...>>  {};
+
+template <typename check, typename ...cases>
+struct EqualsAny {
+  static constexpr bool value =
+      Disjunction<std::is_same<check, cases>...>::value;
+};
+
+namespace template_util_inner {
+
 /**
  * @brief Final step of CreateBoolInstantiatedInstance. Now all bool_values are
  *        ready. Instantiate the template and create (i.e. new) an instance.
@@ -125,7 +137,7 @@ inline ReturnT* CreateBoolInstantiatedInstance(Tuple &&args) {
   return template_util_inner::CreateBoolInstantiatedInstanceInner<
       T, ReturnT, bool_values...>(
           std::forward<Tuple>(args),
-          typename template_util_inner::MakeSequence<n_args>::type());
+          typename MakeSequence<n_args>::type());
 }
 
 /**
@@ -225,9 +237,17 @@ inline auto InvokeOnBools(ArgTypes ...args) {
   constexpr std::size_t last = sizeof...(args) - 1;
   return template_util_inner::InvokeOnBoolsInner<last>(
       std::forward_as_tuple(args...),
-      typename template_util_inner::MakeSequence<last>::type());
+      typename MakeSequence<last>::type());
 }
 
+
+template <char ...c>
+struct StringLiteral {
+  inline static std::string ToString() {
+    return std::string({c...});
+  }
+};
+
 /** @} */
 
 }  // namespace quickstep


Mime
View raw message