|
| 1 | +#![cfg(feature = "metadata-duckdb")] |
| 2 | +//! Tests for numeric metadata validation (#58, #59) |
| 3 | +//! |
| 4 | +//! Verifies that negative file_size_bytes and footer_size values in catalog |
| 5 | +//! metadata are caught early with clear error messages instead of wrapping |
| 6 | +//! to huge values via unchecked `as u64`/`as usize` casts. |
| 7 | +
|
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use datafusion::error::Result as DataFusionResult; |
| 11 | +use datafusion::prelude::*; |
| 12 | +use datafusion_ducklake::{DuckLakeCatalog, DuckdbMetadataProvider}; |
| 13 | +use tempfile::TempDir; |
| 14 | + |
| 15 | +/// Creates a catalog with data, then corrupts file_size_bytes to a negative value |
| 16 | +fn create_catalog_with_negative_file_size(catalog_path: &std::path::Path) -> anyhow::Result<()> { |
| 17 | + let conn = duckdb::Connection::open_in_memory()?; |
| 18 | + conn.execute("INSTALL ducklake;", [])?; |
| 19 | + conn.execute("LOAD ducklake;", [])?; |
| 20 | + |
| 21 | + let ducklake_path = format!("ducklake:{}", catalog_path.display()); |
| 22 | + conn.execute(&format!("ATTACH '{}' AS test_catalog;", ducklake_path), [])?; |
| 23 | + |
| 24 | + conn.execute( |
| 25 | + "CREATE TABLE test_catalog.items (id INT, name VARCHAR);", |
| 26 | + [], |
| 27 | + )?; |
| 28 | + conn.execute( |
| 29 | + "INSERT INTO test_catalog.items VALUES (1, 'Widget'), (2, 'Gadget');", |
| 30 | + [], |
| 31 | + )?; |
| 32 | + |
| 33 | + // Detach the DuckLake catalog so we can tamper with the raw metadata |
| 34 | + conn.execute("DETACH test_catalog;", [])?; |
| 35 | + |
| 36 | + // Now open the catalog DB directly and corrupt file_size_bytes |
| 37 | + let meta_conn = duckdb::Connection::open(catalog_path)?; |
| 38 | + meta_conn.execute("UPDATE ducklake_data_file SET file_size_bytes = -1;", [])?; |
| 39 | + |
| 40 | + Ok(()) |
| 41 | +} |
| 42 | + |
| 43 | +/// Creates a catalog with data, then corrupts footer_size to a negative value |
| 44 | +fn create_catalog_with_negative_footer_size(catalog_path: &std::path::Path) -> anyhow::Result<()> { |
| 45 | + let conn = duckdb::Connection::open_in_memory()?; |
| 46 | + conn.execute("INSTALL ducklake;", [])?; |
| 47 | + conn.execute("LOAD ducklake;", [])?; |
| 48 | + |
| 49 | + let ducklake_path = format!("ducklake:{}", catalog_path.display()); |
| 50 | + conn.execute(&format!("ATTACH '{}' AS test_catalog;", ducklake_path), [])?; |
| 51 | + |
| 52 | + conn.execute( |
| 53 | + "CREATE TABLE test_catalog.items (id INT, name VARCHAR);", |
| 54 | + [], |
| 55 | + )?; |
| 56 | + conn.execute( |
| 57 | + "INSERT INTO test_catalog.items VALUES (1, 'Widget'), (2, 'Gadget');", |
| 58 | + [], |
| 59 | + )?; |
| 60 | + |
| 61 | + // Detach the DuckLake catalog so we can tamper with the raw metadata |
| 62 | + conn.execute("DETACH test_catalog;", [])?; |
| 63 | + |
| 64 | + // Now open the catalog DB directly and set footer_size to negative |
| 65 | + let meta_conn = duckdb::Connection::open(catalog_path)?; |
| 66 | + meta_conn.execute("UPDATE ducklake_data_file SET footer_size = -42;", [])?; |
| 67 | + |
| 68 | + Ok(()) |
| 69 | +} |
| 70 | + |
| 71 | +fn create_catalog(path: &str) -> DataFusionResult<Arc<DuckLakeCatalog>> { |
| 72 | + let provider = DuckdbMetadataProvider::new(path) |
| 73 | + .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?; |
| 74 | + let catalog = DuckLakeCatalog::new(provider) |
| 75 | + .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?; |
| 76 | + Ok(Arc::new(catalog)) |
| 77 | +} |
| 78 | + |
| 79 | +#[tokio::test] |
| 80 | +async fn test_negative_file_size_produces_clear_error() -> DataFusionResult<()> { |
| 81 | + let temp_dir = |
| 82 | + TempDir::new().map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?; |
| 83 | + let catalog_path = temp_dir.path().join("neg_size.ducklake"); |
| 84 | + |
| 85 | + create_catalog_with_negative_file_size(&catalog_path) |
| 86 | + .map_err(|e| datafusion::error::DataFusionError::External(e.into()))?; |
| 87 | + |
| 88 | + let catalog = create_catalog(&catalog_path.to_string_lossy())?; |
| 89 | + let ctx = SessionContext::new(); |
| 90 | + ctx.register_catalog("ducklake", catalog); |
| 91 | + |
| 92 | + let result = ctx |
| 93 | + .sql("SELECT * FROM ducklake.main.items") |
| 94 | + .await? |
| 95 | + .collect() |
| 96 | + .await; |
| 97 | + |
| 98 | + assert!( |
| 99 | + result.is_err(), |
| 100 | + "Query should fail with negative file_size_bytes" |
| 101 | + ); |
| 102 | + let err_msg = result.unwrap_err().to_string(); |
| 103 | + assert!( |
| 104 | + err_msg.contains("Invalid file_size_bytes"), |
| 105 | + "Error should mention invalid file_size_bytes, got: {}", |
| 106 | + err_msg |
| 107 | + ); |
| 108 | + assert!( |
| 109 | + err_msg.contains("-1"), |
| 110 | + "Error should contain the negative value, got: {}", |
| 111 | + err_msg |
| 112 | + ); |
| 113 | + |
| 114 | + Ok(()) |
| 115 | +} |
| 116 | + |
| 117 | +#[tokio::test] |
| 118 | +async fn test_negative_footer_size_is_gracefully_skipped() -> DataFusionResult<()> { |
| 119 | + let temp_dir = |
| 120 | + TempDir::new().map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?; |
| 121 | + let catalog_path = temp_dir.path().join("neg_footer.ducklake"); |
| 122 | + |
| 123 | + create_catalog_with_negative_footer_size(&catalog_path) |
| 124 | + .map_err(|e| datafusion::error::DataFusionError::External(e.into()))?; |
| 125 | + |
| 126 | + let catalog = create_catalog(&catalog_path.to_string_lossy())?; |
| 127 | + let ctx = SessionContext::new(); |
| 128 | + ctx.register_catalog("ducklake", catalog); |
| 129 | + |
| 130 | + // Negative footer_size should be skipped (not used as hint), query should succeed |
| 131 | + let df = ctx.sql("SELECT * FROM ducklake.main.items").await?; |
| 132 | + let batches = df.collect().await?; |
| 133 | + |
| 134 | + let total_rows: usize = batches.iter().map(|b| b.num_rows()).sum(); |
| 135 | + assert_eq!( |
| 136 | + total_rows, 2, |
| 137 | + "Should still return all rows when footer_size is negative" |
| 138 | + ); |
| 139 | + |
| 140 | + Ok(()) |
| 141 | +} |
0 commit comments