You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/02/01 16:54:00 UTC

[jira] [Commented] (ARROW-1491) [C++] Add casting implementations from strings to numbers or boolean

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

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

cpcloud 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_r165417551
 
 

 ##########
 File path: cpp/src/arrow/compute/kernels/cast.cc
 ##########
 @@ -703,6 +706,106 @@ struct CastFunctor<T, DictionaryType,
   }
 };
 
+// ----------------------------------------------------------------------
+// String to Number
+
+template <typename T>
+typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, int8_t>::value &&
+                            !std::is_same<T, uint8_t>::value,
+                        T>::type
+castStringToNumeric(const std::string& s) {
+  return boost::lexical_cast<T>(s);
+}
+
+template <typename T>
+typename std::enable_if<std::is_same<T, int8_t>::value || std::is_same<T, uint8_t>::value,
+                        T>::type
+castStringToNumeric(const std::string& s) {
+  // Convert to int before casting to T
+  // because boost::lexical_cast does not support 8bit int/uint.
+  return boost::numeric_cast<T>(boost::lexical_cast<int>(s));
+}
+
+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());
+
+    auto out_data = GetMutableValues<out_type>(output, 1);
+
+    std::function<out_type(const std::string&)> cast_func;
+
+    for (int64_t i = 0; i < input.length; ++i) {
+      if (input_array.IsNull(i)) {
+        out_data++;
+        continue;
+      }
+
+      std::string s = input_array.GetString(i);
+
+      try {
+        *out_data++ = castStringToNumeric<out_type>(s);
+      } catch (...) {
 
 Review comment:
   I'm concerned about propagating the actual error message instead of just saying "Cast from X to Y failed".

----------------------------------------------------------------
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:
users@infra.apache.org


> [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
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.9.0
>
>




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