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

Return &Arc reference to inner trait object #11103

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions datafusion-cli/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,14 @@ mod tests {
fn setup_context() -> (SessionContext, Arc<dyn SchemaProvider>) {
let mut ctx = SessionContext::new();
ctx.register_catalog_list(Arc::new(DynamicFileCatalog::new(
ctx.state().catalog_list(),
ctx.state().catalog_list().clone(),
Copy link
Contributor

Choose a reason for hiding this comment

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

when I see .clone() Im now thinking what if we comment it the similar way as .unwrap() back in the day. Like say clone is cheap here because it is Arc::clone so only reference gets cloned.... thats just an idea, its too cumbersome to make it happen

Copy link
Contributor

@alamb alamb Jun 25, 2024

Choose a reason for hiding this comment

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

One thing we do in InfluxDB is use this pattern to make it clear

Suggested change
ctx.state().catalog_list().clone(),
Arc::clone(ctx.state().catalog_list())

to make it explicit.

There is a clippy lint https://rust-lang.github.io/rust-clippy/v0.0.212/#clone_on_ref_ptr we could turn on in datafusion to enable this

Here is how it is done in influxdb:

https://github.com/influxdata/influxdb3_core/blob/0f5ecbd6b17f83f7ad4ba55699fc2cd3e151cf94/Cargo.toml#L117-L118

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good idea, let's do it

Copy link
Contributor

Choose a reason for hiding this comment

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

Filed #11143 to track

ctx.state_weak_ref(),
)));

let provider =
&DynamicFileCatalog::new(ctx.state().catalog_list(), ctx.state_weak_ref())
as &dyn CatalogProviderList;
let provider = &DynamicFileCatalog::new(
ctx.state().catalog_list().clone(),
ctx.state_weak_ref(),
) as &dyn CatalogProviderList;
let catalog = provider
.catalog(provider.catalog_names().first().unwrap())
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion datafusion-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async fn main_inner() -> Result<()> {
ctx.refresh_catalogs().await?;
// install dynamic catalog provider that knows how to open files
ctx.register_catalog_list(Arc::new(DynamicFileCatalog::new(
ctx.state().catalog_list(),
ctx.state().catalog_list().clone(),
ctx.state_weak_ref(),
)));
// register `parquet_metadata` table function to get metadata from parquet files
Expand Down
2 changes: 1 addition & 1 deletion datafusion/core/src/execution/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ mod tests {

let catalog_list_weak = {
let state = ctx.state.read();
Arc::downgrade(&state.catalog_list())
Arc::downgrade(state.catalog_list())
};

drop(ctx);
Expand Down
8 changes: 4 additions & 4 deletions datafusion/core/src/execution/session_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,8 @@ impl SessionState {
}

/// Return catalog list
pub fn catalog_list(&self) -> Arc<dyn CatalogProviderList> {
self.catalog_list.clone()
pub fn catalog_list(&self) -> &Arc<dyn CatalogProviderList> {
&self.catalog_list
}

/// set the catalog list
Expand All @@ -835,8 +835,8 @@ impl SessionState {
}

/// Return [SerializerRegistry] for extensions
pub fn serializer_registry(&self) -> Arc<dyn SerializerRegistry> {
self.serializer_registry.clone()
pub fn serializer_registry(&self) -> &Arc<dyn SerializerRegistry> {
&self.serializer_registry
}

/// Return version of the cargo package that produced this query
Expand Down
4 changes: 2 additions & 2 deletions datafusion/expr/src/udaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ impl AggregateUDF {
}

/// Return the underlying [`AggregateUDFImpl`] trait object for this function
pub fn inner(&self) -> Arc<dyn AggregateUDFImpl> {
self.inner.clone()
pub fn inner(&self) -> &Arc<dyn AggregateUDFImpl> {
&self.inner
}

/// Adds additional names that can be used to invoke this function, in
Expand Down
4 changes: 2 additions & 2 deletions datafusion/expr/src/udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ impl ScalarUDF {
}

/// Return the underlying [`ScalarUDFImpl`] trait object for this function
pub fn inner(&self) -> Arc<dyn ScalarUDFImpl> {
self.inner.clone()
pub fn inner(&self) -> &Arc<dyn ScalarUDFImpl> {
&self.inner
}

/// Adds additional names that can be used to invoke this function, in
Expand Down
4 changes: 2 additions & 2 deletions datafusion/expr/src/udwf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ impl WindowUDF {
}

/// Return the underlying [`WindowUDFImpl`] trait object for this function
pub fn inner(&self) -> Arc<dyn WindowUDFImpl> {
self.inner.clone()
pub fn inner(&self) -> &Arc<dyn WindowUDFImpl> {
&self.inner
}

/// Adds additional names that can be used to invoke this function, in
Expand Down