You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "zhuqi-lucas (via GitHub)" <gi...@apache.org> on 2025/02/25 16:45:56 UTC
[PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
zhuqi-lucas opened a new pull request, #14877:
URL: https://github.com/apache/datafusion/pull/14877
## Which issue does this PR close?
- Closes [14810](https://github.com/apache/datafusion/issues/14810)
## Rationale for this change
Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead.
Almost no extra memory usage for datafusion-cli now after this PR.
## What changes are included in this PR?
Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead.
Almost no extra memory usage for datafusion-cli now after this PR.
1. Change all datafusion-cli printing to streaming
2. Support preview the column width format printing, default is 1000 lines
3. Also support csv/json, etc format, max rows support which not supporting before
4. Totally removing the datafusion-cli memory overhead, the only occupied memory is the current batch for batchs iterator.
## Are these changes tested?
Unit test added.
Also mannul tested the datafusion-cli.
## Are there any user-facing changes?
Almost no extra memory usage for datafusion-cli now!
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "zhuqi-lucas (via GitHub)" <gi...@apache.org>.
zhuqi-lucas commented on PR #14877:
URL: https://github.com/apache/datafusion/pull/14877#issuecomment-2683862205
> Thank you @zhuqi-lucas -- this is really cool!
>
> I have a suggestion that I think would make the code better, but we could do it as a follow on PR or never in my opinion.
>
> In addition to the very nice tests you have in this PR I tested it manaully (notes below) and it worked great
>
> Testing without limit:
>
> ```shell
> cargo run -p datafusion-cli -- --maxrows inf -c "select * from 'benchmarks/data/hits.parquet'"
> ```
>
> And output was produced immediately and streamed out
>
> I also tested with
>
> ```
> cargo run -p datafusion-cli -- -c "select * from 'benchmarks/data/hits.parquet'"
> ```
>
> Which printed out the first 40 rows and then continued running but did not print anything, as expected
Thank you @alamb for review, i created a follow-up to do the code clean:
https://github.com/apache/datafusion/issues/14886
And we can do some code clean work including:
1. Add streaming state struct and clean up print batch logic
2. Remove older logics about max-rows
3. Add more docs and remove confusing docs
4. Etc
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "zhuqi-lucas (via GitHub)" <gi...@apache.org>.
zhuqi-lucas commented on code in PR #14877:
URL: https://github.com/apache/datafusion/pull/14877#discussion_r1970885830
##########
datafusion-cli/src/print_format.rs:
##########
@@ -209,6 +211,145 @@ impl PrintFormat {
}
Ok(())
}
+
+ #[allow(clippy::too_many_arguments)]
Review Comment:
Created a follow-up ticket:
https://github.com/apache/datafusion/issues/14886
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #14877:
URL: https://github.com/apache/datafusion/pull/14877#issuecomment-2691547433
FYI we found one issue with this code here (fixed in https://github.com/apache/datafusion/pull/14921):
- https://github.com/apache/datafusion/issues/14920
- https://github.com/apache/datafusion/issues/14926
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #14877:
URL: https://github.com/apache/datafusion/pull/14877
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #14877:
URL: https://github.com/apache/datafusion/pull/14877#discussion_r1970315221
##########
datafusion-cli/src/exec.rs:
##########
@@ -250,37 +245,43 @@ pub(super) async fn exec_and_print(
}
// As the input stream comes, we can generate results.
// However, memory safety is not guaranteed.
Review Comment:
not related to this PR, but I don't understand this comment about Memory safety
##########
datafusion-cli/src/print_format.rs:
##########
@@ -539,6 +680,329 @@ mod tests {
.run();
}
+ #[test]
+ fn test_compute_column_widths() {
+ let schema = three_column_schema();
+ let batches = vec![three_column_batch()];
+ let format = PrintFormat::Table;
+ let widths = format.compute_column_widths(&batches, schema).unwrap();
+ assert_eq!(widths, vec![1, 1, 1]);
+
+ let schema = one_column_schema();
+ let batches = vec![one_column_batch()];
+ let format = PrintFormat::Table;
+ let widths = format.compute_column_widths(&batches, schema).unwrap();
+ assert_eq!(widths, vec![1]);
+
+ let schema = three_column_schema();
+ let batches = vec![three_column_batch_with_widths()];
+ let format = PrintFormat::Table;
+ let widths = format.compute_column_widths(&batches, schema).unwrap();
+ assert_eq!(widths, [7, 5, 6]);
+ }
+
+ #[test]
+ fn test_print_header() {
+ let schema = three_column_schema();
+ let widths = vec![1, 1, 1];
+ let mut writer = Vec::new();
+ let format = PrintFormat::Table;
+ format.print_header(&schema, &widths, &mut writer).unwrap();
+ let expected = &["+---+---+---+", "| a | b | c |", "+---+---+---+"];
+ let binding = String::from_utf8(writer.clone()).unwrap();
+ let actual: Vec<_> = binding.trim_end().split('\n').collect();
+ assert_eq!(actual, expected);
+ }
+
+ #[test]
+ fn test_print_batch_with_same_widths() {
+ let batch = three_column_batch();
+ let widths = vec![1, 1, 1];
+ let mut writer = Vec::new();
+ let format = PrintFormat::Table;
+ format
+ .print_batch_with_widths(&batch, &widths, &mut writer)
+ .unwrap();
+ let expected = &["| 1 | 4 | 7 |", "| 2 | 5 | 8 |", "| 3 | 6 | 9 |"];
+ let binding = String::from_utf8(writer.clone()).unwrap();
+ let actual: Vec<_> = binding.trim_end().split('\n').collect();
+ assert_eq!(actual, expected);
+ }
+
+ #[test]
+ fn test_print_batch_with_different_widths() {
+ let batch = three_column_batch_with_widths();
+ let widths = vec![7, 5, 6];
+ let mut writer = Vec::new();
+ let format = PrintFormat::Table;
+ format
+ .print_batch_with_widths(&batch, &widths, &mut writer)
+ .unwrap();
+ let expected = &[
+ "| 1 | 42222 | 7 |",
+ "| 2222222 | 5 | 8 |",
+ "| 3 | 6 | 922222 |",
+ ];
+ let binding = String::from_utf8(writer.clone()).unwrap();
+ let actual: Vec<_> = binding.trim_end().split('\n').collect();
+ assert_eq!(actual, expected);
+ }
+
+ #[test]
+ fn test_print_dotted_line() {
+ let widths = vec![1, 1, 1];
+ let mut writer = Vec::new();
+ let format = PrintFormat::Table;
+ format.print_dotted_line(&widths, &mut writer).unwrap();
+ let expected = &["| . | . | . |"];
+ let binding = String::from_utf8(writer.clone()).unwrap();
+ let actual: Vec<_> = binding.trim_end().split('\n').collect();
+ assert_eq!(actual, expected);
+ }
+
+ #[test]
+ fn test_print_bottom_border() {
+ let widths = vec![1, 1, 1];
+ let mut writer = Vec::new();
+ let format = PrintFormat::Table;
+ format.print_bottom_border(&widths, &mut writer).unwrap();
+ let expected = &["+---+---+---+"];
+ let binding = String::from_utf8(writer.clone()).unwrap();
+ let actual: Vec<_> = binding.trim_end().split('\n').collect();
+ assert_eq!(actual, expected);
+ }
+
+ #[test]
+ fn test_print_batches_with_maxrows() {
+ let batch = one_column_batch();
+ let schema = one_column_schema();
+ let format = PrintFormat::Table;
+
+ // should print out entire output with no truncation if unlimited or
+ // limit greater than number of batches or equal to the number of batches
+ for max_rows in [MaxRows::Unlimited, MaxRows::Limited(5), MaxRows::Limited(3)] {
+ let mut writer = Vec::new();
+ format
+ .print_batches(
+ &mut writer,
+ schema.clone(),
+ &[batch.clone()],
+ max_rows,
+ true,
+ )
+ .unwrap();
+ let expected = &[
+ "+---+", "| a |", "+---+", "| 1 |", "| 2 |", "| 3 |", "+---+",
+ ];
+ let binding = String::from_utf8(writer.clone()).unwrap();
+ let actual: Vec<_> = binding.trim_end().split('\n').collect();
+ assert_eq!(actual, expected);
+ }
+
+ // should truncate output if limit is less than number of batches
+ let mut writer = Vec::new();
+ format
+ .print_batches(
+ &mut writer,
+ schema.clone(),
+ &[batch.clone()],
+ MaxRows::Limited(1),
+ true,
+ )
+ .unwrap();
+ let expected = &[
+ "+---+", "| a |", "+---+", "| 1 |", "| . |", "| . |", "| . |", "+---+",
+ ];
+ let binding = String::from_utf8(writer.clone()).unwrap();
+ let actual: Vec<_> = binding.trim_end().split('\n').collect();
+ assert_eq!(actual, expected);
+ }
+
+ // test print_batch with different batch widths
+ // and preview count is less than the first batch
Review Comment:
👍
##########
datafusion-cli/src/print_format.rs:
##########
@@ -209,6 +211,145 @@ impl PrintFormat {
}
Ok(())
}
+
+ #[allow(clippy::too_many_arguments)]
Review Comment:
most of the newly added functions don't use fields on `PrintFormat`
I think we could probably avoid the
```rust
#[allow(clippy::too_many_arguments)]
```
By encapsulating all the `mut` variables here in a new struct
Maybe something like
```rust
struct OutputStreamState {
preview_batches: Vec<RecordBatch>,
preview_row_count: usize,
preview_limit: usize,
precomputed_widths: Option<Vec<usize>>,
header_printed: bool,
writer: &mut dyn std::io::Write,
}
impl OutputStreamState {
fn process_batch(&mut self, batch: RecordBatch) { ... }
...
fn compute_column_widths(&self,
batches: &Vec<RecordBatch>,
schema: SchemaRef,
) {...}
}
```
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #14877:
URL: https://github.com/apache/datafusion/pull/14877#issuecomment-2691547654
I believe I also found another related issue with explain plans
- https://github.com/apache/datafusion/issues/14947
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #14877:
URL: https://github.com/apache/datafusion/pull/14877#issuecomment-2685473497
Thanks again @zhuqi-lucas
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "zhuqi-lucas (via GitHub)" <gi...@apache.org>.
zhuqi-lucas commented on code in PR #14877:
URL: https://github.com/apache/datafusion/pull/14877#discussion_r1970879572
##########
datafusion-cli/src/exec.rs:
##########
@@ -250,37 +245,43 @@ pub(super) async fn exec_and_print(
}
// As the input stream comes, we can generate results.
// However, memory safety is not guaranteed.
Review Comment:
It's also confusing to me, maybe we can remove the message. It should be memory safe, we only keep one batch size.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Datafusion-cli: Redesign the datafusion-cli execution and print, make it totally streaming printing without memory overhead [datafusion]
Posted by "zhuqi-lucas (via GitHub)" <gi...@apache.org>.
zhuqi-lucas commented on code in PR #14877:
URL: https://github.com/apache/datafusion/pull/14877#discussion_r1970883672
##########
datafusion-cli/src/print_format.rs:
##########
@@ -209,6 +211,145 @@ impl PrintFormat {
}
Ok(())
}
+
+ #[allow(clippy::too_many_arguments)]
Review Comment:
Thanks @alamb for review, this is a very good suggestion!
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org