emkornfield commented on a change in pull request #10177: URL: https://github.com/apache/arrow/pull/10177#discussion_r690522705
########## File path: java/vector/src/main/java/org/apache/arrow/vector/IntervalMonthDayNanoVector.java ########## @@ -0,0 +1,440 @@ +/* + * 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.arrow.vector; + +import static org.apache.arrow.vector.NullCheckingForGet.NULL_CHECKING_ENABLED; + +import java.time.Duration; +import java.time.Period; + +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.vector.complex.impl.IntervalMonthDayNanoReaderImpl; +import org.apache.arrow.vector.complex.reader.FieldReader; +import org.apache.arrow.vector.holders.IntervalMonthDayNanoHolder; +import org.apache.arrow.vector.holders.NullableIntervalMonthDayNanoHolder; +import org.apache.arrow.vector.types.Types.MinorType; +import org.apache.arrow.vector.types.pojo.Field; +import org.apache.arrow.vector.types.pojo.FieldType; +import org.apache.arrow.vector.util.TransferPair; + +/** + * IntervalMonthDayNanoVectorimplements a fixed width vector (16 bytes) of + * interval (month, days and nanoseconds) values which could be null. + * A validity buffer (bit vector) is maintained to track which elements in the + * vector are null. + */ +public final class IntervalMonthDayNanoVector extends BaseFixedWidthVector { + public static final byte TYPE_WIDTH = 16; + private static final byte DAY_OFFSET = 4; + private static final byte NANOSECOND_OFFSET = 8; + private final FieldReader reader; + + + /** + * Instantiate a IntervalMonthDayNanoVector. This doesn't allocate any memory for + * the data in vector. + * + * @param name name of the vector + * @param allocator allocator for memory management. + */ + public IntervalMonthDayNanoVector(String name, BufferAllocator allocator) { + this(name, FieldType.nullable(MinorType.INTERVALDAY.getType()), allocator); + } + + /** + * Instantiate a IntervalMonthDayNanoVector. This doesn't allocate any memory for + * the data in vector. + * + * @param name name of the vector + * @param fieldType type of Field materialized by this vector + * @param allocator allocator for memory management. + */ + public IntervalMonthDayNanoVector(String name, FieldType fieldType, BufferAllocator allocator) { + this(new Field(name, fieldType, null), allocator); + } + + /** + * Instantiate a IntervalMonthDayNanoVector. This doesn't allocate any memory for + * the data in vector. + * + * @param field field materialized by this vector + * @param allocator allocator for memory management. + */ + public IntervalMonthDayNanoVector(Field field, BufferAllocator allocator) { + super(field, allocator, TYPE_WIDTH); + reader = new IntervalMonthDayNanoReaderImpl(IntervalMonthDayNanoVector.this); + } + + /** + * Get a reader that supports reading values from this vector. + * + * @return Field Reader for this vector + */ + @Override + public FieldReader getReader() { + return reader; + } + + /** + * Get minor type for this vector. The vector holds values belonging + * to a particular type. + * + * @return {@link org.apache.arrow.vector.types.Types.MinorType} + */ + @Override + public MinorType getMinorType() { + return MinorType.INTERVALMONTHDAYNANO; + } + + + /*----------------------------------------------------------------* + | | + | vector value retrieval methods | + | | + *----------------------------------------------------------------*/ + + /** + * Given a data buffer, get the number of months stored at a particular position + * in the vector. + * + * <p>This method should not be used externally. + * + * @param buffer data buffer + * @param index position of the element. + * @return day value stored at the index. + */ + public static int getMonths(final ArrowBuf buffer, final int index) { + return buffer.getInt((long) index * TYPE_WIDTH); + } + + + /** + * Given a data buffer, get the number of days stored at a particular position + * in the vector. + * + * <p>This method should not be used externally. + * + * @param buffer data buffer + * @param index position of the element. + * @return day value stored at the index. + */ + public static int getDays(final ArrowBuf buffer, final int index) { + return buffer.getInt((long) index * TYPE_WIDTH + DAY_OFFSET); + } + + /** + * Given a data buffer, get the get the number of nanoseconds stored at a particular position + * in the vector. + * + * <p>This method should not be used externally. + * + * @param buffer data buffer + * @param index position of the element. + * @return nanoseconds value stored at the index. + */ + public static long getNanoseconds(final ArrowBuf buffer, final int index) { + return buffer.getLong((long) index * TYPE_WIDTH + NANOSECOND_OFFSET); + } + + /** + * Get the element at the given index from the vector. + * + * @param index position of element + * @return element at given index + */ + public ArrowBuf get(int index) throws IllegalStateException { + if (NULL_CHECKING_ENABLED && isSet(index) == 0) { + return null; + } + return valueBuffer.slice((long) index * TYPE_WIDTH, TYPE_WIDTH); + } + + /** + * Get the element at the given index from the vector and + * sets the state in holder. If element at given index + * is null, holder.isSet will be zero. + * + * @param index position of element + */ + public void get(int index, NullableIntervalMonthDayNanoHolder holder) { + if (isSet(index) == 0) { + holder.isSet = 0; + return; + } + final long startIndex = (long) index * TYPE_WIDTH; + holder.isSet = 1; + holder.months = valueBuffer.getInt(startIndex); + holder.days = valueBuffer.getInt(startIndex + DAY_OFFSET); + holder.nanoseconds = valueBuffer.getLong(startIndex + NANOSECOND_OFFSET); + } + + /** + * Same as {@link #get(int)}. + * + * @param index position of element + * @return element at given index + */ + public PeriodDuration getObject(int index) { + if (isSet(index) == 0) { + return null; + } else { + final long startIndex = (long) index * TYPE_WIDTH; + final int months = valueBuffer.getInt(startIndex); + final int days = valueBuffer.getInt(startIndex + DAY_OFFSET); + final long nanoseconds = valueBuffer.getLong(startIndex + NANOSECOND_OFFSET); + + return new PeriodDuration(Period.ofMonths(months).plusDays(days), + Duration.ofNanos(nanoseconds)); + } + } + + /** + * Get the Interval value at a given index as a {@link StringBuilder} object. + * + * @param index position of the element + * @return String Builder object with Interval value as + */ + public StringBuilder getAsStringBuilder(int index) { + if (isSet(index) == 0) { + return null; + } else { + return getAsStringBuilderHelper(index); + } + } + + private StringBuilder getAsStringBuilderHelper(int index) { + final long startIndex = (long) index * TYPE_WIDTH; Review comment: nice catch. -- 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]
