Skip to content

Commit 2214848

Browse files
authored
GH-47263: [MATLAB] Add NumNulls property to arrow.array.ChunkedArray class (#47264)
### Rationale for this change This is a followup to #38422. Now that `NumNulls` is a property on `arrow.array.Array`, we should add `NumNulls` as a property on `arrow.array.ChunkedArray`. ### What changes are included in this PR? Added `NumNulls` as a property to `arrow.array.ChunkedArray`. **Example**: ```matlab >> a1 = arrow.array(1:10); >> a2 = arrow.array([11 12 NaN 14 NaN]); >> a3 = arrow.array([16 17 NaN 18 19]); >> a4 = arrow.array(20:30); >> C1 = arrow.array.ChunkedArray.fromArrays(a1, a2, a3) C1 = ChunkedArray with properties: Type: [1×1 arrow.type.Float64Type] NumChunks: 4 NumElements: 31 NumNulls: 3 >> C1.NumNulls ans = int64 3 ``` ### Are these changes tested? 1. Added a `NumNullsNoSetter` test case to `tChunkedArray.m`. 2. Updated `tChunkedArray/verifyChunkedArray` helper method to verify the `NumNulls` property value is set to the expected value. ### Are there any user-facing changes? Yes. `arrow.array.ChunkedArray` has a new public property called `NumNulls`. * GitHub Issue: #47263 Authored-by: Sarah Gilmore <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent c2d2727 commit 2214848

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

matlab/src/cpp/arrow/matlab/array/proxy/chunked_array.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ChunkedArray::ChunkedArray(std::shared_ptr<arrow::ChunkedArray> chunked_array)
5252
: chunked_array{std::move(chunked_array)} {
5353
// Register Proxy methods.
5454
REGISTER_METHOD(ChunkedArray, getNumElements);
55+
REGISTER_METHOD(ChunkedArray, getNumNulls);
5556
REGISTER_METHOD(ChunkedArray, getNumChunks);
5657
REGISTER_METHOD(ChunkedArray, getChunk);
5758
REGISTER_METHOD(ChunkedArray, getType);
@@ -94,6 +95,13 @@ void ChunkedArray::getNumElements(libmexclass::proxy::method::Context& context)
9495
context.outputs[0] = num_elements_mda;
9596
}
9697

98+
void ChunkedArray::getNumNulls(libmexclass::proxy::method::Context& context) {
99+
namespace mda = ::matlab::data;
100+
mda::ArrayFactory factory;
101+
auto num_nulls_mda = factory.createScalar(chunked_array->null_count());
102+
context.outputs[0] = num_nulls_mda;
103+
}
104+
97105
void ChunkedArray::getNumChunks(libmexclass::proxy::method::Context& context) {
98106
namespace mda = ::matlab::data;
99107
mda::ArrayFactory factory;

matlab/src/cpp/arrow/matlab/array/proxy/chunked_array.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class ChunkedArray : public libmexclass::proxy::Proxy {
3737
protected:
3838
void getNumElements(libmexclass::proxy::method::Context& context);
3939

40+
void getNumNulls(libmexclass::proxy::method::Context& context);
41+
4042
void getNumChunks(libmexclass::proxy::method::Context& context);
4143

4244
void getChunk(libmexclass::proxy::method::Context& context);

matlab/src/matlab/+arrow/+array/ChunkedArray.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Type
2727
NumChunks
2828
NumElements
29+
NumNulls
2930
end
3031

3132
methods
@@ -45,6 +46,10 @@
4546
numElements = obj.Proxy.getNumElements();
4647
end
4748

49+
function numNulls = get.NumNulls(obj)
50+
numNulls = obj.Proxy.getNumNulls();
51+
end
52+
4853
function type = get.Type(obj)
4954
typeStruct = obj.Proxy.getType();
5055
traits = arrow.type.traits.traits(arrow.type.ID(typeStruct.TypeID));
@@ -123,5 +128,5 @@
123128

124129
chunkedArray = arrow.array.ChunkedArray(proxy);
125130
end
126-
end
127-
end
131+
end
132+
end

matlab/test/arrow/array/tChunkedArray.m

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ function TestIsEqualTrue(testCase)
113113
%
114114
% 1. Their Type properties are equal
115115
% 2. Their NumElements properties are equal
116-
% 3. The same elements are considered null
117-
% 4. All corresponding valid elements have the same values
116+
% 3. Their NumNulls properties are equal
117+
% 4. The same elements are considered null
118+
% 5. All corresponding valid elements have the same values
118119
%
119120
% NOTE: Having the same "chunking" is not a requirement for two
120121
% ChunkedArrays to be equal. ChunkedArrays are considered equal
@@ -204,6 +205,18 @@ function NumElementsNoSetter(testCase)
204205
testCase.verifyError(fcn, "MATLAB:class:SetProhibited");
205206
end
206207

208+
function NumNullsNoSetter(testCase)
209+
% Verify an error is thrown when trying to set the value
210+
% of the NumNulls property.
211+
import arrow.array.ChunkedArray
212+
213+
arrays = {testCase.Float64Array1, testCase.Float64Array2, testCase.Float64Array3};
214+
chunkedArray = ChunkedArray.fromArrays(arrays{:});
215+
216+
fcn = @() setfield(chunkedArray, "NumNulls", int64(100));
217+
testCase.verifyError(fcn, "MATLAB:class:SetProhibited");
218+
end
219+
207220
function ChunkNonNumericIndexError(testCase)
208221
% Verify that an error is thrown when a non-numeric index value
209222
% is provided to the chunk() method.
@@ -476,10 +489,14 @@ function verifyChunkedArray(testCase, chunkedArray, opts)
476489
end
477490
testCase.assertTrue(numel(opts.Arrays) == opts.NumChunks);
478491
allNumElements = cellfun(@(a) a.NumElements, opts.Arrays, UniformOutput=true);
492+
allNumNulls = cellfun(@(a) a.NumNulls, opts.Arrays, UniformOutput=true);
493+
479494
expectedNumElements = int64(sum(allNumElements));
495+
expectedNumNulls = int64(sum(allNumNulls));
480496

481497
testCase.verifyEqual(chunkedArray.NumChunks, opts.NumChunks);
482498
testCase.verifyEqual(chunkedArray.NumElements, expectedNumElements);
499+
testCase.verifyEqual(chunkedArray.NumNulls, expectedNumNulls);
483500
testCase.verifyEqual(chunkedArray.Type, opts.Type);
484501

485502
for ii = 1:opts.NumChunks

0 commit comments

Comments
 (0)