Path: blob/main/crates/polars-plan/src/dsl/functions/selectors.rs
8431 views
use super::*;12/// Create a Column Expression based on a column name.3///4/// # Arguments5///6/// * `name` - A string slice that holds the name of the column. If a column with this name does not exist when the7/// LazyFrame is collected, an error is returned.8///9/// # Examples10///11/// ```ignore12/// // select a column name13/// col("foo")14/// ```15///16/// ```ignore17/// // select all columns by using a wildcard18/// col("*")19/// ```20///21/// ```ignore22/// // select specific columns by writing a regular expression that starts with `^` and ends with `$`23/// // only if regex features is activated24/// col("^foo.*$")25/// ```26pub fn col<S>(name: S) -> Expr27where28S: Into<PlSmallStr>,29{30let name = name.into();31match name.as_str() {32"*" => all().as_expr(),33n if is_regex_projection(n) => Expr::Selector(Selector::Matches(name)),34_ => Expr::Column(name),35}36}3738pub fn element() -> Expr {39Expr::Element40}4142/// Selects no columns.43pub fn empty() -> Selector {44Selector::Empty45}4647/// Selects all columns.48pub fn all() -> Selector {49Selector::Wildcard50}5152/// Select multiple columns by name.53pub fn cols<I, S>(names: I) -> Selector54where55I: IntoIterator<Item = S>,56S: Into<PlSmallStr>,57{58by_name(names, true, true)59}6061/// Select multiple columns by dtype.62pub fn dtype_col(dtype: &DataType) -> DataTypeSelector {63DataTypeSelector::AnyOf([dtype.clone()].into())64}6566/// Select multiple columns by dtype.67pub fn dtype_cols<DT: AsRef<[DataType]>>(dtype: DT) -> DataTypeSelector {68let dtypes = dtype.as_ref();69DataTypeSelector::AnyOf(dtypes.into())70}7172/// Select multiple columns by name.73///74/// When `expand_patterns` is `true`, a single wildcard `"*"` and anchored regex patterns75/// (e.g. `"^...$"`) are expanded to their matching columns. When `false`, names are76/// treated as literals.77pub fn by_name<S: Into<PlSmallStr>, I: IntoIterator<Item = S>>(78names: I,79strict: bool,80expand_patterns: bool,81) -> Selector {82if !expand_patterns {83let names = names.into_iter().map(Into::into).collect::<Arc<[_]>>();84return Selector::ByName { names, strict };85}8687// When expand_patterns is true, handle wildcards and regex patterns88let mut selector = None;89let _s = &mut selector;90let names = names91.into_iter()92.map(Into::into)93.filter_map(|name| match name.as_str() {94"*" => {95*_s = Some(std::mem::take(_s).map_or(all(), |s| s | all()));96None97},98n if is_regex_projection(n) => {99let m = Selector::Matches(name);100*_s = Some(std::mem::take(_s).map_or_else(|| m.clone(), |s| s | m.clone()));101None102},103_ => Some(name),104})105.collect::<Arc<[_]>>();106107let no_names = names.is_empty();108let names = Selector::ByName { names, strict };109if let Some(selector) = selector {110if no_names { selector } else { selector | names }111} else {112names113}114}115116/// Select multiple columns by index.117pub fn index_cols<N: AsRef<[i64]>>(indices: N) -> Selector {118let indices = indices.as_ref().into();119Selector::ByIndex {120indices,121strict: true,122}123}124125126