15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
- //! Column expression
18
+ //! Physical column reference: [`Column`]
19
19
20
20
use std:: any:: Any ;
21
21
use std:: hash:: { Hash , Hasher } ;
@@ -33,32 +33,67 @@ use datafusion_expr::ColumnarValue;
33
33
use crate :: physical_expr:: { down_cast_any_ref, PhysicalExpr } ;
34
34
35
35
/// 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
36
67
#[ derive( Debug , Hash , PartialEq , Eq , Clone ) ]
37
68
pub struct Column {
69
+ /// The name of the column (used for debugging and display purposes)
38
70
name : String ,
71
+ /// The index of the column in its schema
39
72
index : usize ,
40
73
}
41
74
42
75
impl 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.
44
78
pub fn new ( name : & str , index : usize ) -> Self {
45
79
Self {
46
80
name : name. to_owned ( ) ,
47
81
index,
48
82
}
49
83
}
50
84
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
52
87
pub fn new_with_schema ( name : & str , schema : & Schema ) -> Result < Self > {
53
88
Ok ( Column :: new ( name, schema. index_of ( name) ?) )
54
89
}
55
90
56
- /// Get the column name
91
+ /// Get the column's name
57
92
pub fn name ( & self ) -> & str {
58
93
& self . name
59
94
}
60
95
61
- /// Get the column index
96
+ /// Get the column's schema index
62
97
pub fn index ( & self ) -> usize {
63
98
self . index
64
99
}
0 commit comments