Skip to content

Commit ed0f5a7

Browse files
committed
改行や空白だけの行がある時にエラーになる問題を修正
1 parent fb0b812 commit ed0f5a7

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

src/main/java/org/embulk/parser/jsonl/JsonlParserPlugin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public void run(TaskSource taskSource, Schema schema, FileInput input, PageOutpu
175175
}
176176
lineNumber++;
177177

178+
// Skip empty lines
179+
if (line.trim().isEmpty()) {
180+
continue;
181+
}
182+
178183
try {
179184
Value value = jsonParser.parse(line);
180185

src/test/java/org/embulk/parser/jsonl/TestJsonlParserPlugin.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,129 @@ record = records.get(1);
241241
}
242242
}
243243

244+
@Test
245+
public void testTrailingEmptyLine() throws Exception {
246+
SchemaConfig schema =
247+
schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
248+
ConfigSource config = config().set("columns", schema);
249+
250+
// Simulates a file with a trailing newline: the last element "" represents the
251+
// empty line
252+
List<Object[]> records =
253+
runParser(
254+
config,
255+
Arrays.asList(
256+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
257+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}",
258+
"")); // Empty line at the end
259+
260+
assertEquals(2, records.size());
261+
assertEquals(true, records.get(0)[0]);
262+
assertEquals(10L, records.get(0)[1]);
263+
assertEquals("first", records.get(0)[2]);
264+
}
265+
266+
@Test
267+
public void testLeadingEmptyLine() throws Exception {
268+
SchemaConfig schema =
269+
schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
270+
ConfigSource config = config().set("columns", schema);
271+
272+
List<Object[]> records =
273+
runParser(
274+
config,
275+
Arrays.asList(
276+
"", // Empty line at the beginning
277+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
278+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
279+
280+
assertEquals(2, records.size());
281+
}
282+
283+
@Test
284+
public void testMiddleEmptyLine() throws Exception {
285+
SchemaConfig schema =
286+
schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
287+
ConfigSource config = config().set("columns", schema);
288+
289+
List<Object[]> records =
290+
runParser(
291+
config,
292+
Arrays.asList(
293+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
294+
"", // Empty line in the middle
295+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
296+
297+
assertEquals(2, records.size());
298+
}
299+
300+
@Test
301+
public void testMultipleConsecutiveEmptyLines() throws Exception {
302+
SchemaConfig schema =
303+
schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
304+
ConfigSource config = config().set("columns", schema);
305+
306+
List<Object[]> records =
307+
runParser(
308+
config,
309+
Arrays.asList(
310+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
311+
"", // Empty line
312+
"", // Empty line
313+
"", // Empty line
314+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
315+
316+
assertEquals(2, records.size());
317+
}
318+
319+
@Test
320+
public void testWhitespaceOnlyLines() throws Exception {
321+
SchemaConfig schema =
322+
schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
323+
ConfigSource config = config().set("columns", schema);
324+
325+
List<Object[]> records =
326+
runParser(
327+
config,
328+
Arrays.asList(
329+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
330+
" ", // Spaces only
331+
"\t", // Tab only
332+
" \t ", // Mixed whitespace
333+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
334+
335+
assertEquals(2, records.size());
336+
}
337+
338+
@Test
339+
public void testOnlyEmptyLines() throws Exception {
340+
SchemaConfig schema =
341+
schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
342+
ConfigSource config = config().set("columns", schema);
343+
344+
List<Object[]> records = runParser(config, Arrays.asList("", " ", "\t", ""));
345+
346+
assertEquals(0, records.size());
347+
}
348+
349+
@Test
350+
public void testEmptyLinesWithStopOnInvalidRecord() throws Exception {
351+
SchemaConfig schema =
352+
schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
353+
ConfigSource config = config().set("columns", schema).set("stop_on_invalid_record", true);
354+
355+
// Empty lines should be skipped even when stop_on_invalid_record is true
356+
List<Object[]> records =
357+
runParser(
358+
config,
359+
Arrays.asList(
360+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
361+
"",
362+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
363+
364+
assertEquals(2, records.size());
365+
}
366+
244367
private ConfigSource config() {
245368
return CONFIG_MAPPER_FACTORY.newConfigSource();
246369
}

0 commit comments

Comments
 (0)