Skip to content

Commit 2b33cc2

Browse files
authored
Print schema option (#30)
* Print schema option * Bump version
1 parent 011741b commit 2b33cc2

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repository = "https://github.com/sundy-li/arrow_cli"
77
edition = "2024"
88
license = "Apache-2.0"
99
name = "arrow_cli"
10-
version = "0.3.0"
10+
version = "0.3.1"
1111

1212

1313
[dependencies]

src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ struct Args {
3939
help = "Execute query using prepared statement"
4040
)]
4141
prepared: bool,
42+
43+
#[clap(long, default_value = "false", help = "Print resultset schema")]
44+
print_schema: bool,
4245
}
4346

4447
#[tokio::main]
@@ -50,9 +53,7 @@ pub async fn main() -> Result<(), ArrowError> {
5053
let url = format!("{protocol}://{}:{}", args.host, args.port);
5154
let endpoint = endpoint(&args, url)?;
5255
let is_repl = atty::is(Stream::Stdin);
53-
let mut session =
54-
session::Session::try_new(endpoint, &args.user, &args.password, is_repl, args.prepared)
55-
.await?;
56+
let mut session = session::Session::try_new(endpoint, is_repl, args).await?;
5657

5758
session.handle().await;
5859
Ok(())

src/session.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,20 @@ use std::io::BufRead;
1313
use tokio::time::Instant;
1414
use tonic::transport::{Channel, Endpoint};
1515

16-
use crate::helper::CliHelper;
16+
use crate::{Args, helper::CliHelper};
1717

1818
pub struct Session {
1919
client: FlightSqlServiceClient<Channel>,
2020
is_repl: bool,
2121
prompt: String,
22-
prepared: bool,
22+
args: Args,
2323
}
2424

2525
impl Session {
2626
pub async fn try_new(
2727
endpoint: Endpoint,
28-
user: &str,
29-
password: &str,
3028
is_repl: bool,
31-
prepared: bool,
29+
args: Args,
3230
) -> Result<Self, ArrowError> {
3331
let channel = endpoint
3432
.connect()
@@ -37,21 +35,21 @@ impl Session {
3735

3836
if is_repl {
3937
println!("Welcome to Arrow CLI v{}.", env!("CARGO_PKG_VERSION"));
40-
println!("Connecting to {} as user {}.", endpoint.uri(), user);
38+
println!("Connecting to {} as user {}.", endpoint.uri(), args.user);
4139
println!();
4240
}
4341

4442
let mut client = FlightSqlServiceClient::new_from_inner(
4543
FlightServiceClient::new(channel).max_decoding_message_size(usize::MAX),
4644
);
47-
let _token = client.handshake(user, password).await?;
45+
let _token = client.handshake(&args.user, &args.password).await?;
4846

4947
let prompt = format!("{} :) ", endpoint.uri().host().unwrap());
5048
Ok(Self {
5149
client,
5250
is_repl,
5351
prompt,
54-
prepared,
52+
args,
5553
})
5654
}
5755

@@ -130,7 +128,7 @@ impl Session {
130128
}
131129

132130
let start = Instant::now();
133-
let flight_info = if self.prepared {
131+
let flight_info = if self.args.prepared {
134132
let mut stmt = self.client.prepare(query.to_string(), None).await?;
135133
let info = stmt.execute().await?;
136134
stmt.close().await?;
@@ -142,7 +140,7 @@ impl Session {
142140
let mut batches: Vec<RecordBatch> = Vec::new();
143141

144142
let mut handles = Vec::with_capacity(flight_info.endpoint.len());
145-
for endpoint in flight_info.endpoint {
143+
for endpoint in flight_info.endpoint.iter() {
146144
let ticket = endpoint
147145
.ticket
148146
.as_ref()
@@ -166,17 +164,20 @@ impl Session {
166164
if is_repl {
167165
let res = pretty_format_batches(batches.as_slice())?;
168166

169-
println!("{res}");
170-
println!();
167+
println!("{res}\n");
168+
169+
if self.args.print_schema {
170+
let schema = flight_info.try_decode_schema()?;
171+
println!("{schema:#?}\n");
172+
}
171173

172174
let rows: usize = batches.iter().map(|b| b.num_rows()).sum();
173175
println!(
174-
"{} rows in set (tickets received in {:.3} sec, rows received in {:.3} sec)",
176+
"{} rows in set (tickets received in {:.3} sec, rows received in {:.3} sec)\n",
175177
rows,
176178
ticket_recv_duration.as_secs_f64(),
177179
rows_recv_duration.as_secs_f64(),
178180
);
179-
println!();
180181
} else {
181182
let res = print_batches_with_sep(batches.as_slice(), b'\t')?;
182183
print!("{res}");

0 commit comments

Comments
 (0)