kou commented on code in PR #12914:
URL: https://github.com/apache/arrow/pull/12914#discussion_r942075017


##########
cpp/src/arrow/filesystem/azurefs.cc:
##########
@@ -0,0 +1,2025 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/filesystem/azurefs.h"
+
+#include <algorithm>
+#include <atomic>
+#include <chrono>
+#include <condition_variable>
+#include <functional>
+#include <memory>
+#include <mutex>
+#include <regex>
+#include <sstream>
+#include <thread>
+#include <unordered_map>
+#include <utility>
+
+#include <azure/core/credentials/credentials.hpp>
+#include <azure/identity/client_secret_credential.hpp>
+#include <azure/identity/managed_identity_credential.hpp>
+#include <azure/storage/blobs.hpp>
+#include <azure/storage/files/datalake.hpp>
+
+#include "arrow/util/windows_fixup.h"
+
+#include "arrow/buffer.h"
+#include "arrow/filesystem/filesystem.h"
+#include "arrow/filesystem/path_util.h"
+#include "arrow/filesystem/util_internal.h"
+#include "arrow/io/interfaces.h"
+#include "arrow/io/memory.h"
+#include "arrow/io/util_internal.h"
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/util/async_generator.h"
+#include "arrow/util/atomic_shared_ptr.h"
+#include "arrow/util/checked_cast.h"
+#include "arrow/util/future.h"
+#include "arrow/util/key_value_metadata.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/optional.h"
+#include "arrow/util/task_group.h"
+#include "arrow/util/thread_pool.h"
+
+namespace arrow {
+
+using internal::Uri;
+
+namespace fs {
+
+static const char kSep = '/';
+
+// -----------------------------------------------------------------------
+// AzureOptions implementation
+
+AzureOptions::AzureOptions() {}
+
+Result<std::string> AzureOptions::GetAccountNameFromConnectionString(
+    const std::string& connection_string) {
+  std::string text = "AccountName=";
+  auto pos_text = connection_string.find(text);
+  if (pos_text == std::string::npos) {
+    return Status::IOError(
+        "Cannot find account name in Azure Blob Storage connection string: '",
+        connection_string, "'");
+  }
+  auto pos_colon = connection_string.find(';');
+  pos_colon = connection_string.find(';', pos_colon + 1);
+  if (pos_colon == std::string::npos) {

Review Comment:
   I wanted to say that some valid inputs can't be parsed not some invalid 
inputs aren't detected.
   How about using `arrow::internal::SplitString()` to parse the connection 
string correctly?
   
   ```diff
   diff --git a/cpp/src/arrow/filesystem/azurefs.cc 
b/cpp/src/arrow/filesystem/azurefs.cc
   index c1712a4b1a..500ae29bce 100644
   --- a/cpp/src/arrow/filesystem/azurefs.cc
   +++ b/cpp/src/arrow/filesystem/azurefs.cc
   @@ -54,6 +54,7 @@
    #include "arrow/util/key_value_metadata.h"
    #include "arrow/util/logging.h"
    #include "arrow/util/optional.h"
   +#include "arrow/util/string.h"
    #include "arrow/util/task_group.h"
    #include "arrow/util/thread_pool.h"
    
   @@ -72,21 +73,19 @@ AzureOptions::AzureOptions() {}
    
    Result<std::string> AzureOptions::GetAccountNameFromConnectionString(
        const std::string& connection_string) {
   -  std::string text = "AccountName=";
   -  auto pos_text = connection_string.find(text);
   -  if (pos_text == std::string::npos) {
   -    return Status::Invalid(
   -        "Cannot find account name in Azure Blob Storage connection string: 
'",
   -        connection_string, "'");
   +  auto parameters = arrow::internal::SplitString(connection_string, ';');
   +  for (const auto& parameter : parameters) {
   +    auto key_value = arrow::internal::SplitString(parameter, '=', 2);
   +    if (key_value[0] == "AccountName") {
   +      if (key_value.size() == 1) {
   +        break;
   +      }
   +      return std::string(key_value[1]);
   +    }
      }
   -  auto pos_semicolon = connection_string.find(';');
   -  pos_semicolon = connection_string.find(';', pos_semicolon + 1);
   -  if (pos_semicolon == std::string::npos) {
   -    return Status::Invalid("Invalid Azure Blob Storage connection string: 
'",
   -                           connection_string, "' passed");
   -  }
   -  std::string account_name = connection_string.substr(pos_text + 
text.size(), pos_semicolon);
   -  return account_name;
   +  return Status::Invalid(
   +      "Cannot find account name in Azure Blob Storage connection string: '",
   +      connection_string, "'");
    }
    
    Status AzureOptions::ConfigureAnonymousCredentials(const std::string& 
account_name) {
   diff --git a/cpp/src/arrow/util/string.cc b/cpp/src/arrow/util/string.cc
   index 3a15860055..00ab8e64c4 100644
   --- a/cpp/src/arrow/util/string.cc
   +++ b/cpp/src/arrow/util/string.cc
   @@ -92,11 +92,16 @@ Status ParseHexValue(const char* data, uint8_t* out) {
    
    namespace internal {
    
   -std::vector<util::string_view> SplitString(util::string_view v, char 
delimiter) {
   +std::vector<util::string_view> SplitString(util::string_view v, char 
delimiter,
   +                                           int64_t limit) {
      std::vector<util::string_view> parts;
      size_t start = 0, end;
      while (true) {
   -    end = v.find(delimiter, start);
   +    if (limit > 0 && static_cast<size_t>(limit - 1) <= parts.size()) {
   +      end = std::string::npos;
   +    } else {
   +      end = v.find(delimiter, start);
   +    }
        parts.push_back(v.substr(start, end - start));
        if (end == std::string::npos) {
          break;
   diff --git a/cpp/src/arrow/util/string.h b/cpp/src/arrow/util/string.h
   index d2c8ac38ee..b2baa0ebed 100644
   --- a/cpp/src/arrow/util/string.h
   +++ b/cpp/src/arrow/util/string.h
   @@ -45,7 +45,8 @@ namespace internal {
    
    /// \brief Split a string with a delimiter
    ARROW_EXPORT
   -std::vector<util::string_view> SplitString(util::string_view v, char delim);
   +std::vector<util::string_view> SplitString(util::string_view v, char delim,
   +                                           int64_t limit = 0);
    
    /// \brief Join strings with a delimiter
    ARROW_EXPORT
   diff --git a/cpp/src/arrow/util/string_test.cc 
b/cpp/src/arrow/util/string_test.cc
   index 057d885fcd..2aa6fccbd9 100644
   --- a/cpp/src/arrow/util/string_test.cc
   +++ b/cpp/src/arrow/util/string_test.cc
   @@ -140,5 +140,31 @@ TEST(SplitString, OnlyDemiliter) {
      EXPECT_EQ(parts[1], "");
    }
    
   +TEST(SplitString, Limit) {
   +  std::string input = "a:b:c";
   +  auto parts = SplitString(input, ':', 2);
   +  ASSERT_EQ(parts.size(), 2);
   +  EXPECT_EQ(parts[0], "a");
   +  EXPECT_EQ(parts[1], "b:c");
   +}
   +
   +TEST(SplitString, LimitOver) {
   +  std::string input = "a:b:c";
   +  auto parts = SplitString(input, ':', 4);
   +  ASSERT_EQ(parts.size(), 3);
   +  EXPECT_EQ(parts[0], "a");
   +  EXPECT_EQ(parts[1], "b");
   +  EXPECT_EQ(parts[2], "c");
   +}
   +
   +TEST(SplitString, LimitZero) {
   +  std::string input = "a:b:c";
   +  auto parts = SplitString(input, ':', 0);
   +  ASSERT_EQ(parts.size(), 3);
   +  EXPECT_EQ(parts[0], "a");
   +  EXPECT_EQ(parts[1], "b");
   +  EXPECT_EQ(parts[2], "c");
   +}
   +
    }  // namespace internal
    }  // namespace arrow
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to