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

Resolved issue #6: Stream query in release mode. #7

Merged
merged 7 commits into from
Feb 14, 2024
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
15 changes: 13 additions & 2 deletions src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ mod getters;
mod insert;
pub mod prelude;
mod query;
mod stream;

use clickhouse::Client;
use clickhouse::{Client, Compression};

#[derive(Clone)]
pub struct ProtonClient {
client: Client,
// A separate client without compression is currently necessary to enable
// streaming queries in release builds. See issue #6 on Github:
// https://github.com/timeplus-io/proton-rust-client/issues/6
client_without_compression: Client,
url: String,
}

Expand All @@ -36,7 +41,13 @@ impl ProtonClient {
/// ```
pub fn new(url: &str) -> Self {
let client = Client::default().with_url(url.to_string());
let client_without_compression =
Client::with_compression(client.clone(), Compression::None);
let url = url.to_string();
Self { client, url }
Self {
client,
client_without_compression,
url,
}
}
}
51 changes: 51 additions & 0 deletions src/lib/stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::prelude::{ProtonClient, ProtonClientError, Result};
use clickhouse::query::RowCursor;
use clickhouse::Row;
use serde::Deserialize;

impl ProtonClient {
///
/// Executes a streaming query, returning a [`RowCursor`] to obtain results
/// as they become available from the stream. The key difference compared to fetch is that,
/// for streaming query, the returned result is a unbounded stream. Also,
/// a fetch_stream query will keep running continuously returning fresh data
/// until the application terminates..
///
/// # Example
///
/// ```no_run
/// use proton_client::ProtonClient;
/// use proton_client::prelude::Result;
///
/// async fn example() -> Result<()> {
///
/// #[derive(Debug, clickhouse::Row, serde::Deserialize)]
/// struct MyRow {
/// no: u32,
/// name: String,
/// }
///
/// let client = ProtonClient::new("http://localhost:3218");
///
/// let mut cursor = client
/// .fetch_stream::<MyRow>("SELECT ?fields from (test_stream) WHERE no BETWEEN 500 AND 504")
/// .await
/// .expect("[main/fetch]: Failed to fetch stream data");
///
/// while let Some(MyRow { name, no }) = cursor.next().await.expect("[main/fetch]: Failed to fetch data") {
/// println!("{name}: {no}");
/// }
/// # Ok(()) }
/// ```
pub async fn fetch_stream<T>(&self, query: &str) -> Result<RowCursor<T>>
where
T: Row + for<'b> Deserialize<'b>,
{
// Here we use the client without compression. For details, see:
// https://github.com/timeplus-io/proton-rust-client/issues/6
match self.client_without_compression.query(query).fetch::<T>() {
Ok(cursor) => Ok(cursor),
Err(e) => Err(ProtonClientError::FetchFailed(e.to_string())),
}
}
}
Loading