Skip to content

Commit b2f8e94

Browse files
authored
Minor: Improve zero partition check when inserting into MemTable (#14024)
* Improve zero partition check when inserting into `MemTable` * update err msg
1 parent 775c9a5 commit b2f8e94

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

datafusion/core/src/datasource/memory.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,6 @@ impl TableProvider for MemTable {
265265
input: Arc<dyn ExecutionPlan>,
266266
insert_op: InsertOp,
267267
) -> Result<Arc<dyn ExecutionPlan>> {
268-
if self.batches.is_empty() {
269-
return plan_err!("Cannot insert into MemTable with zero partitions.");
270-
}
271-
272268
// If we are inserting into the table, any sort order may be messed up so reset it here
273269
*self.sort_order.lock() = vec![];
274270

@@ -297,7 +293,7 @@ impl TableProvider for MemTable {
297293
if insert_op != InsertOp::Append {
298294
return not_impl_err!("{insert_op} not implemented for MemoryTable yet");
299295
}
300-
let sink = Arc::new(MemSink::new(self.batches.clone()));
296+
let sink = Arc::new(MemSink::try_new(self.batches.clone())?);
301297
Ok(Arc::new(DataSinkExec::new(
302298
input,
303299
sink,
@@ -340,9 +336,11 @@ impl MemSink {
340336
/// Creates a new [`MemSink`].
341337
///
342338
/// The caller is responsible for ensuring that there is at least one partition to insert into.
343-
fn new(batches: Vec<PartitionData>) -> Self {
344-
assert!(!batches.is_empty());
345-
Self { batches }
339+
fn try_new(batches: Vec<PartitionData>) -> Result<Self> {
340+
if batches.is_empty() {
341+
return plan_err!("Cannot insert into MemTable with zero partitions");
342+
}
343+
Ok(Self { batches })
346344
}
347345
}
348346

@@ -805,7 +803,7 @@ mod tests {
805803
.unwrap_err();
806804
// Ensure that there is a descriptive error message
807805
assert_eq!(
808-
"Error during planning: Cannot insert into MemTable with zero partitions.",
806+
"Error during planning: Cannot insert into MemTable with zero partitions",
809807
experiment_result.strip_backtrace()
810808
);
811809
Ok(())

0 commit comments

Comments
 (0)