Github user kiszk commented on a diff in the pull request: https://github.com/apache/spark/pull/13680#discussion_r69386582 --- Diff: sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/UnsafeArrayWriter.java --- @@ -33,91 +38,147 @@ // The offset of the global buffer where we start to write this array. private int startingOffset; + // The number of elements in this array + private int numElements; + + private int headerInBytes; + + private void assertIndexIsValid(int index) { + assert index >= 0 : "index (" + index + ") should >= 0"; + assert index < numElements : "index (" + index + ") should < " + numElements; + } + public void initialize(BufferHolder holder, int numElements, int fixedElementSize) { - // We need 4 bytes to store numElements and 4 bytes each element to store offset. - final int fixedSize = 4 + 4 * numElements; + this.numElements = numElements; + this.headerInBytes = calculateHeaderPortionInBytes(numElements); this.holder = holder; this.startingOffset = holder.cursor; - holder.grow(fixedSize); - Platform.putInt(holder.buffer, holder.cursor, numElements); - holder.cursor += fixedSize; + // Grows the global buffer ahead for header and fixed size data. + holder.grow(headerInBytes + fixedElementSize * numElements); + + // Initialize information in header + Platform.putInt(holder.buffer, startingOffset, numElements); + Arrays.fill(holder.buffer, startingOffset + 4 - Platform.BYTE_ARRAY_OFFSET, + startingOffset + headerInBytes - Platform.BYTE_ARRAY_OFFSET, (byte)0); - // Grows the global buffer ahead for fixed size data. - holder.grow(fixedElementSize * numElements); + holder.cursor += (headerInBytes + fixedElementSize * numElements); } - private long getElementOffset(int ordinal) { - return startingOffset + 4 + 4 * ordinal; + private long getElementOffset(int ordinal, int scale) { + return startingOffset + headerInBytes + ordinal * scale; + } + + public void setOffsetAndSize(int ordinal, long currentCursor, long size) { + final long relativeOffset = currentCursor - startingOffset; + final long offsetAndSize = (relativeOffset << 32) | size; + + write(ordinal, offsetAndSize); } public void setNullAt(int ordinal) { - final int relativeOffset = holder.cursor - startingOffset; - // Writes negative offset value to represent null element. - Platform.putInt(holder.buffer, getElementOffset(ordinal), -relativeOffset); + throw new UnsupportedOperationException("setNullAt() is not supported"); --- End diff -- Removed
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org