Skip to content

Commit fa43106

Browse files
authored
GH-37705: [Java] Extra input methods for binary writers (#37791)
### Rationale for this change ByteBuffer and byte[] are commonly used to hold binary data. The current writers require working with ArrowBuf objects which need to be populated by copying from these types, then copying into the vector. ### What changes are included in this PR? Add methods to VarBinary and LargeVarBinary writers to take in common binary parameters - byte[] and ByteBuffer. The writer now sets these objects on the Vectors directly. ### Are these changes tested? Yes. * Closes: #37705 Authored-by: James Duong <duong.james@gmail.com> Signed-off-by: David Li <li.davidm96@gmail.com>
1 parent 0e6a683 commit fa43106

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed

java/vector/src/main/codegen/templates/AbstractFieldWriter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ public void write(${name}Holder holder) {
124124
}
125125
</#if>
126126

127+
<#if minor.class?ends_with("VarBinary")>
128+
public void writeTo${minor.class}(byte[] value) {
129+
fail("${name}");
130+
}
131+
132+
public void writeTo${minor.class}(byte[] value, int offset, int length) {
133+
fail("${name}");
134+
}
135+
136+
public void writeTo${minor.class}(ByteBuffer value) {
137+
fail("${name}");
138+
}
139+
140+
public void writeTo${minor.class}(ByteBuffer value, int offset, int length) {
141+
fail("${name}");
142+
}
143+
</#if>
144+
127145
</#list></#list>
128146

129147
public void writeNull() {

java/vector/src/main/codegen/templates/ComplexWriters.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,28 @@ public void writeNull() {
180180
vector.setValueCount(idx()+1);
181181
}
182182
</#if>
183+
184+
<#if minor.class?ends_with("VarBinary")>
185+
public void writeTo${minor.class}(byte[] value) {
186+
vector.setSafe(idx(), value);
187+
vector.setValueCount(idx() + 1);
188+
}
189+
190+
public void writeTo${minor.class}(byte[] value, int offset, int length) {
191+
vector.setSafe(idx(), value, offset, length);
192+
vector.setValueCount(idx() + 1);
193+
}
194+
195+
public void writeTo${minor.class}(ByteBuffer value) {
196+
vector.setSafe(idx(), value, 0, value.remaining());
197+
vector.setValueCount(idx() + 1);
198+
}
199+
200+
public void writeTo${minor.class}(ByteBuffer value, int offset, int length) {
201+
vector.setSafe(idx(), value, offset, length);
202+
vector.setValueCount(idx() + 1);
203+
}
204+
</#if>
183205
}
184206
185207
<@pp.changeOutputFile name="/org/apache/arrow/vector/complex/writer/${eName}Writer.java" />
@@ -223,6 +245,17 @@ public interface ${eName}Writer extends BaseWriter {
223245
@Deprecated
224246
public void writeBigEndianBytesTo${minor.class}(byte[] value);
225247
</#if>
248+
249+
<#if minor.class?ends_with("VarBinary")>
250+
public void writeTo${minor.class}(byte[] value);
251+
252+
public void writeTo${minor.class}(byte[] value, int offset, int length);
253+
254+
public void writeTo${minor.class}(ByteBuffer value);
255+
256+
public void writeTo${minor.class}(ByteBuffer value, int offset, int length);
257+
</#if>
258+
226259
}
227260

228261
</#list>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.arrow.vector.complex.writer;
19+
20+
import java.nio.ByteBuffer;
21+
22+
import org.apache.arrow.memory.BufferAllocator;
23+
import org.apache.arrow.memory.RootAllocator;
24+
import org.apache.arrow.vector.LargeVarBinaryVector;
25+
import org.apache.arrow.vector.VarBinaryVector;
26+
import org.apache.arrow.vector.complex.impl.LargeVarBinaryWriterImpl;
27+
import org.apache.arrow.vector.complex.impl.VarBinaryWriterImpl;
28+
import org.junit.After;
29+
import org.junit.Assert;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
33+
public class TestSimpleWriter {
34+
35+
private BufferAllocator allocator;
36+
37+
@Before
38+
public void init() {
39+
allocator = new RootAllocator(Integer.MAX_VALUE);
40+
}
41+
42+
@After
43+
public void terminate() throws Exception {
44+
allocator.close();
45+
}
46+
47+
@Test
48+
public void testWriteByteArrayToVarBinary() {
49+
try (VarBinaryVector vector = new VarBinaryVector("test", allocator);
50+
VarBinaryWriterImpl writer = new VarBinaryWriterImpl(vector)) {
51+
byte[] input = new byte[] { 0x01, 0x02 };
52+
writer.writeToVarBinary(input);
53+
byte[] result = vector.get(0);
54+
Assert.assertArrayEquals(input, result);
55+
}
56+
}
57+
58+
@Test
59+
public void testWriteByteArrayWithOffsetToVarBinary() {
60+
try (VarBinaryVector vector = new VarBinaryVector("test", allocator);
61+
VarBinaryWriterImpl writer = new VarBinaryWriterImpl(vector)) {
62+
byte[] input = new byte[] { 0x01, 0x02 };
63+
writer.writeToVarBinary(input, 1, 1);
64+
byte[] result = vector.get(0);
65+
Assert.assertArrayEquals(new byte[] { 0x02 }, result);
66+
}
67+
}
68+
69+
@Test
70+
public void testWriteByteBufferToVarBinary() {
71+
try (VarBinaryVector vector = new VarBinaryVector("test", allocator);
72+
VarBinaryWriterImpl writer = new VarBinaryWriterImpl(vector)) {
73+
byte[] input = new byte[] { 0x01, 0x02 };
74+
ByteBuffer buffer = ByteBuffer.wrap(input);
75+
writer.writeToVarBinary(buffer);
76+
byte[] result = vector.get(0);
77+
Assert.assertArrayEquals(input, result);
78+
}
79+
}
80+
81+
@Test
82+
public void testWriteByteBufferWithOffsetToVarBinary() {
83+
try (VarBinaryVector vector = new VarBinaryVector("test", allocator);
84+
VarBinaryWriterImpl writer = new VarBinaryWriterImpl(vector)) {
85+
byte[] input = new byte[] { 0x01, 0x02 };
86+
ByteBuffer buffer = ByteBuffer.wrap(input);
87+
writer.writeToVarBinary(buffer, 1, 1);
88+
byte[] result = vector.get(0);
89+
Assert.assertArrayEquals(new byte[] { 0x02 }, result);
90+
}
91+
}
92+
93+
@Test
94+
public void testWriteByteArrayToLargeVarBinary() {
95+
try (LargeVarBinaryVector vector = new LargeVarBinaryVector("test", allocator);
96+
LargeVarBinaryWriterImpl writer = new LargeVarBinaryWriterImpl(vector)) {
97+
byte[] input = new byte[] { 0x01, 0x02 };
98+
writer.writeToLargeVarBinary(input);
99+
byte[] result = vector.get(0);
100+
Assert.assertArrayEquals(input, result);
101+
}
102+
}
103+
104+
@Test
105+
public void testWriteByteArrayWithOffsetToLargeVarBinary() {
106+
try (LargeVarBinaryVector vector = new LargeVarBinaryVector("test", allocator);
107+
LargeVarBinaryWriterImpl writer = new LargeVarBinaryWriterImpl(vector)) {
108+
byte[] input = new byte[] { 0x01, 0x02 };
109+
writer.writeToLargeVarBinary(input, 1, 1);
110+
byte[] result = vector.get(0);
111+
Assert.assertArrayEquals(new byte[] { 0x02 }, result);
112+
}
113+
}
114+
115+
@Test
116+
public void testWriteByteBufferToLargeVarBinary() {
117+
try (LargeVarBinaryVector vector = new LargeVarBinaryVector("test", allocator);
118+
LargeVarBinaryWriterImpl writer = new LargeVarBinaryWriterImpl(vector)) {
119+
byte[] input = new byte[] { 0x01, 0x02 };
120+
ByteBuffer buffer = ByteBuffer.wrap(input);
121+
writer.writeToLargeVarBinary(buffer);
122+
byte[] result = vector.get(0);
123+
Assert.assertArrayEquals(input, result);
124+
}
125+
}
126+
127+
@Test
128+
public void testWriteByteBufferWithOffsetToLargeVarBinary() {
129+
try (LargeVarBinaryVector vector = new LargeVarBinaryVector("test", allocator);
130+
LargeVarBinaryWriterImpl writer = new LargeVarBinaryWriterImpl(vector)) {
131+
byte[] input = new byte[] { 0x01, 0x02 };
132+
ByteBuffer buffer = ByteBuffer.wrap(input);
133+
writer.writeToLargeVarBinary(buffer, 1, 1);
134+
byte[] result = vector.get(0);
135+
Assert.assertArrayEquals(new byte[] { 0x02 }, result);
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)