-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Consolidate sql examples #11173
Closed
Closed
Consolidate sql examples #11173
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c661218
Consolidate SQL examples
alamb f31f0a4
consolidate parquet_sql_multiple_files
alamb d13bfe9
Consolidate regexp
alamb 70b1ce4
Consolidate to_char
alamb a3b9d36
conolidate to_date and to_timestamp
alamb 6ada9f7
Consolidate make_date
alamb cbd16c2
consolidate avro demo
alamb 26cef02
Consolidate another, update readme
alamb 89f6570
Merge remote-tracking branch 'apache/main' into alamb/consolidate_exa…
alamb e1c5685
fix path
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,17 +15,37 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//! This file contains several examples of how to run queries using DataFusion's | ||
//! DataFrame API: | ||
//! | ||
//! * [`parquet`]: query a single Parquet file | ||
//! * [`to_date_demo`]: use the `to_date` function to convert dates to strings | ||
//! * [`to_timestamp_demo`]: use the `to_timestamp` function to convert strings to timestamps | ||
//! * [`make_date_demo`]: use the `make_date` function to create dates from year, month, and day | ||
|
||
use arrow::array::{Int32Array, RecordBatch, StringArray}; | ||
use datafusion::arrow::datatypes::{DataType, Field, Schema}; | ||
use datafusion::error::Result; | ||
use datafusion::prelude::*; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::sync::Arc; | ||
use tempfile::tempdir; | ||
|
||
/// This example demonstrates executing a simple query against an Arrow data source (Parquet) and | ||
/// fetching results, using the DataFrame trait | ||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
parquet().await?; | ||
to_date_demo().await?; | ||
to_timestamp_demo().await?; | ||
make_date_demo().await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// This example demonstrates executing a simple query against an Arrow data | ||
/// source (Parquet) and fetching results, using the DataFrame trait | ||
|
||
async fn parquet() -> Result<()> { | ||
// create local execution context | ||
let ctx = SessionContext::new(); | ||
|
||
|
@@ -109,3 +129,135 @@ async fn example_read_csv_file_with_schema(file_path: &str) -> DataFrame { | |
// Register a lazy DataFrame by using the context and option provider | ||
ctx.read_csv(file_path, csv_read_option).await.unwrap() | ||
} | ||
|
||
/// This example demonstrates how to use the to_date series | ||
/// of functions in the DataFrame API | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of the consolidated examples had both SQL and dataframe examples, so I split such examples into dataframe.rs and sql.rs |
||
async fn to_date_demo() -> Result<()> { | ||
// define a schema. | ||
let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Utf8, false)])); | ||
|
||
// define data. | ||
let batch = RecordBatch::try_new( | ||
schema, | ||
vec![Arc::new(StringArray::from(vec![ | ||
"2020-09-08T13:42:29Z", | ||
"2020-09-08T13:42:29.190855-05:00", | ||
"2020-08-09 12:13:29", | ||
"2020-01-02", | ||
]))], | ||
)?; | ||
|
||
// declare a new context. In spark API, this corresponds to a new spark SQLsession | ||
let ctx = SessionContext::new(); | ||
|
||
// declare a table in memory. In spark API, this corresponds to createDataFrame(...). | ||
ctx.register_batch("t", batch)?; | ||
let df = ctx.table("t").await?; | ||
|
||
// use to_date function to convert col 'a' to timestamp type using the default parsing | ||
let df = df.with_column("a", to_date(vec![col("a")]))?; | ||
|
||
let df = df.select_columns(&["a"])?; | ||
|
||
// print the results | ||
df.show().await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// This example demonstrates how to use the to_timestamp series | ||
/// of functions in the DataFrame API | ||
async fn to_timestamp_demo() -> Result<()> { | ||
// define a schema. | ||
let schema = Arc::new(Schema::new(vec![ | ||
Field::new("a", DataType::Utf8, false), | ||
Field::new("b", DataType::Utf8, false), | ||
])); | ||
|
||
// define data. | ||
let batch = RecordBatch::try_new( | ||
schema, | ||
vec![ | ||
Arc::new(StringArray::from(vec![ | ||
"2020-09-08T13:42:29Z", | ||
"2020-09-08T13:42:29.190855-05:00", | ||
"2020-08-09 12:13:29", | ||
"2020-01-02", | ||
])), | ||
Arc::new(StringArray::from(vec![ | ||
"2020-09-08T13:42:29Z", | ||
"2020-09-08T13:42:29.190855-05:00", | ||
"08-09-2020 13/42/29", | ||
"09-27-2020 13:42:29-05:30", | ||
])), | ||
], | ||
)?; | ||
|
||
// declare a new context. In spark API, this corresponds to a new spark SQLsession | ||
let ctx = SessionContext::new(); | ||
|
||
// declare a table in memory. In spark API, this corresponds to createDataFrame(...). | ||
ctx.register_batch("t", batch)?; | ||
let df = ctx.table("t").await?; | ||
|
||
// use to_timestamp function to convert col 'a' to timestamp type using the default parsing | ||
let df = df.with_column("a", to_timestamp(vec![col("a")]))?; | ||
// use to_timestamp_seconds function to convert col 'b' to timestamp(Seconds) type using a list | ||
// of chrono formats (https://docs.rs/chrono/latest/chrono/format/strftime/index.html) to try | ||
let df = df.with_column( | ||
"b", | ||
to_timestamp_seconds(vec![ | ||
col("b"), | ||
lit("%+"), | ||
lit("%d-%m-%Y %H/%M/%S"), | ||
lit("%m-%d-%Y %H:%M:%S%#z"), | ||
]), | ||
)?; | ||
|
||
let df = df.select_columns(&["a", "b"])?; | ||
|
||
// print the results | ||
df.show().await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// This example demonstrates how to use the make_date | ||
/// function in the DataFrame API as well as via sql. | ||
async fn make_date_demo() -> Result<()> { | ||
// define a schema. | ||
let schema = Arc::new(Schema::new(vec![ | ||
Field::new("y", DataType::Int32, false), | ||
Field::new("m", DataType::Int32, false), | ||
Field::new("d", DataType::Int32, false), | ||
])); | ||
|
||
// define data. | ||
let batch = RecordBatch::try_new( | ||
schema, | ||
vec![ | ||
Arc::new(Int32Array::from(vec![2020, 2021, 2022, 2023, 2024])), | ||
Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5])), | ||
Arc::new(Int32Array::from(vec![15, 16, 17, 18, 19])), | ||
], | ||
)?; | ||
|
||
// declare a new context. In spark API, this corresponds to a new spark SQLsession | ||
let ctx = SessionContext::new(); | ||
|
||
// declare a table in memory. In spark API, this corresponds to createDataFrame(...). | ||
ctx.register_batch("t", batch)?; | ||
let df = ctx.table("t").await?; | ||
|
||
// use make_date function to convert col 'y', 'm' & 'd' to a date | ||
let df = df.with_column("a", make_date(col("y"), col("m"), col("d")))?; | ||
// use make_date function to convert col 'y' & 'm' with a static day to a date | ||
let df = df.with_column("b", make_date(col("y"), col("m"), lit(22)))?; | ||
|
||
let df = df.select_columns(&["a", "b"])?; | ||
|
||
// print the results | ||
df.show().await?; | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think putting the related functionality together in one example will make it easier for people to find what they are looking for (especially as the number of examples in DataFusion continues to grow)