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

use Into<String> as argument type wherever applicable #615

Merged
merged 3 commits into from
Jun 28, 2021

Conversation

houqp
Copy link
Member

@houqp houqp commented Jun 24, 2021

Rationale for this change

Following the article shared by @jorgecarleitao: https://phaazon.net/blog/on-owning-borrowing-pub-interface. Using impl Into<T> as argument type makes it possible for function to take ownership of an argument while providing the flexibility to the callers to decide whether they want to pass argument by reference or by value.

@Dandandan also proposed this pattern in #55 for the builder interface: https://github.com/apache/arrow-datafusion/blob/d55a10569b3a24195bed2d67cc6414c63b6b2336/datafusion/src/logical_plan/builder.rs#L264

I just never thought of extending it to other existing functions and types until I saw that article. I think we can apply this same pattern to other types as well like LogicalPlanBuilder::from, which takes a reference of LogicalPlan as argument and clones it internally.

What changes are included in this PR?

Change &str to impl Into<String> for functions that does internal clone of the string.

Are there any user-facing changes?

No breaking API change.

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 don't see the value in a few of these changes, but otherwise things look good to me. Thank you @houqp

options: CsvReadOptions,
projection: Option<Vec<usize>>,
) -> Result<Self> {
let provider = Arc::new(CsvFile::try_new(path, options)?);
let path = path.into();
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this pattern just doesn't make a lot of sense to me -- if someone passed in a &str then this code will now create a copy (a String) and then simply use a reference to that copy (rather than the path itself)

Copy link
Member Author

Choose a reason for hiding this comment

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

the owned value is passed along to Self::scan at the end of the function.

provider: Arc<dyn TableProvider>,
projection: Option<Vec<usize>>,
) -> Result<Self> {
let table_name = table_name.into();
Copy link
Contributor

Choose a reason for hiding this comment

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

This change makes sense to me as this function previously was making an explicit copy and no can reuse the copy the caller passed in, if any 👍

            table_name: table_name.to_string(),

@@ -44,10 +44,10 @@ pub struct Column {

impl Column {
/// Create Column from unqualified name.
pub fn from_name(name: String) -> Self {
pub fn from_name(name: impl Into<String>) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@alamb
Copy link
Contributor

alamb commented Jun 27, 2021

I think this one is ready - it just needs to be rebased

@alamb alamb merged commit e86f8e9 into apache:master Jun 28, 2021
@houqp houqp deleted the qp_into branch July 4, 2021 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datafusion Changes in the datafusion crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants