Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds output format options to query command #659

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions one/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false

[dependencies]
anyhow.workspace = true
arrow.workspace = true
arrow-cast.workspace = true
arrow-flight.workspace = true
async-stream.workspace = true
Expand Down
57 changes: 53 additions & 4 deletions one/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use std::{sync::Arc, time::Duration};
use std::{io::Cursor, sync::Arc, time::Duration};

use anyhow::{bail, Context, Result};
use arrow_cast::{cast_with_options, CastOptions};
use arrow_flight::{
sql::{client::FlightSqlServiceClient, CommandGetDbSchemas, CommandGetTables},
FlightInfo,
};
use clap::Args;
use clap::Subcommand;
use clap::{Args, ValueEnum};
use core::str;
use datafusion::arrow::{
array::{ArrayRef, Datum as _, RecordBatch, StringArray},
datatypes::Schema,
util::pretty::pretty_format_batches,
};
use futures::TryStreamExt;
use tokio::io::AsyncWriteExt as _;
use tonic::transport::{Channel, Endpoint};

#[derive(Args, Debug)]
Expand All @@ -30,6 +31,20 @@ pub struct QueryOpts {

#[clap(subcommand)]
cmd: Command,

/// Output format of the results.
#[arg(short, long, default_value = "table", env = "CERAMIC_ONE_QUERY_OUTPUT")]
output: Output,
}

#[derive(ValueEnum, Clone, Debug)]
enum Output {
/// Output results in a human readable tabular form
Table,
/// Output results as a csv file with headers.
Csv,
/// Output results as new line delimited json objects.
Json,
}

/// Different available commands.
Expand Down Expand Up @@ -181,8 +196,42 @@ pub async fn run(opts: QueryOpts) -> Result<()> {
.await
.context("read flight data")?;

let res = pretty_format_batches(batches.as_slice()).context("format results")?;
println!("{res}");
match opts.output {
Output::Table => {
let res = pretty_format_batches(batches.as_slice()).context("format results")?;
println!("{res}");
}
Output::Csv => {
let mut buffer = Vec::new();
let mut header = true;
for batch in &batches {
{
let mut writer = arrow::csv::WriterBuilder::new()
.with_header(header)
.build(Cursor::new(&mut buffer));
writer.write(batch)?;
// Write the header only once
header = false;
}
tokio::io::stdout().write_all(&buffer).await?;
buffer.clear();
}
}
Output::Json => {
let mut buffer = Vec::new();
for batch in &batches {
{
let mut writer =
arrow::json::Writer::<_, arrow::json::writer::LineDelimited>::new(
Cursor::new(&mut buffer),
);
writer.write(batch)?;
}
tokio::io::stdout().write_all(&buffer).await?;
buffer.clear();
}
}
}

Ok(())
}
Expand Down