Skip to content

Commit 055093c

Browse files
committed
fix: ensure that delete on an empty table works
I believe this was already fixed in 0.30.0 but these tests should make sure that's the case Closes #3983 Signed-off-by: R. Tyler Croy <rtyler@brokenco.de>
1 parent 6a6821e commit 055093c

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

crates/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ rstest = { version = "0.26.1" }
100100
serial_test = "3"
101101
tempfile = { workspace = true }
102102
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
103+
tokio-test = "0.4.5"
103104
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
104105

105106
[features]

crates/core/src/operations/delete.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,32 @@
55
//! that contain records that satisfy the predicate. Once files are determined
66
//! they are rewritten without the records.
77
//!
8-
//!
98
//! Predicates MUST be deterministic otherwise undefined behaviour may occur during the
109
//! scanning and rewriting phase.
1110
//!
1211
//! # Example
13-
//! ```rust ignore
14-
//! let table = open_table("../path/to/table")?;
15-
//! let (table, metrics) = DeleteBuilder::new(table.object_store(), table.state)
16-
//! .with_predicate(col("col1").eq(lit(1)))
17-
//! .await?;
12+
//! ```
13+
//! # use datafusion::logical_expr::{col, lit};
14+
//! # use deltalake_core::{DeltaTable, kernel::{DataType, PrimitiveType, StructType, StructField}};
15+
//! # use deltalake_core::operations::delete::DeleteBuilder;
16+
//! # tokio_test::block_on(async {
17+
//! # let schema = StructType::try_new(vec![
18+
//! # StructField::new(
19+
//! # "id".to_string(),
20+
//! # DataType::Primitive(PrimitiveType::String),
21+
//! # true,
22+
//! # )]).expect("Failed to generate schema for test");
23+
//! # let table = DeltaTable::try_from_url(url::Url::parse("memory://").unwrap())
24+
//! # .await.expect("Failed to construct DeltaTable instance for test")
25+
//! # .create()
26+
//! # .with_columns(schema.fields().cloned())
27+
//! # .await
28+
//! # .expect("Failed to create test table");
29+
//! let (table, metrics) = table.delete()
30+
//! .with_predicate(col("id").eq(lit(102)))
31+
//! .await
32+
//! .expect("Failed to delete");
33+
//! # })
1834
//! ````
1935
2036
use async_trait::async_trait;
@@ -470,8 +486,8 @@ async fn execute(
470486

471487
#[cfg(test)]
472488
mod tests {
473-
use crate::DeltaTable;
474-
use crate::TableProperty;
489+
use super::*;
490+
475491
use crate::kernel::DataType as DeltaDataType;
476492
use crate::operations::collect_sendable_stream;
477493
use crate::protocol::*;
@@ -480,6 +496,7 @@ mod tests {
480496
use crate::writer::test_utils::{
481497
get_arrow_schema, get_delta_schema, get_record_batch, setup_table_with_configuration,
482498
};
499+
use crate::{DeltaResult, DeltaTable, TableProperty};
483500
use arrow::array::Int32Array;
484501
use arrow::datatypes::{Field, Schema};
485502
use arrow::record_batch::RecordBatch;
@@ -493,6 +510,7 @@ mod tests {
493510
use datafusion::physical_plan::ExecutionPlan;
494511
use datafusion::prelude::*;
495512
use delta_kernel::schema::PrimitiveType;
513+
use pretty_assertions::assert_eq;
496514
use serde_json::json;
497515
use std::sync::Arc;
498516

@@ -509,6 +527,24 @@ mod tests {
509527
table
510528
}
511529

530+
#[tokio::test]
531+
async fn test_delete_on_empty_table() -> DeltaResult<()> {
532+
let table = setup_table(None).await;
533+
let batch = get_record_batch(None, false);
534+
let table = write_batch(table, batch).await;
535+
assert_eq!(Some(1), table.version());
536+
537+
let (table, _metrics) = DeleteBuilder::new(table.log_store(), None).await?;
538+
assert_eq!(Some(2), table.version());
539+
540+
let (table, _metrics) = DeleteBuilder::new(table.log_store(), None).await?;
541+
assert_eq!(Some(2), table.version());
542+
543+
let actual = get_data(&table).await;
544+
assert!(actual.is_empty());
545+
Ok(())
546+
}
547+
512548
#[tokio::test]
513549
async fn test_delete_when_delta_table_is_append_only() {
514550
let table = setup_table_with_configuration(TableProperty::AppendOnly, Some("true")).await;

0 commit comments

Comments
 (0)