Github user davies commented on a diff in the pull request: https://github.com/apache/spark/pull/8747#discussion_r41168843 --- Diff: sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/UnsafeArrayWriter.java --- @@ -0,0 +1,179 @@ +/* + * 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.codegen; + +import org.apache.spark.sql.catalyst.expressions.UnsafeArrayData; +import org.apache.spark.sql.types.Decimal; +import org.apache.spark.unsafe.Platform; +import org.apache.spark.unsafe.types.CalendarInterval; +import org.apache.spark.unsafe.types.UTF8String; + +/** + * A helper class to write data into global row buffer using `UnsafeArrayData` format, + * used by {@link org.apache.spark.sql.catalyst.expressions.codegen.GenerateUnsafeProjection}. + */ +public class UnsafeArrayWriter { + + private BufferHolder holder; + // The offset of the global buffer where we start to write this array. + private int startingOffset; + + public void initialize( + BufferHolder holder, + int numElements, + boolean needHeader, + int fixedElementSize) { + + // We need 4 bytes each element to store offset. + final int fixedSize = 4 * numElements; + + if (needHeader) { + // If header is required, we need extra 4 bytes to store the header. + holder.grow(fixedSize + 4); + // Writes the number of elements into first 4 bytes; + Platform.putInt(holder.buffer, holder.cursor, numElements); + holder.cursor += 4; + } else { + holder.grow(fixedSize); + } + + this.holder = holder; + this.startingOffset = holder.cursor; + + holder.cursor += fixedSize; + + // Grows the global buffer ahead for fixed size data. + if (fixedElementSize > 0) { + holder.grow(fixedElementSize * numElements); + } + } + + private long getElementOffset(int ordinal) { + return startingOffset + 4 * ordinal; + } + + 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); + } + + public void setOffset(int ordinal) { + final int relativeOffset = holder.cursor - startingOffset; + Platform.putInt(holder.buffer, getElementOffset(ordinal), relativeOffset); + } + + --- End diff -- remove empty lines
--- 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