Repository navigation
Consolidate custom data source examples (#18142) #18553
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
Merged
alamb
merged 3 commits into
apache:main
from
cj-zhukov:cj-zhukov/consolidate-examples-custom-data-source
Nov 9, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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
126 changes: 126 additions & 0 deletions
126
datafusion-examples/examples/custom_data_source/main.rs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // 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 examples are all related to extending or defining how DataFusion reads data | ||
| //! | ||
| //! These examples demonstrate how DataFusion reads data. | ||
| //! | ||
| //! ## Usage | ||
| //! ```bash | ||
| //! cargo run --example custom_data_source -- [csv_json_opener|csv_sql_streaming|custom_datasource|custom_file_casts|custom_file_format|file_stream_provider] | ||
| //! ``` | ||
| //! | ||
| //! Each subcommand runs a corresponding example: | ||
| //! - `csv_json_opener` — use low level FileOpener APIs to read CSV/JSON into Arrow RecordBatches | ||
| //! - `csv_sql_streaming` — build and run a streaming query plan from a SQL statement against a local CSV file | ||
| //! - `custom_datasource` — run queries against a custom datasource (TableProvider) | ||
| //! - `custom_file_casts` — implement custom casting rules to adapt file schemas | ||
| //! - `custom_file_format` — write data to a custom file format | ||
| //! - `file_stream_provider` — run a query on FileStreamProvider which implements StreamProvider for reading and writing to arbitrary stream sources/sinks | ||
|
|
||
| mod csv_json_opener; | ||
| mod csv_sql_streaming; | ||
| mod custom_datasource; | ||
| mod custom_file_casts; | ||
| mod custom_file_format; | ||
| mod file_stream_provider; | ||
|
|
||
| use std::str::FromStr; | ||
|
|
||
| use datafusion::error::{DataFusionError, Result}; | ||
|
|
||
| enum ExampleKind { | ||
| CsvJsonOpener, | ||
| CsvSqlStreaming, | ||
| CustomDatasource, | ||
| CustomFileCasts, | ||
| CustomFileFormat, | ||
| FileFtreamProvider, | ||
| } | ||
|
|
||
| impl AsRef<str> for ExampleKind { | ||
| fn as_ref(&self) -> &str { | ||
| match self { | ||
| Self::CsvJsonOpener => "csv_json_opener", | ||
| Self::CsvSqlStreaming => "csv_sql_streaming", | ||
| Self::CustomDatasource => "custom_datasource", | ||
| Self::CustomFileCasts => "custom_file_casts", | ||
| Self::CustomFileFormat => "custom_file_format", | ||
| Self::FileFtreamProvider => "file_stream_provider", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for ExampleKind { | ||
| type Err = DataFusionError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self> { | ||
| match s { | ||
| "csv_json_opener" => Ok(Self::CsvJsonOpener), | ||
| "csv_sql_streaming" => Ok(Self::CsvSqlStreaming), | ||
| "custom_datasource" => Ok(Self::CustomDatasource), | ||
| "custom_file_casts" => Ok(Self::CustomFileCasts), | ||
| "custom_file_format" => Ok(Self::CustomFileFormat), | ||
| "file_stream_provider" => Ok(Self::FileFtreamProvider), | ||
| _ => Err(DataFusionError::Execution(format!("Unknown example: {s}"))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ExampleKind { | ||
| const ALL: [Self; 6] = [ | ||
| Self::CsvJsonOpener, | ||
| Self::CsvSqlStreaming, | ||
| Self::CustomDatasource, | ||
| Self::CustomFileCasts, | ||
| Self::CustomFileFormat, | ||
| Self::FileFtreamProvider, | ||
| ]; | ||
|
|
||
| const EXAMPLE_NAME: &str = "custom_data_source"; | ||
|
|
||
| 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("|") | ||
| ); | ||
|
|
||
| let arg = std::env::args().nth(1).ok_or_else(|| { | ||
| eprintln!("{usage}"); | ||
| DataFusionError::Execution("Missing argument".to_string()) | ||
| })?; | ||
|
|
||
| match arg.parse::<ExampleKind>()? { | ||
| ExampleKind::CsvJsonOpener => csv_json_opener::csv_json_opener().await?, | ||
| ExampleKind::CsvSqlStreaming => csv_sql_streaming::csv_sql_streaming().await?, | ||
| ExampleKind::CustomDatasource => custom_datasource::custom_datasource().await?, | ||
| ExampleKind::CustomFileCasts => custom_file_casts::custom_file_casts().await?, | ||
| ExampleKind::CustomFileFormat => custom_file_format::custom_file_format().await?, | ||
| ExampleKind::FileFtreamProvider => { | ||
| file_stream_provider::file_stream_provider().await? | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or 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 contains hidden or 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
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.
Thanks @cj-zhukov
it would be awesome to show the example command line to run specific example or all examples using cargo on the example overview
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 add this.