Skip to content

ARROW-835: [Format][C++][Java] Create a new Duration type #3644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cpp/src/arrow/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,26 @@ const uint8_t* FixedSizeBinaryArray::GetValue(int64_t i) const {
return raw_values_ + (i + data_->offset) * byte_width_;
}

// ----------------------------------------------------------------------
// Day time interval

DayTimeIntervalArray::DayTimeIntervalArray(const std::shared_ptr<ArrayData>& data) {
SetData(data);
}

DayTimeIntervalArray::DayTimeIntervalArray(const std::shared_ptr<DataType>& type,
int64_t length,
const std::shared_ptr<Buffer>& data,
const std::shared_ptr<Buffer>& null_bitmap,
int64_t null_count, int64_t offset)
: PrimitiveArray(type, length, data, null_bitmap, null_count, offset) {}

DayTimeIntervalType::DayMilliseconds DayTimeIntervalArray::GetValue(int64_t i) const {
DCHECK(i < length());
return *reinterpret_cast<const DayTimeIntervalType::DayMilliseconds*>(
raw_values_ + (i + data_->offset) * byte_width());
}

// ----------------------------------------------------------------------
// Decimal

Expand Down
30 changes: 30 additions & 0 deletions cpp/src/arrow/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,36 @@ class ARROW_EXPORT FixedSizeBinaryArray : public PrimitiveArray {
int32_t byte_width_;
};

/// DayTimeArray
/// ---------------------
/// \brief Array of Day and Millisecond values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I'm a bit disappointed that we aren't deprecating this type =/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to get dragged into what it means deprecate this, should be easy enough to do if we want it. As noted below I tried to say it was optional which I seem to recall seeing this was ok on a previous ML thread.

class ARROW_EXPORT DayTimeIntervalArray : public PrimitiveArray {
public:
using TypeClass = DayTimeIntervalType;

explicit DayTimeIntervalArray(const std::shared_ptr<ArrayData>& data);

DayTimeIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
const std::shared_ptr<Buffer>& data,
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
int64_t null_count = kUnknownNullCount, int64_t offset = 0);

TypeClass::DayMilliseconds GetValue(int64_t i) const;
TypeClass::DayMilliseconds Value(int64_t i) const { return GetValue(i); }

// For compability with Take kernel.
TypeClass::DayMilliseconds GetView(int64_t i) const { return GetValue(i); }

int32_t byte_width() const { return sizeof(TypeClass::DayMilliseconds); }

const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }

protected:
inline void SetData(const std::shared_ptr<ArrayData>& data) {
this->PrimitiveArray::SetData(data);
}
};

// ----------------------------------------------------------------------
// Decimal128Array

Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/array/builder_dict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ struct UnifyDictionaryValues {
" dictionaries is not implemented");
}

Status Visit(const DayTimeIntervalType&, void* = nullptr) {
return Status::NotImplemented(
"Unification of DayTime"
" dictionaries is not implemented");
}

template <typename T>
Status Visit(const T&,
typename internal::DictionaryTraits<T>::MemoTableType* = nullptr) {
Expand Down
5 changes: 0 additions & 5 deletions cpp/src/arrow/array/builder_primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,6 @@ using Int8Builder = NumericBuilder<Int8Type>;
using Int16Builder = NumericBuilder<Int16Type>;
using Int32Builder = NumericBuilder<Int32Type>;
using Int64Builder = NumericBuilder<Int64Type>;
using TimestampBuilder = NumericBuilder<TimestampType>;
using Time32Builder = NumericBuilder<Time32Type>;
using Time64Builder = NumericBuilder<Time64Type>;
using Date32Builder = NumericBuilder<Date32Type>;
using Date64Builder = NumericBuilder<Date64Type>;

using HalfFloatBuilder = NumericBuilder<HalfFloatType>;
using FloatBuilder = NumericBuilder<FloatType>;
Expand Down
70 changes: 70 additions & 0 deletions cpp/src/arrow/array/builder_time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// Contains declarations of time related Arrow builder types.

#pragma once

#include <memory>

#include "arrow/array.h"
#include "arrow/array/builder_base.h"
#include "arrow/array/builder_binary.h"
#include "arrow/array/builder_primitive.h"
#include "arrow/buffer-builder.h"
#include "arrow/status.h"
#include "arrow/type_traits.h"
#include "arrow/util/macros.h"

namespace arrow {

class ARROW_EXPORT DayTimeIntervalBuilder : public ArrayBuilder {
public:
using DayMilliseconds = DayTimeIntervalType::DayMilliseconds;

explicit DayTimeIntervalBuilder(MemoryPool* pool ARROW_MEMORY_POOL_DEFAULT)
: DayTimeIntervalBuilder(day_time_interval(), pool) {}

DayTimeIntervalBuilder(std::shared_ptr<DataType> type,
MemoryPool* pool ARROW_MEMORY_POOL_DEFAULT)
: ArrayBuilder(type, pool),
builder_(fixed_size_binary(sizeof(DayMilliseconds)), pool) {}

void Reset() override { builder_.Reset(); }
Status Resize(int64_t capacity) override { return builder_.Resize(capacity); }
Status Append(DayMilliseconds day_millis) {
return builder_.Append(reinterpret_cast<uint8_t*>(&day_millis));
}
void UnsafeAppend(DayMilliseconds day_millis) {
builder_.UnsafeAppend(reinterpret_cast<uint8_t*>(&day_millis));
}
using ArrayBuilder::UnsafeAppendNull;
Status AppendNull() override { return builder_.AppendNull(); }
Status AppendNulls(int64_t length) override { return builder_.AppendNulls(length); }
Status FinishInternal(std::shared_ptr<ArrayData>* out) override {
auto result = builder_.FinishInternal(out);
if (*out != NULLPTR) {
(*out)->type = type();
}
return result;
}

private:
FixedSizeBinaryBuilder builder_;
};

} // namespace arrow
15 changes: 15 additions & 0 deletions cpp/src/arrow/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Status MakeBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type,
BUILDER_CASE(INT64, Int64Builder);
BUILDER_CASE(DATE32, Date32Builder);
BUILDER_CASE(DATE64, Date64Builder);
BUILDER_CASE(DURATION, DurationBuilder);
BUILDER_CASE(TIME32, Time32Builder);
BUILDER_CASE(TIME64, Time64Builder);
BUILDER_CASE(TIMESTAMP, TimestampBuilder);
Expand All @@ -111,6 +112,18 @@ Status MakeBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type,
DictionaryBuilderCase visitor = {pool, dict_type, out};
return VisitTypeInline(*dict_type.dictionary()->type(), &visitor);
}
case Type::INTERVAL: {
const auto& interval_type = internal::checked_cast<const IntervalType&>(*type);
if (interval_type.interval_type() == IntervalType::MONTHS) {
out->reset(new MonthIntervalBuilder(type, pool));
return Status::OK();
}
if (interval_type.interval_type() == IntervalType::DAY_TIME) {
out->reset(new DayTimeIntervalBuilder(pool));
return Status::OK();
}
break;
}
case Type::LIST: {
std::unique_ptr<ArrayBuilder> value_builder;
std::shared_ptr<DataType> value_type =
Expand Down Expand Up @@ -146,6 +159,8 @@ Status MakeBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type,
type->ToString());
}
}
return Status::NotImplemented("MakeBuilder: cannot construct builder for type ",
type->ToString());
}

} // namespace arrow
1 change: 1 addition & 0 deletions cpp/src/arrow/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "arrow/array/builder_dict.h" // IWYU pragma: export
#include "arrow/array/builder_nested.h" // IWYU pragma: export
#include "arrow/array/builder_primitive.h" // IWYU pragma: export
#include "arrow/array/builder_time.h" // IWYU pragma: export
#include "arrow/status.h"
#include "arrow/util/visibility.h"

Expand Down
11 changes: 10 additions & 1 deletion cpp/src/arrow/compare.cc
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,18 @@ class TypeEqualsVisitor {
return Status::OK();
}

template <typename T>
typename std::enable_if<std::is_base_of<IntervalType, T>::value, Status>::type Visit(
const T& left) {
const auto& right = checked_cast<const IntervalType&>(right_);
result_ = right.interval_type() == left.interval_type();
return Status::OK();
}

template <typename T>
typename std::enable_if<std::is_base_of<TimeType, T>::value ||
std::is_base_of<DateType, T>::value,
std::is_base_of<DateType, T>::value ||
std::is_base_of<DurationType, T>::value,
Status>::type
Visit(const T& left) {
const auto& right = checked_cast<const T&>(right_);
Expand Down
Loading