Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 22 additions & 7 deletions dev-tools/omdb/src/bin/omdb/oxql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub struct OxqlArgs {
/// Print the total elapsed query duration.
#[clap(long = "elapsed")]
print_elapsed: bool,

/// Execute the query and exit.
#[clap(long, short, value_name = "QUERY")]
execute: Option<String>,
}

impl OxqlArgs {
Expand All @@ -54,13 +58,24 @@ impl OxqlArgs {
print_elapsed: self.print_elapsed,
};

oxql::shell(
native_addr.ip(),
native_addr.port(),
log.new(slog::o!("component" => "clickhouse-client")),
opts,
)
.await
if let Some(query) = &self.execute {
oxql::exec_query(
native_addr.ip(),
native_addr.port(),
log.new(slog::o!("component" => "clickhouse-client")),
opts,
query.to_owned(),
)
.await
} else {
oxql::shell(
native_addr.ip(),
native_addr.port(),
log.new(slog::o!("component" => "clickhouse-client")),
opts,
)
.await
}
}

/// Resolve the ClickHouse native TCP socket address.
Expand Down
47 changes: 41 additions & 6 deletions oximeter/db/src/shells/oxql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ pub async fn shell(
opts: ShellOptions,
) -> anyhow::Result<()> {
// Create the client.
let client = make_client(address, port, &log).await?;

// A workaround to ensure the client has all available timeseries when the
// shell starts.
let dummy = "foo:bar".parse().unwrap();
let _ = client.schema_for_timeseries(&dummy).await;
let client = make_oxql_client(address, port, &log).await?;

// Create the line-editor.
let mut ed = Reedline::create();
Expand Down Expand Up @@ -157,6 +152,46 @@ pub async fn shell(
}
}

/// Execute the provided OxQL query.
pub async fn exec_query(
address: IpAddr,
port: u16,
log: Logger,
opts: ShellOptions,
statement: String,
) -> anyhow::Result<()> {
// Create the client.
let client = make_oxql_client(address, port, &log).await?;

let result = client
.oxql_query(
statement.trim().trim_end_matches(';'),
QueryAuthzScope::Fleet,
)
.await?;

print_tables(&result.tables);
println!();
print_query_summary(&result, opts.print_elapsed, opts.print_summaries);

Ok(())
}

/// Create an OxQL client and prime its schema cache.
async fn make_oxql_client(
address: IpAddr,
port: u16,
log: &Logger,
) -> anyhow::Result<Client> {
let client = make_client(address, port, log).await?;

// Workaround to ensure the client has all available timeseries.
let dummy = "foo:bar".parse().unwrap();
let _ = client.schema_for_timeseries(&dummy).await;

Ok(client)
}

/// Describe a single timeseries.
async fn describe_timeseries(
client: &Client,
Expand Down
Loading