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-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org