forked from apache/drill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestBasics.java
238 lines (215 loc) · 7.25 KB
/
TestBasics.java
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* 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.drill.exec.store.easy.json.loader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.apache.drill.common.exceptions.EmptyErrorContext;
import org.apache.drill.common.exceptions.UserException;
import org.apache.drill.common.types.TypeProtos.MinorType;
import org.apache.drill.exec.physical.resultSet.impl.ProjectionFilter;
import org.apache.drill.exec.physical.resultSet.project.Projections;
import org.apache.drill.exec.physical.rowSet.RowSet;
import org.apache.drill.exec.physical.rowSet.RowSetTestUtils;
import org.apache.drill.exec.record.metadata.SchemaBuilder;
import org.apache.drill.exec.record.metadata.TupleMetadata;
import org.apache.drill.exec.store.easy.json.loader.BaseJsonLoaderTest.JsonLoaderFixture;
import org.apache.drill.test.rowSet.RowSetUtilities;
import org.junit.Test;
public class TestBasics extends BaseJsonLoaderTest {
@Test
public void testEmpty() {
String json = "";
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.open(json);
RowSet results = loader.next();
assertNull(results);
loader.close();
}
@Test
public void testEmptyTuple() {
final String json = "{} {} {}";
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.open(json);
RowSet results = loader.next();
assertEquals(3, results.rowCount());
assertTrue(results.schema().isEmpty());
assertNotNull(results);
loader.close();
}
@Test
public void testRootArray() {
final String json = "[{a: 10}, {a: 20}, {a: 30}]";
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.open(json);
RowSet results = loader.next();
assertNotNull(results);
TupleMetadata expectedSchema = new SchemaBuilder()
.addNullable("a", MinorType.BIGINT)
.build();
RowSet expected = fixture.rowSetBuilder(expectedSchema)
.addRow(10)
.addRow(20)
.addRow(30)
.build();
RowSetUtilities.verify(expected, results);
assertNull(loader.next());
loader.close();
}
@Test
public void testLeadingTrailingWhitespace() {
final String json = "{\" a\": 10, \" b\": 20, \" c \": 30}";
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.open(json);
RowSet results = loader.next();
assertNotNull(results);
TupleMetadata expectedSchema = new SchemaBuilder()
.addNullable("a", MinorType.BIGINT)
.addNullable("b", MinorType.BIGINT)
.addNullable("c", MinorType.BIGINT)
.build();
RowSet expected = fixture.rowSetBuilder(expectedSchema)
.addRow(10, 20, 30)
.build();
RowSetUtilities.verify(expected, results);
assertNull(loader.next());
loader.close();
}
@Test
public void testCaseInsensitive() {
final String json = "{a: 10, Bob: 110} {A: 20, bOb: 120} {\" a \": 30, BoB: 130}";
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.open(json);
RowSet results = loader.next();
assertNotNull(results);
TupleMetadata expectedSchema = new SchemaBuilder()
.addNullable("a", MinorType.BIGINT)
.addNullable("Bob", MinorType.BIGINT)
.build();
RowSet expected = fixture.rowSetBuilder(expectedSchema)
.addRow(10, 110)
.addRow(20, 120)
.addRow(30, 130)
.build();
RowSetUtilities.verify(expected, results);
assertNull(loader.next());
loader.close();
}
@Test
public void testProjection() {
String json =
"{a: 10, b: true}\n" +
"{a: 20, b: [\"what?\"]}\n" +
"{a: 30, b: {c: \"oh, my!\"}}" +
"{a: 40}" +
"{a: 50, b: [[{x: [[{y: []}]]}]]}";
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.rsLoaderOptions.projection(
Projections.parse(RowSetTestUtils.projectList("a")));
loader.open(json);
RowSet results = loader.next();
assertNotNull(results);
TupleMetadata expectedSchema = new SchemaBuilder()
.addNullable("a", MinorType.BIGINT)
.build();
RowSet expected = fixture.rowSetBuilder(expectedSchema)
.addRow(10)
.addRow(20)
.addRow(30)
.addRow(40)
.addRow(50)
.build();
RowSetUtilities.verify(expected, results);
assertNull(loader.next());
loader.close();
}
@Test
public void testMissingEndObject() {
expectError("{a: 0} {a: 100", "Error parsing JSON");
}
@Test
public void testMissingValue() {
expectError("{a: 0} {a: ", "Error parsing JSON");
}
/**
* When parsing an array, the Jackson JSON parser raises
* an error for a missing close bracket.
*/
@Test
public void testMissingEndOuterArray() {
expectError("[{a: 0}, {a: 100}", "Error parsing JSON");
}
@Test
public void testEmptyKey() {
expectError("{\"\": 10}", "does not allow empty keys");
}
@Test
public void testBlankKey() {
expectError("{\" \": 10}", "does not allow empty keys");
}
@Test
public void testRootArrayDisallowed() {
final String json = "[{a: 0}, {a: 100}, {a: null}]";
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.jsonOptions.skipOuterList = false;
try {
loader.open(json);
fail();
} catch (UserException e) {
assertTrue(e.getMessage().contains("outer array support is not enabled"));
}
}
protected static void expectError(String json, String msg) {
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.open(json);
try {
loader.next();
fail();
} catch (UserException e) {
assertTrue(e.getMessage().contains(msg));
}
loader.close();
}
/**
* Test syntax error recover. Recovery is not perfect. The
* input contains six records: the second is bad. But, the parser
* consumes records 3 and 4 trying to recover.
*/
@Test
public void testRecovery() {
final String json = "{a: 1} {a: {a: 3} {a: 4} {a: 5} {a: 6}";
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.jsonOptions.skipMalformedRecords = true;
loader.open(json);
RowSet results = loader.next();
assertNotNull(results);
TupleMetadata expectedSchema = new SchemaBuilder()
.addNullable("a", MinorType.BIGINT)
.build();
RowSet expected = fixture.rowSetBuilder(expectedSchema)
.addRow(1)
.addRow(5)
.addRow(6)
.build();
RowSetUtilities.verify(expected, results);
assertNull(loader.next());
loader.close();
}
}