You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2022/12/30 20:13:38 UTC

[GitHub] [pinot] richardstartin opened a new pull request, #10044: push unsigned byte comparison down into ValueReader

richardstartin opened a new pull request, #10044:
URL: https://github.com/apache/pinot/pull/10044

   This pushes the unsigned comparison operation down into the `ValueReader` so that a string doesn't need to be materialised to do the comparison. For `FixedByteValueReaderWriter`, which has showed up as a CPU bottleneck in pretty much every profile of Pinot query evaluation I have looked at, because the work done to find where the string ends within the fixed width bucket before copying the string can be skipped. I will mark this as ready for review once I've run some benchmarks to demonstrate the impact of the change.


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on PR #10044:
URL: https://github.com/apache/pinot/pull/10044#issuecomment-1373383973

   > > I think @klsince's question needs addressing - what happens when there are two invalid UTF8 sequences at the mismatch, can we return 0 spuriously, I'll address that before merging.
   > 
   > Could that ever happen? All the UTF-8 are encoded from strings.
   
   It can’t happen because at the very least the parameter came from the query layer as a string. I can’t think of a way to break this logic except when the behaviour is undefined for the string class itself. When I first implemented a simpler version of this, a lot of test cases elsewhere in Pinot failed, even after my own tests which generated a comprehensive range of UTF8 sequences was passing, so the test suite is quite sensitive to this change, and is passing now.


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061013153


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {

Review Comment:
   (Code format) Please fix the indentation



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;

Review Comment:
   Why `~0x3F` instead of `~7`?



##########
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/segment/index/readerwriter/ValueReaderComparisonTest.java:
##########
@@ -0,0 +1,330 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.segment.index.readerwriter;
+
+import java.io.IOException;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.pinot.segment.local.io.util.FixedByteValueReaderWriter;
+import org.apache.pinot.segment.local.io.util.ValueReader;
+import org.apache.pinot.segment.local.io.util.VarLengthValueReader;
+import org.apache.pinot.segment.local.io.util.VarLengthValueWriter;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+public class ValueReaderComparisonTest {
+    @DataProvider
+    public static Object[] text() {

Review Comment:
   (Code format) Please fix the indentation



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {
+                    // just need nonzero first byte
+                    return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+                } else if (bytes[bytes.length - 1] == 0) {
+                    // we can't store zero padding, but can have it in parameters, check the last byte
+                    return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+                } else {
+                    // check if the stored string continues beyond the length of the parameter
+                    return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+                }
+            } else {
+                // then we know the length precisely or know that the parameter is at least as long as we can store
+                return length - bytes.length;
+            }
+        }
+        byte ourByte = dataBuffer.getByte(startOffset + mismatchPosition);
+        if (ourByte == 0) {
+            // then we are storing the empty string
+            return -1;
+        }
+        byte theirByte = bytes[mismatchPosition];
+        if (theirByte == 0) {
+            return 1;
+        }

Review Comment:
   I don't think we need these special handling since they are not very common case. We may change `first > 0` to `first >= 0` to handle the 0 byte in `compareUtf8()`



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {
+                    // just need nonzero first byte
+                    return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+                } else if (bytes[bytes.length - 1] == 0) {
+                    // we can't store zero padding, but can have it in parameters, check the last byte
+                    return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+                } else {
+                    // check if the stored string continues beyond the length of the parameter
+                    return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+                }
+            } else {
+                // then we know the length precisely or know that the parameter is at least as long as we can store
+                return length - bytes.length;
+            }
+        }
+        byte ourByte = dataBuffer.getByte(startOffset + mismatchPosition);
+        if (ourByte == 0) {
+            // then we are storing the empty string
+            return -1;
+        }
+        byte theirByte = bytes[mismatchPosition];
+        if (theirByte == 0) {
+            return 1;
+        }
+        // we know the position of the mismatch but need to do utf8 decoding before comparison
+        // to respect collation rules
+        return compareUtf8(dataBuffer, startOffset, buffer, mismatchPosition);
+    }
+
+    private static int compareUtf8(PinotDataBuffer ourBuffer, long ourStartOffset,
+                           ByteBuffer theirBuffer, int mismatchPosition) {
+        char ours1 = '?';
+        char ours2 = '?';
+        char theirs1 = '?';
+        char theirs2 = '?';
+
+        // 1. seek backwards from mismatch position in each buffer to find start of each utf8 sequence
+        long ourPosition = ourStartOffset + mismatchPosition;
+        while (ourPosition > ourStartOffset && isUtf8Continuation(ourBuffer.getByte(ourPosition))) {

Review Comment:
   The boundary check is redundant. We can also move both our and their position together for better performance



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {
+                    // just need nonzero first byte
+                    return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+                } else if (bytes[bytes.length - 1] == 0) {
+                    // we can't store zero padding, but can have it in parameters, check the last byte
+                    return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+                } else {
+                    // check if the stored string continues beyond the length of the parameter
+                    return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+                }
+            } else {
+                // then we know the length precisely or know that the parameter is at least as long as we can store
+                return length - bytes.length;
+            }
+        }
+        byte ourByte = dataBuffer.getByte(startOffset + mismatchPosition);
+        if (ourByte == 0) {
+            // then we are storing the empty string
+            return -1;
+        }
+        byte theirByte = bytes[mismatchPosition];
+        if (theirByte == 0) {
+            return 1;
+        }
+        // we know the position of the mismatch but need to do utf8 decoding before comparison
+        // to respect collation rules
+        return compareUtf8(dataBuffer, startOffset, buffer, mismatchPosition);
+    }
+
+    private static int compareUtf8(PinotDataBuffer ourBuffer, long ourStartOffset,
+                           ByteBuffer theirBuffer, int mismatchPosition) {
+        char ours1 = '?';
+        char ours2 = '?';
+        char theirs1 = '?';
+        char theirs2 = '?';
+
+        // 1. seek backwards from mismatch position in each buffer to find start of each utf8 sequence
+        long ourPosition = ourStartOffset + mismatchPosition;
+        while (ourPosition > ourStartOffset && isUtf8Continuation(ourBuffer.getByte(ourPosition))) {
+            ourPosition--;
+        }
+        int theirPosition = mismatchPosition;
+        while (theirPosition > 0 && isUtf8Continuation(theirBuffer.get(theirPosition))) {
+            theirPosition--;
+        }
+        // 2. decode to get the 1 or 2 characters containing where the mismatch lies
+        {
+            byte first = ourBuffer.getByte(ourPosition);
+            int control = first & 0xF0;
+            if (first > 0) {
+                ours1 = (char) (first & 0xFF);
+            } else if (control < 0xE0) {
+                ours1 = decode(first, ourBuffer.getByte(ourPosition + 1));
+            } else if (control == 0xE0) {
+                ours1 = decode(first, ourBuffer.getByte(ourPosition + 1),
+                        ourBuffer.getByte(ourPosition + 2));
+            } else {
+                int codepoint = decode(first, ourBuffer.getByte(ourPosition + 1),
+                        ourBuffer.getByte(ourPosition + 2), ourBuffer.getByte(ourPosition + 3));
+                if (Character.isValidCodePoint(codepoint)) {

Review Comment:
   Does the logic handle the case when the code point is invalid for one value but valid for the other? I feel using `'?'` as the missing character can cause unexpected behavior in such case.



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {

Review Comment:
   Since we don't allow zero byte in unpadded string, IMO we don't need to handle comparison with trailing zero byte in parameter, and we can simplify it by always `return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;`. Essentially we count them as equal when the comparison value has trailing zero.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061970177


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+
+public class ValueReaderComparisons {
+
+  private ValueReaderComparisons() {
+  }
+
+  private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+    boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+    if (littleEndian) {
+      buffer.order(ByteOrder.LITTLE_ENDIAN);
+    }
+    int limit = Math.min(length, buffer.limit());
+    int loopBound = limit & ~0x7;
+    int i = 0;
+    for (; i < loopBound; i += 8) {
+      long ours = dataBuffer.getLong(startOffset + i);
+      long theirs = buffer.getLong(i);
+      if (ours != theirs) {
+        long difference = ours ^ theirs;
+        return i + ((littleEndian ? Long.numberOfTrailingZeros(difference) : Long.numberOfLeadingZeros(difference))
+            >>> 3);
+      }
+    }
+    for (; i < limit; i++) {
+      byte ours = dataBuffer.getByte(startOffset + i);
+      byte theirs = buffer.get(i);
+      if (ours != theirs) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded, byte[] bytes) {
+    // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+    ByteBuffer buffer = ByteBuffer.wrap(bytes);
+    int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+    if (mismatchPosition == -1) {
+      if (padded && bytes.length < length) {
+        // need to figure out whether the unpadded string is longer than the parameter or not
+        if (bytes.length == 0) {
+          // just need nonzero first byte
+          return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+        } else if (bytes[bytes.length - 1] == 0) {
+          // we can't store zero except as padding, but can have it in parameters, so check the last byte
+          return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+        } else {
+          // check if the stored string continues beyond the length of the parameter
+          return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+        }
+      } else {
+        // then we know the length precisely or know that the parameter is at least as long as we can store
+        return length - bytes.length;
+      }
+    }
+    // we know the position of the mismatch but need to do utf8 decoding before comparison
+    // to respect collation rules

Review Comment:
   Hi @klsince it's mostly to do with comparing invalid UTF8 with non ASCII text. For example, comparing the strings 
   "𠜎" (UTF8=f0a09c8e) and the garbage string which renders as "�ػ" (UTF8=efbfbdd8bb) then there is a mismatch at zero, but 
   ```java
   Byte.compareUnsigned((byte)0xf0, (byte)0xef);
   ``` 
   gives you -1, but 
   ```java
   new String(BytesUtils.toByteArray("f0a09c8e"), StandardCharsets.UTF_8).compareTo(new String(BytesUtils.toByteArray("efbfbdd8bb"));
   ``` 
   agrees with the implementation on this branch that the comparison is 10172, because f0a09c8e is a valid UTF8 sequence and efbfbdd8bb isn't.
   
   As for whether `compareUtf8` can return zero, that's a good question. It could return zero for two invalid UTF8 sequences, so it will need more testing against `String`. Thanks for raising this.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061263198


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {

Review Comment:
   I thought so too, but there are pre-existing test cases for this, e.g. `LoaderTest.testPadding` so I added this logic to pass those tests.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on PR #10044:
URL: https://github.com/apache/pinot/pull/10044#issuecomment-1371990042

   > LGTM. Thanks for adding the tests!
   
   I think @klsince's question needs addressing - what happens when there are two invalid UTF8 sequences at the mismatch, can we return 0 spuriously, I'll address that before merging.


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push unsigned byte comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1059521413


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReader.java:
##########
@@ -20,6 +20,9 @@
 
 import java.io.Closeable;
 import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;

Review Comment:
   there might be a better place for the shared logic than a static on this interface, which would keep `PinotDataBuffer` out of this interface. I'm open to suggestions.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061282306


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {

Review Comment:
   I just don't have my IDE set up like yours, and I tweaked the formatting manually until checkstyle passed. Has the project considered auto formatting with spotless as suggested [here](https://github.com/apache/pinot/pull/10000#issuecomment-1355938476)? I use it on other projects and it takes the pain out of maintaining consistent style without synchronising IDEs (not everyone uses IntelliJ) and IDE settings globally.
   
   In the short term I will find the IDE settings and reformat the files I've changed 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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on pull request #10044: push unsigned byte comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on PR #10044:
URL: https://github.com/apache/pinot/pull/10044#issuecomment-1368102134

   It seems there are some corner cases to address so benchmarking is premature


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push unsigned byte comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1059521413


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReader.java:
##########
@@ -20,6 +20,9 @@
 
 import java.io.Closeable;
 import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;

Review Comment:
   there might be a better place for the shared logic than a static on this interface, which would keep `PinotDataBuffer` out of this interface. I'm open to suggestions.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1062520494


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+
+public class ValueReaderComparisons {
+
+  private ValueReaderComparisons() {
+  }
+
+  private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+    boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+    if (littleEndian) {
+      buffer.order(ByteOrder.LITTLE_ENDIAN);
+    }
+    int limit = Math.min(length, buffer.limit());
+    int loopBound = limit & ~0x7;
+    int i = 0;
+    for (; i < loopBound; i += 8) {
+      long ours = dataBuffer.getLong(startOffset + i);
+      long theirs = buffer.getLong(i);
+      if (ours != theirs) {
+        long difference = ours ^ theirs;
+        return i + ((littleEndian ? Long.numberOfTrailingZeros(difference) : Long.numberOfLeadingZeros(difference))
+            >>> 3);
+      }
+    }
+    for (; i < limit; i++) {
+      byte ours = dataBuffer.getByte(startOffset + i);
+      byte theirs = buffer.get(i);
+      if (ours != theirs) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded, byte[] bytes) {
+    // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+    ByteBuffer buffer = ByteBuffer.wrap(bytes);
+    int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+    if (mismatchPosition == -1) {
+      if (padded && bytes.length < length) {
+        // need to figure out whether the unpadded string is longer than the parameter or not
+        if (bytes.length == 0) {
+          // just need nonzero first byte
+          return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+        } else if (bytes[bytes.length - 1] == 0) {
+          // we can't store zero except as padding, but can have it in parameters, so check the last byte
+          return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+        } else {
+          // check if the stored string continues beyond the length of the parameter
+          return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+        }
+      } else {
+        // then we know the length precisely or know that the parameter is at least as long as we can store
+        return length - bytes.length;
+      }
+    }
+    // we know the position of the mismatch but need to do utf8 decoding before comparison
+    // to respect collation rules

Review Comment:
   I just added more testing and I can't make `compareUtf8` produce zero if either the stored data or the parameter were obtained from `String` instance.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061263982


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {
+                    // just need nonzero first byte
+                    return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+                } else if (bytes[bytes.length - 1] == 0) {
+                    // we can't store zero padding, but can have it in parameters, check the last byte
+                    return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+                } else {
+                    // check if the stored string continues beyond the length of the parameter
+                    return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+                }
+            } else {
+                // then we know the length precisely or know that the parameter is at least as long as we can store
+                return length - bytes.length;
+            }
+        }
+        byte ourByte = dataBuffer.getByte(startOffset + mismatchPosition);
+        if (ourByte == 0) {
+            // then we are storing the empty string
+            return -1;
+        }
+        byte theirByte = bytes[mismatchPosition];
+        if (theirByte == 0) {
+            return 1;
+        }

Review Comment:
   Yes, that's a good simplification.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061262121


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;

Review Comment:
   Good catch, this is a debugging artifact.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] klsince commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
klsince commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061898335


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+
+public class ValueReaderComparisons {
+
+  private ValueReaderComparisons() {
+  }
+
+  private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+    boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+    if (littleEndian) {
+      buffer.order(ByteOrder.LITTLE_ENDIAN);
+    }
+    int limit = Math.min(length, buffer.limit());
+    int loopBound = limit & ~0x7;
+    int i = 0;
+    for (; i < loopBound; i += 8) {
+      long ours = dataBuffer.getLong(startOffset + i);
+      long theirs = buffer.getLong(i);
+      if (ours != theirs) {
+        long difference = ours ^ theirs;
+        return i + ((littleEndian ? Long.numberOfTrailingZeros(difference) : Long.numberOfLeadingZeros(difference))
+            >>> 3);
+      }
+    }
+    for (; i < limit; i++) {
+      byte ours = dataBuffer.getByte(startOffset + i);
+      byte theirs = buffer.get(i);
+      if (ours != theirs) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded, byte[] bytes) {
+    // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+    ByteBuffer buffer = ByteBuffer.wrap(bytes);
+    int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+    if (mismatchPosition == -1) {
+      if (padded && bytes.length < length) {
+        // need to figure out whether the unpadded string is longer than the parameter or not
+        if (bytes.length == 0) {
+          // just need nonzero first byte
+          return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+        } else if (bytes[bytes.length - 1] == 0) {
+          // we can't store zero except as padding, but can have it in parameters, so check the last byte
+          return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+        } else {
+          // check if the stored string continues beyond the length of the parameter
+          return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+        }
+      } else {
+        // then we know the length precisely or know that the parameter is at least as long as we can store
+        return length - bytes.length;
+      }
+    }
+    // we know the position of the mismatch but need to do utf8 decoding before comparison
+    // to respect collation rules

Review Comment:
   Could you help add an example in the comment to help understand the need of compareUtf8()? 
   
   Would compareUtf8() return 0? If so, is there need to compare the remaining bytes after the mismatchPosition? 



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang commented on pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on PR #10044:
URL: https://github.com/apache/pinot/pull/10044#issuecomment-1372916301

   > I think @klsince's question needs addressing - what happens when there are two invalid UTF8 sequences at the mismatch, can we return 0 spuriously, I'll address that before merging.
   
   Could that ever happen? All the UTF-8 are encoded from strings.


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push unsigned byte comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1059802361


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BaseImmutableDictionary.java:
##########
@@ -259,8 +259,7 @@ protected int binarySearch(byte[] value) {
 
     while (low <= high) {
       int mid = (low + high) >>> 1;
-      byte[] midValue = _valueReader.getBytes(mid, _numBytesPerValue);
-      int compareResult = ByteArray.compare(midValue, value);
+      int compareResult = _valueReader.compareUnsignedBytes(mid, _numBytesPerValue, value);

Review Comment:
   The existing logic here appears to be broken for fixed length `FixedByteValueReaderWriter`: it compares `value` with the _padded_ stored value retrieved by `ValueReader.getBytes`. This comparison will break ties using the lengths when mismatches can't be found, meaning the comparison will never yield zero unless `value` is also padded. I can only find testing for this method with byte arrays with the same size, so this problem would not arise in tests, but I can't find where the padding required to make this work with arbitrary length byte arrays is taking place.
   
   I decided to roll this change back as addressing this problem is bigger than the scope of the change I wanted to make, and `STRING` columns are much more common than `BYTES`. I refactored the code to make adding an unsigned comparison trivial once this has been resolved.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061330332


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {
+                    // just need nonzero first byte
+                    return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+                } else if (bytes[bytes.length - 1] == 0) {
+                    // we can't store zero padding, but can have it in parameters, check the last byte
+                    return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+                } else {
+                    // check if the stored string continues beyond the length of the parameter
+                    return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+                }
+            } else {
+                // then we know the length precisely or know that the parameter is at least as long as we can store
+                return length - bytes.length;
+            }
+        }
+        byte ourByte = dataBuffer.getByte(startOffset + mismatchPosition);
+        if (ourByte == 0) {
+            // then we are storing the empty string
+            return -1;
+        }
+        byte theirByte = bytes[mismatchPosition];
+        if (theirByte == 0) {
+            return 1;
+        }
+        // we know the position of the mismatch but need to do utf8 decoding before comparison
+        // to respect collation rules
+        return compareUtf8(dataBuffer, startOffset, buffer, mismatchPosition);
+    }
+
+    private static int compareUtf8(PinotDataBuffer ourBuffer, long ourStartOffset,
+                           ByteBuffer theirBuffer, int mismatchPosition) {
+        char ours1 = '?';
+        char ours2 = '?';
+        char theirs1 = '?';
+        char theirs2 = '?';
+
+        // 1. seek backwards from mismatch position in each buffer to find start of each utf8 sequence
+        long ourPosition = ourStartOffset + mismatchPosition;
+        while (ourPosition > ourStartOffset && isUtf8Continuation(ourBuffer.getByte(ourPosition))) {
+            ourPosition--;
+        }
+        int theirPosition = mismatchPosition;
+        while (theirPosition > 0 && isUtf8Continuation(theirBuffer.get(theirPosition))) {
+            theirPosition--;
+        }
+        // 2. decode to get the 1 or 2 characters containing where the mismatch lies
+        {
+            byte first = ourBuffer.getByte(ourPosition);
+            int control = first & 0xF0;
+            if (first > 0) {
+                ours1 = (char) (first & 0xFF);
+            } else if (control < 0xE0) {
+                ours1 = decode(first, ourBuffer.getByte(ourPosition + 1));
+            } else if (control == 0xE0) {
+                ours1 = decode(first, ourBuffer.getByte(ourPosition + 1),
+                        ourBuffer.getByte(ourPosition + 2));
+            } else {
+                int codepoint = decode(first, ourBuffer.getByte(ourPosition + 1),
+                        ourBuffer.getByte(ourPosition + 2), ourBuffer.getByte(ourPosition + 3));
+                if (Character.isValidCodePoint(codepoint)) {

Review Comment:
   I've changed this to the same replacement character used by the `String` class to ensure that comparisons are consistent with it.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push unsigned byte comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1059782099


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BaseImmutableDictionary.java:
##########
@@ -259,8 +259,7 @@ protected int binarySearch(byte[] value) {
 
     while (low <= high) {
       int mid = (low + high) >>> 1;
-      byte[] midValue = _valueReader.getBytes(mid, _numBytesPerValue);
-      int compareResult = ByteArray.compare(midValue, value);
+      int compareResult = _valueReader.compareUnsignedBytes(mid, _numBytesPerValue, value);

Review Comment:
   this introduces a bug as things stand as it will compare to the prefix before the first zero in the stored data, I will fix this soon and add some testing around this scenario.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061329509


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {
+                    // just need nonzero first byte
+                    return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+                } else if (bytes[bytes.length - 1] == 0) {
+                    // we can't store zero padding, but can have it in parameters, check the last byte
+                    return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+                } else {
+                    // check if the stored string continues beyond the length of the parameter
+                    return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+                }
+            } else {
+                // then we know the length precisely or know that the parameter is at least as long as we can store
+                return length - bytes.length;
+            }
+        }
+        byte ourByte = dataBuffer.getByte(startOffset + mismatchPosition);
+        if (ourByte == 0) {
+            // then we are storing the empty string
+            return -1;
+        }
+        byte theirByte = bytes[mismatchPosition];
+        if (theirByte == 0) {
+            return 1;
+        }
+        // we know the position of the mismatch but need to do utf8 decoding before comparison
+        // to respect collation rules
+        return compareUtf8(dataBuffer, startOffset, buffer, mismatchPosition);
+    }
+
+    private static int compareUtf8(PinotDataBuffer ourBuffer, long ourStartOffset,
+                           ByteBuffer theirBuffer, int mismatchPosition) {
+        char ours1 = '?';
+        char ours2 = '?';
+        char theirs1 = '?';
+        char theirs2 = '?';
+
+        // 1. seek backwards from mismatch position in each buffer to find start of each utf8 sequence
+        long ourPosition = ourStartOffset + mismatchPosition;
+        while (ourPosition > ourStartOffset && isUtf8Continuation(ourBuffer.getByte(ourPosition))) {

Review Comment:
   I've changed this so there's only one loop, having used the `String` class before storage and in the query layer makes this safe enough.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061976752


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+
+public class ValueReaderComparisons {
+
+  private ValueReaderComparisons() {
+  }
+
+  private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+    boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+    if (littleEndian) {
+      buffer.order(ByteOrder.LITTLE_ENDIAN);
+    }
+    int limit = Math.min(length, buffer.limit());
+    int loopBound = limit & ~0x7;
+    int i = 0;
+    for (; i < loopBound; i += 8) {
+      long ours = dataBuffer.getLong(startOffset + i);
+      long theirs = buffer.getLong(i);
+      if (ours != theirs) {
+        long difference = ours ^ theirs;
+        return i + ((littleEndian ? Long.numberOfTrailingZeros(difference) : Long.numberOfLeadingZeros(difference))
+            >>> 3);
+      }
+    }
+    for (; i < limit; i++) {
+      byte ours = dataBuffer.getByte(startOffset + i);
+      byte theirs = buffer.get(i);
+      if (ours != theirs) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded, byte[] bytes) {
+    // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+    ByteBuffer buffer = ByteBuffer.wrap(bytes);
+    int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+    if (mismatchPosition == -1) {
+      if (padded && bytes.length < length) {
+        // need to figure out whether the unpadded string is longer than the parameter or not
+        if (bytes.length == 0) {

Review Comment:
   IMO we can simplify this logic and change `LoaderTest` to make it pass. If we want to keep the behavior the same in this PR, we can do a separate PR with just this behavior change.



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+
+public class ValueReaderComparisons {
+
+  private ValueReaderComparisons() {
+  }
+
+  private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+    boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+    if (littleEndian) {
+      buffer.order(ByteOrder.LITTLE_ENDIAN);
+    }
+    int limit = Math.min(length, buffer.limit());
+    int loopBound = limit & ~0x7;
+    int i = 0;
+    for (; i < loopBound; i += 8) {
+      long ours = dataBuffer.getLong(startOffset + i);
+      long theirs = buffer.getLong(i);
+      if (ours != theirs) {
+        long difference = ours ^ theirs;
+        return i + ((littleEndian ? Long.numberOfTrailingZeros(difference) : Long.numberOfLeadingZeros(difference))
+            >>> 3);
+      }
+    }
+    for (; i < limit; i++) {
+      byte ours = dataBuffer.getByte(startOffset + i);
+      byte theirs = buffer.get(i);
+      if (ours != theirs) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded, byte[] bytes) {
+    // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+    ByteBuffer buffer = ByteBuffer.wrap(bytes);
+    int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+    if (mismatchPosition == -1) {
+      if (padded && bytes.length < length) {
+        // need to figure out whether the unpadded string is longer than the parameter or not
+        if (bytes.length == 0) {
+          // just need nonzero first byte
+          return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+        } else if (bytes[bytes.length - 1] == 0) {
+          // we can't store zero except as padding, but can have it in parameters, so check the last byte
+          return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;

Review Comment:
   I think we can directly `return -1` here as the last byte is always 0 under this branch?



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] codecov-commenter commented on pull request #10044: push unsigned byte comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on PR #10044:
URL: https://github.com/apache/pinot/pull/10044#issuecomment-1368099375

   # [Codecov](https://codecov.io/gh/apache/pinot/pull/10044?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#10044](https://codecov.io/gh/apache/pinot/pull/10044?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (b0cac0e) into [master](https://codecov.io/gh/apache/pinot/commit/27ac27ead0158f994d7d5688debf69f6ef26201c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (27ac27e) will **decrease** coverage by `45.14%`.
   > The diff coverage is `0.00%`.
   
   ```diff
   @@              Coverage Diff              @@
   ##             master   #10044       +/-   ##
   =============================================
   - Coverage     70.42%   25.27%   -45.15%     
   + Complexity     5691       44     -5647     
   =============================================
     Files          1994     1983       -11     
     Lines        107550   107270      -280     
     Branches      16349    16326       -23     
   =============================================
   - Hits          75743    27116    -48627     
   - Misses        26507    77412    +50905     
   + Partials       5300     2742     -2558     
   ```
   
   | Flag | Coverage Δ | |
   |---|---|---|
   | integration1 | `25.27% <0.00%> (+0.09%)` | :arrow_up: |
   | integration2 | `?` | |
   | unittests1 | `?` | |
   | unittests2 | `?` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Impacted Files](https://codecov.io/gh/apache/pinot/pull/10044?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [...ment/local/io/util/FixedByteValueReaderWriter.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc2VnbWVudC1sb2NhbC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3Qvc2VnbWVudC9sb2NhbC9pby91dGlsL0ZpeGVkQnl0ZVZhbHVlUmVhZGVyV3JpdGVyLmphdmE=) | `0.00% <0.00%> (-93.55%)` | :arrow_down: |
   | [...pache/pinot/segment/local/io/util/ValueReader.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc2VnbWVudC1sb2NhbC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3Qvc2VnbWVudC9sb2NhbC9pby91dGlsL1ZhbHVlUmVhZGVyLmphdmE=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
   | [...ot/segment/local/io/util/VarLengthValueReader.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc2VnbWVudC1sb2NhbC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3Qvc2VnbWVudC9sb2NhbC9pby91dGlsL1Zhckxlbmd0aFZhbHVlUmVhZGVyLmphdmE=) | `0.00% <0.00%> (-72.98%)` | :arrow_down: |
   | [...segment/index/readers/BaseImmutableDictionary.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc2VnbWVudC1sb2NhbC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3Qvc2VnbWVudC9sb2NhbC9zZWdtZW50L2luZGV4L3JlYWRlcnMvQmFzZUltbXV0YWJsZURpY3Rpb25hcnkuamF2YQ==) | `0.00% <0.00%> (-94.08%)` | :arrow_down: |
   | [...ain/java/org/apache/pinot/spi/utils/LoopUtils.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvdXRpbHMvTG9vcFV0aWxzLmphdmE=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
   | [...in/java/org/apache/pinot/spi/utils/BytesUtils.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvdXRpbHMvQnl0ZXNVdGlscy5qYXZh) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
   | [...java/org/apache/pinot/spi/trace/BaseRecording.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvdHJhY2UvQmFzZVJlY29yZGluZy5qYXZh) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
   | [...java/org/apache/pinot/spi/trace/NoOpRecording.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvdHJhY2UvTm9PcFJlY29yZGluZy5qYXZh) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
   | [...ava/org/apache/pinot/spi/config/table/FSTType.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvY29uZmlnL3RhYmxlL0ZTVFR5cGUuamF2YQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
   | [...ava/org/apache/pinot/spi/config/user/RoleType.java](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGlub3Qtc3BpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9waW5vdC9zcGkvY29uZmlnL3VzZXIvUm9sZVR5cGUuamF2YQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
   | ... and [1450 more](https://codecov.io/gh/apache/pinot/pull/10044/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
   
   :mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061273375


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {
+                    // just need nonzero first byte
+                    return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+                } else if (bytes[bytes.length - 1] == 0) {
+                    // we can't store zero padding, but can have it in parameters, check the last byte
+                    return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+                } else {
+                    // check if the stored string continues beyond the length of the parameter
+                    return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+                }
+            } else {
+                // then we know the length precisely or know that the parameter is at least as long as we can store
+                return length - bytes.length;
+            }
+        }
+        byte ourByte = dataBuffer.getByte(startOffset + mismatchPosition);
+        if (ourByte == 0) {
+            // then we are storing the empty string
+            return -1;
+        }
+        byte theirByte = bytes[mismatchPosition];
+        if (theirByte == 0) {
+            return 1;
+        }
+        // we know the position of the mismatch but need to do utf8 decoding before comparison
+        // to respect collation rules
+        return compareUtf8(dataBuffer, startOffset, buffer, mismatchPosition);
+    }
+
+    private static int compareUtf8(PinotDataBuffer ourBuffer, long ourStartOffset,
+                           ByteBuffer theirBuffer, int mismatchPosition) {
+        char ours1 = '?';
+        char ours2 = '?';
+        char theirs1 = '?';
+        char theirs2 = '?';
+
+        // 1. seek backwards from mismatch position in each buffer to find start of each utf8 sequence
+        long ourPosition = ourStartOffset + mismatchPosition;
+        while (ourPosition > ourStartOffset && isUtf8Continuation(ourBuffer.getByte(ourPosition))) {

Review Comment:
   Why do you think it's redundant? We could potentially go backwards forever if the UTF8 is corrupt. Assuming valid UTF8, the mismatch sequences will either have the same length (e.g. 4 bytes starting with 0xFX, with a mismatch in any of the next 3 bytes, or mismatching ASCII characters), so the loops could be fused, but this would be less resilient to corrupt data. The maximum length for either of these loops is 4 iterations, and they access different buffers, so the performance impact that can be made here is minimal.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1062279993


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+
+public class ValueReaderComparisons {
+
+  private ValueReaderComparisons() {
+  }
+
+  private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+    boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+    if (littleEndian) {
+      buffer.order(ByteOrder.LITTLE_ENDIAN);
+    }
+    int limit = Math.min(length, buffer.limit());
+    int loopBound = limit & ~0x7;
+    int i = 0;
+    for (; i < loopBound; i += 8) {
+      long ours = dataBuffer.getLong(startOffset + i);
+      long theirs = buffer.getLong(i);
+      if (ours != theirs) {
+        long difference = ours ^ theirs;
+        return i + ((littleEndian ? Long.numberOfTrailingZeros(difference) : Long.numberOfLeadingZeros(difference))
+            >>> 3);
+      }
+    }
+    for (; i < limit; i++) {
+      byte ours = dataBuffer.getByte(startOffset + i);
+      byte theirs = buffer.get(i);
+      if (ours != theirs) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded, byte[] bytes) {
+    // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+    ByteBuffer buffer = ByteBuffer.wrap(bytes);
+    int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+    if (mismatchPosition == -1) {
+      if (padded && bytes.length < length) {
+        // need to figure out whether the unpadded string is longer than the parameter or not
+        if (bytes.length == 0) {

Review Comment:
   I'm wary of removing test cases, it _looks_ like a regression test, it looks like @siddharthteotia added it so he should probably be consulted before removing it. Let's not do it on this branch though. FWIW I feel like the number of special cases is just the right side of manageable though, and if we can make this distinguish between zero padded strings and their valid prefixes with one special case, I'm personally inclined to do that.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1062280930


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+
+public class ValueReaderComparisons {
+
+  private ValueReaderComparisons() {
+  }
+
+  private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+    boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+    if (littleEndian) {
+      buffer.order(ByteOrder.LITTLE_ENDIAN);
+    }
+    int limit = Math.min(length, buffer.limit());
+    int loopBound = limit & ~0x7;
+    int i = 0;
+    for (; i < loopBound; i += 8) {
+      long ours = dataBuffer.getLong(startOffset + i);
+      long theirs = buffer.getLong(i);
+      if (ours != theirs) {
+        long difference = ours ^ theirs;
+        return i + ((littleEndian ? Long.numberOfTrailingZeros(difference) : Long.numberOfLeadingZeros(difference))
+            >>> 3);
+      }
+    }
+    for (; i < limit; i++) {
+      byte ours = dataBuffer.getByte(startOffset + i);
+      byte theirs = buffer.get(i);
+      if (ours != theirs) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded, byte[] bytes) {
+    // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+    ByteBuffer buffer = ByteBuffer.wrap(bytes);
+    int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+    if (mismatchPosition == -1) {
+      if (padded && bytes.length < length) {
+        // need to figure out whether the unpadded string is longer than the parameter or not
+        if (bytes.length == 0) {
+          // just need nonzero first byte
+          return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+        } else if (bytes[bytes.length - 1] == 0) {
+          // we can't store zero except as padding, but can have it in parameters, so check the last byte
+          return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;

Review Comment:
   I recall there was a test case failing without this logic, I'll find out which one and simplify the logic if I can't find it.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on PR #10044:
URL: https://github.com/apache/pinot/pull/10044#issuecomment-1372275649

   FWIW I think this code is correct, and I know it addresses a bottleneck which can account for up to 20-25% of CPU samples when I've profiled servers Pinot in the past, but I can't be the one to merge this PR because I'm not taking on the risk of it being incorrect, it was just a fun problem to solve. So please vet this PR some more and merge it at your own risk.


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061277160


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {
+    }
+
+    private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+        boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+        if (littleEndian) {
+            buffer.order(ByteOrder.LITTLE_ENDIAN);
+        }
+        int limit = Math.min(length, buffer.limit());
+        int loopBound = limit & ~0x3F;
+        int i = 0;
+        for (; i < loopBound; i += 8) {
+            long ours = dataBuffer.getLong(startOffset + i);
+            long theirs = buffer.getLong(i);
+            if (ours != theirs) {
+                long difference = ours ^ theirs;
+                return i + ((littleEndian
+                        ? Long.numberOfTrailingZeros(difference)
+                        : Long.numberOfLeadingZeros(difference)) >>> 3);
+            }
+        }
+        for (; i < limit; i++) {
+            byte ours = dataBuffer.getByte(startOffset + i);
+            byte theirs = buffer.get(i);
+            if (ours != theirs) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded,
+                           byte[] bytes) {
+        // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+        ByteBuffer buffer = ByteBuffer.wrap(bytes);
+        int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+        if (mismatchPosition == -1) {
+            // need to figure out whether the unpadded string is longer than the parameter or not
+            if (padded && bytes.length < length) {
+                if (bytes.length == 0) {
+                    // just need nonzero first byte
+                    return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+                } else if (bytes[bytes.length - 1] == 0) {
+                    // we can't store zero padding, but can have it in parameters, check the last byte
+                    return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;
+                } else {
+                    // check if the stored string continues beyond the length of the parameter
+                    return dataBuffer.getByte(startOffset + bytes.length) == 0 ? 0 : 1;
+                }
+            } else {
+                // then we know the length precisely or know that the parameter is at least as long as we can store
+                return length - bytes.length;
+            }
+        }
+        byte ourByte = dataBuffer.getByte(startOffset + mismatchPosition);
+        if (ourByte == 0) {
+            // then we are storing the empty string
+            return -1;
+        }
+        byte theirByte = bytes[mismatchPosition];
+        if (theirByte == 0) {
+            return 1;
+        }
+        // we know the position of the mismatch but need to do utf8 decoding before comparison
+        // to respect collation rules
+        return compareUtf8(dataBuffer, startOffset, buffer, mismatchPosition);
+    }
+
+    private static int compareUtf8(PinotDataBuffer ourBuffer, long ourStartOffset,
+                           ByteBuffer theirBuffer, int mismatchPosition) {
+        char ours1 = '?';
+        char ours2 = '?';
+        char theirs1 = '?';
+        char theirs2 = '?';
+
+        // 1. seek backwards from mismatch position in each buffer to find start of each utf8 sequence
+        long ourPosition = ourStartOffset + mismatchPosition;
+        while (ourPosition > ourStartOffset && isUtf8Continuation(ourBuffer.getByte(ourPosition))) {
+            ourPosition--;
+        }
+        int theirPosition = mismatchPosition;
+        while (theirPosition > 0 && isUtf8Continuation(theirBuffer.get(theirPosition))) {
+            theirPosition--;
+        }
+        // 2. decode to get the 1 or 2 characters containing where the mismatch lies
+        {
+            byte first = ourBuffer.getByte(ourPosition);
+            int control = first & 0xF0;
+            if (first > 0) {
+                ours1 = (char) (first & 0xFF);
+            } else if (control < 0xE0) {
+                ours1 = decode(first, ourBuffer.getByte(ourPosition + 1));
+            } else if (control == 0xE0) {
+                ours1 = decode(first, ourBuffer.getByte(ourPosition + 1),
+                        ourBuffer.getByte(ourPosition + 2));
+            } else {
+                int codepoint = decode(first, ourBuffer.getByte(ourPosition + 1),
+                        ourBuffer.getByte(ourPosition + 2), ourBuffer.getByte(ourPosition + 3));
+                if (Character.isValidCodePoint(codepoint)) {

Review Comment:
   Yes, this is a good observation. The `String` class mostly takes care of this for us because it will either throw or replace when it encounters an invalid codepoint, and each entry in the dictionary was a string before being UTF8 encoded, as was the parameter, but I think we need to be resilient here in case things change elsewhere. Making the missing values zero would at least ensure consistent ordering for invalid codepoints.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1061869181


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+public class ValueReaderComparisons {
+
+    private ValueReaderComparisons() {

Review Comment:
   Agreed. We have created #6790 to track this, but the work is not travail because we need to switch from intellij to eclipse format which is supported by spotless.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #10044:
URL: https://github.com/apache/pinot/pull/10044#discussion_r1062528508


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/util/ValueReaderComparisons.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.io.util;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.pinot.segment.spi.memory.PinotDataBuffer;
+
+
+public class ValueReaderComparisons {
+
+  private ValueReaderComparisons() {
+  }
+
+  private static int mismatch(PinotDataBuffer dataBuffer, long startOffset, int length, ByteBuffer buffer) {
+    boolean littleEndian = dataBuffer.order() == ByteOrder.LITTLE_ENDIAN;
+    if (littleEndian) {
+      buffer.order(ByteOrder.LITTLE_ENDIAN);
+    }
+    int limit = Math.min(length, buffer.limit());
+    int loopBound = limit & ~0x7;
+    int i = 0;
+    for (; i < loopBound; i += 8) {
+      long ours = dataBuffer.getLong(startOffset + i);
+      long theirs = buffer.getLong(i);
+      if (ours != theirs) {
+        long difference = ours ^ theirs;
+        return i + ((littleEndian ? Long.numberOfTrailingZeros(difference) : Long.numberOfLeadingZeros(difference))
+            >>> 3);
+      }
+    }
+    for (; i < limit; i++) {
+      byte ours = dataBuffer.getByte(startOffset + i);
+      byte theirs = buffer.get(i);
+      if (ours != theirs) {
+        return i;
+      }
+    }
+    return -1;
+  }
+
+  static int compareUtf8Bytes(PinotDataBuffer dataBuffer, long startOffset, int length, boolean padded, byte[] bytes) {
+    // can use MethodHandles.byteArrayViewVarHandle here after dropping JDK8
+    ByteBuffer buffer = ByteBuffer.wrap(bytes);
+    int mismatchPosition = mismatch(dataBuffer, startOffset, length, buffer);
+    if (mismatchPosition == -1) {
+      if (padded && bytes.length < length) {
+        // need to figure out whether the unpadded string is longer than the parameter or not
+        if (bytes.length == 0) {
+          // just need nonzero first byte
+          return dataBuffer.getByte(startOffset) == 0 ? 0 : 1;
+        } else if (bytes[bytes.length - 1] == 0) {
+          // we can't store zero except as padding, but can have it in parameters, so check the last byte
+          return dataBuffer.getByte(startOffset + bytes.length - 1) == 0 ? -1 : 0;

Review Comment:
   I simplified this.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on pull request #10044: push unsigned byte comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
richardstartin commented on PR #10044:
URL: https://github.com/apache/pinot/pull/10044#issuecomment-1368091574

   Here are some preliminary numbers from my laptop (these need taking with a pinch of salt but look very promising)
   
   master:
   
   ```
   Benchmark                                                            Mode  Cnt     Score      Error  Units
   BenchmarkStringVarLengthDictionary.fixedStringDictionaryGet          avgt    5   253.967 ±  593.088  ms/op
   BenchmarkStringVarLengthDictionary.fixedStringDictionaryIndexOf      avgt    5  8123.103 ± 2528.026  ms/op
   BenchmarkStringVarLengthDictionary.varLengthStringDictionaryGet      avgt    5   224.049 ±  312.375  ms/op
   BenchmarkStringVarLengthDictionary.varLengthStringDictionaryIndexOf  avgt    5  8071.103 ± 2135.835  ms/op
   ```
   
   branch
   ```
   Benchmark                                                            Mode  Cnt     Score      Error  Units
   BenchmarkStringVarLengthDictionary.fixedStringDictionaryGet          avgt    5   253.967 ±  593.088  ms/op
   BenchmarkStringVarLengthDictionary.fixedStringDictionaryIndexOf      avgt    5  8123.103 ± 2528.026  ms/op
   BenchmarkStringVarLengthDictionary.varLengthStringDictionaryGet      avgt    5   224.049 ±  312.375  ms/op
   BenchmarkStringVarLengthDictionary.varLengthStringDictionaryIndexOf  avgt    5  8071.103 ± 2135.835  ms/op
   ```


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang merged pull request #10044: push utf8 comparison down into ValueReader

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang merged PR #10044:
URL: https://github.com/apache/pinot/pull/10044


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org