Add or replace a column in the DataFrame. # Example ``` # use datafusion::prelude::*; # use datafusion::error::Result; # #[tokio::main] # async fn main() -> Result<()> { let ctx = SessionContext::new(); let df = ctx .read_csv("tests/data/example.csv", CsvReadOptions::new()) .await?; let df = df.with_column("ab_sum", col("a") + col("b"))?; # Ok(()) # } ```
(self, name: &str, expr: Expr)
| 2178 | /// # } |
| 2179 | /// ``` |
| 2180 | pub fn with_column(self, name: &str, expr: Expr) -> Result<DataFrame> { |
| 2181 | let window_func_exprs = find_window_exprs([&expr]); |
| 2182 | |
| 2183 | let original_names: HashSet<String> = self |
| 2184 | .plan |
| 2185 | .schema() |
| 2186 | .iter() |
| 2187 | .map(|(_, f)| f.name().clone()) |
| 2188 | .collect(); |
| 2189 | |
| 2190 | // Maybe build window plan |
| 2191 | let plan = if window_func_exprs.is_empty() { |
| 2192 | self.plan |
| 2193 | } else { |
| 2194 | LogicalPlanBuilder::window_plan(self.plan, window_func_exprs)? |
| 2195 | }; |
| 2196 | |
| 2197 | let new_column = expr.alias(name); |
| 2198 | let mut col_exists = false; |
| 2199 | |
| 2200 | let mut fields: Vec<(Expr, bool)> = plan |
| 2201 | .schema() |
| 2202 | .iter() |
| 2203 | .filter_map(|(qualifier, field)| { |
| 2204 | // Skip new fields introduced by window_plan |
| 2205 | if !original_names.contains(field.name()) { |
| 2206 | return None; |
| 2207 | } |
| 2208 | |
| 2209 | if field.name() == name { |
| 2210 | col_exists = true; |
| 2211 | Some((new_column.clone(), true)) |
| 2212 | } else { |
| 2213 | let e = col(Column::from((qualifier, field))); |
| 2214 | Some((e, self.projection_requires_validation)) |
| 2215 | } |
| 2216 | }) |
| 2217 | .collect(); |
| 2218 | |
| 2219 | if !col_exists { |
| 2220 | fields.push((new_column, true)); |
| 2221 | } |
| 2222 | |
| 2223 | let project_plan = LogicalPlanBuilder::from(plan) |
| 2224 | .project_with_validation(fields)? |
| 2225 | .build()?; |
| 2226 | |
| 2227 | Ok(DataFrame { |
| 2228 | session_state: self.session_state, |
| 2229 | plan: project_plan, |
| 2230 | projection_requires_validation: false, |
| 2231 | }) |
| 2232 | } |
| 2233 | |
| 2234 | /// Rename one column by applying a new projection. This is a no-op if the column to be |
| 2235 | /// renamed does not exist. |