Skip to content

Conversation

@tustvold
Copy link
Contributor

@tustvold tustvold commented Dec 14, 2022

Which issue does this PR close?

Part of #4617

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added the core Core DataFusion crate label Dec 14, 2022
@tustvold tustvold added the api change Changes the API exposed to users of the crate label Dec 14, 2022
@tustvold tustvold force-pushed the dataframe-arc-rwlock branch from d2091d9 to 215f8db Compare December 15, 2022 08:10
@tustvold tustvold marked this pull request as ready for review December 15, 2022 08:43
@tustvold tustvold requested a review from alamb December 15, 2022 11:51
@alamb
Copy link
Contributor

alamb commented Dec 15, 2022

🤔 there appears to be some sort of deadlock in the tests

https://github.com/apache/arrow-datafusion/actions/runs/3702235488/jobs/6272289305


test sql::select::select_values_list ... ok
test sql::select::test_prepare_statement ... ok
test sql::select::unprojected_filter ... ok
test sql::select::use_between_expression_in_select_query ... ok
test sql::order::sort_with_lots_of_repetition_values ... ok
test sql::set_variable::set_bool_variable_bad_value ... ok
test sql::set_variable::set_bool_variable has been running for over 60 seconds
test sql::set_variable::set_time_zone has been running for over 60 seconds
Error: The operation was canceled.

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 @tustvold the idea looks great to me, though I think we need to solve the CI deadlock

@tustvold
Copy link
Contributor Author

Lol, even more reason to remove these locks #4617

LogicalPlan::SetVariable(SetVariable {
variable, value, ..
}) => {
let config_options = &self.state.write().config.config_options;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm actually somewhat surprised this was compiling, there must be some magic going on to extend the lifetime of the temporary lock guard, which is wild

// original state after it has been cloned, they will not be picked up by the
// clone but that is okay, as it is equivalent to postponing the state update
// by keeping the lock until the end of the function scope.
state.clone()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In practice the lock meant that we were still cloning SessionState fairly frequently, better to just be explicit about it and optimise from there

}

state_cloned.create_physical_plan(&self.plan).await
/// Temporary pending #4626
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 temporary pending moving the physical plan lowering onto DataFrame from SessionState which is tracked by #4626 and #4629

state_cloned.create_physical_plan(&self.plan).await
/// Temporary pending #4626
async fn create_physical_plan_impl(&mut self) -> Result<Arc<dyn ExecutionPlan>> {
self.session_state.execution_props.start_execution();
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 nice because we are no longer mutating state on the actual SessionContext

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 like it -- thank you @tustvold

There is likely a change of behavior here:

Specifically prior to this PR if you made a dataframe and then modified the SessionContext the dataframe might see some of those changes.

However I think the old behavior would be quite surprising and so I like this change a lot

impl DataFrame {
/// Create a new Table based on an existing logical plan
pub fn new(session_state: Arc<RwLock<SessionState>>, plan: LogicalPlan) -> Self {
pub fn new(session_state: SessionState, plan: LogicalPlan) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️


/// Write a `DataFrame` to a CSV file.
pub async fn write_csv(self, path: &str) -> Result<()> {
let state = self.session_state.read().clone();
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah all the explicit cloning is definitely a warning sign

@tustvold tustvold merged commit 42b3a6c into apache:master Dec 17, 2022
@ursabot
Copy link

ursabot commented Dec 17, 2022

Benchmark runs are scheduled for baseline = 414487c and contender = 42b3a6c. 42b3a6c is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@mingmwang
Copy link
Contributor

@tustvold @alamb

I'm a little surprised by this change. In my opinion, SessionState should be the internal state of a SessionContext, it can be borrowed/shared, making SessionState owned by the DataFrame will break the purpose that it is the state of the session. We should not have many SessionStates for a single Session in the system.

@tustvold
Copy link
Contributor Author

tustvold commented Dec 20, 2022

The idea is that DataFrame contains a snapshot of SessionContext, i.e. a clone of SessionState? This will allow moving planning and execution off SessionContext to both resolve the current circular dependency, and also make the state consistent across planning and execution

@alamb
Copy link
Contributor

alamb commented Dec 20, 2022

@mingmwang I think the key proposal by @tustvold is that once a query is planned, it has only a read-only copy of the SessionState rather than mutable access to the current SessionState

This has several seemingly nice properties, such as a configuration value changes that are made after a query is planned, will not affect the plan (it will only affect subsequently planned queries). Do you agree this sounds like reasonable behavior? Update: I didn't see #4617 (comment) so it sounds like this is reasonable from your perspective

Do you know of any usecases where running a query needs to modify the session context?

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 core Core DataFusion crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants