From 07c1379657912ef8a8b69cc1d5b9f5993fe35205 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Sun, 6 Mar 2016 19:43:01 -0800 Subject: [PATCH] Move some bits from arrow/type.h to type.cc --- cpp/src/arrow/type.cc | 51 ++++++++++++++++++++++++++++++----------- cpp/src/arrow/type.h | 29 ++++++----------------- python/arrow/schema.pyx | 8 +++---- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/cpp/src/arrow/type.cc b/cpp/src/arrow/type.cc index 7c0210e2498b3..cfed238dc416e 100644 --- a/cpp/src/arrow/type.cc +++ b/cpp/src/arrow/type.cc @@ -28,19 +28,30 @@ std::string Field::ToString() const { return ss.str(); } -const std::shared_ptr NA = std::make_shared(); -const std::shared_ptr BOOL = std::make_shared(); -const std::shared_ptr UINT8 = std::make_shared(); -const std::shared_ptr UINT16 = std::make_shared(); -const std::shared_ptr UINT32 = std::make_shared(); -const std::shared_ptr UINT64 = std::make_shared(); -const std::shared_ptr INT8 = std::make_shared(); -const std::shared_ptr INT16 = std::make_shared(); -const std::shared_ptr INT32 = std::make_shared(); -const std::shared_ptr INT64 = std::make_shared(); -const std::shared_ptr FLOAT = std::make_shared(); -const std::shared_ptr DOUBLE = std::make_shared(); -const std::shared_ptr STRING = std::make_shared(); +DataType::~DataType() {} + +template +inline std::string PrimitiveType::ToString() const { + std::string result(static_cast(this)->name()); + if (!nullable) { + result.append(" not null"); + } + return result; +} + +StringType::StringType(bool nullable) + : DataType(LogicalType::STRING, nullable) {} + +StringType::StringType(const StringType& other) + : StringType(other.nullable) {} + +std::string StringType::ToString() const { + std::string result(name()); + if (!nullable) { + result.append(" not null"); + } + return result; +} std::string ListType::ToString() const { std::stringstream s; @@ -64,4 +75,18 @@ std::string StructType::ToString() const { return s.str(); } +const std::shared_ptr NA = std::make_shared(); +const std::shared_ptr BOOL = std::make_shared(); +const std::shared_ptr UINT8 = std::make_shared(); +const std::shared_ptr UINT16 = std::make_shared(); +const std::shared_ptr UINT32 = std::make_shared(); +const std::shared_ptr UINT64 = std::make_shared(); +const std::shared_ptr INT8 = std::make_shared(); +const std::shared_ptr INT16 = std::make_shared(); +const std::shared_ptr INT32 = std::make_shared(); +const std::shared_ptr INT64 = std::make_shared(); +const std::shared_ptr FLOAT = std::make_shared(); +const std::shared_ptr DOUBLE = std::make_shared(); +const std::shared_ptr STRING = std::make_shared(); + } // namespace arrow diff --git a/cpp/src/arrow/type.h b/cpp/src/arrow/type.h index 2890e02cd8948..e078e2e656ba3 100644 --- a/cpp/src/arrow/type.h +++ b/cpp/src/arrow/type.h @@ -139,7 +139,7 @@ struct DataType { type(type), nullable(nullable) {} - virtual ~DataType() {} + virtual ~DataType(); bool Equals(const DataType* other) { // Call with a pointer so more friendly to subclasses @@ -218,13 +218,7 @@ struct PrimitiveType : public DataType { explicit PrimitiveType(bool nullable = true) : DataType(Derived::type_enum, nullable) {} - virtual std::string ToString() const { - std::string result(static_cast(this)->name()); - if (!nullable) { - result.append(" not null"); - } - return result; - } + std::string ToString() const override; }; #define PRIMITIVE_DECL(TYPENAME, C_TYPE, ENUM, SIZE, NAME) \ @@ -294,34 +288,25 @@ struct ListType : public DataType { explicit ListType(const TypePtr& value_type, bool nullable = true) : DataType(LogicalType::LIST, nullable), value_type(value_type) {} - virtual ~ListType() {} static char const *name() { return "list"; } - virtual std::string ToString() const; + std::string ToString() const override; }; // String is a logical type consisting of a physical list of 1-byte values struct StringType : public DataType { - explicit StringType(bool nullable = true) - : DataType(LogicalType::STRING, nullable) {} + explicit StringType(bool nullable = true); - StringType(const StringType& other) - : StringType() {} + StringType(const StringType& other); static char const *name() { return "string"; } - virtual std::string ToString() const { - std::string result(name()); - if (!nullable) { - result.append(" not null"); - } - return result; - } + std::string ToString() const override; }; struct StructType : public DataType { @@ -341,7 +326,7 @@ struct StructType : public DataType { return fields_.size(); } - virtual std::string ToString() const; + std::string ToString() const override; }; extern const std::shared_ptr NA; diff --git a/python/arrow/schema.pyx b/python/arrow/schema.pyx index c5bb61a3e6f46..04e233a9e7bee 100644 --- a/python/arrow/schema.pyx +++ b/python/arrow/schema.pyx @@ -35,12 +35,12 @@ cdef class DataType: self.sp_type = type self.type = type.get() - def __repr__(self): - return 'DataType({0})'.format(self._type_repr()) - - def _type_repr(self): + def __str__(self): return frombytes(self.type.ToString()) + def __repr__(self): + return 'DataType({0})'.format(str(self)) + cdef class Field: def __cinit__(self, object name, DataType type):