|
| 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.assertTrue; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import org.apache.arrow.memory.BufferAllocator; |
| 31 | +import org.apache.arrow.memory.RootAllocator; |
| 32 | +import org.apache.arrow.vector.BigIntVector; |
| 33 | +import org.apache.arrow.vector.VarCharVector; |
| 34 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 35 | +import org.apache.arrow.vector.ipc.ArrowReader; |
| 36 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 37 | +import org.apache.arrow.vector.types.pojo.Field; |
| 38 | +import org.apache.arrow.vector.types.pojo.FieldType; |
| 39 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.junit.jupiter.api.io.TempDir; |
| 42 | + |
| 43 | +class SessionContextJsonTest { |
| 44 | + |
| 45 | + private static Path writeJson(Path dir, String name, String contents) throws IOException { |
| 46 | + Path file = dir.resolve(name); |
| 47 | + Files.writeString(file, contents); |
| 48 | + return file; |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void registerJsonInfersSchemaAndCounts(@TempDir Path tempDir) throws Exception { |
| 53 | + Path file = |
| 54 | + writeJson( |
| 55 | + tempDir, |
| 56 | + "people.json", |
| 57 | + "{\"id\":1,\"name\":\"alice\"}\n" |
| 58 | + + "{\"id\":2,\"name\":\"bob\"}\n" |
| 59 | + + "{\"id\":3,\"name\":\"carol\"}\n"); |
| 60 | + |
| 61 | + try (BufferAllocator allocator = new RootAllocator(); |
| 62 | + SessionContext ctx = new SessionContext()) { |
| 63 | + ctx.registerJson("people", file.toAbsolutePath().toString()); |
| 64 | + |
| 65 | + try (DataFrame df = ctx.sql("SELECT COUNT(*) FROM people"); |
| 66 | + ArrowReader reader = df.collect(allocator)) { |
| 67 | + assertTrue(reader.loadNextBatch()); |
| 68 | + BigIntVector count = (BigIntVector) reader.getVectorSchemaRoot().getVector(0); |
| 69 | + assertEquals(3L, count.get(0)); |
| 70 | + } |
| 71 | + |
| 72 | + try (DataFrame df = ctx.sql("SELECT name FROM people WHERE id = 2"); |
| 73 | + ArrowReader reader = df.collect(allocator)) { |
| 74 | + assertTrue(reader.loadNextBatch()); |
| 75 | + VectorSchemaRoot root = reader.getVectorSchemaRoot(); |
| 76 | + assertEquals(1, root.getRowCount()); |
| 77 | + VarCharVector names = (VarCharVector) root.getVector(0); |
| 78 | + assertEquals("bob", new String(names.get(0))); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void readJsonWithExplicitSchema(@TempDir Path tempDir) throws Exception { |
| 85 | + Path file = |
| 86 | + writeJson( |
| 87 | + tempDir, "headerless.json", "{\"id\":10,\"name\":\"x\"}\n{\"id\":20,\"name\":\"y\"}\n"); |
| 88 | + |
| 89 | + Schema schema = |
| 90 | + new Schema( |
| 91 | + List.of( |
| 92 | + new Field("id", FieldType.nullable(new ArrowType.Int(64, true)), null), |
| 93 | + new Field("name", FieldType.nullable(new ArrowType.Utf8()), null))); |
| 94 | + |
| 95 | + try (BufferAllocator allocator = new RootAllocator(); |
| 96 | + SessionContext ctx = new SessionContext(); |
| 97 | + DataFrame df = |
| 98 | + ctx.readJson(file.toAbsolutePath().toString(), new NdJsonReadOptions().schema(schema)); |
| 99 | + ArrowReader reader = df.collect(allocator)) { |
| 100 | + assertTrue(reader.loadNextBatch()); |
| 101 | + VectorSchemaRoot root = reader.getVectorSchemaRoot(); |
| 102 | + assertEquals(2, root.getRowCount()); |
| 103 | + assertEquals("id", root.getSchema().getFields().get(0).getName()); |
| 104 | + assertEquals("name", root.getSchema().getFields().get(1).getName()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void registerJsonWithCustomExtension(@TempDir Path tempDir) throws Exception { |
| 110 | + Path file = writeJson(tempDir, "data.ndjson", "{\"x\":10,\"y\":20}\n{\"x\":30,\"y\":40}\n"); |
| 111 | + |
| 112 | + try (BufferAllocator allocator = new RootAllocator(); |
| 113 | + SessionContext ctx = new SessionContext()) { |
| 114 | + ctx.registerJson( |
| 115 | + "t", file.toAbsolutePath().toString(), new NdJsonReadOptions().fileExtension(".ndjson")); |
| 116 | + |
| 117 | + try (DataFrame df = ctx.sql("SELECT SUM(x) + SUM(y) FROM t"); |
| 118 | + ArrowReader reader = df.collect(allocator)) { |
| 119 | + assertTrue(reader.loadNextBatch()); |
| 120 | + BigIntVector v = (BigIntVector) reader.getVectorSchemaRoot().getVector(0); |
| 121 | + assertEquals(100L, v.get(0)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments