Skip to content
Open
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
72 changes: 28 additions & 44 deletions cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "arrow/compute/api.h"
#include "arrow/type.h"
#include "arrow/type_fwd.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"

#include "arrow/flight/sql/odbc/odbc_impl/json_converter.h"

Expand Down Expand Up @@ -906,6 +908,29 @@ std::shared_ptr<Array> CheckConversion(const arrow::Result<Datum>& result) {
}
}

namespace {
// Converts a time32/time64 array to a timestamp array of the same unit, holding
// today's date plus the time of day.
std::shared_ptr<Array> ConvertTimeToTodayTimestamp(
const std::shared_ptr<Array>& original_array) {
const auto unit =
arrow::internal::checked_cast<const TimeType&>(*original_array->type()).unit();

std::shared_ptr<Array> as_int = original_array;
if (original_array->type_id() == Type::TIME32) {
as_int = CheckConversion(arrow::compute::Cast(as_int, arrow::int32()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(We can't directly cast to int64 here?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately not — there's no time32 -> int64 cast kernel. cast_int64 only registers zero-copy casts from date64, duration, timestamp and time64, while time32 is registered only on cast_int32 (see scalar_cast_numeric.cc), so a direct cast fails with a "not implemented" error. Going through int32 first is zero-copy, so the extra step only costs the widening to int64 that we need anyway for the add. The previous code did the same two-step cast for this reason. I can add a short comment there if you think it helps.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

yea I think adding a short comment here will help.

}
as_int = CheckConversion(arrow::compute::Cast(as_int, arrow::int64()));

const int64_t units_per_second =
arrow::util::GetTimestampConversion(TimeUnit::SECOND, unit).second;
auto shifted = CheckConversion(arrow::compute::Add(
as_int, std::make_shared<Int64Scalar>(GetTodayTimeFromEpoch() * units_per_second)));

return CheckConversion(arrow::compute::Cast(shifted, arrow::timestamp(unit)));
}
} // namespace

ArrayConvertTask GetConverter(Type::type original_type_id, CDataType target_type) {
// The else statement has a convert the works for the most case of array
// conversion. In case, we find conversion that the default one can't handle
Expand All @@ -923,50 +948,9 @@ ArrayConvertTask GetConverter(Type::type original_type_id, CDataType target_type
return CheckConversion(
arrow::compute::CallFunction("cast", {first_converted_array}, &cast_options));
};
} else if (original_type_id == Type::TIME32 && target_type == CDataType_TIMESTAMP) {
return [=](const std::shared_ptr<Array>& original_array) {
arrow::compute::CastOptions cast_options;
cast_options.to_type = arrow::int32();

auto first_converted_array =
CheckConversion(arrow::compute::Cast(original_array, cast_options));

cast_options.to_type = arrow::int64();

auto second_converted_array =
CheckConversion(arrow::compute::Cast(first_converted_array, cast_options));

auto seconds_from_epoch = GetTodayTimeFromEpoch();

auto third_converted_array = CheckConversion(
arrow::compute::Add(second_converted_array,
std::make_shared<Int64Scalar>(seconds_from_epoch * 1000)));

arrow::compute::CastOptions cast_options_2;
cast_options_2.to_type = arrow::timestamp(TimeUnit::MILLI);

return CheckConversion(arrow::compute::Cast(third_converted_array, cast_options_2));
};
} else if (original_type_id == Type::TIME64 && target_type == CDataType_TIMESTAMP) {
return [=](const std::shared_ptr<Array>& original_array) {
arrow::compute::CastOptions cast_options;
cast_options.to_type = arrow::int64();

auto first_converted_array =
CheckConversion(arrow::compute::Cast(original_array, cast_options));

auto seconds_from_epoch = GetTodayTimeFromEpoch();

auto second_converted_array = CheckConversion(arrow::compute::Add(
first_converted_array,
std::make_shared<Int64Scalar>(seconds_from_epoch * 1000000000)));

arrow::compute::CastOptions cast_options_2;
cast_options_2.to_type = arrow::timestamp(TimeUnit::NANO);

return CheckConversion(
arrow::compute::Cast(second_converted_array, cast_options_2));
};
} else if ((original_type_id == Type::TIME32 || original_type_id == Type::TIME64) &&
target_type == CDataType_TIMESTAMP) {
return ConvertTimeToTodayTimestamp;
} else if (original_type_id == Type::STRING && target_type == CDataType_DATE) {
return [=](const std::shared_ptr<Array>& original_array) {
// The Strptime requires a date format. Using the ISO 8601 format
Expand Down
81 changes: 37 additions & 44 deletions cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "arrow/testing/builder.h"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(optional) Suggest to add tests for:

  • null values
  • midnight (value 0) and empty array

#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
#include "gtest/gtest.h"

namespace arrow::flight::sql::odbc {
Expand Down Expand Up @@ -65,64 +67,55 @@ void TestArrayConversion(const std::vector<std::string>& input,
AssertConvertedArray(expected_array, converted_array, input.size(), arrow_type);
}

void TestTime32ArrayConversion(const std::vector<int32_t>& input,
const std::shared_ptr<Array>& expected_array,
CDataType c_type, Type::type arrow_type) {
std::shared_ptr<Array> original_array;
ArrayFromVector<Time32Type, int32_t>(time32(TimeUnit::MILLI), input, &original_array);

auto converted_array = convertArray(original_array, c_type);

AssertConvertedArray(expected_array, converted_array, input.size(), arrow_type);
}
// The converted timestamp must keep the unit of the time array: today's date plus
// the time of day, with the input values otherwise untouched.
template <typename TimeArrowType>
void TestTimeToTimestampConversion(
const std::shared_ptr<DataType>& time_type,
const std::vector<typename TimeArrowType::c_type>& input) {
const auto unit = arrow::internal::checked_cast<const TimeType&>(*time_type).unit();

void TestTime64ArrayConversion(const std::vector<int64_t>& input,
const std::shared_ptr<Array>& expected_array,
CDataType c_type, Type::type arrow_type) {
std::shared_ptr<Array> original_array;
ArrayFromVector<Time64Type, int64_t>(time64(TimeUnit::NANO), input, &original_array);

auto converted_array = convertArray(original_array, c_type);

AssertConvertedArray(expected_array, converted_array, input.size(), arrow_type);
}

TEST(Utils, Time32ToTimeStampArray) {
std::vector<int32_t> input_data = {14896, 17820};
ArrayFromVector<TimeArrowType, typename TimeArrowType::c_type>(time_type, input,
&original_array);

const auto seconds_from_epoch = GetTodayTimeFromEpoch();
const int64_t units_per_second =
arrow::util::GetTimestampConversion(TimeUnit::SECOND, unit).second;
std::vector<int64_t> expected_data;
expected_data.reserve(2);

for (const auto& item : input_data) {
expected_data.emplace_back(item + seconds_from_epoch * 1000);
expected_data.reserve(input.size());
for (const auto& item : input) {
expected_data.emplace_back(item + seconds_from_epoch * units_per_second);
}

std::shared_ptr<Array> expected;
auto timestamp_field = field("timestamp_field", timestamp(TimeUnit::MILLI));
ArrayFromVector<TimestampType, int64_t>(timestamp_field->type(), expected_data,
&expected);
ArrayFromVector<TimestampType, int64_t>(timestamp(unit), expected_data, &expected);

TestTime32ArrayConversion(input_data, expected, CDataType_TIMESTAMP, Type::TIMESTAMP);
}
auto converted_array = convertArray(original_array, CDataType_TIMESTAMP);

TEST(Utils, Time64ToTimeStampArray) {
std::vector<int64_t> input_data = {1579489200000, 1646881200000};
ASSERT_TRUE(converted_array->type()->Equals(*timestamp(unit)))
<< converted_array->type()->ToString();
AssertConvertedArray(expected, converted_array, input.size(), Type::TIMESTAMP);
}

const auto seconds_from_epoch = GetTodayTimeFromEpoch();
std::vector<int64_t> expected_data;
expected_data.reserve(2);
// 12:34:56 and 23:59:59, with as many fractional digits as the unit allows.
TEST(Utils, Time32SecondsToTimeStampArray) {
TestTimeToTimestampConversion<Time32Type>(time32(TimeUnit::SECOND), {45296, 86399});
}

for (const auto& item : input_data) {
expected_data.emplace_back(item + seconds_from_epoch * 1000000000);
}
TEST(Utils, Time32MillisToTimeStampArray) {
TestTimeToTimestampConversion<Time32Type>(time32(TimeUnit::MILLI),
{45296123, 86399999});
}

std::shared_ptr<Array> expected;
auto timestamp_field = field("timestamp_field", timestamp(TimeUnit::NANO));
ArrayFromVector<TimestampType, int64_t>(timestamp_field->type(), expected_data,
&expected);
TEST(Utils, Time64MicrosToTimeStampArray) {
TestTimeToTimestampConversion<Time64Type>(time64(TimeUnit::MICRO),
{45296123456, 86399999999});
}

TestTime64ArrayConversion(input_data, expected, CDataType_TIMESTAMP, Type::TIMESTAMP);
TEST(Utils, Time64NanosToTimeStampArray) {
TestTimeToTimestampConversion<Time64Type>(time64(TimeUnit::NANO),
{45296123456789, 86399999999999});
}

TEST(Utils, StringToDateArray) {
Expand Down
Loading