Repository navigation
Consolidate dataframe examples (#18142) #18862
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 all 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,95 @@ | ||
| // 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. | ||
|
|
||
| //! # These are core DataFrame API usage | ||
| //! | ||
| //! These examples demonstrate core DataFrame API usage. | ||
| //! | ||
| //! ## Usage | ||
| //! ```bash | ||
| //! cargo run --example dataframe -- [dataframe|deserialize_to_struct] | ||
| //! ``` | ||
| //! | ||
| //! Each subcommand runs a corresponding example: | ||
| //! - `dataframe` — run a query using a DataFrame API against parquet files, csv files, and in-memory data, including multiple subqueries | ||
| //! - `deserialize_to_struct` — convert query results (Arrow ArrayRefs) into Rust structs | ||
|
|
||
| mod dataframe; | ||
| mod deserialize_to_struct; | ||
|
|
||
| use std::str::FromStr; | ||
|
|
||
| use datafusion::error::{DataFusionError, Result}; | ||
|
|
||
| enum ExampleKind { | ||
| Dataframe, | ||
| DeserializeToStruct, | ||
| } | ||
|
|
||
| impl AsRef<str> for ExampleKind { | ||
| fn as_ref(&self) -> &str { | ||
| match self { | ||
| Self::Dataframe => "dataframe", | ||
| Self::DeserializeToStruct => "deserialize_to_struct", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for ExampleKind { | ||
| type Err = DataFusionError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self> { | ||
| match s { | ||
| "dataframe" => Ok(Self::Dataframe), | ||
| "deserialize_to_struct" => Ok(Self::DeserializeToStruct), | ||
| _ => Err(DataFusionError::Execution(format!("Unknown example: {s}"))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ExampleKind { | ||
| const ALL: [Self; 2] = [Self::Dataframe, Self::DeserializeToStruct]; | ||
|
|
||
| const EXAMPLE_NAME: &str = "dataframe"; | ||
|
|
||
| fn variants() -> Vec<&'static str> { | ||
| Self::ALL.iter().map(|x| x.as_ref()).collect() | ||
| } | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<()> { | ||
| let usage = format!( | ||
| "Usage: cargo run --example {} -- [{}]", | ||
| ExampleKind::EXAMPLE_NAME, | ||
| ExampleKind::variants().join("|") | ||
|
Contributor
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. BTW for some reason cargo run --example dataframedoesn't show me the valid options, it only tells me the argument is missing 🤔
Member
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. The options are listed here: '-- [dataframe|default_column_values|deserialize_to_struct]' Maybe another format should be used...
Contributor
Author
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. Thanks! I see what you mean - while the valid options are included in the usage line, they are not very visible, especially since they appear only after the “Missing argument” error. Usage:
cargo run --example dataframe <EXAMPLE>
Examples:
dataframe
default_column_values
deserialize_to_struct
Contributor
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. I think I just missed the options in my haste. Maybe we can improve the formatting for these errors as some follow on PR |
||
| ); | ||
|
|
||
| let arg = std::env::args().nth(1).ok_or_else(|| { | ||
| eprintln!("{usage}"); | ||
| DataFusionError::Execution("Missing argument".to_string()) | ||
| })?; | ||
|
|
||
| match arg.parse::<ExampleKind>()? { | ||
| ExampleKind::Dataframe => dataframe::dataframe_example().await?, | ||
| ExampleKind::DeserializeToStruct => { | ||
| deserialize_to_struct::deserialize_to_struct().await? | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
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 am not quite sure that this is a "dataframe" example -- it does use the DataFrame API but I think it is really illustrating how to provide a custom data source that fills in default column values
I suggest we move it to https://github.com/apache/datafusion/tree/main/datafusion-examples/examples/custom_data_source instead
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.
Agree with you, let's move the example