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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.vector
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertNull
;
23 import java
.time
.Duration
;
25 import org
.apache
.arrow
.memory
.RootAllocator
;
26 import org
.apache
.arrow
.vector
.holders
.NullableDurationHolder
;
27 import org
.apache
.arrow
.vector
.types
.TimeUnit
;
28 import org
.apache
.arrow
.vector
.types
.pojo
.ArrowType
;
29 import org
.junit
.After
;
30 import org
.junit
.Before
;
31 import org
.junit
.Test
;
33 public class TestDurationVector
{
34 RootAllocator allocator
;
38 allocator
= new DirtyRootAllocator(Long
.MAX_VALUE
, (byte) 100);
42 public void terminate() {
47 public void testSecBasics() {
48 try (DurationVector secVector
= TestUtils
.newVector(DurationVector
.class, "second",
49 new ArrowType
.Duration(TimeUnit
.SECOND
), allocator
)) {
51 secVector
.allocateNew();
53 secVector
.setSafe(1, 1000);
54 secVector
.setValueCount(2);
55 assertNull(secVector
.getObject(0));
56 assertEquals(Duration
.ofSeconds(1000), secVector
.getObject(1));
57 assertNull(secVector
.getAsStringBuilder(0));
58 assertEquals("PT16M40S", secVector
.getAsStringBuilder(1).toString());
60 NullableDurationHolder holder
= new NullableDurationHolder();
61 secVector
.get(0, holder
);
62 assertEquals(0, holder
.isSet
);
63 secVector
.get(1, holder
);
64 assertEquals(1 , holder
.isSet
);
65 assertEquals(1000 , holder
.value
);
70 public void testMilliBasics() {
71 try (DurationVector milliVector
= TestUtils
.newVector(DurationVector
.class, "nanos",
72 new ArrowType
.Duration(TimeUnit
.MILLISECOND
), allocator
)) {
74 milliVector
.allocateNew();
75 milliVector
.setNull(0);
76 milliVector
.setSafe(1, 1000);
77 milliVector
.setValueCount(2);
78 assertNull(milliVector
.getObject(0));
79 assertEquals(Duration
.ofSeconds(1), milliVector
.getObject(1));
80 assertNull(milliVector
.getAsStringBuilder(0));
81 assertEquals("PT1S", milliVector
.getAsStringBuilder(1).toString());
83 NullableDurationHolder holder
= new NullableDurationHolder();
84 milliVector
.get(0, holder
);
85 assertEquals(0, holder
.isSet
);
86 milliVector
.get(1, holder
);
87 assertEquals(1 , holder
.isSet
);
88 assertEquals(1000 , holder
.value
);
93 public void testMicroBasics() {
94 try (DurationVector microVector
= TestUtils
.newVector(DurationVector
.class, "micro",
95 new ArrowType
.Duration(TimeUnit
.MICROSECOND
), allocator
)) {
97 microVector
.allocateNew();
98 microVector
.setNull(0);
99 microVector
.setSafe(1, 1000);
100 microVector
.setValueCount(2);
101 assertNull(microVector
.getObject(0));
102 assertEquals(Duration
.ofMillis(1), microVector
.getObject(1));
103 assertNull(microVector
.getAsStringBuilder(0));
104 assertEquals("PT0.001S", microVector
.getAsStringBuilder(1).toString());
106 NullableDurationHolder holder
= new NullableDurationHolder();
107 microVector
.get(0, holder
);
108 assertEquals(0, holder
.isSet
);
109 microVector
.get(1, holder
);
110 assertEquals(1 , holder
.isSet
);
111 assertEquals(1000 , holder
.value
);
116 public void testNanosBasics() {
117 try (DurationVector nanoVector
= TestUtils
.newVector(DurationVector
.class, "nanos",
118 new ArrowType
.Duration(TimeUnit
.NANOSECOND
), allocator
)) {
120 nanoVector
.allocateNew();
121 nanoVector
.setNull(0);
122 nanoVector
.setSafe(1, 1000000);
123 nanoVector
.setValueCount(2);
124 assertNull(nanoVector
.getObject(0));
125 assertEquals(Duration
.ofMillis(1), nanoVector
.getObject(1));
126 assertNull(nanoVector
.getAsStringBuilder(0));
127 assertEquals("PT0.001S", nanoVector
.getAsStringBuilder(1).toString());
129 NullableDurationHolder holder
= new NullableDurationHolder();
130 nanoVector
.get(0, holder
);
131 assertEquals(0, holder
.isSet
);
132 nanoVector
.get(1, holder
);
133 assertEquals(1 , holder
.isSet
);
134 assertEquals(1000000 , holder
.value
);