-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add macro for creating DataFrame (#16090) #16104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use arrow::array::{ArrayRef, BooleanArray, Float32Array, Int32Array, StringArray}; | ||
|
|
||
| /// Converts a vector or array into an ArrayRef. | ||
| pub trait IntoArrayRef { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef; | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<i32> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(Int32Array::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<Option<i32>> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(Int32Array::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [i32; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(Int32Array::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [Option<i32>; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(Int32Array::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<f32> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(Float32Array::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<Option<f32>> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(Float32Array::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [f32; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(Float32Array::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [Option<f32>; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(Float32Array::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<&str> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(StringArray::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<Option<&str>> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(StringArray::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [&'static str; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(StringArray::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [Option<&'static str>; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(StringArray::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<String> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(StringArray::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<Option<String>> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(StringArray::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [String; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(StringArray::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [Option<String>; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(StringArray::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<bool> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(BooleanArray::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl IntoArrayRef for Vec<Option<bool>> { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(BooleanArray::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [bool; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(BooleanArray::from(Vec::from(*self))) | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> IntoArrayRef for [Option<bool>; N] { | ||
| fn into_array_ref(self: Box<Self>) -> ArrayRef { | ||
| Arc::new(BooleanArray::from(Vec::from(*self))) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| /// Macro for creating DataFrame. | ||
| /// # Example | ||
| /// ``` | ||
| /// use datafusion::prelude::df; | ||
| /// # use datafusion::error::Result; | ||
| /// # #[tokio::main] | ||
| /// # async fn main() -> Result<()> { | ||
| /// let df = df!( | ||
| /// "id" => [1, 2, 3], | ||
| /// "name" => ["foo", "bar", "baz"] | ||
| /// )?; | ||
| /// df.show().await?; | ||
| /// // +----+------+, | ||
| /// // | id | name |, | ||
| /// // +----+------+, | ||
| /// // | 1 | foo |, | ||
| /// // | 2 | bar |, | ||
| /// // | 3 | baz |, | ||
| /// // +----+------+, | ||
| /// let df_empty = df!()?; // empty DataFrame | ||
| /// assert_eq!(df_empty.schema().fields().len(), 0); | ||
| /// assert_eq!(df_empty.count().await?, 0); | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| #[macro_export] | ||
| macro_rules! df { | ||
|
||
| () => {{ | ||
| use std::sync::Arc; | ||
|
|
||
| use datafusion::prelude::SessionContext; | ||
| use datafusion::arrow::array::RecordBatch; | ||
| use datafusion::arrow::datatypes::Schema; | ||
|
|
||
| let ctx = SessionContext::new(); | ||
| let batch = RecordBatch::new_empty(Arc::new(Schema::empty())); | ||
| ctx.read_batch(batch) | ||
| }}; | ||
|
|
||
| ($($col_name:expr => $data:expr),+ $(,)?) => {{ | ||
| use datafusion::prelude::DataFrame; | ||
| use datafusion::common::array_conversion::IntoArrayRef; | ||
|
|
||
| let columns = vec![ | ||
| $( ($col_name, Box::new($data) as Box<dyn IntoArrayRef>) ),+ | ||
| ]; | ||
| DataFrame::from_columns(columns) | ||
| }}; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm feeling this file somewhat repeats what was done in #12846 ? can we reuse it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point! I'm going to try to reuse it.