|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datafusion; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | + |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.List; |
| 29 | +import java.util.stream.Stream; |
| 30 | + |
| 31 | +import org.apache.arrow.memory.BufferAllocator; |
| 32 | +import org.apache.arrow.memory.RootAllocator; |
| 33 | +import org.apache.arrow.vector.BigIntVector; |
| 34 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 35 | +import org.apache.arrow.vector.ipc.ArrowReader; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.api.io.TempDir; |
| 38 | + |
| 39 | +class DataFrameWriteJsonTest { |
| 40 | + |
| 41 | + /** |
| 42 | + * Build a 3-row DataFrame {@code (id BIGINT, name VARCHAR)}, write it as JSON via the SUT, then |
| 43 | + * read the result back through {@link SessionContext#registerJson} and run {@code COUNT(*)}. |
| 44 | + * Returns the row count. |
| 45 | + */ |
| 46 | + private static long countRowsAt(Path dirOrFile, NdJsonReadOptions readOpts) throws Exception { |
| 47 | + try (BufferAllocator allocator = new RootAllocator(); |
| 48 | + SessionContext ctx = new SessionContext()) { |
| 49 | + ctx.registerJson("t", dirOrFile.toAbsolutePath().toString(), readOpts); |
| 50 | + try (DataFrame df = ctx.sql("SELECT COUNT(*) FROM t"); |
| 51 | + ArrowReader reader = df.collect(allocator)) { |
| 52 | + assertTrue(reader.loadNextBatch()); |
| 53 | + VectorSchemaRoot root = reader.getVectorSchemaRoot(); |
| 54 | + assertEquals(1, root.getRowCount()); |
| 55 | + return ((BigIntVector) root.getVector(0)).get(0); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** Materialise a 3-row table the test can hand to {@code writeJson}. */ |
| 61 | + private static String threeRowSql() { |
| 62 | + return "SELECT * FROM (VALUES (CAST(1 AS BIGINT), 'alice')," |
| 63 | + + " (CAST(2 AS BIGINT), 'bob'), (CAST(3 AS BIGINT), 'carol')) AS t(id, name)"; |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void writeJsonRoundTripsRowCount(@TempDir Path tempDir) throws Exception { |
| 68 | + Path out = tempDir.resolve("out"); |
| 69 | + |
| 70 | + try (SessionContext ctx = new SessionContext(); |
| 71 | + DataFrame df = ctx.sql(threeRowSql())) { |
| 72 | + df.writeJson(out.toString()); |
| 73 | + } |
| 74 | + |
| 75 | + assertEquals(3L, countRowsAt(out, new NdJsonReadOptions())); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void writeJsonSingleFileProducesOneFile(@TempDir Path tempDir) throws Exception { |
| 80 | + Path out = tempDir.resolve("out.json"); |
| 81 | + |
| 82 | + try (SessionContext ctx = new SessionContext(); |
| 83 | + DataFrame df = ctx.sql(threeRowSql())) { |
| 84 | + df.writeJson(out.toString(), new JsonWriteOptions().singleFileOutput(true)); |
| 85 | + } |
| 86 | + |
| 87 | + assertTrue(Files.isRegularFile(out), "expected single file at " + out); |
| 88 | + assertEquals(3L, countRowsAt(out, new NdJsonReadOptions())); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void writeJsonWithGzipCompressionRoundTrips(@TempDir Path tempDir) throws Exception { |
| 93 | + Path out = tempDir.resolve("gz-out"); |
| 94 | + |
| 95 | + try (SessionContext ctx = new SessionContext(); |
| 96 | + DataFrame df = ctx.sql(threeRowSql())) { |
| 97 | + df.writeJson( |
| 98 | + out.toString(), new JsonWriteOptions().fileCompressionType(FileCompressionType.GZIP)); |
| 99 | + } |
| 100 | + |
| 101 | + try (Stream<Path> stream = Files.walk(out)) { |
| 102 | + List<Path> files = stream.filter(Files::isRegularFile).toList(); |
| 103 | + assertTrue(!files.isEmpty(), "expected at least one part-file under " + out); |
| 104 | + for (Path p : files) { |
| 105 | + assertTrue( |
| 106 | + p.getFileName().toString().endsWith(".gz"), |
| 107 | + "expected .gz suffix on " + p.getFileName()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + NdJsonReadOptions readOpts = |
| 112 | + new NdJsonReadOptions() |
| 113 | + .fileCompressionType(FileCompressionType.GZIP) |
| 114 | + .fileExtension(".json.gz"); |
| 115 | + assertEquals(3L, countRowsAt(out, readOpts)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void writeJsonDefaultsToDirectoryEvenWithExtensionInPath(@TempDir Path tempDir) throws Exception { |
| 120 | + // The Javadoc promises "directory unless overridden via singleFileOutput(true)". DataFusion's |
| 121 | + // own DataFrameWriteOptions defaults to Automatic mode, where an extension in the path |
| 122 | + // (".json" here) silently flips the output to a single file. The native handler explicitly |
| 123 | + // pins the default to directory mode so this contract holds regardless of path shape. |
| 124 | + Path out = tempDir.resolve("out.json"); |
| 125 | + |
| 126 | + try (SessionContext ctx = new SessionContext(); |
| 127 | + DataFrame df = ctx.sql(threeRowSql())) { |
| 128 | + df.writeJson(out.toString()); |
| 129 | + } |
| 130 | + |
| 131 | + assertTrue(Files.isDirectory(out), "expected directory output at " + out + ", got a file"); |
| 132 | + assertEquals(3L, countRowsAt(out, new NdJsonReadOptions())); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void writeJsonRetainsDataFrame(@TempDir Path tempDir) throws Exception { |
| 137 | + Path out = tempDir.resolve("retained"); |
| 138 | + |
| 139 | + try (SessionContext ctx = new SessionContext(); |
| 140 | + DataFrame df = ctx.sql(threeRowSql())) { |
| 141 | + df.writeJson(out.toString()); |
| 142 | + assertEquals(3L, df.count()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void writeJsonRejectsNullPath() { |
| 148 | + try (SessionContext ctx = new SessionContext(); |
| 149 | + DataFrame df = ctx.sql(threeRowSql())) { |
| 150 | + assertThrows(IllegalArgumentException.class, () -> df.writeJson(null)); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void writeJsonRejectsNullOptions(@TempDir Path tempDir) { |
| 156 | + Path out = tempDir.resolve("out"); |
| 157 | + try (SessionContext ctx = new SessionContext(); |
| 158 | + DataFrame df = ctx.sql(threeRowSql())) { |
| 159 | + assertThrows(IllegalArgumentException.class, () -> df.writeJson(out.toString(), null)); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments