|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.comet.vector; |
| 21 | + |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import org.apache.arrow.memory.RootAllocator; |
| 27 | +import org.apache.arrow.vector.VarBinaryVector; |
| 28 | +import org.apache.arrow.vector.VarCharVector; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertArrayEquals; |
| 31 | +import static org.junit.Assert.assertEquals; |
| 32 | +import static org.junit.Assert.assertNull; |
| 33 | + |
| 34 | +public class TestCometPlainVector { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testGetUTF8StringWithVariableWidthVector() { |
| 38 | + try (RootAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { |
| 39 | + VarCharVector vector = new VarCharVector("strings", allocator); |
| 40 | + vector.allocateNew(); |
| 41 | + vector.setSafe(0, bytes("alpha")); |
| 42 | + vector.setSafe(1, bytes("")); |
| 43 | + vector.setSafe(2, bytes("spark")); |
| 44 | + vector.setValueCount(4); // row 3 is null (validity bit not set) |
| 45 | + |
| 46 | + try (CometPlainVector cv = new CometPlainVector(vector, false)) { |
| 47 | + assertEquals("alpha", cv.getUTF8String(0).toString()); |
| 48 | + assertEquals("", cv.getUTF8String(1).toString()); |
| 49 | + assertEquals("spark", cv.getUTF8String(2).toString()); |
| 50 | + assertNull(cv.getUTF8String(3)); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testGetBinaryWithVariableWidthVector() { |
| 57 | + try (RootAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { |
| 58 | + VarBinaryVector vector = new VarBinaryVector("bytes", allocator); |
| 59 | + vector.allocateNew(); |
| 60 | + vector.setSafe(0, new byte[] {1, 2, 3}, 0, 3); |
| 61 | + vector.setSafe(1, new byte[0], 0, 0); |
| 62 | + vector.setSafe(2, new byte[] {4, 5}, 0, 2); |
| 63 | + vector.setValueCount(4); // row 3 is null (validity bit not set) |
| 64 | + |
| 65 | + try (CometPlainVector cv = new CometPlainVector(vector, false)) { |
| 66 | + assertArrayEquals(new byte[] {1, 2, 3}, cv.getBinary(0)); |
| 67 | + assertArrayEquals(new byte[0], cv.getBinary(1)); |
| 68 | + assertArrayEquals(new byte[] {4, 5}, cv.getBinary(2)); |
| 69 | + assertNull(cv.getBinary(3)); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static byte[] bytes(String s) { |
| 75 | + return s.getBytes(StandardCharsets.UTF_8); |
| 76 | + } |
| 77 | +} |
0 commit comments