数据类型#
参见
数据类型控制着物理数据如何被解释。它们的 规范 允许不同的 Arrow 实现之间进行二进制互操作,包括来自不同编程语言和运行时(例如,可以使用 pyarrow.jvm
桥接模块从 Python 和 Java 访问相同的数据,而无需复制)。
关于 C++ 中数据类型的信息可以用三种方式表示
使用
arrow::DataType
实例(例如,作为函数参数)使用
arrow::DataType
具体子类(例如,作为模板参数)使用
arrow::Type::type
枚举值(例如,作为 switch 语句的条件)
第一种形式(使用 arrow::DataType
实例)是最惯用的和灵活的。运行时参数化类型只能用 DataType 实例完全表示。例如,arrow::TimestampType
需要在运行时使用 arrow::TimeUnit::type
参数构造;arrow::Decimal128Type
带有 scale 和 precision 参数;arrow::ListType
带有完整的子类型(本身是一个 arrow::DataType
实例)。
另两种形式可以在性能至关重要的情况下使用,以避免支付动态类型和多态性的代价。但是,对于参数化类型,仍然可能需要一定量的运行时切换。在编译时不可能具体化所有可能的类型,因为 Arrow 数据类型允许任意嵌套。
创建数据类型#
要实例化数据类型,建议调用提供的 工厂函数
std::shared_ptr<arrow::DataType> type;
// A 16-bit integer type
type = arrow::int16();
// A 64-bit timestamp type (with microsecond granularity)
type = arrow::timestamp(arrow::TimeUnit::MICRO);
// A list type of single-precision floating-point values
type = arrow::list(arrow::float32());
类型特征#
如果不使用类型特征,编写可以处理具体 arrow::DataType
子类的代码将非常冗长。Arrow 的类型特征将 Arrow 数据类型映射到专门的数组、标量、构建器和其他关联的类型。例如,Boolean 类型具有以下特征
template <>
struct TypeTraits<BooleanType> {
using ArrayType = BooleanArray;
using BuilderType = BooleanBuilder;
using ScalarType = BooleanScalar;
using CType = bool;
static constexpr int64_t bytes_required(int64_t elements) {
return bit_util::BytesForBits(elements);
}
constexpr static bool is_parameter_free = true;
static inline std::shared_ptr<DataType> type_singleton() { return boolean(); }
};
有关这些字段的解释,请参见 类型特征。
使用类型特征,可以编写可以处理各种 Arrow 类型的模板函数。例如,要编写一个为任何 Arrow 数值类型创建斐波那契值数组的函数
template <typename DataType,
typename BuilderType = typename arrow::TypeTraits<DataType>::BuilderType,
typename ArrayType = typename arrow::TypeTraits<DataType>::ArrayType,
typename CType = typename arrow::TypeTraits<DataType>::CType>
arrow::Result<std::shared_ptr<ArrayType>> MakeFibonacci(int32_t n) {
BuilderType builder;
CType val = 0;
CType next_val = 1;
for (int32_t i = 0; i < n; ++i) {
builder.Append(val);
CType temp = val + next_val;
val = next_val;
next_val = temp;
}
std::shared_ptr<ArrayType> out;
ARROW_RETURN_NOT_OK(builder.Finish(&out));
return out;
}
对于某些常见情况,类本身具有类型关联。使用
Scalar::TypeClass
获取标量的数据类型类Array::TypeClass
获取数组的数据类型类DataType::c_type
获取 Arrow 数据类型的关联 C 类型
与 std::type_traits 中提供的类型特征类似,Arrow 提供了类型谓词,例如 is_number_type
以及包装 std::enable_if_t
的相应模板,例如 enable_if_number
。这些可以约束模板函数仅为相关类型编译,这在需要实现其他重载时很有用。例如,要编写一个用于任何数值(整数或浮点数)数组的求和函数
template <typename ArrayType, typename DataType = typename ArrayType::TypeClass,
typename CType = typename DataType::c_type>
arrow::enable_if_number<DataType, CType> SumArray(const ArrayType& array) {
CType sum = 0;
for (std::optional<CType> value : array) {
if (value.has_value()) {
sum += value.value();
}
}
return sum;
}
有关这些的列表,请参见 类型谓词。
访问者模式#
为了处理 arrow::DataType
、arrow::Scalar
或 arrow::Array
,您可能需要编写基于特定 Arrow 类型进行专门化的逻辑。在这些情况下,请使用 访问者模式。Arrow 提供了模板函数
要使用这些函数,请为每个专门的类型实现 Status Visit()
方法,然后将类实例传递给内联访问函数。为了避免重复的代码,请使用上一节中记录的类型特征。作为一个简短的例子,下面是如何在任意数值类型的列中求和
class TableSummation {
double partial = 0.0;
public:
arrow::Result<double> Compute(std::shared_ptr<arrow::RecordBatch> batch) {
for (std::shared_ptr<arrow::Array> array : batch->columns()) {
ARROW_RETURN_NOT_OK(arrow::VisitArrayInline(*array, this));
}
return partial;
}
// Default implementation
arrow::Status Visit(const arrow::Array& array) {
return arrow::Status::NotImplemented("Cannot compute sum for array of type ",
array.type()->ToString());
}
template <typename ArrayType, typename T = typename ArrayType::TypeClass>
arrow::enable_if_number<T, arrow::Status> Visit(const ArrayType& array) {
for (std::optional<typename T::c_type> value : array) {
if (value.has_value()) {
partial += static_cast<double>(value.value());
}
}
return arrow::Status::OK();
}
};
Arrow 还提供了抽象访问者类(arrow::TypeVisitor
、arrow::ScalarVisitor
、arrow::ArrayVisitor
)以及每个相应基类型上的 Accept()
方法(例如,arrow::Array::Accept()
)。但是,这些无法使用模板函数实现,因此您通常更喜欢使用内联类型访问器。