You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by li...@apache.org on 2022/08/02 12:57:39 UTC

[arrow] branch master updated: ARROW-17214: [C++] Add scalar casts to string types for list based types (#13737)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4bd3d2ecbb ARROW-17214: [C++] Add scalar casts to string types for list based types (#13737)
4bd3d2ecbb is described below

commit 4bd3d2ecbb6f4f311d59098892b7dca5421c15e4
Author: LouisClt <lo...@hotmail.fr>
AuthorDate: Tue Aug 2 14:57:32 2022 +0200

    ARROW-17214: [C++] Add scalar casts to string types for list based types (#13737)
    
    Following https://lists.apache.org/thread/rp7vpjtt4lgtjxj35oyjyqh9b6on94jf discussion, here is the PR corresponding to the feature : be able to cast list-like types (maps, lists, fixed-size lists) to string type.
    
    It produces output such as :
    list<item: int64>{null, 1}
    map<string, int64>{{key:string = a, value:int64 = 2}, {key:string = b, value:int64 = 45}}
    map<struct<x: int16, y: bool>, int64>{{key:struct<x: int16, y: bool> = {x:int16 = 884, y:bool = true}, value:int64 = 2}, {key:struct<x: int16, y: bool> = {x:int16 = 874, y:bool = false}, value:int64 = null}}
    fixed_size_list<item: float>[3]{4, 5, 6}
    
    I tried to be coherent with the rest of the CastTo methods.
    Feel free to comment.
    
    Lead-authored-by: LouisClt <lo...@hotmail.fr>
    Co-authored-by: Louis CALOT <ca...@isdom.isoft.fr>
    Co-authored-by: David Li <li...@gmail.com>
    Signed-off-by: David Li <li...@gmail.com>
---
 c_glib/test/test-list-scalar.rb | 13 ++-----------
 c_glib/test/test-map-scalar.rb  | 13 +------------
 cpp/src/arrow/scalar.cc         | 14 ++++++++++++++
 cpp/src/arrow/scalar_test.cc    |  4 ++++
 4 files changed, 21 insertions(+), 23 deletions(-)

diff --git a/c_glib/test/test-list-scalar.rb b/c_glib/test/test-list-scalar.rb
index 0ddbf60bc0..06633ee3bb 100644
--- a/c_glib/test/test-list-scalar.rb
+++ b/c_glib/test/test-list-scalar.rb
@@ -41,17 +41,8 @@ class TestListScalar < Test::Unit::TestCase
   end
 
   def test_to_s
-    assert_equal(<<-LIST.strip, @scalar.to_s)
-[
-  [
-    [
-      1,
-      2,
-      3
-    ]
-  ]
-]
-                 LIST
+    assert_equal("list<item: list<value: int8>>[list<value: int8>[1, 2, 3]]",
+                 @scalar.to_s)
   end
 
   def test_value
diff --git a/c_glib/test/test-map-scalar.rb b/c_glib/test/test-map-scalar.rb
index 1e004569ef..541a7b32ea 100644
--- a/c_glib/test/test-map-scalar.rb
+++ b/c_glib/test/test-map-scalar.rb
@@ -57,18 +57,7 @@ class TestMapScalar < Test::Unit::TestCase
 
   def test_to_s
     assert_equal(<<-MAP.strip, @scalar.to_s)
-[
-  keys:
-  [
-    "hello",
-    "world"
-  ]
-  values:
-  [
-    1,
-    2
-  ]
-]
+map<string, int8>[{key:string = hello, value:int8 = 1}, {key:string = world, value:int8 = 2}]
                  MAP
   end
 
diff --git a/cpp/src/arrow/scalar.cc b/cpp/src/arrow/scalar.cc
index 21e1cdedc2..5ed92f0947 100644
--- a/cpp/src/arrow/scalar.cc
+++ b/cpp/src/arrow/scalar.cc
@@ -1041,6 +1041,20 @@ Status CastImpl(const StructScalar& from, StringScalar* to) {
   return Status::OK();
 }
 
+// list based types (list, large list and map (fixed sized list too)) to string
+Status CastImpl(const BaseListScalar& from, StringScalar* to) {
+  std::stringstream ss;
+  ss << from.type->ToString() << "[";
+  for (int64_t i = 0; i < from.value->length(); i++) {
+    if (i > 0) ss << ", ";
+    ARROW_ASSIGN_OR_RAISE(auto value, from.value->GetScalar(i));
+    ss << value->ToString();
+  }
+  ss << ']';
+  to->value = Buffer::FromString(ss.str());
+  return Status::OK();
+}
+
 Status CastImpl(const UnionScalar& from, StringScalar* to) {
   const auto& union_ty = checked_cast<const UnionType&>(*from.type);
   std::stringstream ss;
diff --git a/cpp/src/arrow/scalar_test.cc b/cpp/src/arrow/scalar_test.cc
index 265ee3e94e..bf001fc6fd 100644
--- a/cpp/src/arrow/scalar_test.cc
+++ b/cpp/src/arrow/scalar_test.cc
@@ -1049,11 +1049,15 @@ class TestListScalar : public ::testing::Test {
     ASSERT_OK(scalar.ValidateFull());
     ASSERT_TRUE(scalar.is_valid);
     AssertTypeEqual(scalar.type, type_);
+    // list<item: int16>[1, 2, null]
+    ASSERT_THAT(scalar.ToString(), ::testing::AllOf(::testing::HasSubstr("item: int16"),
+                                                    ::testing::EndsWith("[1, 2, null]")));
 
     auto null_scalar = CheckMakeNullScalar(type_);
     ASSERT_OK(null_scalar->ValidateFull());
     ASSERT_FALSE(null_scalar->is_valid);
     AssertTypeEqual(null_scalar->type, type_);
+    ASSERT_EQ(null_scalar->ToString(), "null");
   }
 
   void TestValidateErrors() {