1515// specific language governing permissions and limitations
1616// under the License.
1717
18- //! Column expression
18+ //! Physical column reference: [`Column`]
1919
2020use std:: any:: Any ;
2121use std:: hash:: { Hash , Hasher } ;
@@ -33,32 +33,67 @@ use datafusion_expr::ColumnarValue;
3333use crate :: physical_expr:: { down_cast_any_ref, PhysicalExpr } ;
3434
3535/// Represents the column at a given index in a RecordBatch
36+ ///
37+ /// This is a physical expression that represents a column at a given index in an
38+ /// arrow [`Schema`] / [`RecordBatch`].
39+ ///
40+ /// Unlike the [logical `Expr::Column`], this expression is always resolved by schema index,
41+ /// even though it does have a name. This is because the physical plan is always
42+ /// resolved to a specific schema and there is no concept of "relation"
43+ ///
44+ /// # Example:
45+ /// If the schema is `a`, `b`, `c` the `Column` for `b` would be represented by
46+ /// index 1, since `b` is the second colum in the schema.
47+ ///
48+ /// ```
49+ /// # use datafusion_physical_expr::expressions::Column;
50+ /// # use arrow::datatypes::{DataType, Field, Schema};
51+ /// // Schema with columns a, b, c
52+ /// let schema = Schema::new(vec![
53+ /// Field::new("a", DataType::Int32, false),
54+ /// Field::new("b", DataType::Int32, false),
55+ /// Field::new("c", DataType::Int32, false),
56+ /// ]);
57+ ///
58+ /// // reference to column b is index 1
59+ /// let column_b = Column::new_with_schema("b", &schema).unwrap();
60+ /// assert_eq!(column_b.index(), 1);
61+ ///
62+ /// // reference to column c is index 2
63+ /// let column_c = Column::new_with_schema("c", &schema).unwrap();
64+ /// assert_eq!(column_c.index(), 2);
65+ /// ```
66+ /// [logical `Expr::Column`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/enum.Expr.html#variant.Column
3667#[ derive( Debug , Hash , PartialEq , Eq , Clone ) ]
3768pub struct Column {
69+ /// The name of the column (used for debugging and display purposes)
3870 name : String ,
71+ /// The index of the column in its schema
3972 index : usize ,
4073}
4174
4275impl Column {
43- /// Create a new column expression
76+ /// Create a new column expression which references the
77+ /// column with the given index in the schema.
4478 pub fn new ( name : & str , index : usize ) -> Self {
4579 Self {
4680 name : name. to_owned ( ) ,
4781 index,
4882 }
4983 }
5084
51- /// Create a new column expression based on column name and schema
85+ /// Create a new column expression which references the
86+ /// column with the given name in the schema
5287 pub fn new_with_schema ( name : & str , schema : & Schema ) -> Result < Self > {
5388 Ok ( Column :: new ( name, schema. index_of ( name) ?) )
5489 }
5590
56- /// Get the column name
91+ /// Get the column's name
5792 pub fn name ( & self ) -> & str {
5893 & self . name
5994 }
6095
61- /// Get the column index
96+ /// Get the column's schema index
6297 pub fn index ( & self ) -> usize {
6398 self . index
6499 }
0 commit comments