[ 
https://issues.apache.org/jira/browse/ARROW-2118?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16395688#comment-16395688
 ] 

ASF GitHub Bot commented on ARROW-2118:
---------------------------------------

wesm closed pull request #1735: ARROW-2118: [C++] Fix misleading error when 
memory mapping a zero-length file
URL: https://github.com/apache/arrow/pull/1735
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/cpp/src/arrow/io/file.cc b/cpp/src/arrow/io/file.cc
index d44d90cbe..02cc4dbbd 100644
--- a/cpp/src/arrow/io/file.cc
+++ b/cpp/src/arrow/io/file.cc
@@ -624,16 +624,22 @@ class MemoryMappedFile::MemoryMap : public MutableBuffer {
       is_mutable_ = false;
     }
 
-    void* result = mmap(nullptr, static_cast<size_t>(file_->size()), 
prot_flags, map_mode,
-                        file_->fd(), 0);
-    if (result == MAP_FAILED) {
-      std::stringstream ss;
-      ss << "Memory mapping file failed, errno: " << errno;
-      return Status::IOError(ss.str());
+    size_ = file_->size();
+
+    void* result = nullptr;
+
+    // Memory mapping fails when file size is 0
+    if (size_ > 0) {
+      result =
+          mmap(nullptr, static_cast<size_t>(size_), prot_flags, map_mode, 
file_->fd(), 0);
+      if (result == MAP_FAILED) {
+        std::stringstream ss;
+        ss << "Memory mapping file failed: " << std::strerror(errno);
+        return Status::IOError(ss.str());
+      }
     }
 
     data_ = mutable_data_ = reinterpret_cast<uint8_t*>(result);
-    size_ = file_->size();
 
     position_ = 0;
 
diff --git a/cpp/src/arrow/io/io-file-test.cc b/cpp/src/arrow/io/io-file-test.cc
index 2a4acab59..53218ca85 100644
--- a/cpp/src/arrow/io/io-file-test.cc
+++ b/cpp/src/arrow/io/io-file-test.cc
@@ -467,6 +467,16 @@ class TestMemoryMappedFile : public ::testing::Test, 
public MemoryMapFixture {
 
 TEST_F(TestMemoryMappedFile, InvalidUsages) {}
 
+TEST_F(TestMemoryMappedFile, ZeroSizeFlie) {
+  std::string path = "io-memory-map-zero-size";
+  std::shared_ptr<MemoryMappedFile> result;
+  ASSERT_OK(InitMemoryMap(0, path, &result));
+
+  int64_t size = 0;
+  ASSERT_OK(result->Tell(&size));
+  ASSERT_EQ(0, size);
+}
+
 TEST_F(TestMemoryMappedFile, WriteRead) {
   const int64_t buffer_size = 1024;
   std::vector<uint8_t> buffer(buffer_size);
diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py
index fe680133b..8557340e0 100644
--- a/python/pyarrow/tests/test_io.py
+++ b/python/pyarrow/tests/test_io.py
@@ -592,6 +592,14 @@ def test_memory_map_writer(tmpdir):
     assert f.read(3) == b'foo'
 
 
+def test_memory_zero_length(tmpdir):
+    path = os.path.join(str(tmpdir), guid())
+    f = open(path, 'wb')
+    f.close()
+    with pa.memory_map(path, mode='r+b') as memory_map:
+        assert memory_map.size() == 0
+
+
 def test_os_file_writer(tmpdir):
     SIZE = 4096
     arr = np.random.randint(0, 256, size=SIZE).astype('u1')


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [Python] Improve error message when calling parquet.read_table on an empty 
> file
> -------------------------------------------------------------------------------
>
>                 Key: ARROW-2118
>                 URL: https://issues.apache.org/jira/browse/ARROW-2118
>             Project: Apache Arrow
>          Issue Type: Improvement
>          Components: Python
>            Reporter: Wes McKinney
>            Assignee: Wes McKinney
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.9.0
>
>
> Currently it raises an exception about memory mapping failing



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to