{"action":"created","issue":{"url":"https://api.github.com/repos/apache/spark/issues/21139","repository_url":"https://api.github.com/repos/apache/spark","labels_url":"https://api.github.com/repos/apache/spark/issues/21139/labels{/name}","comments_url":"https://api.github.com/repos/apache/spark/issues/21139/comments","events_url":"https://api.github.com/repos/apache/spark/issues/21139/events","html_url":"https://github.com/apache/spark/pull/21139","id":317138451,"node_id":"MDExOlB1bGxSZXF1ZXN0MTgzNjY5OTU3","number":21139,"title":"[SPARK-24066][SQL]Add a window exchange rule to eliminate redundant physical plan SortExec","user":{"login":"heary-cao","id":20164092,"node_id":"MDQ6VXNlcjIwMTY0MDky","avatar_url":"https://avatars1.githubusercontent.com/u/20164092?v=4","gravatar_id":"","url":"https://api.github.com/users/heary-cao","html_url":"https://github.com/heary-cao","followers_url":"https://api.github.com/users/heary-cao/followers","following_url":"https://api.github.com/users/heary-cao/following{/other_user}","gists_url":"https://api.github.com/users/heary-cao/gists{/gist_id}","starred_url":"https://api.github.com/users/heary-cao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heary-cao/subscriptions","organizations_url":"https://api.github.com/users/heary-cao/orgs","repos_url":"https://api.github.com/users/heary-cao/repos","events_url":"https://api.github.com/users/heary-cao/events{/privacy}","received_events_url":"https://api.github.com/users/heary-cao/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":10,"created_at":"2018-04-24T09:15:41Z","updated_at":"2018-10-22T11:37:28Z","closed_at":null,"author_association":"CONTRIBUTOR","pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/21139","html_url":"https://github.com/apache/spark/pull/21139","diff_url":"https://github.com/apache/spark/pull/21139.diff","patch_url":"https://github.com/apache/spark/pull/21139.patch"},"body":"## What changes were proposed in this pull request?\r\nCurrently, when the order field of window function has a subset relationship, SparkSQL will randomly generate different physical plan.\r\nSimilar like:\r\n\r\n```\r\ncase class DistinctAgg(a: Int, b: Float, c: Double, d: Int, e: String)\r\nval df = spark.sparkContext.parallelize(\r\n      DistinctAgg(8, 2, 3, 4, \"a\") ::\r\n      DistinctAgg(9, 3, 4, 5, \"b\") ::\r\n      DistinctAgg(3, 4, 5, 6, \"c\") ::\r\n      DistinctAgg(3, 4, 5, 7, \"c\") ::\r\n      DistinctAgg(3, 4, 5, 8, \"c\") ::\r\n      DistinctAgg(3, 6, 6, 9, \"d\") ::\r\n      DistinctAgg(30, 40, 50, 60, \"e\") ::\r\n      DistinctAgg(41, 51, 61, 71, null) ::\r\n      DistinctAgg(42, 52, 62, 72, null) ::\r\n      DistinctAgg(43, 53, 63, 73, \"k\") ::Nil).toDF()\r\ndf.createOrReplaceTempView(\"distinctAgg\")\r\n\r\nselect a, b, c, \r\navg(b) over(partition by a order by b) as sumIb, \r\nsum(d) over(partition by a order by b, c) as sumId, d \r\nfrom distinctAgg\t\r\n\r\n```\r\nThe physics plan will produce different results randomly.\t\t\r\n**One**: there is only one sort of physical plan\t \r\n```\r\n== Physical Plan ==\r\n*(3) Project [a#181, b#182, c#183, sumId#210L, sumIb#209L, d#184]\r\n+- Window [sum(cast(b#182 as bigint)) windowspecdefinition(a#181, b#182 ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS sumIb#209L], [a#181], [b#182 ASC NULLS FIRST]\r\n   +- Window [sum(cast(d#184 as bigint)) windowspecdefinition(a#181, b#182 ASC NULLS FIRST, c#183 ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS sumId#210L], [a#181], [b#182 ASC NULLS FIRST, c#183 ASC NULLS FIRST]\r\n      +- *(2) Sort [a#181 ASC NULLS FIRST, b#182 ASC NULLS FIRST, c#183 ASC NULLS FIRST], false, 0\r\n         +- Exchange hashpartitioning(a#181, 5)\r\n            +- *(1) Project [a#181, b#182, c#183, d#184]\r\n               +- *(1) SerializeFromObject [assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).a AS a#181, assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).b AS b#182, assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).c AS c#183, assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).d AS d#184, staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).e, true, false) AS e#185]\r\n                  +- Scan ExternalRDDScan[obj#180]\r\n\r\n```\r\n**Another one**: there is two sort of physical plans\r\n```\r\n== Physical Plan ==\r\n*(4) Project [a#181, b#182, c#183, sumId#210L, sumIb#209L, d#184]\r\n+- Window [sum(cast(d#184 as bigint)) windowspecdefinition(a#181, b#182 ASC NULLS FIRST, c#183 ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS sumId#210L], [a#181], [b#182 ASC NULLS FIRST, c#183 ASC NULLS FIRST]\r\n   +- *(3) Sort [a#181 ASC NULLS FIRST, b#182 ASC NULLS FIRST, c#183 ASC NULLS FIRST], false, 0\r\n      +- Window [sum(cast(b#182 as bigint)) windowspecdefinition(a#181, b#182 ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS sumIb#209L], [a#181], [b#182 ASC NULLS FIRST]\r\n         +- *(2) Sort [a#181 ASC NULLS FIRST, b#182 ASC NULLS FIRST], false, 0\r\n            +- Exchange hashpartitioning(a#181, 5)\r\n               +- *(1) Project [a#181, b#182, c#183, d#184]\r\n                  +- *(1) SerializeFromObject [assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).a AS a#181, assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).b AS b#182, assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).c AS c#183, assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).d AS d#184, staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, assertnotnull(input[0, org.apache.spark.sql.test.SQLTestData$DistinctAgg, true]).e, true, false) AS e#185]\r\n                     +- Scan ExternalRDDScan[obj#180]\r\n\r\n```\r\nthis PR add an exchange rule to ensure that no redundant physical plan SortExec is generated.\r\n\r\n## How was this patch tested?\r\n\r\nadd new unit tests.\r\n"},"comment":{"url":"https://api.github.com/repos/apache/spark/issues/comments/431809829","html_url":"https://github.com/apache/spark/pull/21139#issuecomment-431809829","issue_url":"https://api.github.com/repos/apache/spark/issues/21139","id":431809829,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMTgwOTgyOQ==","user":{"login":"AmplabJenkins","id":4031291,"node_id":"MDQ6VXNlcjQwMzEyOTE=","avatar_url":"https://avatars3.githubusercontent.com/u/4031291?v=4","gravatar_id":"","url":"https://api.github.com/users/AmplabJenkins","html_url":"https://github.com/AmplabJenkins","followers_url":"https://api.github.com/users/AmplabJenkins/followers","following_url":"https://api.github.com/users/AmplabJenkins/following{/other_user}","gists_url":"https://api.github.com/users/AmplabJenkins/gists{/gist_id}","starred_url":"https://api.github.com/users/AmplabJenkins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AmplabJenkins/subscriptions","organizations_url":"https://api.github.com/users/AmplabJenkins/orgs","repos_url":"https://api.github.com/users/AmplabJenkins/repos","events_url":"https://api.github.com/users/AmplabJenkins/events{/privacy}","received_events_url":"https://api.github.com/users/AmplabJenkins/received_events","type":"User","site_admin":false},"created_at":"2018-10-22T11:37:28Z","updated_at":"2018-10-22T11:37:28Z","author_association":"NONE","body":"Can one of the admins verify this patch?"}}
{"actor":{"display_login":"AmplabJenkins"}}