Execute this `DataFrame` and write the results to `table_name`. Returns a single [RecordBatch] containing a single column and row representing the count of total rows written. Unlike most other `DataFrame` methods, this method executes eagerly. Data is written to the table using the [`TableProvider::insert_into`] method. This is the same underlying implementation used by SQL `INSERT INTO` statem
(
self,
table_name: &str,
write_options: DataFrameWriteOptions,
)
| 1979 | /// method. This is the same underlying implementation used by SQL `INSERT |
| 1980 | /// INTO` statements. |
| 1981 | pub async fn write_table( |
| 1982 | self, |
| 1983 | table_name: &str, |
| 1984 | write_options: DataFrameWriteOptions, |
| 1985 | ) -> Result<Vec<RecordBatch>, DataFusionError> { |
| 1986 | let plan = if write_options.sort_by.is_empty() { |
| 1987 | self.plan |
| 1988 | } else { |
| 1989 | LogicalPlanBuilder::from(self.plan) |
| 1990 | .sort(write_options.sort_by)? |
| 1991 | .build()? |
| 1992 | }; |
| 1993 | |
| 1994 | let table_ref: TableReference = table_name.into(); |
| 1995 | let table_schema = self.session_state.schema_for_ref(table_ref.clone())?; |
| 1996 | let target = match table_schema.table(table_ref.table()).await? { |
| 1997 | Some(ref provider) => Ok(Arc::clone(provider)), |
| 1998 | _ => plan_err!("No table named '{table_name}'"), |
| 1999 | }?; |
| 2000 | |
| 2001 | let target = Arc::new(DefaultTableSource::new(target)); |
| 2002 | |
| 2003 | let plan = LogicalPlanBuilder::insert_into( |
| 2004 | plan, |
| 2005 | table_ref, |
| 2006 | target, |
| 2007 | write_options.insert_op, |
| 2008 | )? |
| 2009 | .build()?; |
| 2010 | |
| 2011 | DataFrame { |
| 2012 | session_state: self.session_state, |
| 2013 | plan, |
| 2014 | projection_requires_validation: self.projection_requires_validation, |
| 2015 | } |
| 2016 | .collect() |
| 2017 | .await |
| 2018 | } |
| 2019 | |
| 2020 | /// Execute the `DataFrame` and write the results to CSV file(s). |
| 2021 | /// |