Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-246: [Java] UnionVector doesn't call allocateNew() when creating it's vect… #110

Closed
wants to merge 3 commits into from
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: 2 additions & 0 deletions java/vector/src/main/codegen/templates/UnionVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public UnionVector(MaterializedField field, BufferAllocator allocator, CallBack
this.allocator = allocator;
this.internalMap = new MapVector("internal", allocator, callBack);
this.typeVector = internalMap.addOrGet("types", new MajorType(MinorType.UINT1, DataMode.REQUIRED), UInt1Vector.class);
this.typeVector.allocateNew();
this.typeVector.zeroVector();
this.field.addChild(internalMap.getField().clone());
this.majorType = field.getType();
this.callBack = callBack;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* 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 org.apache.arrow.memory.AllocationReservation;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.BufferManager;

import io.netty.buffer.ArrowBuf;
import io.netty.buffer.ByteBufAllocator;

/**
* Wrapper around a buffer delegate that populates any allocated buffer with a constant
* value. Useful for testing if value vectors are properly resetting their buffers.
*/
public class DirtyBufferAllocator implements BufferAllocator {

private final BufferAllocator delegate;
private final byte fillValue;

DirtyBufferAllocator(final BufferAllocator delegate, final byte fillValue) {
this.delegate = delegate;
this.fillValue = fillValue;
}

@Override
public ArrowBuf buffer(int size) {
return buffer(size, null);
}

@Override
public ArrowBuf buffer(int size, BufferManager manager) {
ArrowBuf buffer = delegate.buffer(size, manager);
// contaminate the buffer
for (int i = 0; i < buffer.capacity(); i++) {
buffer.setByte(i, fillValue);
}

return buffer;
}

@Override
public ByteBufAllocator getAsByteBufAllocator() {
return delegate.getAsByteBufAllocator();
}

@Override
public BufferAllocator newChildAllocator(String name, long initReservation, long maxAllocation) {
return delegate.newChildAllocator(name, initReservation, maxAllocation);
}

@Override
public void close() {
delegate.close();
}

@Override
public long getAllocatedMemory() {
return delegate.getAllocatedMemory();
}

@Override
public void setLimit(long newLimit) {
delegate.setLimit(newLimit);
}

@Override
public long getLimit() {
return delegate.getLimit();
}

@Override
public long getPeakMemoryAllocation() {
return delegate.getPeakMemoryAllocation();
}

@Override
public AllocationReservation newReservation() {
return delegate.newReservation();
}

@Override
public ArrowBuf getEmpty() {
return delegate.getEmpty();
}

@Override
public String getName() {
return delegate.getName();
}

@Override
public boolean isOverLimit() {
return delegate.isOverLimit();
}

@Override
public String toVerboseString() {
return delegate.toVerboseString();
}

@Override
public void assertOpen() {
delegate.assertOpen();
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* 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.junit.Assert.assertEquals;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.complex.UnionVector;
import org.apache.arrow.vector.holders.NullableUInt4Holder;
import org.apache.arrow.vector.holders.UInt4Holder;
import org.apache.arrow.vector.types.MaterializedField;
import org.apache.arrow.vector.types.Types;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestUnionVector {
private final static String EMPTY_SCHEMA_PATH = "";

private BufferAllocator allocator;

@Before
public void init() {
allocator = new RootAllocator(Long.MAX_VALUE);
}

@After
public void terminate() throws Exception {
allocator.close();
}

@Test
public void testUnionVector() throws Exception {
final MaterializedField field = MaterializedField.create(EMPTY_SCHEMA_PATH, UInt4Holder.TYPE);

final BufferAllocator alloc = new DirtyBufferAllocator(allocator, (byte) 100);

UnionVector unionVector = new UnionVector(field, alloc, null);

final NullableUInt4Holder uInt4Holder = new NullableUInt4Holder();
uInt4Holder.value = 100;
uInt4Holder.isSet = 1;

try {
// write some data
final UnionVector.Mutator mutator = unionVector.getMutator();
mutator.setType(0, Types.MinorType.UINT4);
mutator.setSafe(0, uInt4Holder);
mutator.setType(2, Types.MinorType.UINT4);
mutator.setSafe(2, uInt4Holder);
mutator.setValueCount(4);

// check that what we wrote is correct
final UnionVector.Accessor accessor = unionVector.getAccessor();
assertEquals(4, accessor.getValueCount());

assertEquals(false, accessor.isNull(0));
assertEquals(100, accessor.getObject(0));

assertEquals(true, accessor.isNull(1));

assertEquals(false, accessor.isNull(2));
assertEquals(100, accessor.getObject(2));

assertEquals(true, accessor.isNull(3));

} finally {
unionVector.clear();
}
}

}