Skip to content

Commit 54228d7

Browse files
alambcomphead
andauthored
Add example for LogicalPlanBuilder::insert_into (#14663)
* Add example for `LogicalPlanBuilder::insert_into` * Update datafusion/expr/src/logical_plan/builder.rs --------- Co-authored-by: Oleks V <[email protected]>
1 parent 580e622 commit 54228d7

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

datafusion/core/src/dataframe/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ impl DataFrame {
15481548

15491549
let plan = LogicalPlanBuilder::insert_into(
15501550
plan,
1551-
table_name.to_owned(),
1551+
table_ref,
15521552
target,
15531553
write_options.insert_op,
15541554
)?

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,40 @@ impl LogicalPlanBuilder {
380380
})))
381381
}
382382

383-
/// Create a [DmlStatement] for inserting the contents of this builder into the named table
383+
/// Create a [`DmlStatement`] for inserting the contents of this builder into the named table.
384+
///
385+
/// Note, use a [`DefaultTableSource`] to insert into a [`TableProvider`]
386+
///
387+
/// [`DefaultTableSource`]: https://docs.rs/datafusion/latest/datafusion/datasource/default_table_source/struct.DefaultTableSource.html
388+
/// [`TableProvider`]: https://docs.rs/datafusion/latest/datafusion/catalog/trait.TableProvider.html
389+
///
390+
/// # Example:
391+
/// ```
392+
/// # use datafusion_expr::{lit, LogicalPlanBuilder,
393+
/// # logical_plan::builder::LogicalTableSource,
394+
/// # };
395+
/// # use std::sync::Arc;
396+
/// # use arrow::datatypes::{Schema, DataType, Field};
397+
/// # use datafusion_expr::dml::InsertOp;
398+
/// #
399+
/// # fn test() -> datafusion_common::Result<()> {
400+
/// # let employee_schema = Arc::new(Schema::new(vec![
401+
/// # Field::new("id", DataType::Int32, false),
402+
/// # ])) as _;
403+
/// # let table_source = Arc::new(LogicalTableSource::new(employee_schema));
404+
/// // VALUES (1), (2)
405+
/// let input = LogicalPlanBuilder::values(vec![vec![lit(1)], vec![lit(2)]])?
406+
/// .build()?;
407+
/// // INSERT INTO MyTable VALUES (1), (2)
408+
/// let insert_plan = LogicalPlanBuilder::insert_into(
409+
/// input,
410+
/// "MyTable",
411+
/// table_source,
412+
/// InsertOp::Append,
413+
/// )?;
414+
/// # Ok(())
415+
/// # }
416+
/// ```
384417
pub fn insert_into(
385418
input: LogicalPlan,
386419
table_name: impl Into<TableReference>,

0 commit comments

Comments
 (0)