Path: blob/main/crates/polars-plan/src/dsl/functions/selectors.rs
6940 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}3738/// Selects no columns.39pub fn empty() -> Selector {40Selector::Empty41}4243/// Selects all columns.44pub fn all() -> Selector {45Selector::Wildcard46}4748/// Select multiple columns by name.49pub fn cols<I, S>(names: I) -> Selector50where51I: IntoIterator<Item = S>,52S: Into<PlSmallStr>,53{54by_name(names, true)55}5657/// Select multiple columns by dtype.58pub fn dtype_col(dtype: &DataType) -> DataTypeSelector {59DataTypeSelector::AnyOf([dtype.clone()].into())60}6162/// Select multiple columns by dtype.63pub fn dtype_cols<DT: AsRef<[DataType]>>(dtype: DT) -> DataTypeSelector {64let dtypes = dtype.as_ref();65DataTypeSelector::AnyOf(dtypes.into())66}6768/// Select multiple columns by dtype.69pub fn by_name<S: Into<PlSmallStr>, I: IntoIterator<Item = S>>(names: I, strict: bool) -> Selector {70let mut selector = None;71let _s = &mut selector;72let names = names73.into_iter()74.map(Into::into)75.filter_map(|name| match name.as_str() {76"*" => {77*_s = Some(std::mem::take(_s).map_or(all(), |s| s | all()));78None79},80n if is_regex_projection(n) => {81let m = Selector::Matches(name);82*_s = Some(std::mem::take(_s).map_or_else(|| m.clone(), |s| s | m.clone()));83None84},85_ => Some(name),86})87.collect::<Arc<[_]>>();8889let no_names = names.is_empty();90let names = Selector::ByName { names, strict };91if let Some(selector) = selector {92if no_names { selector } else { selector | names }93} else {94names95}96}9798/// Select multiple columns by index.99pub fn index_cols<N: AsRef<[i64]>>(indices: N) -> Selector {100let indices = indices.as_ref().into();101Selector::ByIndex {102indices,103strict: true,104}105}106107108