kosiew commented on code in PR #19370:
URL: https://github.com/apache/datafusion/pull/19370#discussion_r2646494134
##########
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:
Curious why there is no assertion of 0 lines here like you did for arrow,
parquet files?
##########
datafusion/core/src/datasource/file_format/csv.rs:
##########
@@ -1537,6 +1537,46 @@ mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn test_write_empty_csv_from_sql() -> Result<()> {
+ let ctx = SessionContext::new();
+ let tmp_dir = tempfile::TempDir::new()?;
+ let path = format!("{}/empty_sql.csv",
tmp_dir.path().to_string_lossy());
+ let df = ctx.sql("SELECT CAST(1 AS BIGINT) AS id LIMIT 0").await?;
+ df.write_csv(&path, crate::dataframe::DataFrameWriteOptions::new(),
None)
+ .await?;
+ assert!(std::path::Path::new(&path).exists());
+
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn test_write_empty_csv_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(StringArray::from(Vec::<Option<&str>>::new())),
+ ],
+ )?;
+
+ let tmp_dir = tempfile::TempDir::new()?;
+ let path = format!("{}/empty_batch.csv",
tmp_dir.path().to_string_lossy());
+
+ // Write empty RecordBatch
+ let df = ctx.read_batch(empty_batch.clone())?;
+ df.write_csv(&path, crate::dataframe::DataFrameWriteOptions::new(),
None)
+ .await?;
+ // Expected the file to exist
+ assert!(std::path::Path::new(&path).exists());
Review Comment:
Curious why there is no assertion of 0 lines here like you did for arrow,
parquet files?
--
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]