npawar commented on a change in pull request #6968: URL: https://github.com/apache/incubator-pinot/pull/6968#discussion_r639170675
########## File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/serde/GenericRowSerializer.java ########## @@ -0,0 +1,228 @@ +/** + * 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.core.segment.processing.serde; + +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.segment.spi.memory.PinotDataBuffer; +import org.apache.pinot.spi.data.FieldSpec; +import org.apache.pinot.spi.data.FieldSpec.DataType; +import org.apache.pinot.spi.data.readers.GenericRow; +import org.apache.pinot.spi.utils.StringUtils; + + +/** + * Utility class to serialize the {@link GenericRow}. + * The bytes are stored in NATIVE order. The data should be deserialized by the {@link GenericRowDeserializer} on the + * same host to ensure that both of them are using the same byte order. + */ +public class GenericRowSerializer { + private final int _numFields; + private final String[] _fieldNames; + private final boolean[] _isSingleValueFields; + private final DataType[] _storedTypes; + // Cache the encoded string bytes + private final Object[] _stringBytes; + // Store index for null value fields + private final Map<String, Integer> _fieldIndexMap; + + public GenericRowSerializer(List<FieldSpec> fieldSpecs, boolean includeNullValueFields) { + _numFields = fieldSpecs.size(); + _fieldNames = new String[_numFields]; + _isSingleValueFields = new boolean[_numFields]; + _storedTypes = new DataType[_numFields]; + _stringBytes = new Object[_numFields]; + for (int i = 0; i < _numFields; i++) { + FieldSpec fieldSpec = fieldSpecs.get(i); + _fieldNames[i] = fieldSpec.getName(); + _isSingleValueFields[i] = fieldSpec.isSingleValueField(); + _storedTypes[i] = fieldSpec.getDataType().getStoredType(); + } + if (includeNullValueFields) { + _fieldIndexMap = new HashMap<>(); + for (int i = 0; i < _numFields; i++) { + _fieldIndexMap.put(_fieldNames[i], i); + } + } else { + _fieldIndexMap = null; + } + } + + /** + * Serializes the given {@link GenericRow}. + */ + public byte[] serialize(GenericRow row) { + int numBytes = 0; + + // First pass: calculate the number of bytes required + for (int i = 0; i < _numFields; i++) { + Object value = row.getValue(_fieldNames[i]); + + if (_isSingleValueFields[i]) { + switch (_storedTypes[i]) { + case INT: + numBytes += Integer.BYTES; + break; + case LONG: + numBytes += Long.BYTES; + break; + case FLOAT: + numBytes += Float.BYTES; + break; + case DOUBLE: + numBytes += Double.BYTES; + break; + case STRING: + byte[] stringBytes = StringUtils.encodeUtf8((String) value); + numBytes += Integer.BYTES + stringBytes.length; + _stringBytes[i] = stringBytes; + break; + case BYTES: + numBytes += Integer.BYTES + ((byte[]) value).length; + break; + default: + throw new IllegalStateException("Unsupported SV stored type: " + _storedTypes[i]); + } + } else { + Object[] multiValue = (Object[]) value; + int numValues = multiValue.length; + numBytes += Integer.BYTES; // Number of values + + switch (_storedTypes[i]) { + case INT: + numBytes += Integer.BYTES * numValues; + break; + case LONG: + numBytes += Long.BYTES * numValues; + break; + case FLOAT: + numBytes += Float.BYTES * numValues; + break; + case DOUBLE: + numBytes += Double.BYTES * numValues; + break; + case STRING: + numBytes += Integer.BYTES * numValues; + byte[][] stringBytesArray = new byte[numValues][]; + for (int j = 0; j < numValues; j++) { + byte[] stringBytes = StringUtils.encodeUtf8((String) multiValue[j]); + numBytes += stringBytes.length; + stringBytesArray[j] = stringBytes; + } + _stringBytes[i] = stringBytesArray; + break; + default: + throw new IllegalStateException("Unsupported MV stored type: " + _storedTypes[i]); + } + } + } + + // Serialize null value fields if enabled + if (_fieldIndexMap != null) { + Set<String> nullValueFields = row.getNullValueFields(); + numBytes += Integer.BYTES * (1 + nullValueFields.size()); + } + + byte[] serializedBytes = new byte[numBytes]; + ByteBuffer byteBuffer = ByteBuffer.wrap(serializedBytes).order(PinotDataBuffer.NATIVE_ORDER); + + // Second pass: serialize the values + for (int i = 0; i < _numFields; i++) { + Object value = row.getValue(_fieldNames[i]); + + if (_isSingleValueFields[i]) { + switch (_storedTypes[i]) { + case INT: + byteBuffer.putInt((int) value); + break; + case LONG: + byteBuffer.putLong((long) value); + break; + case FLOAT: + byteBuffer.putFloat((float) value); + break; + case DOUBLE: + byteBuffer.putDouble((double) value); + break; + case STRING: + byte[] stringBytes = (byte[]) _stringBytes[i]; + byteBuffer.putInt(stringBytes.length); + byteBuffer.put(stringBytes); + break; + case BYTES: + byte[] bytes = (byte[]) value; + byteBuffer.putInt(bytes.length); + byteBuffer.put(bytes); + break; + default: + throw new IllegalStateException(); Review comment: nit: even though this default block will never be reached, add message to the exception? for code readability and also to catch any mistakes made by future development ########## File path: pinot-core/src/main/java/org/apache/pinot/core/segment/processing/collector/SortOrderComparator.java ########## @@ -0,0 +1,72 @@ +/** + * 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.core.segment.processing.collector; + +import java.util.Comparator; +import org.apache.pinot.spi.data.FieldSpec.DataType; +import org.apache.pinot.spi.utils.ByteArray; + + +/** + * Comparator for values of the sort columns. + */ +public class SortOrderComparator implements Comparator<Object[]> { + private final int _numSortColumns; + private final DataType[] _sortColumnStoredTypes; + + public SortOrderComparator(int numSortColumns, DataType[] sortColumnStoredTypes) { + _numSortColumns = numSortColumns; + _sortColumnStoredTypes = sortColumnStoredTypes; + } + + @Override + public int compare(Object[] o1, Object[] o2) { + for (int i = 0; i < _numSortColumns; i++) { + Object value1 = o1[i]; + Object value2 = o2[i]; + int result; + switch (_sortColumnStoredTypes[i]) { + case INT: + result = Integer.compare((int) value1, (int) value2); + break; + case LONG: + result = Long.compare((long) value1, (long) value2); + break; + case FLOAT: + result = Float.compare((float) value1, (float) value2); + break; + case DOUBLE: + result = Double.compare((double) value1, (double) value2); + break; + case STRING: + result = ((String) value1).compareTo((String) value2); + break; + case BYTES: + result = ByteArray.compare((byte[]) value1, (byte[]) value2); + break; + default: + throw new IllegalStateException(); Review comment: exception msg here? would be useful if this is used in places other than the ConcatCollector too -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org