Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions datafusion/core/src/dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2614,7 +2614,7 @@ impl DataFrame {
/// # async fn main() -> Result<()> {
/// let id: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3]));
/// let name: ArrayRef = Arc::new(StringArray::from(vec!["foo", "bar", "baz"]));
/// let df = DataFrame::from_columns(vec![("id", id), ("name", name)])?;
/// let df = DataFrame::from_columns([("id", id), ("name", name)])?;
/// let expected = vec![
/// "+----+------+",
/// "| id | name |",
Expand All @@ -2628,17 +2628,20 @@ impl DataFrame {
/// # Ok(())
/// # }
/// ```
pub fn from_columns(columns: Vec<(&str, ArrayRef)>) -> Result<Self> {
let fields = columns
.iter()
.map(|(name, array)| Field::new(*name, array.data_type().clone(), true))
.collect::<Vec<_>>();

let arrays = columns
pub fn from_columns<I, S>(columns: I) -> Result<Self>
where
I: IntoIterator<Item = (S, ArrayRef)>,
S: AsRef<str>,
{
let (fields, arrays): (Vec<_>, Vec<_>) = columns
.into_iter()
.map(|(_, array)| array)
.collect::<Vec<_>>();

.map(|(name, array)| {
(
Field::new(name.as_ref(), array.data_type().clone(), true),
array,
)
})
.unzip();
let schema = Arc::new(Schema::new(fields));
let batch = RecordBatch::try_new(schema, arrays)?;
let ctx = SessionContext::new();
Expand Down Expand Up @@ -2695,7 +2698,7 @@ macro_rules! dataframe {
use datafusion::prelude::DataFrame;
use datafusion::common::test_util::IntoArrayRef;

let columns = vec![
let columns = [
$(
($name, $data.into_array_ref()),
)+
Expand Down
47 changes: 26 additions & 21 deletions datafusion/core/tests/dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6968,7 +6968,7 @@ async fn test_dataframe_from_columns() -> Result<()> {
let strings: ArrayRef =
Arc::new(StringArray::from(vec![Some("foo"), Some("bar"), None]));

let df = DataFrame::from_columns(vec![
let columns = [
("bool", bools),
("i8", i8s),
("i16", i16s),
Expand All @@ -6982,10 +6982,10 @@ async fn test_dataframe_from_columns() -> Result<()> {
("f32", f32s),
("f64", f64s),
("str", strings),
])?;
];

assert_eq!(df.schema().fields().len(), 13);
assert_eq!(df.clone().count().await?, 3);
let df1 = DataFrame::from_columns(columns.clone())?;
let df2 = DataFrame::from_columns(columns.to_vec())?;

let expected_types = [
("bool", DataType::Boolean),
Expand All @@ -7003,26 +7003,31 @@ async fn test_dataframe_from_columns() -> Result<()> {
("str", DataType::Utf8),
];

let schema = df.schema();
for df in [df1, df2] {
assert_eq!(df.schema().fields().len(), expected_types.len());
assert_eq!(df.clone().count().await?, 3);

for (name, data_type) in expected_types {
assert_eq!(schema.field_with_name(None, name)?.data_type(), &data_type);
}
let schema = df.schema();

let rows = df.sort(vec![col("i32").sort(true, true)])?;
for (name, data_type) in &expected_types {
assert_eq!(schema.field_with_name(None, name)?.data_type(), data_type);
}

assert_batches_eq!(
&[
"+-------+----+-----+-----+-----+----+-----+-----+-----+-----+-----+-----+-----+",
"| bool | i8 | i16 | i32 | i64 | u8 | u16 | u32 | u64 | f16 | f32 | f64 | str |",
"+-------+----+-----+-----+-----+----+-----+-----+-----+-----+-----+-----+-----+",
"| true | -1 | -1 | -1 | -1 | 0 | 0 | 0 | 0 | 1 | 1.0 | 1.0 | foo |",
"| false | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 2 | 2.0 | 2.0 | bar |",
"| true | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 | 3 | 3.0 | 3.0 | |",
"+-------+----+-----+-----+-----+----+-----+-----+-----+-----+-----+-----+-----+",
],
&rows.collect().await?
);
let rows = df.sort(vec![col("i32").sort(true, true)])?;

assert_batches_eq!(
&[
"+-------+----+-----+-----+-----+----+-----+-----+-----+-----+-----+-----+-----+",
"| bool | i8 | i16 | i32 | i64 | u8 | u16 | u32 | u64 | f16 | f32 | f64 | str |",
"+-------+----+-----+-----+-----+----+-----+-----+-----+-----+-----+-----+-----+",
"| true | -1 | -1 | -1 | -1 | 0 | 0 | 0 | 0 | 1 | 1.0 | 1.0 | foo |",
"| false | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 2 | 2.0 | 2.0 | bar |",
"| true | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 | 3 | 3.0 | 3.0 | |",
"+-------+----+-----+-----+-----+----+-----+-----+-----+-----+-----+-----+-----+",
],
&rows.collect().await?
);
}

Ok(())
}
Expand Down