[
https://issues.apache.org/jira/browse/ARROW-1773?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16257937#comment-16257937
]
ASF GitHub Bot commented on ARROW-1773:
---------------------------------------
wesm closed pull request #1310: ARROW-1773: [C++] Add casts from date/time
types to compatible signed integers
URL: https://github.com/apache/arrow/pull/1310
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/cpp/src/arrow/compute/compute-test.cc
b/cpp/src/arrow/compute/compute-test.cc
index 5eada398d..58a991c60 100644
--- a/cpp/src/arrow/compute/compute-test.cc
+++ b/cpp/src/arrow/compute/compute-test.cc
@@ -334,6 +334,9 @@ TEST_F(TestCast, TimestampToTimestamp) {
&arr);
CheckZeroCopy(*arr, timestamp(TimeUnit::SECOND));
+ // ARROW-1773, cast to integer
+ CheckZeroCopy(*arr, int64());
+
// Divide, truncate
vector<int64_t> v8 = {0, 100123, 200456, 1123, 2456};
vector<int64_t> e8 = {0, 100, 200, 1, 2};
@@ -432,7 +435,7 @@ TEST_F(TestCast, TimestampToDate32_Date64) {
timestamp(TimeUnit::SECOND), v_second_nofail, is_valid, date32(), v_day,
options);
}
-TEST_F(TestCast, TimeToTime) {
+TEST_F(TestCast, TimeToCompatible) {
CastOptions options;
vector<bool> is_valid = {true, false, true, true, true};
@@ -474,6 +477,16 @@ TEST_F(TestCast, TimeToTime) {
ArrayFromVector<Time64Type, int64_t>(time64(TimeUnit::MICRO), is_valid, v7,
&arr);
CheckZeroCopy(*arr, time64(TimeUnit::MICRO));
+ // ARROW-1773: cast to int64
+ CheckZeroCopy(*arr, int64());
+
+ vector<int32_t> v7_2 = {0, 70000, 2000, 1000, 0};
+ ArrayFromVector<Time32Type, int32_t>(time32(TimeUnit::SECOND), is_valid,
v7_2, &arr);
+ CheckZeroCopy(*arr, time32(TimeUnit::SECOND));
+
+ // ARROW-1773: cast to int64
+ CheckZeroCopy(*arr, int32());
+
// Divide, truncate
vector<int32_t> v8 = {0, 100123, 200456, 1123, 2456};
vector<int32_t> e8 = {0, 100, 200, 1, 2};
@@ -515,7 +528,7 @@ TEST_F(TestCast, TimeToTime) {
options);
}
-TEST_F(TestCast, DateToDate) {
+TEST_F(TestCast, DateToCompatible) {
CastOptions options;
vector<bool> is_valid = {true, false, true, true, true};
@@ -535,9 +548,15 @@ TEST_F(TestCast, DateToDate) {
ArrayFromVector<Date32Type, int32_t>(date32(), is_valid, v2, &arr);
CheckZeroCopy(*arr, date32());
+ // ARROW-1773: zero copy cast to integer
+ CheckZeroCopy(*arr, int32());
+
ArrayFromVector<Date64Type, int64_t>(date64(), is_valid, v3, &arr);
CheckZeroCopy(*arr, date64());
+ // ARROW-1773: zero copy cast to integer
+ CheckZeroCopy(*arr, int64());
+
// Divide, truncate
vector<int64_t> v8 = {0, 100 * F + 123, 200 * F + 456, F + 123, 2 * F + 456};
vector<int32_t> e8 = {0, 100, 200, 1, 2};
diff --git a/cpp/src/arrow/compute/kernels/cast.cc
b/cpp/src/arrow/compute/kernels/cast.cc
index 6a42ec8b2..c866054ea 100644
--- a/cpp/src/arrow/compute/kernels/cast.cc
+++ b/cpp/src/arrow/compute/kernels/cast.cc
@@ -91,10 +91,14 @@ struct is_zero_copy_cast<
// From integers to date/time types with zero copy
template <typename O, typename I>
struct is_zero_copy_cast<
- O, I, typename std::enable_if<std::is_base_of<Integer, I>::value &&
- (std::is_base_of<TimeType, O>::value ||
- std::is_base_of<DateType, O>::value ||
- std::is_base_of<TimestampType,
O>::value)>::type> {
+ O, I,
+ typename std::enable_if<
+ (std::is_base_of<Integer, I>::value &&
+ (std::is_base_of<TimeType, O>::value || std::is_base_of<DateType,
O>::value ||
+ std::is_base_of<TimestampType, O>::value)) ||
+ (std::is_base_of<Integer, O>::value &&
+ (std::is_base_of<TimeType, I>::value || std::is_base_of<DateType,
I>::value ||
+ std::is_base_of<TimestampType, I>::value))>::type> {
using O_T = typename O::c_type;
using I_T = typename I::c_type;
@@ -809,24 +813,29 @@ class CastKernel : public UnaryKernel {
#define DATE32_CASES(FN, IN_TYPE) \
FN(Date32Type, Date32Type); \
- FN(Date32Type, Date64Type);
+ FN(Date32Type, Date64Type); \
+ FN(Date32Type, Int32Type);
#define DATE64_CASES(FN, IN_TYPE) \
FN(Date64Type, Date64Type); \
- FN(Date64Type, Date32Type);
+ FN(Date64Type, Date32Type); \
+ FN(Date64Type, Int64Type);
#define TIME32_CASES(FN, IN_TYPE) \
FN(Time32Type, Time32Type); \
- FN(Time32Type, Time64Type);
+ FN(Time32Type, Time64Type); \
+ FN(Time32Type, Int32Type);
#define TIME64_CASES(FN, IN_TYPE) \
FN(Time64Type, Time32Type); \
- FN(Time64Type, Time64Type);
+ FN(Time64Type, Time64Type); \
+ FN(Time64Type, Int64Type);
#define TIMESTAMP_CASES(FN, IN_TYPE) \
FN(TimestampType, TimestampType); \
FN(TimestampType, Date32Type); \
- FN(TimestampType, Date64Type);
+ FN(TimestampType, Date64Type); \
+ FN(TimestampType, Int64Type);
#define DICTIONARY_CASES(FN, IN_TYPE) \
FN(IN_TYPE, NullType); \
diff --git a/python/pyarrow/tests/test_array.py
b/python/pyarrow/tests/test_array.py
index c061e6820..b7b0b1833 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -389,6 +389,59 @@ def test_dictionary_encode_simple():
assert result.equals(expected)
+def test_cast_time32_to_int():
+ arr = pa.array(np.array([0, 1, 2], dtype='int32'),
+ type=pa.time32('s'))
+ expected = pa.array([0, 1, 2], type='i4')
+
+ result = arr.cast('i4')
+ assert result.equals(expected)
+
+
+def test_cast_time64_to_int():
+ arr = pa.array(np.array([0, 1, 2], dtype='int64'),
+ type=pa.time64('us'))
+ expected = pa.array([0, 1, 2], type='i8')
+
+ result = arr.cast('i8')
+ assert result.equals(expected)
+
+
+def test_cast_timestamp_to_int():
+ arr = pa.array(np.array([0, 1, 2], dtype='int64'),
+ type=pa.timestamp('us'))
+ expected = pa.array([0, 1, 2], type='i8')
+
+ result = arr.cast('i8')
+ assert result.equals(expected)
+
+
+def test_cast_date32_to_int():
+ arr = pa.array([0, 1, 2], type='i4')
+
+ result1 = arr.cast('date32')
+ result2 = result1.cast('i4')
+
+ expected1 = pa.array([
+ datetime.date(1970, 1, 1),
+ datetime.date(1970, 1, 2),
+ datetime.date(1970, 1, 3)
+ ]).cast('date32')
+
+ assert result1.equals(expected1)
+ assert result2.equals(arr)
+
+
+def test_cast_date64_to_int():
+ arr = pa.array(np.array([0, 1, 2], dtype='int64'),
+ type=pa.date64())
+ expected = pa.array([0, 1, 2], type='i8')
+
+ result = arr.cast('i8')
+
+ assert result.equals(expected)
+
+
def test_simple_type_construction():
result = pa.lib.TimestampType()
with pytest.raises(TypeError):
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> [C++] Add casts from date/time types to compatible signed integers
> ------------------------------------------------------------------
>
> Key: ARROW-1773
> URL: https://issues.apache.org/jira/browse/ARROW-1773
> Project: Apache Arrow
> Issue Type: New Feature
> Reporter: Wes McKinney
> Assignee: Licht Takeuchi
> Labels: pull-request-available
> Fix For: 0.8.0
>
>
> e.g.
> {code}
> In [3]: arr = pa.array([1,2,3], type='i4')
> In [4]: arr.cast('date32')
> Out[4]:
> <pyarrow.lib.Date32Array object at 0x7fc35f8b56d8>
> [
> datetime.date(1970, 1, 2),
> datetime.date(1970, 1, 3),
> datetime.date(1970, 1, 4)
> ]
> In [5]: arr.cast('date32').cast('i4')
> ---------------------------------------------------------------------------
> ArrowNotImplementedError Traceback (most recent call last)
> <ipython-input-5-7b6f6cf1cced> in <module>()
> ----> 1 arr.cast('date32').cast('i4')
> /home/wesm/code/arrow/python/pyarrow/array.pxi in pyarrow.lib.Array.cast
> (/home/wesm/code/arrow/python/build/temp.linux-x86_64-3.5/lib.cxx:28923)()
> 266
> 267 with nogil:
> --> 268 check_status(Cast(_context(), self.ap[0], type.sp_type,
> 269 options, &result))
> 270
> /home/wesm/code/arrow/python/pyarrow/error.pxi in pyarrow.lib.check_status
> (/home/wesm/code/arrow/python/build/temp.linux-x86_64-3.5/lib.cxx:8306)()
> 83 raise ArrowKeyError(message)
> 84 elif status.IsNotImplemented():
> ---> 85 raise ArrowNotImplementedError(message)
> 86 elif status.IsTypeError():
> 87 raise ArrowTypeError(message)
> ArrowNotImplementedError:
> /home/wesm/code/arrow/cpp/src/arrow/compute/cast.cc:920 code:
> GetCastFunction(*array.type(), out_type, options, &func)
> No cast implemented from date32[day] to int32
> {code}
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)