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