Skip to content
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

Add standalone example of using the SQL frontend #11088

Merged
merged 11 commits into from
Jul 2, 2024

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Jun 23, 2024

Which issue does this PR close?

Part of #10855

Rationale for this change

The current rewrite_expr.rs example has three distinct examples:

  1. Using DataFusion LogicalPlanning without execution
  2. Implementing an AnalyzerRule
  3. Implementing an OptimizerRule

Each I think should be its own example to make them easier to find and more full featured

What changes are included in this PR?

  1. Add example sql_frontend.rs
  2. Improve documentation of TableSource trait

Are these changes tested?

Yes by CI

Are there any user-facing changes?

@github-actions github-actions bot added the logical-expr Logical plan and expressions label Jun 23, 2024
@alamb alamb marked this pull request as ready for review June 23, 2024 22:34
@alamb alamb added the documentation Improvements or additions to documentation label Jun 23, 2024
@alamb alamb requested a review from andygrove June 23, 2024 22:34
@alamb
Copy link
Contributor Author

alamb commented Jun 23, 2024

@andygrove or @jdye64 I wonder if you might have some time to review this PR as I think it is relevant to to your use of DataFusion

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 27, 2024
@alamb
Copy link
Contributor Author

alamb commented Jun 28, 2024

I am working on getting the CI to run successfully on this PR, see #11173

@alamb
Copy link
Contributor Author

alamb commented Jun 28, 2024

// The initial LogicalPlan is a mechanical translation from the parsed SQL
// and often can not run without the Analyzer passes.
//
// In this example, `person.age` is actually a different data type (Int32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the source schema data type defined as Uint8

// (Int64). However, this plan is not as efficient as it could be, as it
// will require casting *each row* of the input to UInt64 before comparison
// to 21 and 32. To optimize this query's performance, it is better to cast
// the constants once at plan time to Int32.
Copy link
Contributor

@mustafasrepo mustafasrepo Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be to Uint8

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch -- fixed in 6152fd6

Copy link
Contributor

@mustafasrepo mustafasrepo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @alamb for this PR.

let context_provider = MyContextProvider::default();
let sql_to_rel = SqlToRel::new(&context_provider);
let logical_plan = sql_to_rel.sql_statement_to_plan(statements[0].clone())?;
println!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can use assert! here to let the user know the expected output?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a good idea -- I will do so (the existing examples are inconsistent with that).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #11230

config.options(),
observe_analyzer,
)?;
println!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert?

// 4. Pushed the filter into the scan
let optimized_plan =
Optimizer::new().optimize(analyzed_plan, &config, observe_optimizer)?;
println!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert?

Copy link
Contributor

@comphead comphead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm thanks @alamb

@alamb alamb merged commit 3421b52 into apache:main Jul 2, 2024
23 checks passed
@alamb
Copy link
Contributor Author

alamb commented Jul 2, 2024

Thanks everyone for the comments and feedback

@alamb alamb deleted the alamb/sql_frontend branch July 2, 2024 20:53
comphead added a commit to comphead/arrow-datafusion that referenced this pull request Jul 8, 2024
* Add standalone example of using the SQL frontend

* Disable debug info for all example jobs

* Revert "Disable debug info for all example jobs"

This reverts commit f222a10.

* fix type

* Int32 --> Int8

* Use assert_eq rather than println

* Update datafusion-examples/examples/sql_frontend.rs

Co-authored-by: Oleks V <[email protected]>

---------

Co-authored-by: Oleks V <[email protected]>
findepi pushed a commit to findepi/datafusion that referenced this pull request Jul 16, 2024
* Add standalone example of using the SQL frontend

* Disable debug info for all example jobs

* Revert "Disable debug info for all example jobs"

This reverts commit f222a10.

* fix type

* Int32 --> Int8

* Use assert_eq rather than println

* Update datafusion-examples/examples/sql_frontend.rs

Co-authored-by: Oleks V <[email protected]>

---------

Co-authored-by: Oleks V <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logical-expr Logical plan and expressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants