Skip to content

[SPARK-52813][CONNECT] Allow DAGs in Spark Connect #51516

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hvanhovell
Copy link
Contributor

What changes were proposed in this pull request?

This PR adds support to turn connect plans into DAGs. Currently we inline the structure of a reused query fragment during serialization. This can cause massive bloat, increase plan size. This hurts over the wire performance, and it hurts planning performance.

In order to allow DAGs we add reference relation type. This reference relation contains an plan_id that points to a plan defined in an parent/root WithRelations node.

I have created an optimization rule for scala that leverages this functionality by DAGifing a plan before sending it over the wire.

Why are the changes needed?

This changes in this PR can decrease plan size significantly. This benefits over the wire performance, and it allows for faster planning.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

I have added unit tests for the PlanOptimization in PlanOptimizationSuite.
I have added an end to end test to ClientDatasetSuite.
I still need to add a self contained server side test

Was this patch authored or co-authored using generative AI tooling?

No.

getSubqueryExpression.getSubqueryType match {
case proto.SubqueryExpression.SubqueryType.SUBQUERY_TYPE_SCALAR =>
UnresolvedScalarSubqueryPlanId(planId)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can remove the Unresolved*PlanId expressions.

case other =>
throw InvalidInputErrors.sqlCommandExpectsSqlOrWithRelations(other)
// Only allow a SQL relation or a SQL relation nested in a WithRelations relation.
if (!relation.hasSql &&
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 check is a bit arbitrary. Do we really care?

}
val df = Dataset.ofRows(session, transformRelation(relation), tracker)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Capture the parse time here, use the transformRelation time as a proxy. Long term we may want to look at the use of QueryPlanTracker in general.

val plan = if (relation.getCommon.hasPlanId) {
getCachedRelation(relation.getCommon.getPlanId)
} else {
transformRelation(relation)
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 am being paranoid here. I hope this does not happen...

@@ -2881,79 +2890,6 @@ class SparkConnectPlanner(
}
}

private def isValidSQLWithRefs(query: proto.WithRelations): Boolean = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of this code is replaced by CTEs...

transformSqlWithRefs(getWithRelations)
// Register the plans in the relation cache, so they can be resolved while
// transforming the root relation into a LogicalPlan.
val namedReferences = mutable.Buffer.empty[(String, proto.Relation)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we throw if there are duplicate names?

throw InvalidInputErrors.invalidWithRelationReference()
}
if (common.hasPlanId) {
relationCache.put(common.getPlanId, ref)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we throw if the id is already registered to a different relation?

@hvanhovell hvanhovell changed the title [SPARK-52813][CONNECT] [SPARK-52813][CONNECT] Allow DAGs in Spark Connect Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant