This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/master by this push:
new b44238d add --quiet/-q flag and allow timing info to be turned on/off
(#323)
b44238d is described below
commit b44238d05094ab0fa0171769ce8b890a0045e1e1
Author: Jiayu Liu <[email protected]>
AuthorDate: Fri May 14 02:38:19 2021 +0800
add --quiet/-q flag and allow timing info to be turned on/off (#323)
* add print options and allow timing info to be turned on/off
* remove self reference
* use quiet
---
datafusion-cli/src/format.rs | 17 -------
datafusion-cli/src/lib.rs | 56 ++++++++++++++++++++++
datafusion-cli/src/main.rs | 63 +++++++++----------------
datafusion-cli/src/{format => }/print_format.rs | 0
4 files changed, 77 insertions(+), 59 deletions(-)
diff --git a/datafusion-cli/src/format.rs b/datafusion-cli/src/format.rs
deleted file mode 100644
index c5da78f..0000000
--- a/datafusion-cli/src/format.rs
+++ /dev/null
@@ -1,17 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-pub mod print_format;
diff --git a/datafusion-cli/src/lib.rs b/datafusion-cli/src/lib.rs
new file mode 100644
index 0000000..5bd16e3
--- /dev/null
+++ b/datafusion-cli/src/lib.rs
@@ -0,0 +1,56 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+pub mod print_format;
+
+use datafusion::arrow::record_batch::RecordBatch;
+use datafusion::error::Result;
+use print_format::PrintFormat;
+use std::time::Instant;
+
+#[derive(Debug, Clone)]
+pub struct PrintOptions {
+ pub format: PrintFormat,
+ pub quiet: bool,
+}
+
+fn print_timing_info(row_count: usize, now: Instant) {
+ println!(
+ "{} {} in set. Query took {} seconds.",
+ row_count,
+ if row_count == 1 { "row" } else { "rows" },
+ now.elapsed().as_secs()
+ );
+}
+
+impl PrintOptions {
+ /// print the batches to stdout using the specified format
+ pub fn print_batches(&self, batches: &[RecordBatch]) -> Result<()> {
+ let now = Instant::now();
+ if batches.is_empty() {
+ if !self.quiet {
+ print_timing_info(0, now);
+ }
+ } else {
+ self.format.print_batches(batches)?;
+ if !self.quiet {
+ let row_count: usize = batches.iter().map(|b|
b.num_rows()).sum();
+ print_timing_info(row_count, now);
+ }
+ }
+ Ok(())
+ }
+}
diff --git a/datafusion-cli/src/main.rs b/datafusion-cli/src/main.rs
index 2360d46..f36b5d9 100644
--- a/datafusion-cli/src/main.rs
+++ b/datafusion-cli/src/main.rs
@@ -17,19 +17,16 @@
#![allow(bare_trait_objects)]
-mod format;
-
use clap::{crate_version, App, Arg};
use datafusion::error::Result;
use datafusion::execution::context::{ExecutionConfig, ExecutionContext};
-use format::print_format::PrintFormat;
+use datafusion_cli::{print_format::PrintFormat, PrintOptions};
use rustyline::Editor;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::Path;
-use std::time::Instant;
#[tokio::main]
pub async fn main() {
@@ -72,6 +69,13 @@ pub async fn main() {
.validator(is_valid_format)
.takes_value(true),
)
+ .arg(
+ Arg::with_name("quite")
+ .help("Reduce printing other than the results and work
quietly")
+ .short("q")
+ .long("quiet")
+ .takes_value(false),
+ )
.get_matches();
if let Some(path) = matches.value_of("data-path") {
@@ -88,26 +92,29 @@ pub async fn main() {
execution_config = execution_config.with_batch_size(batch_size);
};
- let print_format = matches
+ let format = matches
.value_of("format")
.expect("No format is specified")
.parse::<PrintFormat>()
.expect("Invalid format");
+ let quiet = matches.is_present("quiet");
+ let print_options = PrintOptions { format, quiet };
+
if let Some(file_path) = matches.value_of("file") {
let file = File::open(file_path)
.unwrap_or_else(|err| panic!("cannot open file '{}': {}",
file_path, err));
let mut reader = BufReader::new(file);
- exec_from_lines(&mut reader, execution_config, print_format).await;
+ exec_from_lines(&mut reader, execution_config, print_options).await;
} else {
- exec_from_repl(execution_config, print_format).await;
+ exec_from_repl(execution_config, print_options).await;
}
}
async fn exec_from_lines(
reader: &mut BufReader<File>,
execution_config: ExecutionConfig,
- print_format: PrintFormat,
+ print_options: PrintOptions,
) {
let mut ctx = ExecutionContext::with_config(execution_config);
let mut query = "".to_owned();
@@ -121,7 +128,7 @@ async fn exec_from_lines(
let line = line.trim_end();
query.push_str(line);
if line.ends_with(';') {
- match exec_and_print(&mut ctx, print_format.clone(),
query).await {
+ match exec_and_print(&mut ctx, print_options.clone(),
query).await {
Ok(_) => {}
Err(err) => println!("{:?}", err),
}
@@ -138,14 +145,14 @@ async fn exec_from_lines(
// run the left over query if the last statement doesn't contain ‘;’
if !query.is_empty() {
- match exec_and_print(&mut ctx, print_format, query).await {
+ match exec_and_print(&mut ctx, print_options, query).await {
Ok(_) => {}
Err(err) => println!("{:?}", err),
}
}
}
-async fn exec_from_repl(execution_config: ExecutionConfig, print_format:
PrintFormat) {
+async fn exec_from_repl(execution_config: ExecutionConfig, print_options:
PrintOptions) {
let mut ctx = ExecutionContext::with_config(execution_config);
let mut rl = Editor::<()>::new();
@@ -163,7 +170,7 @@ async fn exec_from_repl(execution_config: ExecutionConfig,
print_format: PrintFo
Ok(ref line) if line.trim_end().ends_with(';') => {
query.push_str(line.trim_end());
rl.add_history_entry(query.clone());
- match exec_and_print(&mut ctx, print_format.clone(),
query).await {
+ match exec_and_print(&mut ctx, print_options.clone(),
query).await {
Ok(_) => {}
Err(err) => println!("{:?}", err),
}
@@ -220,39 +227,11 @@ fn is_exit_command(line: &str) -> bool {
async fn exec_and_print(
ctx: &mut ExecutionContext,
- print_format: PrintFormat,
+ print_options: PrintOptions,
sql: String,
) -> Result<()> {
- let now = Instant::now();
-
let df = ctx.sql(&sql)?;
let results = df.collect().await?;
-
- if results.is_empty() {
- println!(
- "0 rows in set. Query took {} seconds.",
- now.elapsed().as_secs()
- );
- return Ok(());
- }
-
- print_format.print_batches(&results)?;
-
- let row_count: usize = results.iter().map(|b| b.num_rows()).sum();
-
- if row_count > 1 {
- println!(
- "{} row in set. Query took {} seconds.",
- row_count,
- now.elapsed().as_secs()
- );
- } else {
- println!(
- "{} rows in set. Query took {} seconds.",
- row_count,
- now.elapsed().as_secs()
- );
- }
-
+ print_options.print_batches(&results)?;
Ok(())
}
diff --git a/datafusion-cli/src/format/print_format.rs
b/datafusion-cli/src/print_format.rs
similarity index 100%
rename from datafusion-cli/src/format/print_format.rs
rename to datafusion-cli/src/print_format.rs