This is an automated email from the ASF dual-hosted git repository.

jonah pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 487b952cf1 Return error message during planning when inserting into a 
MemTable with zero partitions. (#14011)
487b952cf1 is described below

commit 487b952cf1a748cc79724638f13e66761a6665e2
Author: Tobias Schwarzinger <[email protected]>
AuthorDate: Mon Jan 6 10:48:35 2025 +0100

    Return error message during planning when inserting into a MemTable with 
zero partitions. (#14011)
---
 datafusion/core/src/datasource/memory.rs | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/datafusion/core/src/datasource/memory.rs 
b/datafusion/core/src/datasource/memory.rs
index c1e0bea0b3..e8cf216ee9 100644
--- a/datafusion/core/src/datasource/memory.rs
+++ b/datafusion/core/src/datasource/memory.rs
@@ -265,6 +265,10 @@ impl TableProvider for MemTable {
         input: Arc<dyn ExecutionPlan>,
         insert_op: InsertOp,
     ) -> Result<Arc<dyn ExecutionPlan>> {
+        if self.batches.is_empty() {
+            return plan_err!("Cannot insert into MemTable with zero 
partitions.");
+        }
+
         // If we are inserting into the table, any sort order may be messed up 
so reset it here
         *self.sort_order.lock() = vec![];
 
@@ -333,7 +337,11 @@ impl DisplayAs for MemSink {
 }
 
 impl MemSink {
+    /// Creates a new [`MemSink`].
+    ///
+    /// The caller is responsible for ensuring that there is at least one 
partition to insert into.
     fn new(batches: Vec<PartitionData>) -> Self {
+        assert!(!batches.is_empty());
         Self { batches }
     }
 }
@@ -779,4 +787,27 @@ mod tests {
         assert_eq!(resulting_data_in_table[0].len(), 2);
         Ok(())
     }
+
+    // Test inserting a batch into a MemTable without any partitions
+    #[tokio::test]
+    async fn test_insert_into_zero_partition() -> Result<()> {
+        // Create a new schema with one field called "a" of type Int32
+        let schema = Arc::new(Schema::new(vec![Field::new("a", 
DataType::Int32, false)]));
+
+        // Create a new batch of data to insert into the table
+        let batch = RecordBatch::try_new(
+            schema.clone(),
+            vec![Arc::new(Int32Array::from(vec![1, 2, 3]))],
+        )?;
+        // Run the experiment and expect an error
+        let experiment_result = experiment(schema, vec![], 
vec![vec![batch.clone()]])
+            .await
+            .unwrap_err();
+        // Ensure that there is a descriptive error message
+        assert_eq!(
+            "Error during planning: Cannot insert into MemTable with zero 
partitions.",
+            experiment_result.strip_backtrace()
+        );
+        Ok(())
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to