Apache DataFusion SQL Query Engine
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
//! # These are core DataFrame API usage
//!
//! These examples demonstrate core DataFrame API usage.
//!
//! ## Usage
//! ```bash
//! cargo run --example dataframe -- [all|aggregate|dataframe|deserialize_to_struct|cache_factory]
//! ```
//!
//! Each subcommand runs a corresponding example:
//! - `all` — run all examples included in this module
//!
//! - `aggregate`
//! (file: aggregate.rs, desc: Global and grouped aggregation with `select` and `aggregate`)
//!
//! - `cache_factory`
//! (file: cache_factory.rs, desc: Custom lazy caching for DataFrames using `CacheFactory`)
//
//! - `dataframe`
//! (file: dataframe.rs, desc: Query DataFrames from various sources and write output)
//!
//! - `deserialize_to_struct`
//! (file: deserialize_to_struct.rs, desc: Convert Arrow arrays into Rust structs)
mod aggregate;
mod cache_factory;
mod dataframe;
mod deserialize_to_struct;
use datafusion::error::{DataFusionError, Result};
use strum::{IntoEnumIterator, VariantNames};
use strum_macros::{Display, EnumIter, EnumString, VariantNames};
#[derive(EnumIter, EnumString, Display, VariantNames)]
#[strum(serialize_all = "snake_case")]
enum ExampleKind {
All,
Aggregate,
Dataframe,
DeserializeToStruct,
CacheFactory,
}
impl ExampleKind {
const EXAMPLE_NAME: &str = "dataframe";
fn runnable() -> impl Iterator<Item = ExampleKind> {
ExampleKind::iter().filter(|v| !matches!(v, ExampleKind::All))
}
async fn run(&self) -> Result<()> {
match self {
ExampleKind::All => {
for example in ExampleKind::runnable() {
println!("Running example: {example}");
Box::pin(example.run()).await?;
}
}
ExampleKind::Aggregate => {
aggregate::aggregate_example().await?;
}
ExampleKind::Dataframe => {
dataframe::dataframe_example().await?;
}
ExampleKind::DeserializeToStruct => {
deserialize_to_struct::deserialize_to_struct().await?;
}
ExampleKind::CacheFactory => {
cache_factory::cache_dataframe_with_custom_logic().await?;
}
}
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<()> {
let usage = format!(
"Usage: cargo run --example {} -- [{}]",
ExampleKind::EXAMPLE_NAME,
ExampleKind::VARIANTS.join("|")
);
let example: ExampleKind = std::env::args()
.nth(1)
.unwrap_or_else(|| ExampleKind::All.to_string())
.parse()
.map_err(|_| DataFusionError::Execution(format!("Unknown example. {usage}")))?;
example.run().await
}