AlSchlo commented on code in PR #54134: URL: https://github.com/apache/spark/pull/54134#discussion_r2797096662
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/MaxMinByKHeap.scala: ########## @@ -0,0 +1,172 @@ +/* + * 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.spark.sql.catalyst.expressions.aggregate + +import java.nio.{ByteBuffer, ByteOrder} + +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData} +import org.apache.spark.sql.types.DataType + +/** + * Helper for MaxMinByK aggregate providing heap operations. + * Heap operates on indices to avoid copying large values. + * + * Binary heap layout: [size (4 bytes), idx0 (4 bytes), idx1 (4 bytes), ..., idx(k-1) (4 bytes)] + * Total size: (k + 1) * 4 bytes + * + * All integers are stored in little-endian byte order for direct binary manipulation. + */ +object MaxMinByKHeap { + + def getSize(heap: Array[Byte]): Int = + ByteBuffer.wrap(heap, 0, 4).order(ByteOrder.LITTLE_ENDIAN).getInt + + def setSize(heap: Array[Byte], size: Int): Unit = + ByteBuffer.wrap(heap, 0, 4).order(ByteOrder.LITTLE_ENDIAN).putInt(size) + + def getIdx(heap: Array[Byte], pos: Int): Int = + ByteBuffer.wrap(heap, (pos + 1) * 4, 4).order(ByteOrder.LITTLE_ENDIAN).getInt + + def setIdx(heap: Array[Byte], pos: Int, idx: Int): Unit = + ByteBuffer.wrap(heap, (pos + 1) * 4, 4).order(ByteOrder.LITTLE_ENDIAN).putInt(idx) + + def swap(heap: Array[Byte], i: Int, j: Int): Unit = { + val tmp = getIdx(heap, i) + setIdx(heap, i, getIdx(heap, j)) + setIdx(heap, j, tmp) + } + + def siftUp( + heap: Array[Byte], + pos: Int, + orderings: Array[Any], + compare: (Any, Any) => Int): Unit = { + var current = pos + while (current > 0) { + val parent = (current - 1) / 2 + val curOrd = orderings(getIdx(heap, current)) + val parOrd = orderings(getIdx(heap, parent)) + + if (compare(curOrd, parOrd) < 0) { + swap(heap, current, parent) + current = parent + } else { + return + } + } + } + + def siftDown( + heap: Array[Byte], + pos: Int, + size: Int, + orderings: Array[Any], + compare: (Any, Any) => Int): Unit = { + var current = pos + while (2 * current + 1 < size) { + val left = 2 * current + 1 + val right = left + 1 + val leftOrd = orderings(getIdx(heap, left)) + + val preferred = if (right < size) { + val rightOrd = orderings(getIdx(heap, right)) + if (compare(rightOrd, leftOrd) < 0) right else left + } else { + left + } + + val curOrd = orderings(getIdx(heap, current)) + val prefOrd = orderings(getIdx(heap, preferred)) + if (compare(curOrd, prefOrd) <= 0) { + return + } + + swap(heap, current, preferred) + current = preferred + } + } + + /** + * Insert element into heap. If heap is full, replaces root if new element is better. + */ + def insert( + value: Any, + ord: Any, + k: Int, + valuesArr: Array[Any], + orderingsArr: Array[Any], + heap: Array[Byte], + compare: (Any, Any) => Int): Unit = { + val size = getSize(heap) + if (size < k) { + valuesArr(size) = InternalRow.copyValue(value) + orderingsArr(size) = InternalRow.copyValue(ord) + + setIdx(heap, size, size) + siftUp(heap, size, orderingsArr, compare) + setSize(heap, size + 1) + } else if (compare(ord, orderingsArr(getIdx(heap, 0))) > 0) { + val idx = getIdx(heap, 0) + valuesArr(idx) = InternalRow.copyValue(value) + orderingsArr(idx) = InternalRow.copyValue(ord) + + siftDown(heap, 0, size, orderingsArr, compare) + } + } + + /** + * Get mutable array from buffer for in-place updates. + * Converts UnsafeArrayData (after spill) to GenericArrayData. + */ + def getMutableArray(buffer: InternalRow, offset: Int, elementType: DataType): Array[Any] = { + buffer.getArray(offset) match { + case g: GenericArrayData => + g.array.asInstanceOf[Array[Any]] + case other => + val size = other.numElements() + val newArr = new Array[Any](size) + + for (i <- 0 until size) { + if (!other.isNullAt(i)) { + newArr(i) = InternalRow.copyValue(other.get(i, elementType)) + } + } + + val newArrayData = new GenericArrayData(newArr) + buffer.update(offset, newArrayData) + newArr + } + } + + /** + * Get mutable heap binary buffer from buffer for in-place updates. + * Copies the binary data if needed (e.g., after spill to UnsafeRow). Review Comment: In `UnsafeRow` which extends `InternalRow` ``` @Override public byte[] getBinary(int ordinal) { if (isNullAt(ordinal)) { return null; } else { final long offsetAndSize = getLong(ordinal); final int offset = (int) (offsetAndSize >> 32); final int size = (int) offsetAndSize; final byte[] bytes = new byte[size]; Platform.copyMemory( baseObject, baseOffset + offset, bytes, Platform.BYTE_ARRAY_OFFSET, size ); return bytes; } } ``` For `SpecificInternalRow` it returns the same reference indeed. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
