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

Convert LogicalPlanBuilder to use Arc<LogicalPlan> #12040

Merged
merged 6 commits into from
Aug 22, 2024
Merged

Conversation

jc4x4
Copy link
Contributor

@jc4x4 jc4x4 commented Aug 16, 2024

Which issue does this PR close?

Closes #10485.

Rationale for this change

Refactor to avoid unnecessary copies of LogicalPlan.

What changes are included in this PR?

Update struct to use Arc. Verified test passes.

Used Arc::clone as much as I can; and unwrap_arc when a owned LogicalPlan is required.

Keep pub fn input unchanged as LogicalPlan to limit change scope. If we change the pub fn, we can also get rid of this pattern like below. If this broader change is wanted, I can follow up with a separate PR.

unnest(unwrap_arc(self.plan), ...).map(Self::from)

Are these changes tested?

Run unit tests inside builder.rs

Are there any user-facing changes?

No. Pub fn is explicitly not affected.

Summary
Update struct to use Arc. Verified test passes.

Used Arc::clone as much as I can; and unwrap_arc when a owned
LogicalPlan is required.

Keep pub fn input unchanged as LogicalPlan to limit change scope. If we
change the pub fn, we can also get rid of this pattern:

```
unnest(unwrap_arc(self.plan), ...).map(Self::from)
```
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.

Thank you @jc4x4 🙏 This looks like a good idea to me

I started the CI tests for this PR and left a few comments

datafusion/expr/src/logical_plan/builder.rs Outdated Show resolved Hide resolved
datafusion/expr/src/logical_plan/builder.rs Outdated Show resolved Hide resolved
@@ -145,7 +150,7 @@ impl LogicalPlanBuilder {
coerce_plan_expr_for_schema(&recursive_term, self.plan.schema())?;
Ok(Self::from(LogicalPlan::RecursiveQuery(RecursiveQuery {
name,
static_term: Arc::new(self.plan.clone()),
static_term: Arc::clone(&self.plan),
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems the only place a deep clone is "saved"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another example in line 470.

                project((*input).clone(), expr)
                project(unwrap_arc(input), expr)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes, here it might avoid the clone as well 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW I think we can avoid the clone by changing the signature from

    pub fn to_recursive_query(
        &self,

to

    pub fn to_recursive_query(
        self,

(the builder should be consumed)

Copy link
Contributor

Choose a reason for hiding this comment

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

Another example in line 470.

I think since this is an associated function it could also be done indpendently to the change of the internal implementation of LogicalaPlanBuilder

@jc4x4
Copy link
Contributor Author

jc4x4 commented Aug 20, 2024

Looked at the conflict. It can be resolved by changing Self::from to Self::new.

@alamb
Copy link
Contributor

alamb commented Aug 20, 2024

I took the liberty of merging this PR up from main to resolve the conflict

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.

Thank you @Dandandan and @jc4x4

I would be happy to merge this PR but I also think we could remove the clones in other ways that are smaller

However, in general, I think this PR improves the code / experience a bit so I think merging it is a good idea.

@@ -145,7 +150,7 @@ impl LogicalPlanBuilder {
coerce_plan_expr_for_schema(&recursive_term, self.plan.schema())?;
Ok(Self::from(LogicalPlan::RecursiveQuery(RecursiveQuery {
name,
static_term: Arc::new(self.plan.clone()),
static_term: Arc::clone(&self.plan),
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW I think we can avoid the clone by changing the signature from

    pub fn to_recursive_query(
        &self,

to

    pub fn to_recursive_query(
        self,

(the builder should be consumed)

@@ -145,7 +150,7 @@ impl LogicalPlanBuilder {
coerce_plan_expr_for_schema(&recursive_term, self.plan.schema())?;
Ok(Self::from(LogicalPlan::RecursiveQuery(RecursiveQuery {
name,
static_term: Arc::new(self.plan.clone()),
static_term: Arc::clone(&self.plan),
Copy link
Contributor

Choose a reason for hiding this comment

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

Another example in line 470.

I think since this is an associated function it could also be done indpendently to the change of the internal implementation of LogicalaPlanBuilder

@jc4x4
Copy link
Contributor Author

jc4x4 commented Aug 20, 2024

Thank you @Dandandan and @jc4x4

I would be happy to merge this PR but I also think we could remove the clones in other ways that are smaller

However, in general, I think this PR improves the code / experience a bit so I think merging it is a good idea.

Good catch. I've updated.

@jc4x4 jc4x4 requested a review from Dandandan August 22, 2024 00:54
@alamb alamb merged commit 3c281f6 into apache:main Aug 22, 2024
24 checks passed
@alamb
Copy link
Contributor

alamb commented Aug 22, 2024

Thanks again @jc4x4

@alamb alamb mentioned this pull request Aug 22, 2024
@jc4x4 jc4x4 deleted the arc-logical branch August 23, 2024 09:43
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.

Convert internal representation of LogicalPlanBuilder from LogicalPlan to Arc<LogicalPlan>
3 participants