Skip to content

Commit c01c71f

Browse files
waynexia2010YOUY01
andauthored
ensure MemTable has at least one partition (#16754)
Co-authored-by: Yongting You <[email protected]>
1 parent ce3f62a commit c01c71f

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

datafusion-examples/examples/sql_analysis.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ from
274274
for table in tables {
275275
ctx.register_table(
276276
table.name,
277-
Arc::new(MemTable::try_new(Arc::new(table.schema.clone()), vec![])?),
277+
Arc::new(MemTable::try_new(
278+
Arc::new(table.schema.clone()),
279+
vec![vec![]],
280+
)?),
278281
)?;
279282
}
280283
// We can create a LogicalPlan from a SQL query like this

datafusion/catalog/src/memory/table.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,16 @@ pub struct MemTable {
6767
}
6868

6969
impl MemTable {
70-
/// Create a new in-memory table from the provided schema and record batches
70+
/// Create a new in-memory table from the provided schema and record batches.
71+
///
72+
/// Requires at least one partition. To construct an empty `MemTable`, pass
73+
/// `vec![vec![]]` as the `partitions` argument, this represents one partition with
74+
/// no batches.
7175
pub fn try_new(schema: SchemaRef, partitions: Vec<Vec<RecordBatch>>) -> Result<Self> {
76+
if partitions.is_empty() {
77+
return plan_err!("No partitions provided, expected at least one partition");
78+
}
79+
7280
for batches in partitions.iter().flatten() {
7381
let batches_schema = batches.schema();
7482
if !schema.contains(&batches_schema) {

datafusion/core/src/datasource/memory_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ mod tests {
446446
.unwrap_err();
447447
// Ensure that there is a descriptive error message
448448
assert_eq!(
449-
"Error during planning: Cannot insert into MemTable with zero partitions",
449+
"Error during planning: No partitions provided, expected at least one partition",
450450
experiment_result.strip_backtrace()
451451
);
452452
Ok(())

datafusion/core/tests/dataframe/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4851,7 +4851,7 @@ async fn use_var_provider() -> Result<()> {
48514851
Field::new("bar", DataType::Int64, false),
48524852
]));
48534853

4854-
let mem_table = Arc::new(MemTable::try_new(schema, vec![])?);
4854+
let mem_table = Arc::new(MemTable::try_new(schema, vec![vec![]])?);
48554855

48564856
let config = SessionConfig::new()
48574857
.with_target_partitions(4)

docs/source/library-user-guide/building-logical-plans.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ async fn main() -> Result<(), DataFusionError> {
181181
// TableProvider. For this example, we don't provide any data
182182
// but in production code, this would have `RecordBatch`es with
183183
// in memory data
184-
let table_provider = Arc::new(MemTable::try_new(Arc::new(schema), vec![])?);
184+
let table_provider = Arc::new(MemTable::try_new(Arc::new(schema), vec![vec![]])?);
185185
// Use the provider_as_source function to convert the TableProvider to a table source
186186
let table_source = provider_as_source(table_provider);
187187

0 commit comments

Comments
 (0)