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: Stream SQLite writes #24

Merged
Merged
Changes from 6 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
55 changes: 31 additions & 24 deletions src/sqlite/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use datafusion::{
};
use futures::StreamExt;
use snafu::prelude::*;
use tokio::runtime::Handle;

use crate::util::{
constraints, on_conflict::OnConflict, retriable_error::check_and_mark_retriable_error,
Expand Down Expand Up @@ -119,30 +120,32 @@ impl DataSink for SqliteDataSink {
data: SendableRecordBatchStream,
_context: &Arc<TaskContext>,
) -> datafusion::common::Result<u64> {
let mut num_rows: u64 = 0;

let (batch_tx, mut batch_rx) = tokio::sync::mpsc::channel::<RecordBatch>(1);
let mut db_conn = self.sqlite.connect().await.map_err(to_datafusion_error)?;
let sqlite_conn = Sqlite::sqlite_conn(&mut db_conn).map_err(to_datafusion_error)?;

let data_batches_result = data
.collect::<Vec<datafusion::common::Result<RecordBatch>>>()
.await;

let data_batches: Vec<RecordBatch> = data_batches_result
.into_iter()
.collect::<Result<Vec<_>, _>>()
.map_err(check_and_mark_retriable_error)?;

constraints::validate_batch_with_constraints(&data_batches, self.sqlite.constraints())
.await
.context(super::ConstraintViolationSnafu)
.map_err(to_datafusion_error)?;

for data_batch in &data_batches {
num_rows += u64::try_from(data_batch.num_rows()).map_err(|e| {
DataFusionError::Execution(format!("Unable to convert num_rows() to u64: {e}"))
})?;
}
let constraints = self.sqlite.constraints().clone();
let mut data = data;
let task = tokio::spawn(async move {
let mut num_rows: u64 = 0;
while let Some(data_batch) = data.next().await {
let data_batch = data_batch.map_err(check_and_mark_retriable_error)?;
num_rows += u64::try_from(data_batch.num_rows()).map_err(|e| {
DataFusionError::Execution(format!("Unable to convert num_rows() to u64: {e}"))
})?;

constraints::validate_batch_with_constraints(&[data_batch.clone()], &constraints)
.await
.context(super::ConstraintViolationSnafu)
.map_err(to_datafusion_error)?;

batch_tx.send(data_batch).await.map_err(|err| {
DataFusionError::Execution(format!("Error sending data batch: {err}"))
})?;
}

Ok::<_, DataFusionError>(num_rows)
});

let overwrite = self.overwrite;
let sqlite = Arc::clone(&self.sqlite);
Expand All @@ -156,9 +159,9 @@ impl DataSink for SqliteDataSink {
sqlite.delete_all_table_data(&transaction)?;
}

for batch in data_batches {
if batch.num_rows() > 0 {
sqlite.insert_batch(&transaction, batch, on_conflict.as_ref())?;
while let Some(data_batch) = batch_rx.blocking_recv() {
Copy link
Collaborator

@sgrebnov sgrebnov Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peasee - can we perform a read query while streaming results (refreshing)? I have a concern that we can lock table for quite some time

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can perform read queries for tables except the ones still accelerating. If you try querying a table that's accelerating, the query will hang until the acceleration finishes before returning values.

This is the existing behavior on main.

if data_batch.num_rows() > 0 {
sqlite.insert_batch(&transaction, data_batch, on_conflict.as_ref())?;
}
}

Expand All @@ -170,6 +173,10 @@ impl DataSink for SqliteDataSink {
.context(super::UnableToInsertIntoTableAsyncSnafu)
.map_err(to_datafusion_error)?;

let num_rows = task.await.map_err(|err| {
DataFusionError::Execution(format!("Error sending data batch: {err}"))
})??;

Ok(num_rows)
}
}
Expand Down
Loading