LiaCastaneda commented on code in PR #26890: URL: https://github.com/apache/datafusion/pull/26890#discussion_r4480067259
########## datafusion/substrait/README.md: ########## @@ -28,3 +28,30 @@ plans. See [API Docs] for details and examples. [apache datafusion]: https://datafusion.apache.org/ [substrait]: https://substrait.io [api docs]: https://docs.rs/datafusion-substrait/latest + +## Migrating custom consumers to explicit scope context Review Comment: Breaking API changes are usually documented per release in ` docs/source/library-user-guide/upgrading/<version>.md`, I think the first 2 paragraphs can be moved (or copied) there ########## datafusion/substrait/src/logical_plan/consumer/rel/join_rel.rs: ########## @@ -15,17 +31,26 @@ use substrait::proto::{JoinRel, join_rel}; pub async fn from_join_rel( consumer: &impl SubstraitConsumer, join: &JoinRel, + context: &SubstraitConsumerContext, ) -> datafusion::common::Result<LogicalPlan> { if join.post_join_filter.is_some() { return not_impl_err!("JoinRel with post_join_filter is not yet supported"); } - let left: LogicalPlanBuilder = LogicalPlanBuilder::from( - consumer.consume_rel(join.left.as_ref().unwrap()).await?, - ); - let right = LogicalPlanBuilder::from( - consumer.consume_rel(join.right.as_ref().unwrap()).await?, - ); + let (Some(left), Some(right)) = (join.left.as_deref(), join.right.as_deref()) else { + return substrait_err!("JoinRel requires left and right inputs"); + }; + let [left, right] = consumer + .consume_rel_inputs(&[left, right], context) Review Comment: Do we have any benchmark showing how much faster planning is if we consume both inputs in parallel? ########## datafusion/substrait/src/logical_plan/consumer/substrait_consumer.rs: ########## @@ -668,82 +656,62 @@ impl SubstraitConsumer for DefaultSubstraitConsumer<'_> { .deserialize_logical_plan(&ext_detail.type_url, &ext_detail.value)?; let mut inputs = Vec::with_capacity(rel.inputs.len()); for input in &rel.inputs { - let input_plan = self.consume_rel(input).await?; + let input_plan = self.consume_rel(input, context).await?; inputs.push(input_plan); } let plan = plan.with_exprs_and_inputs(plan.expressions(), inputs)?; Ok(LogicalPlan::Extension(Extension { node: plan })) } - - fn push_lambda_parameters( - &self, - lambda_parameters: &[Type], - input_schema: &DFSchema, - ) -> datafusion::common::Result<Vec<String>> { - self.lambda_consumer - .push_lambda_parameters(self, lambda_parameters, input_schema) - } - - fn pop_lambda_parameters(&self) { - self.lambda_consumer.pop_lambda_parameters() - } - - fn lambda_variable( - &self, - steps_out: usize, - field_idx: usize, - ) -> datafusion::common::Result<Expr> { - self.lambda_consumer.lambda_variable(steps_out, field_idx) - } } -/// Default implementation of lambda related methods of the [SubstraitConsumer] trait +/// Immutable lexical scope for one path through a Substrait plan. /// -/// Can be embedded into a custom [SubstraitConsumer] to implement them -pub struct DefaultSubstraitLambdaConsumer { - inner: RwLock<DefaultSubstraitLambdaConsumerInner>, -} - -struct DefaultSubstraitLambdaConsumerInner { - /// Parameters of the lambdas currently in scope, ordered from innermost - /// to outermost. Index 0 is the lambda being consumed; higher indices - /// are enclosing lambdas, matching the `steps_out` value used by - /// [`DefaultSubstraitLambdaConsumer::lambda_variable`] and `LambdaParameterReference`. +/// Pass the current context to child conversions. Enter a subquery or lambda by +/// deriving a child context; the parent stays unchanged, including on errors or +/// cancellation. Independent inputs may borrow the same context concurrently. +#[derive(Debug, Clone, Default)] +pub struct SubstraitConsumerContext { + outer_schemas: Vec<Arc<DFSchema>>, lambda_parameters: VecDeque<Vec<FieldRef>>, next_lambda_parameter: usize, } Review Comment: Maybe an option is to add something like `with_extension` to this context (similar to how `SessionConfig` can store whatever we want) it would be breaking though so we would need to add another change to `upgrading.md` ########## datafusion/substrait/src/logical_plan/consumer/substrait_consumer.rs: ########## @@ -668,82 +656,62 @@ impl SubstraitConsumer for DefaultSubstraitConsumer<'_> { .deserialize_logical_plan(&ext_detail.type_url, &ext_detail.value)?; let mut inputs = Vec::with_capacity(rel.inputs.len()); for input in &rel.inputs { - let input_plan = self.consume_rel(input).await?; + let input_plan = self.consume_rel(input, context).await?; inputs.push(input_plan); } let plan = plan.with_exprs_and_inputs(plan.expressions(), inputs)?; Ok(LogicalPlan::Extension(Extension { node: plan })) } - - fn push_lambda_parameters( - &self, - lambda_parameters: &[Type], - input_schema: &DFSchema, - ) -> datafusion::common::Result<Vec<String>> { - self.lambda_consumer - .push_lambda_parameters(self, lambda_parameters, input_schema) - } - - fn pop_lambda_parameters(&self) { - self.lambda_consumer.pop_lambda_parameters() - } - - fn lambda_variable( - &self, - steps_out: usize, - field_idx: usize, - ) -> datafusion::common::Result<Expr> { - self.lambda_consumer.lambda_variable(steps_out, field_idx) - } } -/// Default implementation of lambda related methods of the [SubstraitConsumer] trait +/// Immutable lexical scope for one path through a Substrait plan. /// -/// Can be embedded into a custom [SubstraitConsumer] to implement them -pub struct DefaultSubstraitLambdaConsumer { - inner: RwLock<DefaultSubstraitLambdaConsumerInner>, -} - -struct DefaultSubstraitLambdaConsumerInner { - /// Parameters of the lambdas currently in scope, ordered from innermost - /// to outermost. Index 0 is the lambda being consumed; higher indices - /// are enclosing lambdas, matching the `steps_out` value used by - /// [`DefaultSubstraitLambdaConsumer::lambda_variable`] and `LambdaParameterReference`. +/// Pass the current context to child conversions. Enter a subquery or lambda by +/// deriving a child context; the parent stays unchanged, including on errors or +/// cancellation. Independent inputs may borrow the same context concurrently. +#[derive(Debug, Clone, Default)] +pub struct SubstraitConsumerContext { + outer_schemas: Vec<Arc<DFSchema>>, lambda_parameters: VecDeque<Vec<FieldRef>>, next_lambda_parameter: usize, } Review Comment: Maybe this is far fetched, but could users with their own `SubstraitConsumer` be storing values on it that are no longer valid now that both sides of a join are built in parallel? I think it might break if a custom consumer sets a value when entering a node and unsets it on the way back. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
