MCPcopy Create free account
hub / github.com/apache/datafusion / format_batches_with_maxrows

Function format_batches_with_maxrows

datafusion-cli/src/print_format.rs:109–154  ·  view source on GitHub ↗
(
    writer: &mut W,
    batches: &[RecordBatch],
    maxrows: MaxRows,
    format_options: &FormatOptions,
)

Source from the content-addressed store, hash-verified

107}
108
109fn format_batches_with_maxrows<W: std::io::Write>(
110 writer: &mut W,
111 batches: &[RecordBatch],
112 maxrows: MaxRows,
113 format_options: &FormatOptions,
114) -> Result<()> {
115 let options: arrow::util::display::FormatOptions = format_options.try_into()?;
116
117 match maxrows {
118 MaxRows::Limited(maxrows) => {
119 // Filter batches to meet the maxrows condition
120 let mut filtered_batches = Vec::new();
121 let mut row_count: usize = 0;
122 let mut over_limit = false;
123 for batch in batches {
124 if row_count + batch.num_rows() > maxrows {
125 // If adding this batch exceeds maxrows, slice the batch
126 let limit = maxrows - row_count;
127 let sliced_batch = batch.slice(0, limit);
128 filtered_batches.push(sliced_batch);
129 over_limit = true;
130 break;
131 } else {
132 filtered_batches.push(batch.clone());
133 row_count += batch.num_rows();
134 }
135 }
136
137 let formatted =
138 pretty_format_batches_with_options(&filtered_batches, &options)?;
139 if over_limit {
140 let mut formatted_str = format!("{formatted}");
141 formatted_str = keep_only_maxrows(&formatted_str, maxrows);
142 writeln!(writer, "{formatted_str}")?;
143 } else {
144 writeln!(writer, "{formatted}")?;
145 }
146 }
147 MaxRows::Unlimited => {
148 let formatted = pretty_format_batches_with_options(batches, &options)?;
149 writeln!(writer, "{formatted}")?;
150 }
151 }
152
153 Ok(())
154}
155
156impl PrintFormat {
157 /// Print the batches to a writer using the specified format

Callers 1

print_batchesMethod · 0.85

Calls 5

newFunction · 0.85
keep_only_maxrowsFunction · 0.85
sliceMethod · 0.80
pushMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…