Skip to content

Conversation

@cloud-fan
Copy link
Contributor

What changes were proposed in this pull request?

Currently, there are some expressions that overwrite semanticEquals, which makes it not symmetrical. Ideally, expressions should overwrite canonicalized instead of semanticEquals.

This PR marks semanticEquals as final, and implement canonicalized for the few expressions that overwrote semanticEquals before.

Why are the changes needed?

To avoid subtle bugs (I haven't found a real bug yet).

Does this PR introduce any user-facing change?

no

How was this patch tested?

a new test

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 the subclasses of SubqueryExpression have implemented canonicalized.

@cloud-fan
Copy link
Contributor Author

cc @viirya @maropu

@github-actions github-actions bot added the SQL label Jun 11, 2021
Comment on lines -281 to -285
Copy link
Member

Choose a reason for hiding this comment

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

Don't need canonicalized?

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 default implementation works for AttributeReference

Comment on lines -130 to -134
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's already there, in L164

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I agree with the idea.

@dongjoon-hyun
Copy link
Member

BTW, could you make CI happy?

Copy link
Member

@viirya viirya left a comment

Choose a reason for hiding this comment

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

Looks good if CI can pass.

@SparkQA
Copy link

SparkQA commented Jun 11, 2021

Test build #139711 has finished for PR 32885 at commit 73aa6f2.

  • This patch fails to build.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jun 11, 2021

Kubernetes integration test unable to build dist.

exiting with code: 1
URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44234/

@HyukjinKwon
Copy link
Member

Yeah, +1 if CI passes.

// map and/or the sort-based aggregation once it has processed a given number of input rows.
private val testFallbackStartsAt: Option[(Int, Int)] = {
sqlContext.getConf("spark.sql.TungstenAggregate.testFallbackStartsAt", null) match {
Option(sqlContext).map { sc =>
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 a hidden bug. SubqueryExpression will be sent to the executor side and build Projection, and be put in EquivalentExpressions, which needs to call canonicalized.

This means, Spark may serialize and send HashAggregateExec to the executor side, where sqlContext should be null.

It's hidden for a long time because ScalarSubquery didn't implement canonicalized, so the bug is not triggered. However, it also means semanticHash is wrong.

I think it only affects common subquery elimination, and shouldn't be a serious bug.

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 the long term, I think we should only send an "expression evaluator" to the executor side. The semantic check should only be done in the driver side.

Copy link
Contributor

@peter-toth peter-toth Jun 15, 2021

Choose a reason for hiding this comment

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

Sorry for the late comment @cloud-fan, but I think I've run into this issue before:
https://github.com/apache/spark/pull/28885/files#diff-9b62cef6bfdeb6c802bb120c7a724a974d5067a69585285bebb64c48603f8d6fR105-R108. The point is that there might be other nodes where canonicalization on executor side can cause issues. SortExec.enableRadixSort is the other one I found.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

InSubqueryExec already implements canonicalized before this PR, so we need to fix these bugs anyway.

I have an idea to fix this problem in all physical plans:

  1. remove SparkPlan.sqlContext, so that we can catch all the callers of it
  2. add @transient final val session = SparkSession.getActiveSession.orNull, to replace the previous sqlContext
  3. override conf in SparkPlan: if (session != null) session.sessionState.conf else SQLConf.get

@peter-toth what do you think? AFAIK the only reason to access SparkPlan.sqlContext at executor side is to read a conf, and we can do that with SQLConf.get at executor side.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me.

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 quite busy this week and may not have time to implement this idea recently. @peter-toth feel free to pick up this idea and open a PR if you have time, or I'll do it next or next next week. Thanks in advance!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, thanks. I will try to open a PR this week.

Copy link
Contributor

Choose a reason for hiding this comment

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

Opened #32947.

HashAggregate [c_customer_sk,sum,isEmpty] [sum(CheckOverflow((promote_precision(cast(cast(ss_quantity as decimal(10,0)) as decimal(12,2))) * promote_precision(cast(ss_sales_price as decimal(12,2)))), DecimalType(18,2), true)),sum(CheckOverflow((promote_precision(cast(cast(ss_quantity as decimal(10,0)) as decimal(12,2))) * promote_precision(cast(ss_sales_price as decimal(12,2)))), DecimalType(18,2), true)),sum,isEmpty]
InputAdapter
ReusedExchange [c_customer_sk,sum,isEmpty] #8
ReusedExchange [c_customer_sk,c_first_name,c_last_name] #19
Copy link
Contributor Author

Choose a reason for hiding this comment

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

After the fix, more exchange reuse happens :)

Seems the problem was caused by ReusedSubquery.

@SparkQA
Copy link

SparkQA commented Jun 15, 2021

Kubernetes integration test unable to build dist.

exiting with code: 1
URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44315/

@SparkQA
Copy link

SparkQA commented Jun 15, 2021

Test build #139789 has finished for PR 32885 at commit 5095fff.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@viirya
Copy link
Member

viirya commented Jun 15, 2021

LGTM

@cloud-fan
Copy link
Contributor Author

thanks for the review, merging to master!

@cloud-fan cloud-fan closed this in a50bd8f Jun 15, 2021
domybest11 pushed a commit to domybest11/spark that referenced this pull request Jun 15, 2022
Currently, there are some expressions that overwrite `semanticEquals`, which makes it not symmetrical. Ideally, expressions should overwrite `canonicalized` instead of `semanticEquals`.

This PR marks `semanticEquals` as final, and implement `canonicalized` for the few expressions that overwrote `semanticEquals` before.

To avoid subtle bugs (I haven't found a real bug yet).

no

a new test

Closes apache#32885 from cloud-fan/attr.

Authored-by: Wenchen Fan <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants