Skip to content
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

feat(cpp-client): Additional Cython support for LocalDate and LocalTime #6105

Merged
merged 1 commit into from
Sep 22, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,6 @@ class GenericArrayColumnSource final : public deephaven::dhcore::column::Mutable
using BooleanArrayColumnSource = GenericArrayColumnSource<bool>;
using StringArrayColumnSource = GenericArrayColumnSource<std::string>;
using DateTimeArrayColumnSource = GenericArrayColumnSource<DateTime>;
using LocalDateArrayColumnSource = GenericArrayColumnSource<LocalDate>;
using LocalTimeArrayColumnSource = GenericArrayColumnSource<LocalTime>;
} // namespace deephaven::dhcore::column
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ struct DeephavenTraits<LocalTime> {
*/
class DateTime {
public:
using rep_t = int64_t;

/**
* This method exists to document and enforce an assumption in Cython, namely that this
* class has the same representation as an int64_t. This constexpr method always returns
* true (or fails to compile).
*/
static constexpr bool IsBlittableToInt64() {
static_assert(
std::is_trivially_copyable_v<DateTime> &&
std::has_unique_object_representations_v<DateTime> &&
std::is_same_v<rep_t, std::int64_t>);
return true;
}

/**
* Converts nanoseconds-since-UTC-epoch to DateTime. The Deephaven null value sentinel is
* turned into DateTime(0).
Expand Down Expand Up @@ -431,6 +446,21 @@ class DateTime {
*/
class LocalDate {
public:
using rep_t = int64_t;

/**
* This method exists to document and enforce an assumption in Cython, namely that this
* class has the same representation as an int64_t. This constexpr method always returns
* true (or fails to compile).
*/
static constexpr bool IsBlittableToInt64() {
static_assert(
std::is_trivially_copyable_v<LocalDate> &&
std::has_unique_object_representations_v<LocalDate> &&
std::is_same_v<rep_t, std::int64_t>);
return true;
}

/**
* Creates an instance of LocalDate from the specified year, month, and day.
*/
Expand Down Expand Up @@ -488,6 +518,21 @@ class LocalDate {
*/
class LocalTime {
public:
using rep_t = int64_t;

/**
* This method exists to document and enforce an assumption in Cython, namely that this
* class has the same representation as an int64_t. This constexpr method always returns
* true (or fails to compile).
*/
static constexpr bool IsBlittableToInt64() {
static_assert(
std::is_trivially_copyable_v<LocalTime> &&
std::has_unique_object_representations_v<LocalTime> &&
std::is_same_v<rep_t, std::int64_t>);
return true;
}

/**
* Creates an instance of LocalTime from the specified hour, minute, and second.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class CythonSupport {
const uint8_t *validity_begin, const uint8_t *validity_end, size_t num_elements);
static std::shared_ptr<ColumnSource> CreateDateTimeColumnSource(const int64_t *data_begin, const int64_t *data_end,
const uint8_t *validity_begin, const uint8_t *validity_end, size_t num_elements);
static std::shared_ptr<ColumnSource> CreateLocalDateColumnSource(const int64_t *data_begin, const int64_t *data_end,
const uint8_t *validity_begin, const uint8_t *validity_end, size_t num_elements);
static std::shared_ptr<ColumnSource> CreateLocalTimeColumnSource(const int64_t *data_begin, const int64_t *data_end,
const uint8_t *validity_begin, const uint8_t *validity_end, size_t num_elements);

static ElementTypeId::Enum GetElementTypeId(const ColumnSource &column_source);
};
Expand Down
30 changes: 30 additions & 0 deletions cpp-client/deephaven/dhcore/src/utility/cython_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ using deephaven::dhcore::column::BooleanArrayColumnSource;
using deephaven::dhcore::column::ColumnSource;
using deephaven::dhcore::column::ColumnSourceVisitor;
using deephaven::dhcore::column::DateTimeArrayColumnSource;
using deephaven::dhcore::column::LocalDateArrayColumnSource;
using deephaven::dhcore::column::LocalTimeArrayColumnSource;
using deephaven::dhcore::column::StringArrayColumnSource;

namespace deephaven::dhcore::utility {
Expand Down Expand Up @@ -66,6 +68,34 @@ CythonSupport::CreateDateTimeColumnSource(const int64_t *data_begin, const int64
num_elements);
}

std::shared_ptr<ColumnSource>
CythonSupport::CreateLocalDateColumnSource(const int64_t *data_begin, const int64_t *data_end,
const uint8_t *validity_begin, const uint8_t *validity_end, size_t num_elements) {
auto elements = std::make_unique<LocalDate[]>(num_elements);
auto nulls = std::make_unique<bool[]>(num_elements);

for (size_t i = 0; i != num_elements; ++i) {
elements[i] = LocalDate(data_begin[i]);
}
populateArrayFromPackedData(validity_begin, nulls.get(), num_elements, true);
return LocalDateArrayColumnSource::CreateFromArrays(std::move(elements), std::move(nulls),
num_elements);
}

std::shared_ptr<ColumnSource>
CythonSupport::CreateLocalTimeColumnSource(const int64_t *data_begin, const int64_t *data_end,
const uint8_t *validity_begin, const uint8_t *validity_end, size_t num_elements) {
auto elements = std::make_unique<LocalTime[]>(num_elements);
auto nulls = std::make_unique<bool[]>(num_elements);

for (size_t i = 0; i != num_elements; ++i) {
elements[i] = LocalTime(data_begin[i]);
}
populateArrayFromPackedData(validity_begin, nulls.get(), num_elements, true);
return LocalTimeArrayColumnSource::CreateFromArrays(std::move(elements), std::move(nulls),
num_elements);
}

namespace {
struct ElementTypeIdVisitor final : ColumnSourceVisitor {
void Visit(const column::CharColumnSource &/*source*/) final {
Expand Down
Loading