Skip to content

feat: allow datafusion-cli to accept multiple statements #7138

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

Conversation

NiwakaDev
Copy link
Contributor

Which issue does this PR close?

Closes #6793.

Rationale for this change

What changes are included in this PR?

This PR allows datafusion-cli to accept multiple statements.

Are these changes tested?

Are there any user-facing changes?

Copy link
Contributor

@2010YOUY01 2010YOUY01 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! This feature is very useful.

DataFusion CLI v28.0.0
❯ select 1; select 2;
+----------+
| Int64(1) |
+----------+
| 1        |
+----------+
1 row in set. Query took 0.060 seconds.
+----------+
| Int64(2) |
+----------+
| 2        |
+----------+
1 row in set. Query took 0.061 seconds.

I suggest also add an empty line between results to make it look a little bit better

}
_ => ctx.execute_logical_plan(plan).await?,
};
let statements = DFParser::parse_sql(&sql)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

This will use the default dialect, we can use parse_sql_with_dialect() and pass the dialect from the current context into it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure what you mean by the current context -- which one are you thinking of?

Copy link
Contributor

Choose a reason for hiding this comment

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

DataFusion CLI v28.0.0
❯ set datafusion.sql_parser.dialect = 'Postgres';

I'm referring to this option, it can be found in ctx.state()..., and be used to parse statements

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Please let me know if there are any errors or omissions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good to me, thank you!

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 @NiwakaDev -- this is a great contribution

Thank you @2010YOUY01 for the review

cc @jimexist and @r4ntix

Copy link
Contributor

@r4ntix r4ntix left a comment

Choose a reason for hiding this comment

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

LGTM -- 👍

I've left a few suggestions.

print_options.print_batches(&results, now)?;
let results = df.collect().await?;
print_options.print_batches(&results, now)?;
println!();
Copy link
Contributor

@r4ntix r4ntix Aug 1, 2023

Choose a reason for hiding this comment

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

The println!(); here I would suggest to put in the code of fn print_timing_info(). Because it will affect the output of --quiet mode.

suggestion:

// add '\n' at the end of `println!`
fn print_timing_info(row_count: usize, now: Instant) {
    println!(
        "{} {} in set. Query took {:.3} seconds.\n",
        row_count,
        if row_count == 1 { "row" } else { "rows" },
        now.elapsed().as_secs_f64()
    );
}

Copy link
Contributor

Choose a reason for hiding this comment

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

The current --quiet mode output is as follows:

./target/debug/datafusion-cli --command 'select 1;select 2;' -q --format json
[{"Int64(1)":1}]
[{"Int64(2)":2}]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Please let me know if there are any errors or omissions.

@alamb
Copy link
Contributor

alamb commented Aug 1, 2023

🤔 something happened to make CI fail. Perhaps that is what @r4ntix 's comments are about

@NiwakaDev
Copy link
Contributor Author

Right now I guess that the integration tests pass, and I added a new integration test to them.

#[case::exec_multiple_statements(
["--command", "select 1; select 2;", "--format", "json", "-q"],
"[{\"Int64(1)\":1}]\n[{\"Int64(2)\":2}]\n"
)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a new integration test.

Copy link
Contributor

Choose a reason for hiding this comment

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

❤️ Thanks again @NiwakaDev -- I like it

Copy link
Contributor

@r4ntix r4ntix left a comment

Choose a reason for hiding this comment

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

Looks great to me. Thanks again @NiwakaDev ❤️

@alamb alamb merged commit 1901d6f into apache:main Aug 3, 2023
@alamb
Copy link
Contributor

alamb commented Aug 3, 2023

Thanks again everyone -- this is a great contribution @NiwakaDev 🦾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow multiple statements in datafusion-cli
5 participants