nlimpid commented on code in PR #19370:
URL: https://github.com/apache/datafusion/pull/19370#discussion_r2658691025
##########
datafusion/core/src/datasource/file_format/json.rs:
##########
@@ -349,4 +349,42 @@ mod tests {
fn fmt_batches(batches: &[RecordBatch]) -> String {
pretty::pretty_format_batches(batches).unwrap().to_string()
}
+
+ #[tokio::test]
+ async fn test_write_empty_json_from_sql() -> Result<()> {
+ let ctx = SessionContext::new();
+ let tmp_dir = tempfile::TempDir::new()?;
+ let path = format!("{}/empty_sql.json",
tmp_dir.path().to_string_lossy());
+ let df = ctx.sql("SELECT CAST(1 AS BIGINT) AS id LIMIT 0").await?;
+ df.write_json(&path, crate::dataframe::DataFrameWriteOptions::new(),
None)
+ .await?;
+ // Expected the file to exist
+ assert!(std::path::Path::new(&path).exists());
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn test_write_empty_json_from_record_batch() -> Result<()> {
+ let ctx = SessionContext::new();
+ let schema = Arc::new(Schema::new(vec![
+ Field::new("id", DataType::Int64, false),
+ Field::new("name", DataType::Utf8, true),
+ ]));
+ let empty_batch = RecordBatch::try_new(
+ schema.clone(),
+ vec![
+ Arc::new(arrow::array::Int64Array::from(Vec::<i64>::new())),
+
Arc::new(arrow::array::StringArray::from(Vec::<Option<&str>>::new())),
+ ],
+ )?;
+
+ let tmp_dir = tempfile::TempDir::new()?;
+ let path = format!("{}/empty_batch.json",
tmp_dir.path().to_string_lossy());
+ let df = ctx.read_batch(empty_batch.clone())?;
+ df.write_json(&path, crate::dataframe::DataFrameWriteOptions::new(),
None)
+ .await?;
+ // Expected the file to exist
+ assert!(std::path::Path::new(&path).exists());
Review Comment:
For JSON, empty files can't be read back to validate schema, Add file check
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]