You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++中获取Arrow数组的TimeUnit?

如何在C++中获取Arrow数组的TimeUnit?

嘿,看起来你这里混淆了Arrow里Date类型和Time类型的API差异啦!

首先得明确:你当前操作的arrow::Date64Array对应的是arrow::Date64Type,这个类型并没有TimeUnit相关的成员——因为在Arrow的定义里,Date64就是固定以毫秒为单位存储的(记录自Unix纪元以来的毫秒数),所以它的API里不会提供unit()或者Unit这样的接口。你在type.h里看到的unit()方法,其实是给时间类型(比如Time32Type、Time64Type)准备的,只有这些类型才会有不同的时间粒度选项(秒、毫秒、微秒、纳秒)。

如果你的实际需求是处理时间类型的数组(比如Time64Array),正确的获取方式应该是这样的:

// 假设输入数组是Time64类型
auto time_array = static_cast<arrow::Time64Array*>(array);
// 将类型对象转为对应的Time64Type指针
const auto* time_type = static_cast<const arrow::Time64Type*>(time_array->type());
// 调用unit()获取时间单位
arrow::TimeUnit::type unit = time_type->unit();

而如果你确实是在处理Date64Array,那其实不需要额外获取TimeUnit,因为它的单位是固定的毫秒,这是Arrow类型定义里的既定规则。

另外,为了避免强制转换出错,建议在转换前先做类型检查,比如:

if (array->type()->id() == arrow::Type::TIME64) {
    auto time_array = static_cast<arrow::Time64Array*>(array);
    const auto* time_type = static_cast<const arrow::Time64Type*>(time_array->type());
    arrow::TimeUnit::type unit = time_type->unit();
    // 这里写你的后续处理逻辑
} else if (array->type()->id() == arrow::Type::DATE64) {
    // Date64固定使用毫秒单位
    std::cout << "Date64数组默认以毫秒为单位存储" << std::endl;
}

这样能有效避免因为数组类型不匹配导致的运行时错误哦。

备注:内容来源于stack exchange,提问作者Ochetski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 10:33:03