arrow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject arrow git commit: ARROW-1603: [C++] Add BinaryArray::GetString helper method
Date Thu, 05 Oct 2017 09:08:55 GMT
Repository: arrow
Updated Branches:
  refs/heads/master 8b5b22b15 -> 0f819fa73


ARROW-1603: [C++] Add BinaryArray::GetString helper method

In some applications it is useful to get values as `std::string`

Author: Wes McKinney <wes.mckinney@twosigma.com>

Closes #1159 from wesm/ARROW-1603 and squashes the following commits:

a94099d [Wes McKinney] Add BinaryArray::GetString method


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/0f819fa7
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/0f819fa7
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/0f819fa7

Branch: refs/heads/master
Commit: 0f819fa7355d3b391b0083168f0ffd6f329e8d1b
Parents: 8b5b22b
Author: Wes McKinney <wes.mckinney@twosigma.com>
Authored: Thu Oct 5 11:08:50 2017 +0200
Committer: Uwe L. Korn <uwelk@xhochy.com>
Committed: Thu Oct 5 11:08:50 2017 +0200

----------------------------------------------------------------------
 cpp/src/arrow/array-test.cc | 12 ++++++++++++
 cpp/src/arrow/array.h       | 11 +++++++++++
 2 files changed, 23 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/0f819fa7/cpp/src/arrow/array-test.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/array-test.cc b/cpp/src/arrow/array-test.cc
index 4ecf0f9..ef10257 100644
--- a/cpp/src/arrow/array-test.cc
+++ b/cpp/src/arrow/array-test.cc
@@ -1045,6 +1045,18 @@ TEST_F(TestBinaryArray, TestGetValue) {
   }
 }
 
+TEST_F(TestBinaryArray, TestGetString) {
+  for (size_t i = 0; i < expected_.size(); ++i) {
+    if (valid_bytes_[i] == 0) {
+      ASSERT_TRUE(strings_->IsNull(i));
+    } else {
+      std::string val = strings_->GetString(i);
+      ASSERT_EQ(0, std::memcmp(expected_[i].data(), val.c_str(),
+                               val.size()));
+    }
+  }
+}
+
 TEST_F(TestBinaryArray, TestEqualsEmptyStrings) {
   BinaryBuilder builder;
 

http://git-wip-us.apache.org/repos/asf/arrow/blob/0f819fa7/cpp/src/arrow/array.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h
index 4ad60eb..f668b6d 100644
--- a/cpp/src/arrow/array.h
+++ b/cpp/src/arrow/array.h
@@ -444,6 +444,17 @@ class ARROW_EXPORT BinaryArray : public FlatArray {
     return raw_data_ + pos;
   }
 
+  /// \brief Get binary value as a std::string
+  ///
+  /// \param i the value index
+  /// \return the value copied into a std::string
+  std::string GetString(int64_t i) const {
+    int32_t length = 0;
+    const uint8_t* bytes = GetValue(i, &length);
+    return std::string(reinterpret_cast<const char*>(bytes),
+                       static_cast<size_t>(length));
+  }
+
   /// Note that this buffer does not account for any slice offset
   std::shared_ptr<Buffer> value_offsets() const { return data_->buffers[1]; }
 


Mime
View raw message