vvellanki commented on code in PR #12716:
URL: https://github.com/apache/arrow/pull/12716#discussion_r849732089


##########
cpp/src/gandiva/precompiled/string_ops.cc:
##########
@@ -2815,43 +2814,62 @@ const char* soundex_utf8(gdv_int64 ctx, const char* in, 
gdv_int32 in_len,
   }
 
   // The soundex code is composed by one letter and three numbers
+  char* soundex = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(ctx, 
in_len));
   char* ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(ctx, 4));
-  if (ret == nullptr) {
+
+  if (soundex == nullptr || ret == nullptr) {
     gdv_fn_context_set_error_msg(ctx, "Could not allocate memory for output 
string");
     *out_len = 0;
     return "";
   }
 
   int si = 1;
+  int ret_len = 1;
   unsigned char c;
 
   int start_idx = 0;
   for (int i = 0; i < in_len; ++i) {
     if (isalpha(in[i]) > 0) {
+      // Retain the first letter
       ret[0] = toupper(in[i]);
       start_idx = i + 1;
       break;
     }
   }
 
-  for (int i = start_idx, l = in_len; i < l; i++) {
+  soundex[0] = '\0';
+  // Replace consonants with digits and special letters with 0
+  for (int i = start_idx; i < in_len; i++) {
     if (isalpha(in[i]) > 0) {
       c = toupper(in[i]) - 65;
-      if (mappings[c] != '0') {
-        if (mappings[c] != ret[si - 1]) {
-          ret[si] = mappings[c];
-          si++;
-        }
-
-        if (si > 3) break;
+      if (mappings[c] != soundex[si - 1]) {
+        soundex[si] = mappings[c];
+        si++;
       }
     }
   }
 
-  if (si <= 3) {
-    while (si <= 3) {
-      ret[si] = '0';
-      si++;
+  int i = 1;
+  // If the saved letter's digit is the same as the resulting first digit, 
remove the
+  // digit.
+  if (soundex[i] == mappings[ret[0] - 65]) {

Review Comment:
   You have to handle the case where ret[0] is not initialised.. for e.g. the 
input is '123456' - since there are no alphabets, ret[0] will not be initialised
   
   Similarly, you have to handle the case where soundex[1] is not set.. 



##########
cpp/src/gandiva/precompiled/string_ops_test.cc:
##########
@@ -2310,12 +2310,49 @@ TEST(TestStringOps, TestFromHex) {
       ::testing::HasSubstr("Error parsing hex string, one or more bytes are 
not valid."));
   ctx.Reset();
 }
+
 TEST(TestStringOps, TestSoundex) {
   gandiva::ExecutionContext ctx;
   auto ctx_ptr = reinterpret_cast<int64_t>(&ctx);
   int32_t out_len = 0;
   const char* out;
 
+  out = soundex_utf8(ctx_ptr, "robert", 6, &out_len);

Review Comment:
   Add a test case where the input is all numbers
   Add a test case where there is only alphabet and the rest are numbers



##########
cpp/src/gandiva/precompiled/string_ops.cc:
##########
@@ -2789,20 +2789,19 @@ static char mappings[] = {'0', '1', '2', '3', '0', '1', 
'2', '0', '0',
 // the input string followed by a phonetic code. Characters that are not 
alphabetic are
 // ignored. If expression evaluates to the null value, null is returned.
 //
-// The soundex algorith works with the following steps:
+// The soundex algorithm works with the following steps:
 //    1. Retain the first letter of the string and drop all other occurrences 
of a, e, i,
-//    o, u, y, h, w.
+//    o, u, y, h, w. (let's call them special letters)
 //    2. Replace consonants with digits as follows (after the first letter):
 //        b, f, p, v → 1
 //        c, g, j, k, q, s, x, z → 2
 //        d, t → 3
 //        l → 4
 //        m, n → 5
 //        r → 6
-//    3. If two or more letters with the same number are adjacent in the 
original name
-//    (before step 1), only retain the first letter; also two letters with the 
same number
-//    separated by 'h' or 'w' are coded as a single number, whereas such 
letters separated
-//    by a vowel are coded twice. This rule also applies to the first letter.
+//    3. If two or more letters with the same number were adjacent in the 
original name
+//    (before step 1), or adjacent for any intervening from any special 
letters, then omit

Review Comment:
   What does "or adjacent for any intervening from any special letters" mean?



##########
cpp/src/gandiva/precompiled/string_ops.cc:
##########
@@ -2815,43 +2814,62 @@ const char* soundex_utf8(gdv_int64 ctx, const char* in, 
gdv_int32 in_len,
   }
 
   // The soundex code is composed by one letter and three numbers
+  char* soundex = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(ctx, 
in_len));
   char* ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(ctx, 4));
-  if (ret == nullptr) {
+
+  if (soundex == nullptr || ret == nullptr) {
     gdv_fn_context_set_error_msg(ctx, "Could not allocate memory for output 
string");
     *out_len = 0;
     return "";
   }
 
   int si = 1;
+  int ret_len = 1;
   unsigned char c;
 
   int start_idx = 0;
   for (int i = 0; i < in_len; ++i) {
     if (isalpha(in[i]) > 0) {
+      // Retain the first letter
       ret[0] = toupper(in[i]);
       start_idx = i + 1;
       break;
     }
   }
 
-  for (int i = start_idx, l = in_len; i < l; i++) {
+  soundex[0] = '\0';
+  // Replace consonants with digits and special letters with 0
+  for (int i = start_idx; i < in_len; i++) {
     if (isalpha(in[i]) > 0) {
       c = toupper(in[i]) - 65;
-      if (mappings[c] != '0') {
-        if (mappings[c] != ret[si - 1]) {
-          ret[si] = mappings[c];
-          si++;
-        }
-
-        if (si > 3) break;
+      if (mappings[c] != soundex[si - 1]) {
+        soundex[si] = mappings[c];
+        si++;
       }
     }
   }
 
-  if (si <= 3) {
-    while (si <= 3) {
-      ret[si] = '0';
-      si++;
+  int i = 1;
+  // If the saved letter's digit is the same as the resulting first digit, 
remove the
+  // digit.
+  if (soundex[i] == mappings[ret[0] - 65]) {

Review Comment:
   Please change this to:
   if (soundex[1] == mapp) {
    i = 2;
   }



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