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

zanmato 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 37eb3b5010 GH-45005 [C++] Support for fixed-size list in conversion of 
range tuple (#45008)
37eb3b5010 is described below

commit 37eb3b50101aa5e32b5122e61593a1a592a22274
Author: mroz45 <[email protected]>
AuthorDate: Wed Dec 18 11:14:03 2024 +0100

    GH-45005 [C++] Support for fixed-size list in conversion of range tuple 
(#45008)
    
    
    
    ### What changes are included in this PR?
    This PR introduces support for fixed-size lists in Apache Arrow's STL 
conversion.  Added specializations for arrow::stl::ConversionTraits<T> and 
arrow::CTypeTraits<T> to handle std::array as a fixed-size list and also Unit 
Test.
    
    ### Are these changes tested?
    Yes, new type is tested.
    
    ### Are there any user-facing changes?
    
    * GitHub Issue: #45005
    
    Lead-authored-by: kamilt <[email protected]>
    Co-authored-by: Rossi Sun <[email protected]>
    Signed-off-by: Rossi Sun <[email protected]>
---
 cpp/src/arrow/stl.h         | 29 +++++++++++++++++++++++++++++
 cpp/src/arrow/stl_test.cc   | 40 ++++++++++++++++++++++++++++++++++++++++
 cpp/src/arrow/type_traits.h | 10 ++++++++++
 3 files changed, 79 insertions(+)

diff --git a/cpp/src/arrow/stl.h b/cpp/src/arrow/stl.h
index b542ee5c34..ae5462c661 100644
--- a/cpp/src/arrow/stl.h
+++ b/cpp/src/arrow/stl.h
@@ -187,6 +187,35 @@ struct ConversionTraits<std::vector<ValueCType>>
   }
 };
 
+template <class ValueCType, std::size_t N>
+struct ConversionTraits<std::array<ValueCType, N>>
+    : public CTypeTraits<std::array<ValueCType, N>> {
+  static arrow::Status AppendRow(FixedSizeListBuilder& builder,
+                                 const std::array<ValueCType, N>& values) {
+    auto vb =
+        ::arrow::internal::checked_cast<typename 
CTypeTraits<ValueCType>::BuilderType*>(
+            builder.value_builder());
+    ARROW_RETURN_NOT_OK(builder.Append());
+    return vb->AppendValues(values.data(), N);
+  }
+
+  static std::array<ValueCType, N> GetEntry(const ::arrow::FixedSizeListArray& 
array,
+                                            size_t j) {
+    using ElementArrayType = typename TypeTraits<
+        typename stl::ConversionTraits<ValueCType>::ArrowType>::ArrayType;
+
+    const ElementArrayType& value_array =
+        ::arrow::internal::checked_cast<const 
ElementArrayType&>(*array.values());
+
+    std::array<ValueCType, N> arr;
+    for (size_t i = 0; i < N; i++) {
+      arr[i] = stl::ConversionTraits<ValueCType>::GetEntry(value_array,
+                                                           
array.value_offset(j) + i);
+    }
+    return arr;
+  }
+};
+
 template <typename Optional>
 struct ConversionTraits<Optional, enable_if_optional_like<Optional>>
     : public CTypeTraits<typename 
std::decay<decltype(*std::declval<Optional>())>::type> {
diff --git a/cpp/src/arrow/stl_test.cc b/cpp/src/arrow/stl_test.cc
index 48e6f8014c..ce5adf0c0e 100644
--- a/cpp/src/arrow/stl_test.cc
+++ b/cpp/src/arrow/stl_test.cc
@@ -245,6 +245,26 @@ TEST(TestTableFromTupleVector, ListType) {
   ASSERT_TRUE(expected_table->Equals(*table));
 }
 
+TEST(TestTableFromTupleVector, FixedSizeListType) {
+  using tuple_type = std::tuple<std::array<int64_t, 4>>;
+
+  auto expected_schema = std::make_shared<Schema>(
+      FieldVector{field("column1", fixed_size_list(int64(), 4), false)});
+  std::shared_ptr<Array> expected_array =
+      ArrayFromJSON(fixed_size_list(int64(), 4), "[[1, 1, 2, 34], [2, -4, 1, 
1]]");
+  std::shared_ptr<Table> expected_table = Table::Make(expected_schema, 
{expected_array});
+
+  std::vector<tuple_type> rows{tuple_type(std::array<int64_t, 4>{1, 1, 2, 34}),
+                               tuple_type(std::array<int64_t, 4>{2, -4, 1, 
1})};
+  std::vector<std::string> names{"column1"};
+
+  std::shared_ptr<Table> table;
+  ASSERT_OK(TableFromTupleRange(default_memory_pool(), rows, names, &table));
+  ASSERT_OK(table->ValidateFull());
+
+  AssertTablesEqual(*expected_table, *table);
+}
+
 TEST(TestTableFromTupleVector, ReferenceTuple) {
   std::vector<std::string> names{"column1", "column2", "column3", "column4", 
"column5",
                                  "column6", "column7", "column8", "column9", 
"column10"};
@@ -468,6 +488,26 @@ TEST(TestTupleVectorFromTable, ListType) {
   ASSERT_EQ(rows, expected_rows);
 }
 
+TEST(TestTupleVectorFromTable, FixedSizeListType) {
+  using tuple_type = std::tuple<std::array<int64_t, 4>>;
+
+  compute::ExecContext ctx;
+  compute::CastOptions cast_options;
+  auto expected_schema = std::make_shared<Schema>(
+      FieldVector{field("column1", fixed_size_list(int64(), 4), false)});
+  std::shared_ptr<Array> expected_array =
+      ArrayFromJSON(fixed_size_list(int64(), 4), "[[1, 1, 2, 34], [2, -4, 1, 
1]]");
+  std::shared_ptr<Table> table = Table::Make(expected_schema, 
{expected_array});
+  ASSERT_OK(table->ValidateFull());
+
+  std::vector<tuple_type> expected_rows{tuple_type(std::array<int64_t, 4>{1, 
1, 2, 34}),
+                                        tuple_type(std::array<int64_t, 4>{2, 
-4, 1, 1})};
+
+  std::vector<tuple_type> rows(2);
+  ASSERT_OK(TupleRangeFromTable(*table, cast_options, &ctx, &rows));
+  ASSERT_EQ(rows, expected_rows);
+}
+
 TEST(TestTupleVectorFromTable, CastingNeeded) {
   using tuple_type = std::tuple<std::vector<int64_t>>;
 
diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h
index 6da05bd8f1..92009c8560 100644
--- a/cpp/src/arrow/type_traits.h
+++ b/cpp/src/arrow/type_traits.h
@@ -540,6 +540,16 @@ struct CTypeTraits<std::vector<CType>> : public 
TypeTraits<ListType> {
   }
 };
 
+/// \addtogroup c-type-traits
+template <typename CType, std::size_t N>
+struct CTypeTraits<std::array<CType, N>> : public 
TypeTraits<FixedSizeListType> {
+  using ArrowType = FixedSizeListType;
+
+  static auto type_singleton() {
+    return fixed_size_list(CTypeTraits<CType>::type_singleton(), N);
+  }
+};
+
 /// \addtogroup type-traits
 /// @{
 template <>

Reply via email to