kosiew commented on code in PR #27437:
URL: https://github.com/apache/datafusion/pull/27437#discussion_r4583552417
##########
datafusion-cli/src/command.rs:
##########
@@ -248,6 +252,15 @@ impl FromStr for OutputFormat {
impl OutputFormat {
pub fn execute(&self, print_options: &mut PrintOptions) -> Result<()> {
match self {
+ Self::ChangeMaxWidth(width) => {
Review Comment:
Optional: could we add command-state coverage for `\pset maxwidth`? It would
be useful to verify displaying the current value, switching fixed → auto →
unlimited, and rejecting invalid input without changing the previous setting.
##########
datafusion-cli/src/exec.rs:
##########
@@ -250,10 +250,33 @@ impl StatementExecutor {
let (df, adjusted) = self
.create_and_execute_logical_plan(ctx, print_options)
.await?;
+ let (print_options, table_options) = adjusted.into_options();
+ let df = if table_options.explain_format == Some(ExplainFormat::Tree)
+ && print_options.format == PrintFormat::Table
+ {
+ let (mut state, plan) = df.into_parts();
+ let render_width = match (table_options.width,
print_options.maxwidth) {
+ (Some(width), _) => {
+ // The renderer emits whole 29-column boxes, rounding its
limit up.
+ Some((usize::from(width) / 29 * 29).max(1))
Review Comment:
At `--maxwidth 20`, `EXPLAIN SELECT 123` still emits 29-column TREE borders
because the renderer cannot shrink below one box and `print_tree_batches`
bypasses wrapping. Please define a fallback or return an actionable error below
29 columns, including for auto-detected widths, and test the 28/29 boundary.
##########
datafusion-cli/tests/cli_integration.rs:
##########
@@ -675,6 +675,54 @@ fn test_cli_wide_result_set_no_crash() {
assert_cmd_snapshot!(cmd);
}
+#[test]
+fn cli_output_respects_maxwidth() {
Review Comment:
Optional: consider a PTY-backed test for default `auto` width and a paired
redirected-output test. A 150-character value should wrap within a 40-column
terminal but remain unwrapped when stdout is redirected; both passed manual
checks but are not covered by the fixed-width integration test.
##########
datafusion-cli/src/print_format.rs:
##########
@@ -76,70 +108,266 @@ fn print_batches_with_sep<W: std::io::Write>(
Ok(())
}
-fn keep_only_maxrows(s: &str, maxrows: usize) -> String {
- let lines: Vec<String> = s.lines().map(String::from).collect();
-
- assert!(lines.len() >= maxrows + 4); // 4 lines for top and bottom border
-
- let last_line = &lines[lines.len() - 1]; // bottom border line
-
- let spaces = last_line.len().saturating_sub(4);
- let dotted_line = format!("| .{}|", " ".repeat(spaces));
-
- let mut result = lines[0..(maxrows + 3)].to_vec(); // Keep top border and
`maxrows` lines
- result.extend(vec![dotted_line; 3]); // Append ... lines
- result.push(last_line.clone());
-
- result.join("\n")
-}
-
-fn format_batches_with_maxrows<W: std::io::Write>(
+fn print_table_batches<W: std::io::Write>(
writer: &mut W,
+ schema: SchemaRef,
batches: &[RecordBatch],
maxrows: MaxRows,
+ width: Option<u16>,
format_options: &FormatOptions,
+ preserve_indent: Option<PreserveIndent>,
) -> Result<()> {
- let options: arrow::util::display::FormatOptions =
format_options.try_into()?;
-
- match maxrows {
- MaxRows::Limited(maxrows) => {
- // Filter batches to meet the maxrows condition
- let mut filtered_batches = Vec::new();
- let mut row_count: usize = 0;
- let mut over_limit = false;
- for batch in batches {
- if row_count + batch.num_rows() > maxrows {
- // If adding this batch exceeds maxrows, slice the batch
- let limit = maxrows - row_count;
- let sliced_batch = batch.slice(0, limit);
- filtered_batches.push(sliced_batch);
- over_limit = true;
- break;
- } else {
- filtered_batches.push(batch.clone());
- row_count += batch.num_rows();
+ let schema = batches
+ .iter()
+ .find(|batch| batch.num_rows() > 0)
+ .map_or(schema, RecordBatch::schema);
+ if schema.fields().is_empty() || maxrows == MaxRows::Limited(0) {
+ return Ok(());
+ }
+
+ let headers: Vec<_> = schema
+ .fields()
+ .iter()
+ .map(|field| {
+ if format_options.types_info {
+ format!("{}\n{}", field.name(), field.data_type())
+ } else {
+ field.name().to_owned()
+ }
+ })
+ .collect();
+ let options = format_options.try_into()?;
+ let limit = match maxrows {
+ MaxRows::Limited(limit) => limit,
+ MaxRows::Unlimited => usize::MAX,
+ };
+ let mut rows: Vec<Vec<String>> = Vec::new();
+ let mut over_limit = false;
+ for batch in batches {
+ let formatters = batch
+ .columns()
+ .iter()
+ .map(|array| ArrayFormatter::try_new(array.as_ref(), &options))
+ .collect::<std::result::Result<Vec<_>, _>>()?;
+ for row in 0..batch.num_rows() {
+ if rows.len() == limit {
+ over_limit = true;
+ break;
+ }
+ rows.push(
+ formatters
+ .iter()
+ .map(|formatter| formatter.value(row).try_to_string())
+ .collect::<std::result::Result<Vec<_>, _>>()?,
+ );
+ }
+ if over_limit {
+ break;
+ }
+ }
+
+ if let Some(width) = width
+ && usize::from(width) <
minimum_table_width(schema.fields().len().max(2))
+ {
+ let empty = rows.is_empty();
+ if empty {
+ rows.push(
+ schema
+ .fields()
+ .iter()
+ .map(|field| field.data_type().to_string())
+ .collect(),
+ );
+ }
+ let visible_rows = rows.len();
+ for (index, row) in rows.into_iter().enumerate() {
+ if index > 0 {
+ writeln!(writer)?;
+ }
+ if usize::from(width) < minimum_table_width(2) {
+ for (column, (header, value)) in
headers.iter().zip(row).enumerate() {
+ print_wrapped(writer, header, width, None)?;
+ print_wrapped(
+ writer,
+ &value,
+ width,
+ preserve_indent
+ .filter(|option| option.column == column)
+ .map(|option| option.continuation_indent),
+ )?;
}
+ if over_limit && index + 1 == visible_rows {
+ write!(writer, ".\n.\n.\n")?;
+ }
+ continue;
}
+ let vertical_rows: Vec<_> = headers
+ .iter()
+ .zip(row)
+ .map(|(header, value)| vec![header.clone(), value])
+ .collect();
+ let vertical_header = [
+ "Column".to_owned(),
+ if empty { "Type" } else { "Value" }.to_owned(),
+ ];
+ print_table(
+ writer,
+ &vertical_header,
+ &vertical_rows,
+ Some(width),
+ over_limit && index + 1 == visible_rows,
+ None,
+ )?;
+ }
+ } else {
+ print_table(writer, &headers, &rows, width, over_limit,
preserve_indent)?;
+ }
+ Ok(())
+}
- let formatted =
- pretty_format_batches_with_options(&filtered_batches,
&options)?;
- if over_limit {
- let mut formatted_str = format!("{formatted}");
- formatted_str = keep_only_maxrows(&formatted_str, maxrows);
- writeln!(writer, "{formatted_str}")?;
- } else {
- writeln!(writer, "{formatted}")?;
+fn print_table<W: std::io::Write>(
+ writer: &mut W,
+ headers: &[String],
+ rows: &[Vec<String>],
+ width: Option<u16>,
+ over_limit: bool,
+ preserve_indent: Option<PreserveIndent>,
+) -> Result<()> {
+ let mut builder = Builder::default();
+ for row in std::iter::once(headers).chain(rows.iter().map(Vec::as_slice)) {
+ builder.push_record(row);
+ }
+ let marker_start = rows.len() + 1;
+ let column_count = builder.count_columns();
+ if column_count == 0 {
+ return Ok(());
+ }
+ if over_limit {
+ for _ in 0..3 {
+ builder.push_record(
+ std::iter::once(".").chain(std::iter::repeat_n("",
column_count - 1)),
+ );
+ }
+ }
+ let mut table = builder.build();
+ table.with(
+ Style::ascii()
+ .remove_horizontal()
+ .horizontals([(1, HorizontalLine::full('-', '+', '+', '+'))]),
+ );
+ if over_limit {
+ for row in marker_start..marker_start + 3 {
+ table.modify(Cell::new(row, 0), Span::column(0));
+ }
+ }
+ if let Some(width) = width {
+ table.with(
+ Width::wrap(usize::from(width))
+ .keep_words(true)
+ .priority(Priority::max(false)),
+ );
+ if let Some(option) = preserve_indent {
+ let widths = table
+ .get_dimension()
+ .get_widths()
+ .expect("Width::wrap estimates column widths")
+ .to_vec();
+ for (row_index, row) in rows.iter().enumerate() {
+ let text = wrap_indented(
+ &row[option.column],
+ widths[option.column].saturating_sub(COLUMN_PADDING_WIDTH),
+ option.continuation_indent,
+ );
+ table
+ .get_records_mut()
+ .set(Position::new(row_index + 1, option.column), text);
}
+ table.get_dimension_mut().clear_height();
}
- MaxRows::Unlimited => {
- let formatted = pretty_format_batches_with_options(batches,
&options)?;
- writeln!(writer, "{formatted}")?;
+ }
+ writeln!(writer, "{table}")?;
+ Ok(())
+}
+
+fn print_tree_batches<W: std::io::Write>(
+ writer: &mut W,
+ batches: &[RecordBatch],
+ format_options: &FormatOptions,
+) -> Result<()> {
+ let options = format_options.try_into()?;
+ for batch in batches {
+ let kind = ArrayFormatter::try_new(batch.column(0).as_ref(),
&options)?;
+ let plan = ArrayFormatter::try_new(batch.column(1).as_ref(),
&options)?;
+ for row in 0..batch.num_rows() {
+ writeln!(writer, "{}", kind.value(row).try_to_string()?)?;
+ writeln!(writer, "{}", plan.value(row).try_to_string()?)?;
}
}
+ Ok(())
+}
+fn print_wrapped<W: std::io::Write>(
+ writer: &mut W,
+ text: &str,
+ width: u16,
+ continuation_indent: Option<usize>,
+) -> Result<()> {
+ let width = usize::from(width);
+ if continuation_indent.is_none()
+ && text
+ .lines()
+ .all(|line| textwrap::core::display_width(line) <= width)
+ {
+ writeln!(writer, "{text}")?;
+ return Ok(());
+ }
+ let text = match continuation_indent {
+ Some(indent) => wrap_indented(text, width, indent),
+ None => Wrap::wrap(text, width, true),
Review Comment:
At `--maxwidth 1`, `SELECT '中' AS s` succeeds but displays U+FFFD instead of
`中` because `Wrap::wrap` replaces glyphs wider than the available width. Please
handle this case without silently changing displayed values, for example by
returning a clear error, and add a regression test for a two-column character
at width 1.
--
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]