MCPcopy Create free account
hub / github.com/apache/datafusion / with_column

Method with_column

datafusion/core/src/dataframe/mod.rs:2180–2232  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 15

query_make_dateFunction · 0.80
query_to_dateFunction · 0.80
query_to_timestampFunction · 0.80
runFunction · 0.80
build_test_data_frameFunction · 0.80
with_columnFunction · 0.80
with_column_renamedFunction · 0.80
cast_expr_testFunction · 0.80

Calls 14

find_window_exprsFunction · 0.85
collectMethod · 0.80
colFunction · 0.50
mapMethod · 0.45
iterMethod · 0.45
schemaMethod · 0.45
cloneMethod · 0.45
nameMethod · 0.45
is_emptyMethod · 0.45
aliasMethod · 0.45
containsMethod · 0.45

Tested by 15

with_columnFunction · 0.64
with_column_renamedFunction · 0.64
cast_expr_testFunction · 0.64
with_column_nameFunction · 0.64
cache_testFunction · 0.64
cache_producer_testFunction · 0.64
write_table_with_orderFunction · 0.64