blaginin commented on code in PR #27437:
URL: https://github.com/apache/datafusion/pull/27437#discussion_r4593191249
##########
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:
oo thank you fixed. i think we sometimes will go slightly off the width but
it's ok - show the correct data is better than to show something incorrect
--
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]