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

[SPARK-46707][SQL] Added throwable field to expressions to improve predicate pushdown #44716

Closed

Conversation

kelvinjian-db
Copy link
Contributor

What changes were proposed in this pull request?

This PR adds the field throwable to Expression. If an expression is marked as throwable, we will avoid pushing filters containing these expressions through joins, filters, and aggregations (i.e. operators that filter input).

Why are the changes needed?

For predicate pushdown, currently it is possible that we push down a filter that ends up being evaluated on more rows than before it was pushed down (e.g. if we push the filter through a selective join). In this case, it is possible that we now evaluate the filter on a row that will cause a runtime error to be thrown, when prior to pushing this would not have happened.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Added UTs.

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

No.

@github-actions github-actions bot added the SQL label Jan 13, 2024
@kelvinjian-db kelvinjian-db changed the title [SPARK-46707] Added throwable field [SPARK-46707] Added throwable field to expressions to improve predicate pushdown Jan 13, 2024
@09306677806
Copy link

Tnkyo

@HyukjinKwon HyukjinKwon changed the title [SPARK-46707] Added throwable field to expressions to improve predicate pushdown [SPARK-46707][SQL] Added throwable field to expressions to improve predicate pushdown Jan 14, 2024
@@ -2983,6 +2983,9 @@ case class Sequence(

override def nullable: Boolean = children.exists(_.nullable)

// If step is defined, then an error will be thrown if the start and stop do not satisfy the step.
override lazy val throwable: Boolean = stepOpt.isDefined
Copy link
Member

Choose a reason for hiding this comment

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

Can we leverage NoThrow trait?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the problem with NoThrow is that it doesn't mark that many expressions right now (i.e. if we only push down NoThrow expressions then we would be not pushing down most things). we want the opposite here (marking only certain expressions not to push down). defining it this way also allows us to be more precise with defining what is throwable, compared to just setting it for the class

Copy link
Contributor

Choose a reason for hiding this comment

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

These two are similar but be conservative in two opposite directions. Filter pushdown is a very important feature and we don't want to suddenly disable it for many predicates. We only mark sequence as throwable for now.

NoThrow was added for a new optimization and only a few expressions are marked as NoThrow that can be optimized.

Since runtime error is rare, I think the current solution is better to produce more optimized plans for most cases.

Copy link
Member

Choose a reason for hiding this comment

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

If it's rare, why do we add a property to Expression?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because it's indeed an expression property. NoThrow is not the right way to add properties as the caller side needs to do recursion: https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala#L53-L59 , which is not good for code reuse (you need to call a util function)

Copy link
Member

Choose a reason for hiding this comment

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

Should probably remove NoThrow then ... I approved it for now.

@beliefer
Copy link
Contributor

beliefer commented Jan 17, 2024

I'm a bit confused. Why not pushdown predicate through join will not throw exception ?

@kelvinjian-db
Copy link
Contributor Author

@beliefer if we have a plan like

Filter [isnotnull(sequence(col1, col2, 1)]
+- Join …
  +- LocalRelation [col0, col1, col2]
  +- …

it is possible to have a scenario where:

  • if we push down the filter through the join, and then there is a row where col1 > col2 in the LocalRelation, causing an exception to be thrown
  • but if we didn't push down the filter, the join would have filtered out all the rows where col1 > col2, so the exception would not have been thrown

@cloud-fan
Copy link
Contributor

thanks, merging to master!

@cloud-fan cloud-fan closed this in 62956c9 Jan 18, 2024
@beliefer
Copy link
Contributor

@kelvinjian-db I guess it causes an exception if the join key is not related to col1 or col2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants