Skip to content

Commit c79273e

Browse files
authored
fix: Don't use maxrows as a "fetched rows" but calculate it from the batches (#1480)
* fix: Don't use `maxrows` as a "fetched rows" but calculate it from the batches Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org> * Set limit to the DataFrame if max_rows is Limited --------- Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
1 parent 6874852 commit c79273e

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

ballista-cli/src/exec.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ pub async fn exec_from_lines(
4141
print_options: &PrintOptions,
4242
) {
4343
let mut query = "".to_owned();
44-
let max_rows = match print_options.maxrows {
45-
datafusion_cli::print_options::MaxRows::Unlimited => usize::MAX,
46-
datafusion_cli::print_options::MaxRows::Limited(max_rows) => max_rows,
47-
};
4844

4945
for line in reader.lines() {
5046
match line {
@@ -55,7 +51,7 @@ pub async fn exec_from_lines(
5551
let line = line.trim_end();
5652
query.push_str(line);
5753
if line.ends_with(';') {
58-
match exec_and_print(ctx, print_options, query, max_rows).await {
54+
match exec_and_print(ctx, print_options, query).await {
5955
Ok(_) => {}
6056
Err(err) => println!("{err:?}"),
6157
}
@@ -76,7 +72,7 @@ pub async fn exec_from_lines(
7672

7773
// run the left over query if the last statement doesn't contain ‘;’
7874
if !query.is_empty() {
79-
match exec_and_print(ctx, print_options, query, max_rows).await {
75+
match exec_and_print(ctx, print_options, query).await {
8076
Ok(_) => {}
8177
Err(err) => println!("{err:?}"),
8278
}
@@ -109,11 +105,6 @@ pub async fn exec_from_repl(ctx: &SessionContext, print_options: &mut PrintOptio
109105

110106
let mut print_options = print_options.clone();
111107

112-
let max_rows = match print_options.maxrows {
113-
datafusion_cli::print_options::MaxRows::Unlimited => usize::MAX,
114-
datafusion_cli::print_options::MaxRows::Limited(max_rows) => max_rows,
115-
};
116-
117108
loop {
118109
match rl.readline("❯ ") {
119110
Ok(line) if line.starts_with('\\') => {
@@ -152,7 +143,7 @@ pub async fn exec_from_repl(ctx: &SessionContext, print_options: &mut PrintOptio
152143
}
153144
Ok(line) => {
154145
rl.add_history_entry(line.trim_end()).unwrap();
155-
match exec_and_print(ctx, &print_options, line, max_rows).await {
146+
match exec_and_print(ctx, &print_options, line).await {
156147
Ok(_) => {}
157148
Err(err) => eprintln!("{err:?}"),
158149
}
@@ -179,13 +170,19 @@ async fn exec_and_print(
179170
ctx: &SessionContext,
180171
print_options: &PrintOptions,
181172
sql: String,
182-
row_count: usize,
183173
) -> Result<()> {
184174
let now = Instant::now();
185175
let df = ctx.sql(&sql).await?;
176+
let df = match print_options.maxrows {
177+
datafusion_cli::print_options::MaxRows::Unlimited => df,
178+
datafusion_cli::print_options::MaxRows::Limited(max_rows) => {
179+
df.limit(0, Some(max_rows))?
180+
}
181+
};
186182
let schema = Arc::new(df.schema().as_arrow().clone());
187183
let results = df.collect().await?;
188-
print_options.print_batches(schema, &results, now, row_count, &Default::default())?;
184+
let rows = &results.iter().map(|b| b.num_rows()).sum::<usize>();
185+
print_options.print_batches(schema, &results, now, *rows, &Default::default())?;
189186

190187
Ok(())
191188
}

0 commit comments

Comments
 (0)