-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add relation planner extension support to customize SQL planning #17843
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 8 commits into
apache:main
from
geoffreyclaude:feat/custom_relation_planner
Dec 9, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c0b0d5
feat: add relation planner extensions
geoffreyclaude 13410f1
test: add relation planner extension tests
geoffreyclaude de50cb0
example: add relation planner extension examples
geoffreyclaude c0c4b6b
test: revamp relation planner extension tests for clarity
geoffreyclaude 20e2513
refactor: minor changes following PR review
geoffreyclaude 9efba37
refactor: improve relation planner examples for legibility
geoffreyclaude dcabe25
Merge branch 'main' into feat/custom_relation_planner
alamb 13a7907
fix ci
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // 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. | ||
|
|
||
| //! # Relation Planner Examples | ||
| //! | ||
| //! These examples demonstrate how to use custom relation planners to extend | ||
| //! DataFusion's SQL syntax with custom table operators. | ||
| //! | ||
| //! ## Usage | ||
| //! ```bash | ||
| //! cargo run --example relation_planner -- [match_recognize|pivot_unpivot|table_sample] | ||
| //! ``` | ||
| //! | ||
| //! Each subcommand runs a corresponding example: | ||
| //! - `match_recognize` — MATCH_RECOGNIZE pattern matching on event streams | ||
| //! - `pivot_unpivot` — PIVOT and UNPIVOT operations for reshaping data | ||
| //! - `table_sample` — TABLESAMPLE clause for sampling rows from tables | ||
| //! | ||
| //! ## Snapshot Testing | ||
| //! | ||
| //! These examples use [insta](https://insta.rs) for inline snapshot assertions. | ||
| //! If query output changes, regenerate the snapshots with: | ||
| //! ```bash | ||
| //! cargo insta test --example relation_planner --accept | ||
| //! ``` | ||
|
|
||
| mod match_recognize; | ||
| mod pivot_unpivot; | ||
| mod table_sample; | ||
|
|
||
| use std::str::FromStr; | ||
|
|
||
| use datafusion::error::{DataFusionError, Result}; | ||
|
|
||
| enum ExampleKind { | ||
| MatchRecognize, | ||
| PivotUnpivot, | ||
| TableSample, | ||
| } | ||
|
|
||
| impl AsRef<str> for ExampleKind { | ||
| fn as_ref(&self) -> &str { | ||
| match self { | ||
| Self::MatchRecognize => "match_recognize", | ||
| Self::PivotUnpivot => "pivot_unpivot", | ||
| Self::TableSample => "table_sample", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for ExampleKind { | ||
| type Err = DataFusionError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self> { | ||
| match s { | ||
| "match_recognize" => Ok(Self::MatchRecognize), | ||
| "pivot_unpivot" => Ok(Self::PivotUnpivot), | ||
| "table_sample" => Ok(Self::TableSample), | ||
| _ => Err(DataFusionError::Execution(format!("Unknown example: {s}"))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ExampleKind { | ||
| const ALL: [Self; 3] = [Self::MatchRecognize, Self::PivotUnpivot, Self::TableSample]; | ||
|
|
||
| const EXAMPLE_NAME: &str = "relation_planner"; | ||
|
|
||
| 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()) | ||
| })?; | ||
|
|
||
| if arg == "all" { | ||
| for example in ExampleKind::ALL { | ||
| match example { | ||
| ExampleKind::MatchRecognize => match_recognize::match_recognize().await?, | ||
| ExampleKind::PivotUnpivot => pivot_unpivot::pivot_unpivot().await?, | ||
| ExampleKind::TableSample => table_sample::table_sample().await?, | ||
| } | ||
| } | ||
| } else { | ||
| match arg.parse::<ExampleKind>()? { | ||
| ExampleKind::MatchRecognize => match_recognize::match_recognize().await?, | ||
| ExampleKind::PivotUnpivot => pivot_unpivot::pivot_unpivot().await?, | ||
| ExampleKind::TableSample => table_sample::table_sample().await?, | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Test wrappers that enable `cargo insta test --example relation_planner --accept` | ||
| /// to regenerate inline snapshots. Without these, insta cannot run the examples | ||
| /// in test mode since they only have `main()` functions. | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_match_recognize() { | ||
| match_recognize::match_recognize().await.unwrap(); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_pivot_unpivot() { | ||
| pivot_unpivot::pivot_unpivot().await.unwrap(); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_table_sample() { | ||
| table_sample::table_sample().await.unwrap(); | ||
| } | ||
| } |
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.
It might help here to highlight what a custom relation planner means -- something like