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

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

wesm commented on a change in pull request #1387: ARROW-1491: [C++] Add casting 
implementations from strings to numbers or boolean
URL: https://github.com/apache/arrow/pull/1387#discussion_r155318269
 
 

 ##########
 File path: cpp/src/arrow/compute/kernels/cast.cc
 ##########
 @@ -660,6 +663,100 @@ struct CastFunctor<T, DictionaryType,
   }
 };
 
+// ----------------------------------------------------------------------
+// String to Number
+
+template <typename O>
+struct CastFunctor<O, StringType,
+                   typename std::enable_if<std::is_base_of<Number, 
O>::value>::type> {
+  void operator()(FunctionContext* ctx, const CastOptions& options,
+                  const ArrayData& input, ArrayData* output) {
+    using out_type = typename O::c_type;
+    StringArray input_array(input.Copy());
+
+    if (input_array.null_count() > 0) {
+      std::stringstream ss;
+      ss << "Failed to cast NA into " << output->type->ToString();
+      ctx->SetStatus(Status(StatusCode::SerializationError, ss.str()));
+      return;
+    }
+
+    auto out_data = GetMutableValues<out_type>(output, 1);
+
+    std::function<out_type(const std::string&)> cast_func;
+    if (output->type->id() == Type::INT8 || output->type->id() == Type::UINT8) 
{
+      cast_func = [](const std::string& s) {
+        return boost::numeric_cast<out_type>(boost::lexical_cast<int>(s));
+      };
+    } else {
+      cast_func = [](const std::string& s) { return 
boost::lexical_cast<out_type>(s); };
+    }
+
+    for (int64_t i = 0; i < input.length; ++i) {
+      std::string s = input_array.GetString(i);
+
+      try {
+        *out_data++ = cast_func(s);
+      } catch (...) {
+        std::stringstream ss;
+        ss << "Failed to cast String '" << s << "' into " << 
output->type->ToString();
+        ctx->SetStatus(Status(StatusCode::SerializationError, ss.str()));
+        return;
+      }
+    }
+  }
+};
+
+// ----------------------------------------------------------------------
+// String to Boolean
+
+template <typename O>
+struct CastFunctor<O, StringType,
+                   typename std::enable_if<std::is_same<BooleanType, 
O>::value>::type> {
+  void operator()(FunctionContext* ctx, const CastOptions& options,
+                  const ArrayData& input, ArrayData* output) {
+    StringArray input_array(input.Copy());
+    internal::BitmapWriter writer(output->buffers[1]->mutable_data(), 
output->offset,
+                                  input.length);
+
+    if (input_array.null_count() > 0) {
+      std::stringstream ss;
+      ss << "Failed to cast NA into " << output->type->ToString();
+      ctx->SetStatus(Status(StatusCode::SerializationError, ss.str()));
 
 Review comment:
   If the input has nulls, then the output should have nulls in the same 
locations

----------------------------------------------------------------
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:
[email protected]


> [C++] Add casting implementations from strings to numbers or boolean
> --------------------------------------------------------------------
>
>                 Key: ARROW-1491
>                 URL: https://issues.apache.org/jira/browse/ARROW-1491
>             Project: Apache Arrow
>          Issue Type: New Feature
>          Components: C++
>            Reporter: Wes McKinney
>            Assignee: Licht Takeuchi
>              Labels: pull-request-available
>             Fix For: 0.9.0
>
>




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to