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
.c
;
20 import static org
.junit
.jupiter
.api
.Assertions
.assertEquals
;
21 import static org
.junit
.jupiter
.api
.Assertions
.assertThrows
;
22 import static org
.junit
.jupiter
.api
.Assertions
.assertTrue
;
24 import org
.apache
.arrow
.c
.Flags
;
25 import org
.apache
.arrow
.c
.Format
;
26 import org
.apache
.arrow
.vector
.types
.DateUnit
;
27 import org
.apache
.arrow
.vector
.types
.FloatingPointPrecision
;
28 import org
.apache
.arrow
.vector
.types
.IntervalUnit
;
29 import org
.apache
.arrow
.vector
.types
.TimeUnit
;
30 import org
.apache
.arrow
.vector
.types
.UnionMode
;
31 import org
.apache
.arrow
.vector
.types
.pojo
.ArrowType
;
32 import org
.junit
.jupiter
.api
.Test
;
34 public class FormatTest
{
36 public void testAsString() {
37 assertEquals("z", Format
.asString(new ArrowType
.Binary()));
38 assertEquals("b", Format
.asString(new ArrowType
.Bool()));
39 assertEquals("tdD", Format
.asString(new ArrowType
.Date(DateUnit
.DAY
)));
40 assertEquals("tdm", Format
.asString(new ArrowType
.Date(DateUnit
.MILLISECOND
)));
41 assertEquals("d:1,1", Format
.asString(new ArrowType
.Decimal(1, 1, 128)));
42 assertEquals("d:1,1,1", Format
.asString(new ArrowType
.Decimal(1, 1, 1)));
43 assertEquals("d:9,1,1", Format
.asString(new ArrowType
.Decimal(9, 1, 1)));
44 assertEquals("tDs", Format
.asString(new ArrowType
.Duration(TimeUnit
.SECOND
)));
45 assertEquals("tDm", Format
.asString(new ArrowType
.Duration(TimeUnit
.MILLISECOND
)));
46 assertEquals("tDu", Format
.asString(new ArrowType
.Duration(TimeUnit
.MICROSECOND
)));
47 assertEquals("tDn", Format
.asString(new ArrowType
.Duration(TimeUnit
.NANOSECOND
)));
48 assertEquals("w:1", Format
.asString(new ArrowType
.FixedSizeBinary(1)));
49 assertEquals("+w:3", Format
.asString(new ArrowType
.FixedSizeList(3)));
50 assertEquals("e", Format
.asString(new ArrowType
.FloatingPoint(FloatingPointPrecision
.HALF
)));
51 assertEquals("f", Format
.asString(new ArrowType
.FloatingPoint(FloatingPointPrecision
.SINGLE
)));
52 assertEquals("g", Format
.asString(new ArrowType
.FloatingPoint(FloatingPointPrecision
.DOUBLE
)));
53 assertEquals("c", Format
.asString(new ArrowType
.Int(Byte
.SIZE
, true)));
54 assertEquals("C", Format
.asString(new ArrowType
.Int(Byte
.SIZE
, false)));
55 assertEquals("s", Format
.asString(new ArrowType
.Int(Short
.SIZE
, true)));
56 assertEquals("S", Format
.asString(new ArrowType
.Int(Short
.SIZE
, false)));
57 assertEquals("i", Format
.asString(new ArrowType
.Int(Integer
.SIZE
, true)));
58 assertEquals("I", Format
.asString(new ArrowType
.Int(Integer
.SIZE
, false)));
59 assertEquals("l", Format
.asString(new ArrowType
.Int(Long
.SIZE
, true)));
60 assertEquals("L", Format
.asString(new ArrowType
.Int(Long
.SIZE
, false)));
61 assertEquals("tiD", Format
.asString(new ArrowType
.Interval(IntervalUnit
.DAY_TIME
)));
62 assertEquals("tiM", Format
.asString(new ArrowType
.Interval(IntervalUnit
.YEAR_MONTH
)));
63 assertEquals("Z", Format
.asString(new ArrowType
.LargeBinary()));
64 assertEquals("+L", Format
.asString(new ArrowType
.LargeList()));
65 assertEquals("U", Format
.asString(new ArrowType
.LargeUtf8()));
66 assertEquals("+l", Format
.asString(new ArrowType
.List()));
67 assertEquals("+m", Format
.asString(new ArrowType
.Map(true)));
68 assertEquals("n", Format
.asString(new ArrowType
.Null()));
69 assertEquals("+s", Format
.asString(new ArrowType
.Struct()));
70 assertEquals("tts", Format
.asString(new ArrowType
.Time(TimeUnit
.SECOND
, 32)));
71 assertEquals("ttm", Format
.asString(new ArrowType
.Time(TimeUnit
.MILLISECOND
, 32)));
72 assertEquals("ttu", Format
.asString(new ArrowType
.Time(TimeUnit
.MICROSECOND
, 64)));
73 assertEquals("ttn", Format
.asString(new ArrowType
.Time(TimeUnit
.NANOSECOND
, 64)));
74 assertEquals("tss:Timezone", Format
.asString(new ArrowType
.Timestamp(TimeUnit
.SECOND
, "Timezone")));
75 assertEquals("tsm:Timezone", Format
.asString(new ArrowType
.Timestamp(TimeUnit
.MILLISECOND
, "Timezone")));
76 assertEquals("tsu:Timezone", Format
.asString(new ArrowType
.Timestamp(TimeUnit
.MICROSECOND
, "Timezone")));
77 assertEquals("tsn:Timezone", Format
.asString(new ArrowType
.Timestamp(TimeUnit
.NANOSECOND
, "Timezone")));
78 assertEquals("+us:1,1,1", Format
.asString(new ArrowType
.Union(UnionMode
.Sparse
, new int[] { 1, 1, 1 })));
79 assertEquals("+ud:1,1,1", Format
.asString(new ArrowType
.Union(UnionMode
.Dense
, new int[] { 1, 1, 1 })));
80 assertEquals("u", Format
.asString(new ArrowType
.Utf8()));
82 assertThrows(UnsupportedOperationException
.class, () -> Format
.asString(new ArrowType
.Int(1, true)));
83 assertThrows(UnsupportedOperationException
.class, () -> Format
.asString(new ArrowType
.Time(TimeUnit
.SECOND
, 1)));
84 assertThrows(UnsupportedOperationException
.class,
85 () -> Format
.asString(new ArrowType
.Time(TimeUnit
.MILLISECOND
, 64)));
89 public void testAsType() throws IllegalStateException
, NumberFormatException
, UnsupportedOperationException
{
90 assertTrue(Format
.asType("n", 0L) instanceof ArrowType
.Null
);
91 assertTrue(Format
.asType("b", 0L) instanceof ArrowType
.Bool
);
92 assertEquals(new ArrowType
.Int(Byte
.SIZE
, true), Format
.asType("c", 0L));
93 assertEquals(new ArrowType
.Int(Byte
.SIZE
, false), Format
.asType("C", 0L));
94 assertEquals(new ArrowType
.Int(Short
.SIZE
, true), Format
.asType("s", 0L));
95 assertEquals(new ArrowType
.Int(Short
.SIZE
, false), Format
.asType("S", 0L));
96 assertEquals(new ArrowType
.Int(Integer
.SIZE
, true), Format
.asType("i", 0L));
97 assertEquals(new ArrowType
.Int(Integer
.SIZE
, false), Format
.asType("I", 0L));
98 assertEquals(new ArrowType
.Int(Long
.SIZE
, true), Format
.asType("l", 0L));
99 assertEquals(new ArrowType
.Int(Long
.SIZE
, false), Format
.asType("L", 0L));
100 assertEquals(new ArrowType
.FloatingPoint(FloatingPointPrecision
.HALF
), Format
.asType("e", 0L));
101 assertEquals(new ArrowType
.FloatingPoint(FloatingPointPrecision
.SINGLE
), Format
.asType("f", 0L));
102 assertEquals(new ArrowType
.FloatingPoint(FloatingPointPrecision
.DOUBLE
), Format
.asType("g", 0L));
103 assertTrue(Format
.asType("z", 0L) instanceof ArrowType
.Binary
);
104 assertTrue(Format
.asType("Z", 0L) instanceof ArrowType
.LargeBinary
);
105 assertTrue(Format
.asType("u", 0L) instanceof ArrowType
.Utf8
);
106 assertTrue(Format
.asType("U", 0L) instanceof ArrowType
.LargeUtf8
);
107 assertEquals(new ArrowType
.Date(DateUnit
.DAY
), Format
.asType("tdD", 0L));
108 assertEquals(new ArrowType
.Date(DateUnit
.MILLISECOND
), Format
.asType("tdm", 0L));
109 assertEquals(new ArrowType
.Time(TimeUnit
.SECOND
, Integer
.SIZE
), Format
.asType("tts", 0L));
110 assertEquals(new ArrowType
.Time(TimeUnit
.MILLISECOND
, Integer
.SIZE
), Format
.asType("ttm", 0L));
111 assertEquals(new ArrowType
.Time(TimeUnit
.MICROSECOND
, Long
.SIZE
), Format
.asType("ttu", 0L));
112 assertEquals(new ArrowType
.Time(TimeUnit
.NANOSECOND
, Long
.SIZE
), Format
.asType("ttn", 0L));
113 assertEquals(new ArrowType
.Duration(TimeUnit
.SECOND
), Format
.asType("tDs", 0L));
114 assertEquals(new ArrowType
.Duration(TimeUnit
.MILLISECOND
), Format
.asType("tDm", 0L));
115 assertEquals(new ArrowType
.Duration(TimeUnit
.MICROSECOND
), Format
.asType("tDu", 0L));
116 assertEquals(new ArrowType
.Duration(TimeUnit
.NANOSECOND
), Format
.asType("tDn", 0L));
117 assertEquals(new ArrowType
.Interval(IntervalUnit
.YEAR_MONTH
), Format
.asType("tiM", 0L));
118 assertEquals(new ArrowType
.Interval(IntervalUnit
.DAY_TIME
), Format
.asType("tiD", 0L));
119 assertTrue(Format
.asType("+l", 0L) instanceof ArrowType
.List
);
120 assertTrue(Format
.asType("+L", 0L) instanceof ArrowType
.LargeList
);
121 assertTrue(Format
.asType("+s", 0L) instanceof ArrowType
.Struct
);
122 assertEquals(new ArrowType
.Map(false), Format
.asType("+m", 0L));
123 assertEquals(new ArrowType
.Map(true), Format
.asType("+m", Flags
.ARROW_FLAG_MAP_KEYS_SORTED
));
124 assertEquals(new ArrowType
.Decimal(1, 1, 128), Format
.asType("d:1,1", 0L));
125 assertEquals(new ArrowType
.Decimal(1, 1, 1), Format
.asType("d:1,1,1", 0L));
126 assertEquals(new ArrowType
.Decimal(9, 1, 1), Format
.asType("d:9,1,1", 0L));
127 assertEquals(new ArrowType
.FixedSizeBinary(1), Format
.asType("w:1", 0L));
128 assertEquals(new ArrowType
.FixedSizeList(3), Format
.asType("+w:3", 0L));
129 assertEquals(new ArrowType
.Union(UnionMode
.Dense
, new int[] { 1, 1, 1 }), Format
.asType("+ud:1,1,1", 0L));
130 assertEquals(new ArrowType
.Union(UnionMode
.Sparse
, new int[] { 1, 1, 1 }), Format
.asType("+us:1,1,1", 0L));
131 assertEquals(new ArrowType
.Timestamp(TimeUnit
.SECOND
, "Timezone"), Format
.asType("tss:Timezone", 0L));
132 assertEquals(new ArrowType
.Timestamp(TimeUnit
.MILLISECOND
, "Timezone"), Format
.asType("tsm:Timezone", 0L));
133 assertEquals(new ArrowType
.Timestamp(TimeUnit
.MICROSECOND
, "Timezone"), Format
.asType("tsu:Timezone", 0L));
134 assertEquals(new ArrowType
.Timestamp(TimeUnit
.NANOSECOND
, "Timezone"), Format
.asType("tsn:Timezone", 0L));
136 assertThrows(UnsupportedOperationException
.class, () -> Format
.asType("Format", 0L));
137 assertThrows(UnsupportedOperationException
.class, () -> Format
.asType(":", 0L));
138 assertThrows(NumberFormatException
.class, () -> Format
.asType("w:1,2,3", 0L));