cj-zhukov commented on code in PR #18862: URL: https://github.com/apache/datafusion/pull/18862#discussion_r2552292873
########## datafusion-examples/examples/dataframe/main.rs: ########## @@ -0,0 +1,107 @@ +// 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|default_column_values|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 +//! - `default_column_values` — implement custom default value handling for missing columns using field metadata and PhysicalExprAdapter +//! - `deserialize_to_struct` — convert query results (Arrow ArrayRefs) into Rust structs + +mod dataframe; +mod default_column_values; +mod deserialize_to_struct; + +use std::str::FromStr; + +use datafusion::error::{DataFusionError, Result}; + +enum ExampleKind { + Dataframe, + DefaultColumnValues, + DeserializeToStruct, +} + +impl AsRef<str> for ExampleKind { + fn as_ref(&self) -> &str { + match self { + Self::Dataframe => "dataframe", + Self::DefaultColumnValues => "default_column_values", + 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), + "default_column_values" => Ok(Self::DefaultColumnValues), + "deserialize_to_struct" => Ok(Self::DeserializeToStruct), + _ => Err(DataFusionError::Execution(format!("Unknown example: {s}"))), + } + } +} + +impl ExampleKind { + const ALL: [Self; 3] = [ + Self::Dataframe, + Self::DefaultColumnValues, + 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("|") Review Comment: 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. I agree the formatting could be improved to make the valid options clearer. We could print them separately (as a list) or improve the usage string so it’s easier to read. I'm happy to adjust the formatting if you have a preferred style, or I can propose one: ```bash Usage: cargo run --example dataframe <EXAMPLE> Examples: dataframe default_column_values deserialize_to_struct ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
