Skip to content

[SPARK-38578][SQL] AdaptiveSparkPlanExec should ensure user-specified ordering - #35924

Closed
ulysses-you wants to merge 4 commits into
apache:masterfrom
ulysses-you:aqe-ordering
Closed

[SPARK-38578][SQL] AdaptiveSparkPlanExec should ensure user-specified ordering#35924
ulysses-you wants to merge 4 commits into
apache:masterfrom
ulysses-you:aqe-ordering

Conversation

@ulysses-you

@ulysses-youulysses-you commented Mar 21, 2022

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

  • Ensure output ordering using requiredOrdering
  • Override outputOrdering in AdaptiveSparkPlanExec

Why are the changes needed?

AdaptiveSparkPlanExec should ensure the output ordering is the requiredOrdering, so we leverage the EnsureRequirements to add sort if need.

FileFormatWriter will check and add an implicit sort for dynamic partition columns or bucket columns according to the input physical plan. The check became always failure since AQE AdaptiveSparkPlanExec has no outputOrdering.

That casues a redundant sort if user has specified a sort which satisfies the required ordering (dynamic partition and bucket columns).

Does this PR introduce any user-facing change?

no, improve performance

How was this patch tested?

add test

CREATETABLEt1 (c int) USING PARQUET PARTITIONED BY(p string);
CREATETABLEt2 USING PARQUET ASSELECT1 c, 'a' p;
INSERT INTO TABLE t1 PARTITION(p) select c, p from t2 order by p;

Before:
image

After:
image

@ulysses-you

Copy link
Copy Markdown
ContributorAuthor

cc @maryannxue@cloud-fan

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need to override outputPartitioning as well?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I think we can, but there is no requirements about outputPartitioning

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems we can remove this now?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

OptimizeSkewedJoin still use this. I considered unify them, but seems OptimizeSkewedJoin does not affect the required output ordering.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should refine the test name if we don't really test table insertion.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

yeah, refined it

@ulysses-youulysses-you changed the title [SPARK-38578][SQL] Avoid unnecessary sort in FileFormatWriter if user has specified sort in AQE[SPARK-38578][SQL] AdaptiveSparkPlanExec should ensure user-specified orderingMar 23, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// User-specified repartition is only effective when it's the root node, or under
// User-specified sort is only effective when it's the root node, or under

@ulysses-you

Copy link
Copy Markdown
ContributorAuthor

The failed test is irrelevant

ReportSinkMetricsSuite.test ReportSinkMetrics
org.scalatest.exceptions.TestFailedException: Expected null, but got {"metrics-1"="value-1", "metrics-2"="value-2"}

@cloud-fan

Copy link
Copy Markdown
Contributor

Can we rerun the tests?

@ulysses-you

Copy link
Copy Markdown
ContributorAuthor

rebased since that flaky test has been fixed

@cloud-fan

Copy link
Copy Markdown
Contributor

After a second thought, what will happen if we just do a one-line fix override def outputOrdering: Seq[SortOrder] = requiredOrdering? AQE optimization may remove user-specified sort, but it doesn't matter as the file writing will add the necessary sort.

@ulysses-you

Copy link
Copy Markdown
ContributorAuthor

file writing will add the necessary sort.

it does not. FileFormatWriter just checks the initialization SparkPlan if the ordering satisfies the required. If AQE changes the ordering at runtime, FileFormatWriter can not aware the change. So if we override the outputOrdering, we must make sure AQE won't change it.

@ulysses-you

Copy link
Copy Markdown
ContributorAuthor

closed, in favor of #34568

Sign up for freeto 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.

3 participants

@ulysses-you@cloud-fan@singhpk234