Skip to content
Closed
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
2 changes: 1 addition & 1 deletion java/memory/src/main/java/io/netty/buffer/ArrowBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ public ArrowBuf setBytes(int index, ByteBuffer src, int srcIndex, int length) {
ByteBuffer newBuf = src.duplicate();
newBuf.position(srcIndex);
newBuf.limit(srcIndex + length);
udle.setBytes(index + offset, src);
udle.setBytes(index + offset, newBuf);
}
}

Expand Down
85 changes: 85 additions & 0 deletions java/memory/src/test/java/io/netty/buffer/TestArrowBuf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* 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 io.netty.buffer;

import java.nio.ByteBuffer;
import java.util.Arrays;
import org.apache.arrow.memory.RootAllocator;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

public class TestArrowBuf {

private final static int MAX_ALLOCATION = 8 * 1024;
private static RootAllocator allocator;

@BeforeClass
public static void beforeClass() {
allocator = new RootAllocator(MAX_ALLOCATION);
}

@AfterClass
public static void afterClass() {
if (allocator != null) {
allocator.close();
}
}

@Test
public void testSetBytesSliced() {
int arrLength = 64;
byte[] expecteds = new byte[arrLength];
for (int i = 0; i < expecteds.length; i++) {
expecteds[i] = (byte) i;
}
ByteBuffer data = ByteBuffer.wrap(expecteds);
try (ArrowBuf buf = allocator.buffer(expecteds.length)) {
buf.setBytes(0, data, 0, data.capacity());

byte[] actuals = new byte[expecteds.length];
buf.getBytes(0, actuals);
assertArrayEquals(expecteds, actuals);
}
}

@Test
public void testSetBytesUnsliced() {
int arrLength = 64;
byte[] arr = new byte[arrLength];
for (int i = 0; i < arr.length; i++) {
arr[i] = (byte) i;
}
ByteBuffer data = ByteBuffer.wrap(arr);

int from = 10;
int to = arrLength;
byte[] expecteds = Arrays.copyOfRange(arr, from, to);
try (ArrowBuf buf = allocator.buffer(expecteds.length)) {
buf.setBytes(0, data, from, to - from);

byte[] actuals = new byte[expecteds.length];
buf.getBytes(0, actuals);
assertArrayEquals(expecteds, actuals);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
public class TestUtils {

public static NullableVarCharVector newNullableVarCharVector(String name, BufferAllocator allocator) {
return (NullableVarCharVector) FieldType.nullable(new ArrowType.Utf8()).createNewSingleVector(name, allocator, null);
return (NullableVarCharVector)
FieldType.nullable(new ArrowType.Utf8()).createNewSingleVector(name, allocator, null);
}

public static NullableVarBinaryVector newNullableVarBinaryVector(String name, BufferAllocator allocator) {
return (NullableVarBinaryVector)
FieldType.nullable(new ArrowType.Binary()).createNewSingleVector(name, allocator, null);
}

public static <T> T newVector(Class<T> c, String name, ArrowType type, BufferAllocator allocator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
*/

package org.apache.arrow.vector;
import org.apache.arrow.vector.holders.VarCharHolder;
import org.apache.arrow.vector.util.OversizedAllocationException;

import static org.apache.arrow.vector.TestUtils.newNullableVarBinaryVector;
import static org.apache.arrow.vector.TestUtils.newNullableVarCharVector;
import static org.apache.arrow.vector.TestUtils.newVector;
import static org.junit.Assert.assertArrayEquals;
Expand All @@ -27,7 +29,9 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

Expand Down Expand Up @@ -787,12 +791,12 @@ public void testNullableFixedType3() {
* Covered types as of now
*
* -- NullableVarCharVector
* -- NullableVarBinaryVector
*
* TODO:
*
* -- VarCharVector
* -- VarBinaryVector
* -- NullableVarBinaryVector
*/

@Test /* NullableVarCharVector */
Expand All @@ -806,17 +810,26 @@ public void testNullableVarType1() {
m.set(0, STR1);
m.set(1, STR2);
m.set(2, STR3);
m.setSafe(3, STR3, 1, STR3.length - 1);
m.setSafe(4, STR3, 2, STR3.length - 2);
ByteBuffer STR3ByteBuffer = ByteBuffer.wrap(STR3);
m.setSafe(5, STR3ByteBuffer, 1, STR3.length - 1);
m.setSafe(6, STR3ByteBuffer, 2, STR3.length - 2);

// Check the sample strings.
final NullableVarCharVector.Accessor accessor = vector.getAccessor();
assertArrayEquals(STR1, accessor.get(0));
assertArrayEquals(STR2, accessor.get(1));
assertArrayEquals(STR3, accessor.get(2));
assertArrayEquals(Arrays.copyOfRange(STR3, 1, STR3.length), accessor.get(3));
assertArrayEquals(Arrays.copyOfRange(STR3, 2, STR3.length), accessor.get(4));
assertArrayEquals(Arrays.copyOfRange(STR3, 1, STR3.length), accessor.get(5));
assertArrayEquals(Arrays.copyOfRange(STR3, 2, STR3.length), accessor.get(6));

// Ensure null value throws.
boolean b = false;
try {
vector.getAccessor().get(3);
vector.getAccessor().get(7);
} catch (IllegalStateException e) {
b = true;
} finally {
Expand All @@ -825,6 +838,46 @@ public void testNullableVarType1() {
}
}

@Test /* NullableVarBinaryVector */
public void testNullableVarType2() {

// Create a new value vector for 1024 integers.
try (final NullableVarBinaryVector vector = newNullableVarBinaryVector(EMPTY_SCHEMA_PATH, allocator)) {
final NullableVarBinaryVector.Mutator m = vector.getMutator();
vector.allocateNew(1024 * 10, 1024);

m.set(0, STR1);
m.set(1, STR2);
m.set(2, STR3);
m.setSafe(3, STR3, 1, STR3.length - 1);
m.setSafe(4, STR3, 2, STR3.length - 2);
ByteBuffer STR3ByteBuffer = ByteBuffer.wrap(STR3);
m.setSafe(5, STR3ByteBuffer, 1, STR3.length - 1);
m.setSafe(6, STR3ByteBuffer, 2, STR3.length - 2);

// Check the sample strings.
final NullableVarBinaryVector.Accessor accessor = vector.getAccessor();
assertArrayEquals(STR1, accessor.get(0));
assertArrayEquals(STR2, accessor.get(1));
assertArrayEquals(STR3, accessor.get(2));
assertArrayEquals(Arrays.copyOfRange(STR3, 1, STR3.length), accessor.get(3));
assertArrayEquals(Arrays.copyOfRange(STR3, 2, STR3.length), accessor.get(4));
assertArrayEquals(Arrays.copyOfRange(STR3, 1, STR3.length), accessor.get(5));
assertArrayEquals(Arrays.copyOfRange(STR3, 2, STR3.length), accessor.get(6));

// Ensure null value throws.
boolean b = false;
try {
vector.getAccessor().get(7);
} catch (IllegalStateException e) {
b = true;
} finally {
assertTrue(b);
}
}
}


/*
* generic tests
*
Expand Down