deniskuzZ commented on code in PR #5624:
URL: https://github.com/apache/hive/pull/5624#discussion_r2009123676


##########
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSubstr.java:
##########
@@ -96,53 +96,131 @@ private Text evaluateInternal(Text t, int pos, int len) {
       return r;
     }
 
-    String s = t.toString();
-    int[] index = makeIndex(pos, len, s.length());
-    if (index == null) {
+    byte[] utf8String = t.toString().getBytes();
+    populateSubstrOffsets(utf8String, pos, len);
+    if (index[0] == -1) {
       return r;
     }
 
-    r.set(s.substring(index[0], index[1]));
+    r.set(new String(utf8String, index[0], index[1]));
     return r;
   }
 
-  private int[] makeIndex(int pos, int len, int inputLen) {
-    if ((Math.abs(pos) > inputLen)) {
-      return null;
+  private Text evaluateInternal(Text t, int pos) {
+    r.clear();
+
+    byte[] utf8String = t.toString().getBytes();
+    int offset = getSubstrStartOffset(utf8String, pos);
+    if (offset == -1) {
+      return r;
     }
 
-    int start, end;
+    r.set(new String(utf8String, offset, utf8String.length - offset));
+    return r;
+  }
+
+  private void populateSubstrOffsets(byte[] utf8String, int start, int len) {
+    int curIdx = -1;
+    index[0] = -1;
+    index[1] = -1;
+    int end = utf8String.length;
+
+    if (start > 0) {
+      start = start - 1;
+    } else if (start < 0) {
+      int length = 0;
+      for (int i = 0; i != end; ++i) {
+        if ((utf8String[i] & 0xc0) != 0x80) {
+          ++length;
+        }
+      }
+
+      if (-start > length) {
+        return;
+      }
+
+      start = length + start;
+    }
+
+    if (len == 0) {
+      return;
+    } else if (len > end) {
+      len = end;
+    }
 
-    if (pos > 0) {
-      start = pos - 1;
-    } else if (pos < 0) {
-      start = inputLen + pos;
-    } else {
-      start = 0;
+    int endIdx = start + len - 1;
+    for (int i = 0; i != end; ++i) {
+      if ((utf8String[i] & 0xc0) != 0x80) {
+        ++curIdx;
+        if (curIdx == start) {
+          index[0] = i;
+        } else if (curIdx - 1 == endIdx) {
+          index[1] = i - index[0];
+        }
+      }
     }
 
-    if ((inputLen - start) < len) {
-      end = inputLen;
-    } else {
-      end = start + len;
+    if (index[1] == -1) {
+      index[1] = end - index[0];
     }
-    index[0] = start;
-    index[1] = end;
-    return index;
   }
 
-  private final IntWritable maxValue = new IntWritable(Integer.MAX_VALUE);
+  private int getSubstrStartOffset(byte[] utf8String, int start) {

Review Comment:
   same here



-- 
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: gitbox-unsubscr...@hive.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to