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