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

Get expr planners when creating new planner #11485

Merged
merged 8 commits into from
Jul 17, 2024
Merged

Conversation

jayzhan211
Copy link
Contributor

Which issue does this PR close?

Closes #11477 .

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions core Core DataFusion crate labels Jul 16, 2024
@github-actions github-actions bot added the optimizer Optimizer rules label Jul 16, 2024
@jayzhan211 jayzhan211 marked this pull request as ready for review July 16, 2024 01:16
@jayzhan211 jayzhan211 marked this pull request as draft July 16, 2024 01:20
@jayzhan211 jayzhan211 marked this pull request as ready for review July 16, 2024 01:23
@jayzhan211
Copy link
Contributor Author

We may even remove planners in SqlToRel 🤔 ?

/// SQL query planner
pub struct SqlToRel<'a, S: ContextProvider> {
    pub(crate) context_provider: &'a S,
    pub(crate) options: ParserOptions,
    pub(crate) normalizer: IdentNormalizer,
    /// user defined planner extensions
    pub(crate) planners: Vec<Arc<dyn ExprPlanner>>,
}

Signed-off-by: jayzhan211 <[email protected]>
@@ -196,12 +193,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Self::new_with_options(context_provider, ParserOptions::default())
}

/// add an user defined planner
pub fn with_user_defined_planner(mut self, planner: Arc<dyn ExprPlanner>) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can register planner in session state

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes sense to use the underlying session state as the source of truth

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @jayzhan211 this makes sense to me.

I have a suggestion to avoid creating Vec's for all accesses to extension planners. Let me know what you think: jayzhan211#3

I also wonder if we should add a test case to avoid this kind of issue from happening again (whatever API Lance and Delta.rs are using clearly isn't covered by our existing tests)

cc @wjones127 and @rtyler (who also mentioned this #11180 (comment))

@@ -186,8 +185,6 @@ pub struct SqlToRel<'a, S: ContextProvider> {
pub(crate) context_provider: &'a S,
pub(crate) options: ParserOptions,
pub(crate) normalizer: IdentNormalizer,
/// user defined planner extensions
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -196,12 +193,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Self::new_with_options(context_provider, ParserOptions::default())
}

/// add an user defined planner
pub fn with_user_defined_planner(mut self, planner: Arc<dyn ExprPlanner>) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes sense to use the underlying session state as the source of truth

@@ -60,6 +60,9 @@ pub trait ContextProvider {
not_impl_err!("Recursive CTE is not implemented")
}

/// Getter for expr planners
fn get_expr_planners(&self) -> Vec<Arc<dyn ExprPlanner>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

I spent some time trying to figure out if we could avoid this Vec cloning all the time -- with

Suggested change
fn get_expr_planners(&self) -> Vec<Arc<dyn ExprPlanner>>;
fn get_expr_planners(&self) -> &[Arc<dyn ExprPlanner>] {&[]}

I think this is possible -- I made jayzhan211#3 to show how it looks

Copy link
Member

@wjones127 wjones127 left a comment

Choose a reason for hiding this comment

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

Thanks. This seems like a good simplification. I also agree that we could make get_expr_planners return a slice.

Use Slice rather than Vec to access expr planners
@github-actions github-actions bot removed the optimizer Optimizer rules label Jul 17, 2024
Signed-off-by: jayzhan211 <[email protected]>
struct SessionContextProvider<'a> {
state: &'a SessionState,
tables: HashMap<String, Arc<dyn TableSource>>,
}

impl<'a> ContextProvider for SessionContextProvider<'a> {
fn get_expr_planners(&self) -> &[Arc<dyn ExprPlanner>] {
&self.state.expr_planners
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remove expr_planners field in SessionContextProvider and get it from state directly

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! I tried to figure out how to do this previously and got confused about a lock or something. This is very nice 👌

Signed-off-by: jayzhan211 <[email protected]>
struct SessionContextProvider<'a> {
state: &'a SessionState,
tables: HashMap<String, Arc<dyn TableSource>>,
}

impl<'a> ContextProvider for SessionContextProvider<'a> {
fn get_expr_planners(&self) -> &[Arc<dyn ExprPlanner>] {
&self.state.expr_planners
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! I tried to figure out how to do this previously and got confused about a lock or something. This is very nice 👌

@alamb alamb merged commit de0765a into apache:main Jul 17, 2024
23 checks passed
@alamb
Copy link
Contributor

alamb commented Jul 17, 2024

Thanks @jayzhan211 and @wjones127

xinlifoobar pushed a commit to xinlifoobar/datafusion that referenced this pull request Jul 17, 2024
* get expr planners when creating new planner

Signed-off-by: jayzhan211 <[email protected]>

* get expr planner when creating planner

Signed-off-by: jayzhan211 <[email protected]>

* no planners in sqltorel

Signed-off-by: jayzhan211 <[email protected]>

* Add docs about SessionContextProvider

* Use Slice rather than Vec to access expr planners

* add test

Signed-off-by: jayzhan211 <[email protected]>

* clippy

Signed-off-by: jayzhan211 <[email protected]>

---------

Signed-off-by: jayzhan211 <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
xinlifoobar pushed a commit to xinlifoobar/datafusion that referenced this pull request Jul 18, 2024
* get expr planners when creating new planner

Signed-off-by: jayzhan211 <[email protected]>

* get expr planner when creating planner

Signed-off-by: jayzhan211 <[email protected]>

* no planners in sqltorel

Signed-off-by: jayzhan211 <[email protected]>

* Add docs about SessionContextProvider

* Use Slice rather than Vec to access expr planners

* add test

Signed-off-by: jayzhan211 <[email protected]>

* clippy

Signed-off-by: jayzhan211 <[email protected]>

---------

Signed-off-by: jayzhan211 <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
Lordworms pushed a commit to Lordworms/arrow-datafusion that referenced this pull request Jul 23, 2024
* get expr planners when creating new planner

Signed-off-by: jayzhan211 <[email protected]>

* get expr planner when creating planner

Signed-off-by: jayzhan211 <[email protected]>

* no planners in sqltorel

Signed-off-by: jayzhan211 <[email protected]>

* Add docs about SessionContextProvider

* Use Slice rather than Vec to access expr planners

* add test

Signed-off-by: jayzhan211 <[email protected]>

* clippy

Signed-off-by: jayzhan211 <[email protected]>

---------

Signed-off-by: jayzhan211 <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
wiedld pushed a commit to influxdata/arrow-datafusion that referenced this pull request Jul 31, 2024
* get expr planners when creating new planner

Signed-off-by: jayzhan211 <[email protected]>

* get expr planner when creating planner

Signed-off-by: jayzhan211 <[email protected]>

* no planners in sqltorel

Signed-off-by: jayzhan211 <[email protected]>

* Add docs about SessionContextProvider

* Use Slice rather than Vec to access expr planners

* add test

Signed-off-by: jayzhan211 <[email protected]>

* clippy

Signed-off-by: jayzhan211 <[email protected]>

---------

Signed-off-by: jayzhan211 <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate logical-expr Logical plan and expressions sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ExprPlanner not propagated to SqlToRel
3 participants