This is an automated email from the ASF dual-hosted git repository.

maplefu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 95b7181307 GH-44541: [C++] NumericArray<T> should not use ctor from 
parent directly (#44542)
95b7181307 is described below

commit 95b7181307fe133e8c84a0c0be2856ee15348b8e
Author: mwish <[email protected]>
AuthorDate: Mon Nov 11 14:06:29 2024 +0800

    GH-44541: [C++] NumericArray<T> should not use ctor from parent directly 
(#44542)
    
    
    
    ### Rationale for this change
    
    See https://github.com/apache/arrow/issues/44541
    
    ### What changes are included in this PR?
    
    `NumericArray<T>` not using child's ctor.
    
    ### Are these changes tested?
    
    Yes
    
    ### Are there any user-facing changes?
    
    Bugfix
    
    * GitHub Issue: #44541
    
    Authored-by: mwish <[email protected]>
    Signed-off-by: mwish <[email protected]>
---
 cpp/src/arrow/array/array_primitive.h | 18 ++++++++++++++----
 cpp/src/arrow/array/array_test.cc     | 25 +++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/cpp/src/arrow/array/array_primitive.h 
b/cpp/src/arrow/array/array_primitive.h
index 3e2893b7dd..cebf47ad93 100644
--- a/cpp/src/arrow/array/array_primitive.h
+++ b/cpp/src/arrow/array/array_primitive.h
@@ -90,7 +90,9 @@ class NumericArray : public PrimitiveArray {
   using value_type = typename TypeClass::c_type;
   using IteratorType = stl::ArrayIterator<NumericArray<TYPE>>;
 
-  explicit NumericArray(const std::shared_ptr<ArrayData>& data) { 
SetData(data); }
+  explicit NumericArray(const std::shared_ptr<ArrayData>& data) {
+    NumericArray::SetData(data);
+  }
 
   // Only enable this constructor without a type argument for types without 
additional
   // metadata
@@ -99,8 +101,16 @@ class NumericArray : public PrimitiveArray {
                const std::shared_ptr<Buffer>& data,
                const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
                int64_t null_count = kUnknownNullCount, int64_t offset = 0) {
-    SetData(ArrayData::Make(TypeTraits<T1>::type_singleton(), length, 
{null_bitmap, data},
-                            null_count, offset));
+    NumericArray::SetData(ArrayData::Make(TypeTraits<T1>::type_singleton(), 
length,
+                                          {null_bitmap, data}, null_count, 
offset));
+  }
+
+  NumericArray(std::shared_ptr<DataType> type, int64_t length,
+               const std::shared_ptr<Buffer>& data,
+               const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
+               int64_t null_count = kUnknownNullCount, int64_t offset = 0) {
+    NumericArray::SetData(ArrayData::Make(std::move(type), length, 
{null_bitmap, data},
+                                          null_count, offset));
   }
 
   const value_type* raw_values() const { return values_; }
@@ -119,7 +129,7 @@ class NumericArray : public PrimitiveArray {
   IteratorType end() const { return IteratorType(*this, length()); }
 
  protected:
-  using PrimitiveArray::PrimitiveArray;
+  NumericArray() : values_(NULLPTR) {}
 
   void SetData(const std::shared_ptr<ArrayData>& data) {
     this->PrimitiveArray::SetData(data);
diff --git a/cpp/src/arrow/array/array_test.cc 
b/cpp/src/arrow/array/array_test.cc
index d69e00460d..44ccf9687b 100644
--- a/cpp/src/arrow/array/array_test.cc
+++ b/cpp/src/arrow/array/array_test.cc
@@ -585,6 +585,31 @@ TEST_F(TestArray, TestValidateNullCount) {
   }
 }
 
+TEST_F(TestArray, TestValidValues) {
+  // GH-44541: The value_ should be valid when construct.
+  {
+    std::vector<int32_t> original_data{1, 2, 3, 4, 5, 6, 7};
+    std::shared_ptr<Int32Array> arr =
+        std::make_shared<Int32Array>(::arrow::int32(), 7, 
Buffer::Wrap(original_data));
+    for (size_t i = 0; i < original_data.size(); ++i) {
+      EXPECT_TRUE(arr->IsValid(i));
+      EXPECT_FALSE(arr->IsNull(i));
+      EXPECT_EQ(original_data[i], arr->Value(i));
+    }
+  }
+  {
+    // Test non parameter free type.
+    std::vector<int64_t> original_data{1, 2, 3, 4, 5, 6, 7};
+    std::shared_ptr<TimestampArray> arr = std::make_shared<TimestampArray>(
+        ::arrow::timestamp(TimeUnit::MICRO), 7, Buffer::Wrap(original_data));
+    for (size_t i = 0; i < original_data.size(); ++i) {
+      EXPECT_TRUE(arr->IsValid(i));
+      EXPECT_FALSE(arr->IsNull(i));
+      EXPECT_EQ(original_data[i], arr->Value(i));
+    }
+  }
+}
+
 void AssertAppendScalar(MemoryPool* pool, const std::shared_ptr<Scalar>& 
scalar) {
   std::unique_ptr<arrow::ArrayBuilder> builder;
   auto null_scalar = MakeNullScalar(scalar->type);

Reply via email to