Repository navigation
Expand file tree
/
Copy pathFormatTest.java
More file actions
176 lines (169 loc) · 10.4 KB
/
Copy pathFormatTest.java
File metadata and controls
176 lines (169 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* 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.c;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Locale;
import org.apache.arrow.vector.types.DateUnit;
import org.apache.arrow.vector.types.FloatingPointPrecision;
import org.apache.arrow.vector.types.IntervalUnit;
import org.apache.arrow.vector.types.TimeUnit;
import org.apache.arrow.vector.types.UnionMode;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.junit.jupiter.api.Test;
public class FormatTest {
@Test
public void testAsString() {
assertEquals("z", Format.asString(new ArrowType.Binary()));
assertEquals("b", Format.asString(new ArrowType.Bool()));
assertEquals("tdD", Format.asString(new ArrowType.Date(DateUnit.DAY)));
assertEquals("tdm", Format.asString(new ArrowType.Date(DateUnit.MILLISECOND)));
assertEquals("d:1,1", Format.asString(new ArrowType.Decimal(1, 1, 128)));
assertEquals("d:1,1,1", Format.asString(new ArrowType.Decimal(1, 1, 1)));
assertEquals("d:9,1,1", Format.asString(new ArrowType.Decimal(9, 1, 1)));
assertEquals("tDs", Format.asString(new ArrowType.Duration(TimeUnit.SECOND)));
assertEquals("tDm", Format.asString(new ArrowType.Duration(TimeUnit.MILLISECOND)));
assertEquals("tDu", Format.asString(new ArrowType.Duration(TimeUnit.MICROSECOND)));
assertEquals("tDn", Format.asString(new ArrowType.Duration(TimeUnit.NANOSECOND)));
assertEquals("w:1", Format.asString(new ArrowType.FixedSizeBinary(1)));
assertEquals("+w:3", Format.asString(new ArrowType.FixedSizeList(3)));
assertEquals("e", Format.asString(new ArrowType.FloatingPoint(FloatingPointPrecision.HALF)));
assertEquals("f", Format.asString(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)));
assertEquals("g", Format.asString(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)));
assertEquals("c", Format.asString(new ArrowType.Int(Byte.SIZE, true)));
assertEquals("C", Format.asString(new ArrowType.Int(Byte.SIZE, false)));
assertEquals("s", Format.asString(new ArrowType.Int(Short.SIZE, true)));
assertEquals("S", Format.asString(new ArrowType.Int(Short.SIZE, false)));
assertEquals("i", Format.asString(new ArrowType.Int(Integer.SIZE, true)));
assertEquals("I", Format.asString(new ArrowType.Int(Integer.SIZE, false)));
assertEquals("l", Format.asString(new ArrowType.Int(Long.SIZE, true)));
assertEquals("L", Format.asString(new ArrowType.Int(Long.SIZE, false)));
assertEquals("tiD", Format.asString(new ArrowType.Interval(IntervalUnit.DAY_TIME)));
assertEquals("tiM", Format.asString(new ArrowType.Interval(IntervalUnit.YEAR_MONTH)));
assertEquals("Z", Format.asString(new ArrowType.LargeBinary()));
assertEquals("+L", Format.asString(new ArrowType.LargeList()));
assertEquals("U", Format.asString(new ArrowType.LargeUtf8()));
assertEquals("+l", Format.asString(new ArrowType.List()));
assertEquals("+m", Format.asString(new ArrowType.Map(true)));
assertEquals("n", Format.asString(new ArrowType.Null()));
assertEquals("+s", Format.asString(new ArrowType.Struct()));
assertEquals("tts", Format.asString(new ArrowType.Time(TimeUnit.SECOND, 32)));
assertEquals("ttm", Format.asString(new ArrowType.Time(TimeUnit.MILLISECOND, 32)));
assertEquals("ttu", Format.asString(new ArrowType.Time(TimeUnit.MICROSECOND, 64)));
assertEquals("ttn", Format.asString(new ArrowType.Time(TimeUnit.NANOSECOND, 64)));
assertEquals(
"tss:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.SECOND, "Timezone")));
assertEquals(
"tsm:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "Timezone")));
assertEquals(
"tsu:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.MICROSECOND, "Timezone")));
assertEquals(
"tsn:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.NANOSECOND, "Timezone")));
assertEquals(
"+us:1,1,1", Format.asString(new ArrowType.Union(UnionMode.Sparse, new int[] {1, 1, 1})));
assertEquals(
"+ud:1,1,1", Format.asString(new ArrowType.Union(UnionMode.Dense, new int[] {1, 1, 1})));
assertEquals("u", Format.asString(new ArrowType.Utf8()));
assertThrows(
UnsupportedOperationException.class, () -> Format.asString(new ArrowType.Int(1, true)));
assertThrows(
UnsupportedOperationException.class,
() -> Format.asString(new ArrowType.Time(TimeUnit.SECOND, 1)));
assertThrows(
UnsupportedOperationException.class,
() -> Format.asString(new ArrowType.Time(TimeUnit.MILLISECOND, 64)));
}
@Test
public void testAsType()
throws IllegalStateException, NumberFormatException, UnsupportedOperationException {
assertTrue(Format.asType("n", 0L) instanceof ArrowType.Null);
assertTrue(Format.asType("b", 0L) instanceof ArrowType.Bool);
assertEquals(new ArrowType.Int(Byte.SIZE, true), Format.asType("c", 0L));
assertEquals(new ArrowType.Int(Byte.SIZE, false), Format.asType("C", 0L));
assertEquals(new ArrowType.Int(Short.SIZE, true), Format.asType("s", 0L));
assertEquals(new ArrowType.Int(Short.SIZE, false), Format.asType("S", 0L));
assertEquals(new ArrowType.Int(Integer.SIZE, true), Format.asType("i", 0L));
assertEquals(new ArrowType.Int(Integer.SIZE, false), Format.asType("I", 0L));
assertEquals(new ArrowType.Int(Long.SIZE, true), Format.asType("l", 0L));
assertEquals(new ArrowType.Int(Long.SIZE, false), Format.asType("L", 0L));
assertEquals(new ArrowType.FloatingPoint(FloatingPointPrecision.HALF), Format.asType("e", 0L));
assertEquals(
new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), Format.asType("f", 0L));
assertEquals(
new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), Format.asType("g", 0L));
assertTrue(Format.asType("z", 0L) instanceof ArrowType.Binary);
assertTrue(Format.asType("Z", 0L) instanceof ArrowType.LargeBinary);
assertTrue(Format.asType("u", 0L) instanceof ArrowType.Utf8);
assertTrue(Format.asType("U", 0L) instanceof ArrowType.LargeUtf8);
assertEquals(new ArrowType.Date(DateUnit.DAY), Format.asType("tdD", 0L));
assertEquals(new ArrowType.Date(DateUnit.MILLISECOND), Format.asType("tdm", 0L));
assertEquals(new ArrowType.Time(TimeUnit.SECOND, Integer.SIZE), Format.asType("tts", 0L));
assertEquals(new ArrowType.Time(TimeUnit.MILLISECOND, Integer.SIZE), Format.asType("ttm", 0L));
assertEquals(new ArrowType.Time(TimeUnit.MICROSECOND, Long.SIZE), Format.asType("ttu", 0L));
assertEquals(new ArrowType.Time(TimeUnit.NANOSECOND, Long.SIZE), Format.asType("ttn", 0L));
assertEquals(new ArrowType.Duration(TimeUnit.SECOND), Format.asType("tDs", 0L));
assertEquals(new ArrowType.Duration(TimeUnit.MILLISECOND), Format.asType("tDm", 0L));
assertEquals(new ArrowType.Duration(TimeUnit.MICROSECOND), Format.asType("tDu", 0L));
assertEquals(new ArrowType.Duration(TimeUnit.NANOSECOND), Format.asType("tDn", 0L));
assertEquals(new ArrowType.Interval(IntervalUnit.YEAR_MONTH), Format.asType("tiM", 0L));
assertEquals(new ArrowType.Interval(IntervalUnit.DAY_TIME), Format.asType("tiD", 0L));
assertTrue(Format.asType("+l", 0L) instanceof ArrowType.List);
assertTrue(Format.asType("+L", 0L) instanceof ArrowType.LargeList);
assertTrue(Format.asType("+s", 0L) instanceof ArrowType.Struct);
assertEquals(new ArrowType.Map(false), Format.asType("+m", 0L));
assertEquals(new ArrowType.Map(true), Format.asType("+m", Flags.ARROW_FLAG_MAP_KEYS_SORTED));
assertEquals(new ArrowType.Decimal(1, 1, 128), Format.asType("d:1,1", 0L));
assertEquals(new ArrowType.Decimal(1, 1, 1), Format.asType("d:1,1,1", 0L));
assertEquals(new ArrowType.Decimal(9, 1, 1), Format.asType("d:9,1,1", 0L));
assertEquals(new ArrowType.FixedSizeBinary(1), Format.asType("w:1", 0L));
assertEquals(new ArrowType.FixedSizeList(3), Format.asType("+w:3", 0L));
assertEquals(
new ArrowType.Union(UnionMode.Dense, new int[] {1, 1, 1}), Format.asType("+ud:1,1,1", 0L));
assertEquals(
new ArrowType.Union(UnionMode.Sparse, new int[] {1, 1, 1}), Format.asType("+us:1,1,1", 0L));
assertEquals(
new ArrowType.Timestamp(TimeUnit.SECOND, "Timezone"), Format.asType("tss:Timezone", 0L));
assertEquals(
new ArrowType.Timestamp(TimeUnit.MILLISECOND, "Timezone"),
Format.asType("tsm:Timezone", 0L));
assertEquals(
new ArrowType.Timestamp(TimeUnit.MICROSECOND, "Timezone"),
Format.asType("tsu:Timezone", 0L));
assertEquals(
new ArrowType.Timestamp(TimeUnit.NANOSECOND, "Timezone"),
Format.asType("tsn:Timezone", 0L));
assertThrows(UnsupportedOperationException.class, () -> Format.asType("Format", 0L));
assertThrows(UnsupportedOperationException.class, () -> Format.asType(":", 0L));
assertThrows(NumberFormatException.class, () -> Format.asType("w:1,2,3", 0L));
}
@Test
public void testAsStringIgnoresDefaultLocale() {
// Locales that use digits other than 0-9 (Arabic-Indic, Bengali, Devanagari, ...)
// must not leak into the format string, which other implementations parse as ASCII.
Locale saved = Locale.getDefault();
try {
Locale.setDefault(Locale.forLanguageTag("ar-EG"));
assertEquals("d:10,2", Format.asString(new ArrowType.Decimal(10, 2, 128)));
assertEquals("d:10,2,256", Format.asString(new ArrowType.Decimal(10, 2, 256)));
assertEquals("w:16", Format.asString(new ArrowType.FixedSizeBinary(16)));
assertEquals("+w:8", Format.asString(new ArrowType.FixedSizeList(8)));
} finally {
Locale.setDefault(saved);
}
}
}