-
Notifications
You must be signed in to change notification settings - Fork 8
feat(json): expose NdJsonReadOptions via registerJson and readJson #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+689
−42
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
core/src/main/java/org/apache/datafusion/FileCompressionType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * 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.datafusion; | ||
|
|
||
| /** | ||
| * Compression of a file-format source. Shared by {@link CsvReadOptions} and {@link | ||
| * NdJsonReadOptions} (and any future format that exposes the same set of compressions). Variant | ||
| * names match DataFusion's Rust {@code FileCompressionType} so they round-trip across JNI. | ||
| */ | ||
| public enum FileCompressionType { | ||
| UNCOMPRESSED, | ||
| GZIP, | ||
| BZIP2, | ||
| XZ, | ||
| ZSTD | ||
| } |
47 changes: 47 additions & 0 deletions
47
core/src/main/java/org/apache/datafusion/FileCompressionTypes.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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.datafusion; | ||
|
|
||
| /** | ||
| * Internal helpers for translating between the user-facing {@link FileCompressionType} enum and the | ||
| * protobuf-generated {@code org.apache.datafusion.protobuf.FileCompressionType} enum used on the | ||
| * wire. Same variant set, different Java types. | ||
| */ | ||
| final class FileCompressionTypes { | ||
| private FileCompressionTypes() {} | ||
|
|
||
| static org.apache.datafusion.protobuf.FileCompressionType toProto(FileCompressionType t) { | ||
| switch (t) { | ||
| case UNCOMPRESSED: | ||
| return org.apache.datafusion.protobuf.FileCompressionType | ||
| .FILE_COMPRESSION_TYPE_UNCOMPRESSED; | ||
| case GZIP: | ||
| return org.apache.datafusion.protobuf.FileCompressionType.FILE_COMPRESSION_TYPE_GZIP; | ||
| case BZIP2: | ||
| return org.apache.datafusion.protobuf.FileCompressionType.FILE_COMPRESSION_TYPE_BZIP2; | ||
| case XZ: | ||
| return org.apache.datafusion.protobuf.FileCompressionType.FILE_COMPRESSION_TYPE_XZ; | ||
| case ZSTD: | ||
| return org.apache.datafusion.protobuf.FileCompressionType.FILE_COMPRESSION_TYPE_ZSTD; | ||
| default: | ||
| throw new IllegalArgumentException("unhandled FileCompressionType: " + t); | ||
| } | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
core/src/main/java/org/apache/datafusion/NdJsonReadOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * 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.datafusion; | ||
|
|
||
| import org.apache.arrow.vector.types.pojo.Schema; | ||
|
|
||
| /** | ||
| * Configuration knobs for newline-delimited JSON sources passed to {@link | ||
| * SessionContext#registerJson(String, String, NdJsonReadOptions)} and {@link | ||
| * SessionContext#readJson(String, NdJsonReadOptions)}. | ||
| * | ||
| * <p>Mirrors a subset of DataFusion's {@code NdJsonReadOptions}. All setters return {@code this} | ||
| * for fluent chaining. Defaults match the Rust struct: {@code fileExtension = ".json"}, {@code | ||
| * fileCompressionType = UNCOMPRESSED}; {@code schemaInferMaxRecords} unset (the DataFusion default | ||
| * is used). | ||
| */ | ||
| public final class NdJsonReadOptions { | ||
|
|
||
| private String fileExtension = ".json"; | ||
| private FileCompressionType fileCompressionType = FileCompressionType.UNCOMPRESSED; | ||
| private Long schemaInferMaxRecords; | ||
| private Schema schema; | ||
|
|
||
| public NdJsonReadOptions fileExtension(String ext) { | ||
| this.fileExtension = ext; | ||
| return this; | ||
| } | ||
|
|
||
| public NdJsonReadOptions fileCompressionType(FileCompressionType t) { | ||
| this.fileCompressionType = t; | ||
| return this; | ||
| } | ||
|
|
||
| public NdJsonReadOptions schemaInferMaxRecords(long n) { | ||
| if (n < 0) { | ||
| throw new IllegalArgumentException("schemaInferMaxRecords must be non-negative, got " + n); | ||
| } | ||
| this.schemaInferMaxRecords = n; | ||
| return this; | ||
| } | ||
|
|
||
| public NdJsonReadOptions schema(Schema schema) { | ||
| this.schema = schema; | ||
| return this; | ||
| } | ||
|
|
||
| byte[] toBytes() { | ||
| org.apache.datafusion.protobuf.NdJsonReadOptionsProto.Builder b = | ||
| org.apache.datafusion.protobuf.NdJsonReadOptionsProto.newBuilder() | ||
| .setFileExtension(fileExtension) | ||
| .setFileCompressionType(FileCompressionTypes.toProto(fileCompressionType)); | ||
| if (schemaInferMaxRecords != null) { | ||
| b.setSchemaInferMaxRecords(schemaInferMaxRecords); | ||
| } | ||
| return b.build().toByteArray(); | ||
| } | ||
|
|
||
| Schema schema() { | ||
| return schema; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,6 +160,67 @@ public DataFrame readCsv(String path, CsvReadOptions options) { | |
| return new DataFrame(dfHandle); | ||
| } | ||
|
|
||
| public void registerJson(String name, String path) { | ||
| registerJson(name, path, new NdJsonReadOptions()); | ||
| } | ||
|
|
||
| /** | ||
| * Register a newline-delimited JSON file (or directory of NDJSON files) as a table with the | ||
| * supplied {@link NdJsonReadOptions}. | ||
| * | ||
| * @throws RuntimeException if registration fails (path not found, schema inference error, etc.). | ||
| */ | ||
| public void registerJson(String name, String path, NdJsonReadOptions options) { | ||
| if (nativeHandle == 0) { | ||
| throw new IllegalStateException("SessionContext is closed"); | ||
| } | ||
| if (name == null) { | ||
| throw new IllegalArgumentException("registerJson name must be non-null"); | ||
| } | ||
| if (path == null) { | ||
| throw new IllegalArgumentException("registerJson path must be non-null"); | ||
| } | ||
| if (options == null) { | ||
| throw new IllegalArgumentException("registerJson options must be non-null"); | ||
| } | ||
| registerJsonWithOptions( | ||
| nativeHandle, | ||
| name, | ||
| path, | ||
| options.toBytes(), | ||
| options.schema() != null ? serializeSchemaIpc(options.schema()) : null); | ||
| } | ||
|
|
||
| /** Read a newline-delimited JSON file as a {@link DataFrame} without registering it. */ | ||
| public DataFrame readJson(String path) { | ||
| return readJson(path, new NdJsonReadOptions()); | ||
| } | ||
|
|
||
| /** | ||
| * Read a newline-delimited JSON file as a {@link DataFrame} with the supplied {@link | ||
| * NdJsonReadOptions}. | ||
| * | ||
| * @throws RuntimeException if the read fails. | ||
| */ | ||
| public DataFrame readJson(String path, NdJsonReadOptions options) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add null checks for args
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| if (nativeHandle == 0) { | ||
| throw new IllegalStateException("SessionContext is closed"); | ||
| } | ||
| if (path == null) { | ||
| throw new IllegalArgumentException("readJson path must be non-null"); | ||
| } | ||
| if (options == null) { | ||
| throw new IllegalArgumentException("readJson options must be non-null"); | ||
| } | ||
| long dfHandle = | ||
| readJsonWithOptions( | ||
| nativeHandle, | ||
| path, | ||
| options.toBytes(), | ||
| options.schema() != null ? serializeSchemaIpc(options.schema()) : null); | ||
| return new DataFrame(dfHandle); | ||
| } | ||
|
|
||
| public void registerParquet(String name, String path) { | ||
| registerParquet(name, path, new ParquetReadOptions()); | ||
| } | ||
|
|
@@ -247,5 +308,11 @@ private static native void registerCsvWithOptions( | |
| private static native long readCsvWithOptions( | ||
| long handle, String path, byte[] optionsBytes, byte[] schemaIpcBytes); | ||
|
|
||
| private static native void registerJsonWithOptions( | ||
| long handle, String name, String path, byte[] optionsBytes, byte[] schemaIpcBytes); | ||
|
|
||
| private static native long readJsonWithOptions( | ||
| long handle, String path, byte[] optionsBytes, byte[] schemaIpcBytes); | ||
|
|
||
| private static native void closeSessionContext(long handle); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add null checks for the arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done