Skip to content

Commit 4309b85

Browse files
authored
Improve docs and examples for DataTypeExt and FieldExt (#18271)
## Which issue does this PR close? - Follow on to #17986 from @paleolimbot ## Rationale for this change As we thread Field through more of the DataFusion APs, making it easy to convert back and forth with Field will be increasingly important. We added some helper methods in #17986, but I think they could be better documented (I wrote them so this is not a dig at @paleolimbot !) Lets add some more documentation and examples so it is clearer what this code is doing. ## What changes are included in this PR? 1. Add more Documentation and examples so it is clearer what this code is doing. ## Are these changes tested? By CI ## Are there any user-facing changes? More docs, no functional changes
1 parent 2a82897 commit 4309b85

File tree

1 file changed

+77
-9
lines changed

1 file changed

+77
-9
lines changed

datafusion/common/src/datatype.rs

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
//! [DataTypeExt] extension trait for converting DataTypes to Fields
18+
//! [`DataTypeExt`] and [`FieldExt`] extension trait for working with DataTypes to Fields
1919
2020
use crate::arrow::datatypes::{DataType, Field, FieldRef};
2121
use std::sync::Arc;
@@ -27,9 +27,23 @@ pub trait DataTypeExt {
2727
/// This is used to track the places where we convert a [`DataType`]
2828
/// into a nameless field to interact with an API that is
2929
/// capable of representing an extension type and/or nullability.
30+
///
31+
/// For example, it will convert a `DataType::Int32` into
32+
/// `Field::new("", DataType::Int32, true)`.
33+
///
34+
/// ```
35+
/// # use datafusion_common::datatype::DataTypeExt;
36+
/// # use arrow::datatypes::DataType;
37+
/// let dt = DataType::Utf8;
38+
/// let field = dt.into_nullable_field();
39+
/// // result is a nullable Utf8 field with "" name
40+
/// assert_eq!(field.name(), "");
41+
/// assert_eq!(field.data_type(), &DataType::Utf8);
42+
/// assert!(field.is_nullable());
43+
/// ```
3044
fn into_nullable_field(self) -> Field;
3145

32-
/// Convert the type to field ref with nullable type and "" name
46+
/// Convert the type to [`FieldRef`] with nullable type and "" name
3347
///
3448
/// Concise wrapper around [`DataTypeExt::into_nullable_field`] that
3549
/// constructs a [`FieldRef`].
@@ -46,20 +60,74 @@ impl DataTypeExt for DataType {
4660
}
4761
}
4862

49-
/// DataFusion extension methods for Arrow [`Field`]
63+
/// DataFusion extension methods for Arrow [`Field`] and [`FieldRef`]
5064
pub trait FieldExt {
5165
/// Returns a new Field representing a List of this Field's DataType.
66+
///
67+
/// For example if input represents an `Int32`, the return value will
68+
/// represent a `List<Int32>`.
69+
///
70+
/// Example:
71+
/// ```
72+
/// # use std::sync::Arc;
73+
/// # use arrow::datatypes::{DataType, Field};
74+
/// # use datafusion_common::datatype::FieldExt;
75+
/// // Int32 field
76+
/// let int_field = Field::new("my_int", DataType::Int32, true);
77+
/// // convert to a List field
78+
/// let list_field = int_field.into_list();
79+
/// // List<Int32>
80+
/// // Note that the item field name has been renamed to "item"
81+
/// assert_eq!(list_field.data_type(), &DataType::List(Arc::new(
82+
/// Field::new("item", DataType::Int32, true)
83+
/// )));
84+
///
5285
fn into_list(self) -> Self;
5386

54-
/// Return a new Field representing this Field as the item type of a FixedSizeList
87+
/// Return a new Field representing this Field as the item type of a
88+
/// [`DataType::FixedSizeList`]
89+
///
90+
/// For example if input represents an `Int32`, the return value will
91+
/// represent a `FixedSizeList<Int32, size>`.
92+
///
93+
/// Example:
94+
/// ```
95+
/// # use std::sync::Arc;
96+
/// # use arrow::datatypes::{DataType, Field};
97+
/// # use datafusion_common::datatype::FieldExt;
98+
/// // Int32 field
99+
/// let int_field = Field::new("my_int", DataType::Int32, true);
100+
/// // convert to a FixedSizeList field of size 3
101+
/// let fixed_size_list_field = int_field.into_fixed_size_list(3);
102+
/// // FixedSizeList<Int32, 3>
103+
/// // Note that the item field name has been renamed to "item"
104+
/// assert_eq!(
105+
/// fixed_size_list_field.data_type(),
106+
/// &DataType::FixedSizeList(Arc::new(
107+
/// Field::new("item", DataType::Int32, true)),
108+
/// 3
109+
/// ));
110+
///
55111
fn into_fixed_size_list(self, list_size: i32) -> Self;
56112

57-
/// Create a field with the default list field name ("item")
113+
/// Update the field to have the default list field name ("item")
114+
///
115+
/// Lists are allowed to have an arbitrarily named field; however, a name
116+
/// other than 'item' will cause it to fail an == check against a more
117+
/// idiomatically created list in arrow-rs which causes issues.
118+
///
119+
/// For example, if input represents an `Int32` field named "my_int",
120+
/// the return value will represent an `Int32` field named "item".
58121
///
59-
/// Note that lists are allowed to have an arbitrarily named field;
60-
/// however, a name other than 'item' will cause it to fail an
61-
/// == check against a more idiomatically created list in
62-
/// arrow-rs which causes issues.
122+
/// Example:
123+
/// ```
124+
/// # use arrow::datatypes::Field;
125+
/// # use datafusion_common::datatype::FieldExt;
126+
/// let my_field = Field::new("my_int", arrow::datatypes::DataType::Int32, true);
127+
/// let item_field = my_field.into_list_item();
128+
/// assert_eq!(item_field.name(), Field::LIST_FIELD_DEFAULT_NAME);
129+
/// assert_eq!(item_field.name(), "item");
130+
/// ```
63131
fn into_list_item(self) -> Self;
64132
}
65133

0 commit comments

Comments
 (0)