Skip to content

Commit dd0988b

Browse files
milesgrangeredponcepitrou
authored
ARROW-15277: [C++][Python] Use ChunkedArray::Make for chunked_array (#13950)
Supersedes and will close #12096 [ARROW-15277](https://issues.apache.org/jira/browse/ARROW-15277) Lead-authored-by: Miles Granger <[email protected]> Co-authored-by: Eduardo Ponce <[email protected]> Co-authored-by: Antoine Pitrou <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
1 parent 897c186 commit dd0988b

6 files changed

Lines changed: 24 additions & 25 deletions

File tree

‎cpp/src/arrow/chunked_array.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Result<std::shared_ptr<ChunkedArray>> ChunkedArray::Make(ArrayVector chunks,
7272
}
7373
for (const auto& chunk : chunks) {
7474
if (!chunk->type()->Equals(*type)) {
75-
return Status::Invalid("Array chunks must all be same type");
75+
return Status::TypeError("Array chunks must all be same type");
7676
}
7777
}
7878
return std::make_shared<ChunkedArray>(std::move(chunks), std::move(type));

‎cpp/src/arrow/chunked_array_test.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ TEST_F(TestChunkedArray, Make) {
6565
ASSERT_OK_AND_ASSIGN(auto result2, ChunkedArray::Make({chunk0, chunk0}, int8()));
6666
AssertChunkedEqual(*result, *result2);
6767

68-
ASSERT_RAISES(Invalid, ChunkedArray::Make({chunk0, chunk1}));
69-
ASSERT_RAISES(Invalid, ChunkedArray::Make({chunk0}, int16()));
68+
ASSERT_RAISES(TypeError, ChunkedArray::Make({chunk0, chunk1}));
69+
ASSERT_RAISES(TypeError, ChunkedArray::Make({chunk0}, int16()));
7070
}
7171

7272
TEST_F(TestChunkedArray, MakeEmpty) {

‎python/pyarrow/includes/libarrow.pxd‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,10 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
760760
CChunkedArray(const vector[shared_ptr[CArray]]& arrays)
761761
CChunkedArray(const vector[shared_ptr[CArray]]& arrays,
762762
const shared_ptr[CDataType]& type)
763+
764+
@staticmethod
765+
CResult[shared_ptr[CChunkedArray]] Make(vector[shared_ptr[CArray]] chunks,
766+
shared_ptr[CDataType] type)
763767
int64_t length()
764768
int64_t null_count()
765769
int num_chunks()

‎python/pyarrow/table.pxi‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,8 @@ def chunked_array(arrays, type=None):
13031303
cdef:
13041304
Array arr
13051305
vector[shared_ptr[CArray]] c_arrays
1306-
shared_ptr[CChunkedArray] sp_chunked_array
1306+
shared_ptr[CChunkedArray] c_result
1307+
shared_ptr[CDataType] c_type
13071308

13081309
type = ensure_type(type, allow_none=True)
13091310

@@ -1318,25 +1319,13 @@ def chunked_array(arrays, type=None):
13181319
# subsequent arrays to the firstly inferred array type
13191320
# it also spares the inference overhead after the first chunk
13201321
type = arr.type
1321-
else:
1322-
if arr.type != type:
1323-
raise TypeError(
1324-
"All array chunks must have type {}".format(type)
1325-
)
13261322

13271323
c_arrays.push_back(arr.sp_array)
13281324

1329-
if c_arrays.size() == 0 and type is None:
1330-
raise ValueError("When passing an empty collection of arrays "
1331-
"you must also pass the data type")
1332-
1333-
sp_chunked_array.reset(
1334-
new CChunkedArray(c_arrays, pyarrow_unwrap_data_type(type))
1335-
)
1325+
c_type = pyarrow_unwrap_data_type(type)
13361326
with nogil:
1337-
check_status(sp_chunked_array.get().Validate())
1338-
1339-
return pyarrow_wrap_chunked_array(sp_chunked_array)
1327+
c_result = GetResultValue(CChunkedArray.Make(c_arrays, c_type))
1328+
return pyarrow_wrap_chunked_array(c_result)
13401329

13411330

13421331
cdef _schema_from_arrays(arrays, names, metadata, shared_ptr[CSchema]* schema):

‎python/pyarrow/tests/test_compute.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ def test_mode_chunked_array():
394394
assert len(pc.mode(arr)) == 0
395395

396396

397+
def test_empty_chunked_array():
398+
msg = "cannot construct ChunkedArray from empty vector and omitted type"
399+
with pytest.raises(pa.ArrowInvalid, match=msg):
400+
pa.chunked_array([])
401+
402+
pa.chunked_array([], type=pa.int8())
403+
404+
397405
def test_variance():
398406
data = [1, 2, 3, 4, 5, 6, 7, 8]
399407
assert pc.variance(data).as_py() == 5.25

‎python/pyarrow/tests/test_table.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ def test_chunked_array_construction():
9797
assert len(arr) == 3
9898
assert len(arr.chunks) == 2
9999

100-
msg = (
101-
"When passing an empty collection of arrays you must also pass the "
102-
"data type"
103-
)
100+
msg = "cannot construct ChunkedArray from empty vector and omitted type"
104101
with pytest.raises(ValueError, match=msg):
105102
assert pa.chunked_array([])
106103

@@ -143,14 +140,15 @@ def test_chunked_array_to_numpy():
143140

144141

145142
def test_chunked_array_mismatch_types():
146-
with pytest.raises(TypeError):
143+
msg = "chunks must all be same type"
144+
with pytest.raises(TypeError, match=msg):
147145
# Given array types are different
148146
pa.chunked_array([
149147
pa.array([1, 2, 3]),
150148
pa.array([1., 2., 3.])
151149
])
152150

153-
with pytest.raises(TypeError):
151+
with pytest.raises(TypeError, match=msg):
154152
# Given array type is different from explicit type argument
155153
pa.chunked_array([pa.array([1, 2, 3])], type=pa.float64())
156154

0 commit comments

Comments
 (0)