Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions datafusion/tests/data_test_context/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"A": "a", "B": 1}
{"A": "b", "B": 2}
{"A": "c", "B": 3}
Comment thread
larskarg marked this conversation as resolved.
Outdated
3 changes: 3 additions & 0 deletions datafusion/tests/data_test_context/data.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"A": "a", "B": 1}
{"A": "b", "B": 2}
{"A": "c", "B": 3}
34 changes: 34 additions & 0 deletions datafusion/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

import os

import pyarrow as pa
import pyarrow.dataset as ds

Expand Down Expand Up @@ -179,3 +181,35 @@ def test_table_exist(ctx):
ctx.register_dataset("t", dataset)

assert ctx.table_exist("t") is True


def test_read_json(ctx):
path = os.path.dirname(os.path.abspath(__file__))

# Default
test_data_path = os.path.join(path, "data_test_context", "data.json")
df = ctx.read_json(test_data_path)
result = df.collect()

assert result[0].column(0) == pa.array(["a", "b", "c"])
assert result[0].column(1) == pa.array([1, 2, 3])

# Schema
schema = pa.schema(
[
pa.field("A", pa.string(), nullable=True),
]
)
df = ctx.read_json(test_data_path, schema=schema)
result = df.collect()

assert result[0].column(0) == pa.array(["a", "b", "c"])
assert result[0].schema == schema

# File extension
test_data_path = os.path.join(path, "data_test_context", "data.jsonl")
df = ctx.read_json(test_data_path, file_extension=".jsonl")
result = df.collect()

assert result[0].column(0) == pa.array(["a", "b", "c"])
assert result[0].column(1) == pa.array([1, 2, 3])
36 changes: 35 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use datafusion::arrow::record_batch::RecordBatch;
use datafusion::datasource::datasource::TableProvider;
use datafusion::datasource::MemTable;
use datafusion::execution::context::{SessionConfig, SessionContext};
use datafusion::prelude::{CsvReadOptions, ParquetReadOptions};
use datafusion::prelude::{CsvReadOptions, ParquetReadOptions, NdJsonReadOptions};

use crate::catalog::{PyCatalog, PyTable};
use crate::dataframe::PyDataFrame;
Expand Down Expand Up @@ -264,4 +264,38 @@ impl PySessionContext {
fn session_id(&self) -> PyResult<String> {
Ok(self.ctx.session_id())
}

#[allow(clippy::too_many_arguments)]
#[args(
schema = "None",
schema_infer_max_records = "1000",
file_extension = "\".json\"",
table_partition_cols = "vec![]",
)]
fn read_json(
&mut self,
path: PathBuf,
schema: Option<Schema>,
schema_infer_max_records: usize,
file_extension: &str,
table_partition_cols: Vec<String>,
py: Python
) -> PyResult<PyDataFrame> {
let path = path
.to_str()
.ok_or_else(|| PyValueError::new_err("Unable to convert path to a string"))?;

let mut options = NdJsonReadOptions::default()
.table_partition_cols(table_partition_cols);
options.schema = match schema {
Comment thread
larskarg marked this conversation as resolved.
Outdated
Some(x) => Some(Arc::new(x)),
None => None
};
options.schema_infer_max_records = schema_infer_max_records;
options.file_extension = file_extension;

let result = self.ctx.read_json(path, options);
let df = wait_for_future(py, result).map_err(DataFusionError::from)?;
Ok(PyDataFrame::new(df))
}
}