JingsongLi commented on a change in pull request #8029: [FLINK-11898] 
[table-planner-blink] Support code generation for all Blink built-in functions 
and operators
URL: https://github.com/apache/flink/pull/8029#discussion_r268526530
 
 

 ##########
 File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/util/SegmentsUtil.java
 ##########
 @@ -987,4 +1010,82 @@ private static void setTwoByteSlowly(
                segment.put(segOffset, (byte) (LITTLE_ENDIAN ? b2 : b1));
        }
 
+       /**
+        * Copy bytes of segments to output view.
+        * Note: It just copies the data in, not include the length.
+        *
+        * @param segments source segments
+        * @param offset offset for segments
+        * @param sizeInBytes size in bytes
+        * @param target target output view
+        */
+       public static void copyBytesToView(MemorySegment[] segments, int offset,
+                       int sizeInBytes, DataOutputView target) throws 
IOException {
+               for (MemorySegment sourceSegment : segments) {
+                       int curSegRemain = sourceSegment.size() - offset;
+                       if (curSegRemain > 0) {
+                               int copySize = Math.min(curSegRemain, 
sizeInBytes);
+
+                               // TODO after FLINK-11724
+                               byte[] bytes = new byte[copySize];
+                               sourceSegment.get(offset, bytes);
+                               target.write(bytes);
+
+                               sizeInBytes -= copySize;
+                               offset = 0;
+                       } else {
+                               offset -= sourceSegment.size();
+                       }
+
+                       if (sizeInBytes == 0) {
+                               return;
+                       }
+               }
+
+               if (sizeInBytes != 0) {
+                       throw new RuntimeException("No copy finished, this 
should be a bug, " +
+                                       "The remaining length is: " + 
sizeInBytes);
+               }
+       }
+
+       /**
+        * Find equal segments2 in segments1.
+        * @param segments1 segs to find.
+        * @param segments2 sub segs.
+        * @return Return the found offset, return -1 if not find.
+        */
+       public static int find(
+               MemorySegment[] segments1, int offset1, int numBytes1,
+               MemorySegment[] segments2, int offset2, int numBytes2) {
+               if (numBytes2 == 0) { // quick way 1.
+                       return offset1;
+               }
+               if (inFirstSegment(segments1, offset1, numBytes1) &&
+                       inFirstSegment(segments2, offset2, numBytes2)) {
+                       byte first = segments2[0].get(offset2);
+                       int end = numBytes1 - numBytes2 + offset1;
+                       for (int i = offset1; i <= end; i++) {
+                               // quick way 2: equal first byte.
+                               if (segments1[0].get(i) == first &&
+                                       segments1[0].equalTo(segments2[0], i, 
offset2, numBytes2)) {
+                                       return i;
+                               }
+                       }
+                       return -1;
+               } else {
+                       return findSlow(segments1, offset1, numBytes1, 
segments2, offset2, numBytes2);
+               }
+       }
+
+       private static int findSlow(
 
 Review comment:
   findInMultiSegments?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to