emkornfield commented on a change in pull request #11302: URL: https://github.com/apache/arrow/pull/11302#discussion_r721891998
########## File path: cpp/src/arrow/python/python_to_arrow.cc ########## @@ -68,6 +69,95 @@ using internal::MakeConverter; namespace py { +enum class MonthDayNanoField { kMonths, kWeeksAndDays, kDaysOnly, kNanoseconds }; + +template <MonthDayNanoField field> +struct MonthDayNanoTraits; + +struct MonthDayNanoAttrData { + const char* name; + const int64_t multiplier; +}; + +template <> +struct MonthDayNanoTraits<MonthDayNanoField::kMonths> { + using c_type = int32_t; + static constexpr char name[] = "months"; + static const MonthDayNanoAttrData attrs[]; +}; + +const MonthDayNanoAttrData MonthDayNanoTraits<MonthDayNanoField::kMonths>::attrs[] = { + {"years", 1}, {"months", /*months_in_year=*/12}, {nullptr, 0}}; + +template <> +struct MonthDayNanoTraits<MonthDayNanoField::kWeeksAndDays> { + using c_type = int32_t; + static constexpr char name[] = "days"; + static const MonthDayNanoAttrData attrs[]; +}; + +const MonthDayNanoAttrData MonthDayNanoTraits<MonthDayNanoField::kWeeksAndDays>::attrs[] = + {{"weeks", 1}, {"days", /*days_in_week=*/7}, {nullptr, 0}}; + +template <> +struct MonthDayNanoTraits<MonthDayNanoField::kDaysOnly> { + using c_type = int32_t; + static constexpr char name[] = "days"; + static const MonthDayNanoAttrData attrs[]; +}; + +const MonthDayNanoAttrData MonthDayNanoTraits<MonthDayNanoField::kDaysOnly>::attrs[] = { + {"days", 1}, {nullptr, 0}}; + +template <> +struct MonthDayNanoTraits<MonthDayNanoField::kNanoseconds> { + using c_type = int64_t; + static constexpr char name[] = "nanoseconds"; + static const MonthDayNanoAttrData attrs[]; +}; + +const MonthDayNanoAttrData MonthDayNanoTraits<MonthDayNanoField::kNanoseconds>::attrs[] = + {{"hours", 1}, + {"minutes", /*minutes_in_hours=*/60}, + {"seconds", /*seconds_in_minute=*/60}, + {"milliseconds", /*milliseconds_in_seconds*/ 1000}, + {"microseconds", /*microseconds_in_millseconds=*/1000}, + {"nanoseconds", /*nanoseconds_in_microseconds=*/1000}, + {nullptr, 0}}; + +template <MonthDayNanoField field> +struct PopulateMonthDayNano { + using Traits = MonthDayNanoTraits<field>; + inline static Status Field(PyObject* obj, + typename MonthDayNanoTraits<field>::c_type* out, + bool* found_attrs) { + *out = 0; + for (const MonthDayNanoAttrData* attr = &Traits::attrs[0]; attr->multiplier != 0; + ++attr) { + if (attr->multiplier != 1 && + ::arrow::internal::MultiplyWithOverflow( + static_cast<typename Traits::c_type>(attr->multiplier), *out, out)) { + return Status::Invalid("Overflow on: ", (attr - 1)->name); + } + if (PyObject_HasAttrString(obj, attr->name)) { + OwnedRef field_value(PyObject_GetAttrString(obj, attr->name)); Review comment: Good point. Done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org