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

omit some clone when converting sql to logical plan #1945

Merged
merged 5 commits into from
Mar 10, 2022
Merged

omit some clone when converting sql to logical plan #1945

merged 5 commits into from
Mar 10, 2022

Conversation

doki23
Copy link
Contributor

@doki23 doki23 commented Mar 8, 2022

Which issue does this PR close?

Closes #1469.

What changes are included in this PR?

It's an api change.
Take ownership of sql-parser components to emit some clones.
It's hard to me - a new comer of rust and df, and the mutual calls between functions are very complex, so please bear with me :)

@github-actions github-actions bot added datafusion Changes in the datafusion crate sql SQL Planner labels Mar 8, 2022
Copy link
Member

@xudong963 xudong963 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 @doki23

location: location.clone(),
file_type: *file_type,
has_header: *has_header,
name,
Copy link
Member

Choose a reason for hiding this comment

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

👍,clone disappeared

@houqp houqp added the api change Changes the API exposed to users of the crate label Mar 8, 2022
@@ -311,7 +311,7 @@ impl ExecutionContext {
// create a query planner
let state = self.state.lock().clone();
let query_planner = SqlToRel::new(&state);
query_planner.statement_to_plan(&statements[0])
query_planner.statement_to_plan(statements.remove(0))
Copy link
Member

Choose a reason for hiding this comment

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

i wonder what's the motivation behind .remove?

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 is to take ownership of the 1 item from statements. statements.len() == 1 is verified on line 305 above

I think statements.pop().unwrap() is equivalent and might be clearer what is going on, though I think remove(0) is also fine

@jimexist
Copy link
Member

jimexist commented Mar 8, 2022

if we are using so many remove(0) i wonder if we shall switch to VecDeque

Copy link
Member

@jimexist jimexist left a comment

Choose a reason for hiding this comment

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

i can approve to unblock merging but i do think the remove(0) makes the logic quite cryptic. might need a follow up PR to clean up.

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.

I think it looks great -- thank you @doki23

After this PR there is definitely some more cleanup possible such as

    fn sql_expr_to_logical_expr(&self, sql: &SQLExpr, schema: &DFSchema) -> Result<Expr> {

But I can file a follow on ticket for that.

@@ -311,7 +311,7 @@ impl ExecutionContext {
// create a query planner
let state = self.state.lock().clone();
let query_planner = SqlToRel::new(&state);
query_planner.statement_to_plan(&statements[0])
query_planner.statement_to_plan(statements.remove(0))
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 is to take ownership of the 1 item from statements. statements.len() == 1 is verified on line 305 above

I think statements.pop().unwrap() is equivalent and might be clearer what is going on, though I think remove(0) is also fine

let rewrite = DFParser::parse_sql(&query)?;
self.statement_to_plan(&rewrite[0])
let mut rewrite = DFParser::parse_sql(&query)?;
self.statement_to_plan(rewrite.remove(0))
Copy link
Contributor

Choose a reason for hiding this comment

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

I realize this was not added in this PR, but I wonder if we could add an assert_eq!(rewrite.len(), 1) here to make sure we don't accidentally lose one of these statements later on

@alamb alamb changed the title emit some clone when converting sql to logical plan omit some clone when converting sql to logical plan Mar 8, 2022
@doki23
Copy link
Contributor Author

doki23 commented Mar 9, 2022

i can approve to unblock merging but i do think the remove(0) makes the logic quite cryptic. might need a follow up PR to clean up.

+1

@doki23 doki23 closed this Mar 9, 2022
@doki23 doki23 reopened this Mar 9, 2022
@doki23
Copy link
Contributor Author

doki23 commented Mar 9, 2022

I accidentally closed this pr, hope it didn't cause confusion😂

@doki23
Copy link
Contributor Author

doki23 commented Mar 9, 2022

I applied the suggestions, please review again @jimexist @alamb

@@ -311,7 +311,7 @@ impl ExecutionContext {
// create a query planner
let state = self.state.lock().clone();
let query_planner = SqlToRel::new(&state);
query_planner.statement_to_plan(&statements[0])
query_planner.statement_to_plan(statements.pop().unwrap())
Copy link
Member

@houqp houqp Mar 9, 2022

Choose a reason for hiding this comment

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

how about combining the length check and pop logic into a single block to make it more readable? basically something like below:

        let stmt = statements.pop().ok_or(Err(DataFusionError::NotImplemented(
            "The context currently only supports a single SQL statement".to_string()
        )))?;
        // create a query planner
        let state = self.state.lock().clone();
        let query_planner = SqlToRel::new(&state);
        query_planner.statement_to_plan(stmt)

Copy link
Contributor Author

@doki23 doki23 Mar 9, 2022

Choose a reason for hiding this comment

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

Hm, the error description means length of statements should eq 1, but 'statements.pop().ok_or(...)' only indicates that it's >= 1. And I have not found api having same semantics.

doki23 and others added 2 commits March 9, 2022 22:28
@alamb
Copy link
Contributor

alamb commented Mar 9, 2022

@doki23 and @xudong963 I merged this PR with master to resolve (another) conflict in a0f7091 -- I hope you don't mind. I don't want it to be hanging out too long and picking up conflits along the way

@alamb alamb merged commit 67b8272 into apache:master Mar 10, 2022
@alamb
Copy link
Contributor

alamb commented Mar 10, 2022

Thanks again @doki23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api change Changes the API exposed to users of the crate datafusion Changes in the datafusion crate sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert SqlToRel::statement_to_pan to take ownership rather than references.
5 participants