You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2017/02/04 23:33:44 UTC

arrow git commit: ARROW-381: [C++] Simplify primitive array type builders to use a default type singleton

Repository: arrow
Updated Branches:
  refs/heads/master 5b35d6bda -> 84f16624b


ARROW-381: [C++] Simplify primitive array type builders to use a default type singleton

Author: Uwe L. Korn <uw...@xhochy.com>

Closes #316 from xhochy/ARROW-381 and squashes the following commits:

7061d9a [Uwe L. Korn] Use TypeTraits
79e07f1 [Uwe L. Korn] ARROW-381: [C++] Simplify primitive array type builders to use a default type singleton


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/84f16624
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/84f16624
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/84f16624

Branch: refs/heads/master
Commit: 84f16624bb390aebf16318b62ff2ac8238fc4b7c
Parents: 5b35d6b
Author: Uwe L. Korn <uw...@xhochy.com>
Authored: Sat Feb 4 18:33:39 2017 -0500
Committer: Wes McKinney <we...@twosigma.com>
Committed: Sat Feb 4 18:33:39 2017 -0500

----------------------------------------------------------------------
 cpp/src/arrow/builder-benchmark.cc |  2 +-
 cpp/src/arrow/builder.h            |  7 +++++++
 cpp/src/arrow/test-util.h          |  4 ++--
 cpp/src/arrow/type.h               | 17 -----------------
 cpp/src/arrow/type_fwd.h           | 22 ++++++++++++++++++++++
 cpp/src/arrow/type_traits.h        | 31 +++++++++++++++++++++++++++++++
 6 files changed, 63 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/84f16624/cpp/src/arrow/builder-benchmark.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/builder-benchmark.cc b/cpp/src/arrow/builder-benchmark.cc
index 67799a3..b0c3cd1 100644
--- a/cpp/src/arrow/builder-benchmark.cc
+++ b/cpp/src/arrow/builder-benchmark.cc
@@ -30,7 +30,7 @@ static void BM_BuildPrimitiveArrayNoNulls(
   // 2 MiB block
   std::vector<int64_t> data(256 * 1024, 100);
   while (state.KeepRunning()) {
-    Int64Builder builder(default_memory_pool(), arrow::int64());
+    Int64Builder builder(default_memory_pool());
     for (int i = 0; i < kFinalSize; i++) {
       // Build up an array of 512 MiB in size
       builder.Append(data.data(), data.size(), nullptr);

http://git-wip-us.apache.org/repos/asf/arrow/blob/84f16624/cpp/src/arrow/builder.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/builder.h b/cpp/src/arrow/builder.h
index 747da7c..672d2d8 100644
--- a/cpp/src/arrow/builder.h
+++ b/cpp/src/arrow/builder.h
@@ -19,6 +19,7 @@
 #define ARROW_BUILDER_H
 
 #include <cstdint>
+#include <functional>
 #include <memory>
 #include <string>
 #include <vector>
@@ -27,6 +28,7 @@
 #include "arrow/memory_pool.h"
 #include "arrow/status.h"
 #include "arrow/type.h"
+#include "arrow/type_traits.h"
 #include "arrow/util/bit-util.h"
 #include "arrow/util/macros.h"
 #include "arrow/util/visibility.h"
@@ -186,6 +188,11 @@ class ARROW_EXPORT NumericBuilder : public PrimitiveBuilder<T> {
   using typename PrimitiveBuilder<T>::value_type;
   using PrimitiveBuilder<T>::PrimitiveBuilder;
 
+  template <typename T1 = T>
+  explicit NumericBuilder(
+      typename std::enable_if<TypeTraits<T1>::is_parameter_free, MemoryPool*>::type pool)
+      : PrimitiveBuilder<T1>(pool, TypeTraits<T1>::type_singleton()) {}
+
   using PrimitiveBuilder<T>::Append;
   using PrimitiveBuilder<T>::Init;
   using PrimitiveBuilder<T>::Resize;

http://git-wip-us.apache.org/repos/asf/arrow/blob/84f16624/cpp/src/arrow/test-util.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/test-util.h b/cpp/src/arrow/test-util.h
index b59809d..4e52580 100644
--- a/cpp/src/arrow/test-util.h
+++ b/cpp/src/arrow/test-util.h
@@ -294,8 +294,8 @@ class TestBuilder : public ::testing::Test {
   void SetUp() {
     pool_ = default_memory_pool();
     type_ = TypePtr(new UInt8Type());
-    builder_.reset(new UInt8Builder(pool_, type_));
-    builder_nn_.reset(new UInt8Builder(pool_, type_));
+    builder_.reset(new UInt8Builder(pool_));
+    builder_nn_.reset(new UInt8Builder(pool_));
   }
 
  protected:

http://git-wip-us.apache.org/repos/asf/arrow/blob/84f16624/cpp/src/arrow/type.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/type.h b/cpp/src/arrow/type.h
index 77a70d1..8638a3f 100644
--- a/cpp/src/arrow/type.h
+++ b/cpp/src/arrow/type.h
@@ -540,26 +540,9 @@ class ARROW_EXPORT DictionaryType : public FixedWidthType {
 // ----------------------------------------------------------------------
 // Factory functions
 
-std::shared_ptr<DataType> ARROW_EXPORT null();
-std::shared_ptr<DataType> ARROW_EXPORT boolean();
-std::shared_ptr<DataType> ARROW_EXPORT int8();
-std::shared_ptr<DataType> ARROW_EXPORT int16();
-std::shared_ptr<DataType> ARROW_EXPORT int32();
-std::shared_ptr<DataType> ARROW_EXPORT int64();
-std::shared_ptr<DataType> ARROW_EXPORT uint8();
-std::shared_ptr<DataType> ARROW_EXPORT uint16();
-std::shared_ptr<DataType> ARROW_EXPORT uint32();
-std::shared_ptr<DataType> ARROW_EXPORT uint64();
-std::shared_ptr<DataType> ARROW_EXPORT float16();
-std::shared_ptr<DataType> ARROW_EXPORT float32();
-std::shared_ptr<DataType> ARROW_EXPORT float64();
-std::shared_ptr<DataType> ARROW_EXPORT utf8();
-std::shared_ptr<DataType> ARROW_EXPORT binary();
-
 std::shared_ptr<DataType> ARROW_EXPORT list(const std::shared_ptr<Field>& value_type);
 std::shared_ptr<DataType> ARROW_EXPORT list(const std::shared_ptr<DataType>& value_type);
 
-std::shared_ptr<DataType> ARROW_EXPORT date();
 std::shared_ptr<DataType> ARROW_EXPORT timestamp(TimeUnit unit);
 std::shared_ptr<DataType> ARROW_EXPORT time(TimeUnit unit);
 

http://git-wip-us.apache.org/repos/asf/arrow/blob/84f16624/cpp/src/arrow/type_fwd.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/type_fwd.h b/cpp/src/arrow/type_fwd.h
index 334abef..fc4ad3d 100644
--- a/cpp/src/arrow/type_fwd.h
+++ b/cpp/src/arrow/type_fwd.h
@@ -18,6 +18,8 @@
 #ifndef ARROW_TYPE_FWD_H
 #define ARROW_TYPE_FWD_H
 
+#include "arrow/util/visibility.h"
+
 namespace arrow {
 
 class Status;
@@ -104,6 +106,26 @@ using TimestampBuilder = NumericBuilder<TimestampType>;
 struct IntervalType;
 using IntervalArray = NumericArray<IntervalType>;
 
+// ----------------------------------------------------------------------
+// (parameter-free) Factory functions
+
+std::shared_ptr<DataType> ARROW_EXPORT null();
+std::shared_ptr<DataType> ARROW_EXPORT boolean();
+std::shared_ptr<DataType> ARROW_EXPORT int8();
+std::shared_ptr<DataType> ARROW_EXPORT int16();
+std::shared_ptr<DataType> ARROW_EXPORT int32();
+std::shared_ptr<DataType> ARROW_EXPORT int64();
+std::shared_ptr<DataType> ARROW_EXPORT uint8();
+std::shared_ptr<DataType> ARROW_EXPORT uint16();
+std::shared_ptr<DataType> ARROW_EXPORT uint32();
+std::shared_ptr<DataType> ARROW_EXPORT uint64();
+std::shared_ptr<DataType> ARROW_EXPORT float16();
+std::shared_ptr<DataType> ARROW_EXPORT float32();
+std::shared_ptr<DataType> ARROW_EXPORT float64();
+std::shared_ptr<DataType> ARROW_EXPORT utf8();
+std::shared_ptr<DataType> ARROW_EXPORT binary();
+std::shared_ptr<DataType> ARROW_EXPORT date();
+
 }  // namespace arrow
 
 #endif  // ARROW_TYPE_FWD_H

http://git-wip-us.apache.org/repos/asf/arrow/blob/84f16624/cpp/src/arrow/type_traits.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h
index 5616018..5cd5f45 100644
--- a/cpp/src/arrow/type_traits.h
+++ b/cpp/src/arrow/type_traits.h
@@ -33,6 +33,8 @@ struct TypeTraits<UInt8Type> {
   using ArrayType = UInt8Array;
   using BuilderType = UInt8Builder;
   static inline int bytes_required(int elements) { return elements; }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return uint8(); }
 };
 
 template <>
@@ -40,6 +42,8 @@ struct TypeTraits<Int8Type> {
   using ArrayType = Int8Array;
   using BuilderType = Int8Builder;
   static inline int bytes_required(int elements) { return elements; }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return int8(); }
 };
 
 template <>
@@ -48,6 +52,8 @@ struct TypeTraits<UInt16Type> {
   using BuilderType = UInt16Builder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(uint16_t); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return uint16(); }
 };
 
 template <>
@@ -56,6 +62,8 @@ struct TypeTraits<Int16Type> {
   using BuilderType = Int16Builder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(int16_t); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return int16(); }
 };
 
 template <>
@@ -64,6 +72,8 @@ struct TypeTraits<UInt32Type> {
   using BuilderType = UInt32Builder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(uint32_t); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return uint32(); }
 };
 
 template <>
@@ -72,6 +82,8 @@ struct TypeTraits<Int32Type> {
   using BuilderType = Int32Builder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(int32_t); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return int32(); }
 };
 
 template <>
@@ -80,6 +92,8 @@ struct TypeTraits<UInt64Type> {
   using BuilderType = UInt64Builder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(uint64_t); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return uint64(); }
 };
 
 template <>
@@ -88,6 +102,8 @@ struct TypeTraits<Int64Type> {
   using BuilderType = Int64Builder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(int64_t); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return int64(); }
 };
 
 template <>
@@ -96,6 +112,8 @@ struct TypeTraits<DateType> {
   // using BuilderType = DateBuilder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(int64_t); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return date(); }
 };
 
 template <>
@@ -104,6 +122,7 @@ struct TypeTraits<TimestampType> {
   // using BuilderType = TimestampBuilder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(int64_t); }
+  constexpr static bool is_parameter_free = false;
 };
 
 template <>
@@ -112,6 +131,8 @@ struct TypeTraits<HalfFloatType> {
   using BuilderType = HalfFloatBuilder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(uint16_t); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return float16(); }
 };
 
 template <>
@@ -120,6 +141,8 @@ struct TypeTraits<FloatType> {
   using BuilderType = FloatBuilder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(float); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return float32(); }
 };
 
 template <>
@@ -128,6 +151,8 @@ struct TypeTraits<DoubleType> {
   using BuilderType = DoubleBuilder;
 
   static inline int bytes_required(int elements) { return elements * sizeof(double); }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return float64(); }
 };
 
 template <>
@@ -138,18 +163,24 @@ struct TypeTraits<BooleanType> {
   static inline int bytes_required(int elements) {
     return BitUtil::BytesForBits(elements);
   }
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return boolean(); }
 };
 
 template <>
 struct TypeTraits<StringType> {
   using ArrayType = StringArray;
   using BuilderType = StringBuilder;
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return utf8(); }
 };
 
 template <>
 struct TypeTraits<BinaryType> {
   using ArrayType = BinaryArray;
   using BuilderType = BinaryBuilder;
+  constexpr static bool is_parameter_free = true;
+  static inline std::shared_ptr<DataType> type_singleton() { return binary(); }
 };
 
 // Not all type classes have a c_type