Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -960,4 +960,19 @@ public void validate() {
public void endValue(int index, int size) {
sizeBuffer.setInt((long) index * SIZE_WIDTH, size);
}

/** Get data vector start index with the given list index. */
public int getElementStartIndex(int index) {
return offsetBuffer.getInt((long) index * OFFSET_WIDTH);
}

/** Get the size at the given index. */
private int getElementSize(int index) {
return sizeBuffer.getInt((long) index * SIZE_WIDTH);
}

/** Get data vector end index with the given list index. */
public int getElementEndIndex(int index) {
return getElementStartIndex(index) + getElementSize(index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,25 @@ public boolean isSet() {

@Override
public void setPosition(int index) {
int valueCount = vector.getValueCount();
if (valueCount == 0 && index == 0) {
setEmptyPosition(index);
return;
}

UnionListReaderPositionValidator.checkIndex(index, valueCount);
UnionListReaderPositionValidator.checkListBufferReadable(
vector.getOffsetBuffer(), index, OFFSET_WIDTH);

super.setPosition(index);
currentOffset = vector.getElementStartIndex(index) - 1;
maxOffset = vector.getElementEndIndex(index);
}

private void setEmptyPosition(int index) {
super.setPosition(index);
currentOffset = vector.getOffsetBuffer().getLong((long) index * OFFSET_WIDTH) - 1;
maxOffset = vector.getOffsetBuffer().getLong(((long) index + 1L) * OFFSET_WIDTH);
currentOffset = 0;
Comment thread
bodduv marked this conversation as resolved.
maxOffset = 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.apache.arrow.memory.util.LargeMemoryUtil.checkedCastToInt;

import org.apache.arrow.vector.ValueVector;
import org.apache.arrow.vector.complex.BaseLargeRepeatedValueViewVector;
import org.apache.arrow.vector.complex.LargeListViewVector;
import org.apache.arrow.vector.complex.reader.FieldReader;
import org.apache.arrow.vector.holders.UnionHolder;
Expand Down Expand Up @@ -56,18 +55,29 @@ public boolean isSet() {

@Override
public void setPosition(int index) {
super.setPosition(index);
if (vector.getOffsetBuffer().capacity() == 0) {
currentOffset = 0;
size = 0;
} else {
currentOffset =
vector
.getOffsetBuffer()
.getInt(index * (long) BaseLargeRepeatedValueViewVector.OFFSET_WIDTH);
size =
vector.getSizeBuffer().getInt(index * (long) BaseLargeRepeatedValueViewVector.SIZE_WIDTH);
int valueCount = vector.getValueCount();
if (valueCount == 0 && index == 0) {
setEmptyPosition(index);
return;
}

UnionListReaderPositionValidator.checkIndex(index, valueCount);
UnionListReaderPositionValidator.checkListViewBufferReadable(
vector.getOffsetBuffer(),
vector.getSizeBuffer(),
index,
LargeListViewVector.OFFSET_WIDTH,
LargeListViewVector.SIZE_WIDTH);

super.setPosition(index);
currentOffset = vector.getElementStartIndex(index);
size = vector.getElementEndIndex(index) - currentOffset;
}

private void setEmptyPosition(int index) {
super.setPosition(index);
currentOffset = 0;
size = 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,25 @@ public boolean isSet() {

@Override
public void setPosition(int index) {
super.setPosition(index);
if (vector.getOffsetBuffer().capacity() == 0) {
currentOffset = 0;
maxOffset = 0;
} else {
currentOffset = vector.getOffsetBuffer().getInt(index * (long) OFFSET_WIDTH) - 1;
maxOffset = vector.getOffsetBuffer().getInt((index + 1) * (long) OFFSET_WIDTH);
int valueCount = vector.getValueCount();
if (valueCount == 0 && index == 0) {
setEmptyPosition(index);
return;
}

UnionListReaderPositionValidator.checkIndex(index, valueCount);
UnionListReaderPositionValidator.checkListBufferReadable(
vector.getOffsetBuffer(), index, OFFSET_WIDTH);

super.setPosition(index);
currentOffset = vector.getElementStartIndex(index) - 1;
maxOffset = vector.getElementEndIndex(index);
}

private void setEmptyPosition(int index) {
super.setPosition(index);
currentOffset = 0;
maxOffset = 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.arrow.vector.complex.impl;

import org.apache.arrow.memory.ArrowBuf;

/**
* Utility class for validating list and list view reader positions.
*
* <p>A list vector contains an offset buffer that denotes lists boundaries. A list view vector
* contains an offset buffer and a size buffer.
*/
final class UnionListReaderPositionValidator {

private UnionListReaderPositionValidator() {}

/** Check if the given index is within the current value count of the vector. */
static void checkIndex(int index, int valueCount) {
if (index < 0 || index >= valueCount) {
throw new IndexOutOfBoundsException(
String.format("index: %s, expected range [0, %s)", index, valueCount));
}
}

/** Check that the list offset buffer contains entries for {@code index} and {@code index + 1}. */
static void checkListBufferReadable(ArrowBuf offsetBuffer, int index, long offsetWidth) {
long requiredBytes = ((long) index + 2L) * offsetWidth;
long capacity = offsetBuffer.capacity();
if (capacity < requiredBytes) {
throw new IndexOutOfBoundsException(
String.format(
"Offset buffer has capacity %s but reading index %s requires %s bytes",
capacity, index, requiredBytes));
}
}

/** Check that the list view offset and size buffers contain entries for {@code index}. */
static void checkListViewBufferReadable(
ArrowBuf offsetBuffer, ArrowBuf sizeBuffer, int index, long offsetWidth, long sizeWidth) {
long requiredOffsetBytes = ((long) index + 1L) * offsetWidth;
long offsetCapacity = offsetBuffer.capacity();
if (offsetCapacity < requiredOffsetBytes) {
throw new IndexOutOfBoundsException(
String.format(
"Offset buffer has capacity %s but reading index %s requires %s bytes",
offsetCapacity, index, requiredOffsetBytes));
}

long requiredSizeBytes = ((long) index + 1L) * sizeWidth;
long sizeCapacity = sizeBuffer.capacity();
if (sizeCapacity < requiredSizeBytes) {
throw new IndexOutOfBoundsException(
String.format(
"Size buffer has capacity %s but reading index %s requires %s bytes",
sizeCapacity, index, requiredSizeBytes));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.arrow.vector.complex.impl;

import org.apache.arrow.vector.ValueVector;
import org.apache.arrow.vector.complex.BaseRepeatedValueViewVector;
import org.apache.arrow.vector.complex.ListViewVector;
import org.apache.arrow.vector.complex.reader.FieldReader;
import org.apache.arrow.vector.holders.UnionHolder;
Expand Down Expand Up @@ -54,15 +53,29 @@ public boolean isSet() {

@Override
public void setPosition(int index) {
super.setPosition(index);
if (vector.getOffsetBuffer().capacity() == 0) {
currentOffset = 0;
size = 0;
} else {
currentOffset =
vector.getOffsetBuffer().getInt(index * (long) BaseRepeatedValueViewVector.OFFSET_WIDTH);
size = vector.getSizeBuffer().getInt(index * (long) BaseRepeatedValueViewVector.SIZE_WIDTH);
int valueCount = vector.getValueCount();
if (valueCount == 0 && index == 0) {
setEmptyPosition(index);
return;
}

UnionListReaderPositionValidator.checkIndex(index, valueCount);
UnionListReaderPositionValidator.checkListViewBufferReadable(
vector.getOffsetBuffer(),
vector.getSizeBuffer(),
index,
ListViewVector.OFFSET_WIDTH,
ListViewVector.SIZE_WIDTH);

super.setPosition(index);
currentOffset = vector.getElementStartIndex(index);
size = vector.getElementEndIndex(index) - currentOffset;
}

private void setEmptyPosition(int index) {
super.setPosition(index);
currentOffset = 0;
size = 0;
}

@Override
Expand Down
Loading
Loading