Skip to content

Commit

Permalink
Move some bits from arrow/type.h to type.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Mar 7, 2016
1 parent 3a774fb commit 07c1379
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
51 changes: 38 additions & 13 deletions cpp/src/arrow/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,30 @@ std::string Field::ToString() const {
return ss.str();
}

const std::shared_ptr<NullType> NA = std::make_shared<NullType>();
const std::shared_ptr<BooleanType> BOOL = std::make_shared<BooleanType>();
const std::shared_ptr<UInt8Type> UINT8 = std::make_shared<UInt8Type>();
const std::shared_ptr<UInt16Type> UINT16 = std::make_shared<UInt16Type>();
const std::shared_ptr<UInt32Type> UINT32 = std::make_shared<UInt32Type>();
const std::shared_ptr<UInt64Type> UINT64 = std::make_shared<UInt64Type>();
const std::shared_ptr<Int8Type> INT8 = std::make_shared<Int8Type>();
const std::shared_ptr<Int16Type> INT16 = std::make_shared<Int16Type>();
const std::shared_ptr<Int32Type> INT32 = std::make_shared<Int32Type>();
const std::shared_ptr<Int64Type> INT64 = std::make_shared<Int64Type>();
const std::shared_ptr<FloatType> FLOAT = std::make_shared<FloatType>();
const std::shared_ptr<DoubleType> DOUBLE = std::make_shared<DoubleType>();
const std::shared_ptr<StringType> STRING = std::make_shared<StringType>();
DataType::~DataType() {}

template <typename Derived>
inline std::string PrimitiveType<Derived>::ToString() const {
std::string result(static_cast<const Derived*>(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;
Expand All @@ -64,4 +75,18 @@ std::string StructType::ToString() const {
return s.str();
}

const std::shared_ptr<NullType> NA = std::make_shared<NullType>();
const std::shared_ptr<BooleanType> BOOL = std::make_shared<BooleanType>();
const std::shared_ptr<UInt8Type> UINT8 = std::make_shared<UInt8Type>();
const std::shared_ptr<UInt16Type> UINT16 = std::make_shared<UInt16Type>();
const std::shared_ptr<UInt32Type> UINT32 = std::make_shared<UInt32Type>();
const std::shared_ptr<UInt64Type> UINT64 = std::make_shared<UInt64Type>();
const std::shared_ptr<Int8Type> INT8 = std::make_shared<Int8Type>();
const std::shared_ptr<Int16Type> INT16 = std::make_shared<Int16Type>();
const std::shared_ptr<Int32Type> INT32 = std::make_shared<Int32Type>();
const std::shared_ptr<Int64Type> INT64 = std::make_shared<Int64Type>();
const std::shared_ptr<FloatType> FLOAT = std::make_shared<FloatType>();
const std::shared_ptr<DoubleType> DOUBLE = std::make_shared<DoubleType>();
const std::shared_ptr<StringType> STRING = std::make_shared<StringType>();

} // namespace arrow
29 changes: 7 additions & 22 deletions cpp/src/arrow/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<const Derived*>(this)->name());
if (!nullable) {
result.append(" not null");
}
return result;
}
std::string ToString() const override;
};

#define PRIMITIVE_DECL(TYPENAME, C_TYPE, ENUM, SIZE, NAME) \
Expand Down Expand Up @@ -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 {
Expand All @@ -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<NullType> NA;
Expand Down
8 changes: 4 additions & 4 deletions python/arrow/schema.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 07c1379

Please sign in to comment.