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

Prevent SQLite from writing incomplete data on transition errors #101

Merged
merged 1 commit into from
Sep 15, 2024
Merged
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
20 changes: 20 additions & 0 deletions src/sqlite/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ impl DataSink for SqliteDataSink {
_context: &Arc<TaskContext>,
) -> datafusion::common::Result<u64> {
let (batch_tx, mut batch_rx) = tokio::sync::mpsc::channel::<RecordBatch>(1);

// Since the main task/stream can be dropped or fail, we use a oneshot channel to signal that all data is received and we should commit the transaction
let (notify_commit_transaction, mut on_commit_transaction) =
tokio::sync::oneshot::channel();

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)?;

Expand All @@ -144,6 +149,15 @@ impl DataSink for SqliteDataSink {
})?;
}

if notify_commit_transaction.send(()).is_err() {
return Err(DataFusionError::Execution(
"Unable to send message to commit transaction to SQLite writer.".to_string(),
));
};

// Drop the sender to signal the receiver that no more data is coming
drop(batch_tx);

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

Expand All @@ -165,6 +179,12 @@ impl DataSink for SqliteDataSink {
}
}

if on_commit_transaction.try_recv().is_err() {
return Err(tokio_rusqlite::Error::Other(
"No message to commit transaction has been received.".into(),
));
}

transaction.commit()?;

Ok(())
Expand Down