Skip to content

Commit f5091c3

Browse files
Add comment for ProjectionExpr::from_indices
1 parent e20b1a0 commit f5091c3

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

datafusion/physical-expr/src/projection.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,49 @@ impl ProjectionExprs {
149149
}
150150
}
151151

152+
/// Creates a [`ProjectionExpr`] from a list of column indices.
153+
///
154+
/// This is a convenience method for creating simple column-only projections, where each projection expression is a reference to a column
155+
/// in the input schema.
156+
///
157+
/// # Behavior
158+
/// - Ordering: the output projection preserves the exact order of indices provided in the input slice
159+
/// For example, `[2, 0, 1]` will produce projections for columns 2, 0, then 1 in that order
160+
/// - Duplicates: Duplicate indices are allowed and will create multiple projection expressions referencing the same source column
161+
/// For example, `[0, 0]` creates 2 separate projections both referencing column 0
162+
///
163+
/// # Panics
164+
/// Panics if any index in `indices` is out of bounds for the provided schema.
165+
///
166+
/// # Example
167+
///
168+
/// ```rust
169+
/// use std::sync::Arc;
170+
/// use arrow::datatypes::{Schema, Field, DataType};
171+
/// use datafusion_physical_expr::projection::ProjectionExprs;
172+
///
173+
/// // Create a schema with three columns
174+
/// let schema = Arc::new(Schema::new(vec![
175+
/// Field::new("a", DataType::Int32, false),
176+
/// Field::new("b", DataType::Utf8, false),
177+
/// Field::new("c", DataType::Float64, false),
178+
/// ]));
179+
///
180+
/// // Project columns at indices 2 and 0 (c and a) - ordering is preserved
181+
/// let projection = ProjectionExprs::from_indices(&[2, 0], &schema);
182+
///
183+
/// // This creates: SELECT c@2 AS c, a@0 AS a
184+
/// assert_eq!(projection.as_ref().len(), 2);
185+
/// assert_eq!(projection.as_ref()[0].alias, "c");
186+
/// assert_eq!(projection.as_ref()[1].alias, "a");
187+
///
188+
/// // Duplicate indices are allowed
189+
/// let projection_with_dups = ProjectionExprs::from_indices(&[0, 0, 1], &schema);
190+
/// assert_eq!(projection_with_dups.as_ref().len(), 3);
191+
/// assert_eq!(projection_with_dups.as_ref()[0].alias, "a");
192+
/// assert_eq!(projection_with_dups.as_ref()[1].alias, "a"); // duplicate
193+
/// assert_eq!(projection_with_dups.as_ref()[2].alias, "b");
194+
/// ```
152195
pub fn from_indices(indices: &[usize], schema: &SchemaRef) -> Self {
153196
let projection_exprs = indices.iter().map(|&i| {
154197
let field = schema.field(i);

0 commit comments

Comments
 (0)