|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.execution |
| 19 | + |
| 20 | +import org.apache.spark.sql.{DataFrame, QueryTest} |
| 21 | +import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanHelper, DisableAdaptiveExecutionSuite, EnableAdaptiveExecutionSuite} |
| 22 | +import org.apache.spark.sql.internal.SQLConf |
| 23 | +import org.apache.spark.sql.test.SharedSparkSession |
| 24 | + |
| 25 | + |
| 26 | +abstract class RemoveRedundantSortsSuiteBase |
| 27 | + extends QueryTest |
| 28 | + with SharedSparkSession |
| 29 | + with AdaptiveSparkPlanHelper { |
| 30 | + import testImplicits._ |
| 31 | + |
| 32 | + private def checkNumSorts(df: DataFrame, count: Int): Unit = { |
| 33 | + val plan = df.queryExecution.executedPlan |
| 34 | + assert(collectWithSubqueries(plan) { case s: SortExec => s }.length == count) |
| 35 | + } |
| 36 | + |
| 37 | + private def checkSorts(query: String, enabledCount: Int, disabledCount: Int): Unit = { |
| 38 | + withSQLConf(SQLConf.REMOVE_REDUNDANT_SORTS_ENABLED.key -> "true") { |
| 39 | + val df = sql(query) |
| 40 | + checkNumSorts(df, enabledCount) |
| 41 | + val result = df.collect() |
| 42 | + withSQLConf(SQLConf.REMOVE_REDUNDANT_SORTS_ENABLED.key -> "false") { |
| 43 | + val df = sql(query) |
| 44 | + checkNumSorts(df, disabledCount) |
| 45 | + checkAnswer(df, result) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + test("remove redundant sorts with limit") { |
| 51 | + withTempView("t") { |
| 52 | + spark.range(100).select('id as "key").createOrReplaceTempView("t") |
| 53 | + val query = |
| 54 | + """ |
| 55 | + |SELECT key FROM |
| 56 | + | (SELECT key FROM t WHERE key > 10 ORDER BY key DESC LIMIT 10) |
| 57 | + |ORDER BY key DESC |
| 58 | + |""".stripMargin |
| 59 | + checkSorts(query, 0, 1) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + test("remove redundant sorts with broadcast hash join") { |
| 64 | + withTempView("t1", "t2") { |
| 65 | + spark.range(1000).select('id as "key").createOrReplaceTempView("t1") |
| 66 | + spark.range(1000).select('id as "key").createOrReplaceTempView("t2") |
| 67 | + val queryTemplate = """ |
| 68 | + |SELECT t1.key FROM |
| 69 | + | (SELECT key FROM t1 WHERE key > 10 ORDER BY key DESC LIMIT 10) t1 |
| 70 | + |%s |
| 71 | + | (SELECT key FROM t2 WHERE key > 50 ORDER BY key DESC LIMIT 100) t2 |
| 72 | + |ON t1.key = t2.key |
| 73 | + |ORDER BY %s |
| 74 | + """.stripMargin |
| 75 | + |
| 76 | + val innerJoinAsc = queryTemplate.format("JOIN", "t2.key ASC") |
| 77 | + checkSorts(innerJoinAsc, 1, 1) |
| 78 | + |
| 79 | + val innerJoinDesc = queryTemplate.format("JOIN", "t2.key DESC") |
| 80 | + checkSorts(innerJoinDesc, 0, 1) |
| 81 | + |
| 82 | + val innerJoinDesc1 = queryTemplate.format("JOIN", "t1.key DESC") |
| 83 | + checkSorts(innerJoinDesc1, 1, 1) |
| 84 | + |
| 85 | + val leftOuterJoinDesc = queryTemplate.format("LEFT JOIN", "t1.key DESC") |
| 86 | + checkSorts(leftOuterJoinDesc, 0, 1) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + test("remove redundant sorts with sort merge join") { |
| 91 | + withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") { |
| 92 | + withTempView("t1", "t2") { |
| 93 | + spark.range(1000).select('id as "key").createOrReplaceTempView("t1") |
| 94 | + spark.range(1000).select('id as "key").createOrReplaceTempView("t2") |
| 95 | + val query = """ |
| 96 | + |SELECT t1.key FROM |
| 97 | + | (SELECT key FROM t1 WHERE key > 10 ORDER BY key DESC LIMIT 10) t1 |
| 98 | + |JOIN |
| 99 | + | (SELECT key FROM t2 WHERE key > 50 ORDER BY key DESC LIMIT 100) t2 |
| 100 | + |ON t1.key = t2.key |
| 101 | + |ORDER BY t1.key |
| 102 | + """.stripMargin |
| 103 | + |
| 104 | + val queryAsc = query + " ASC" |
| 105 | + checkSorts(queryAsc, 2, 3) |
| 106 | + |
| 107 | + // Top level sort should only be eliminated if it's order is descending with SMJ. |
| 108 | + val queryDesc = query + " DESC" |
| 109 | + checkSorts(queryDesc, 3, 3) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + test("cached sorted data doesn't need to be re-sorted") { |
| 115 | + withSQLConf(SQLConf.REMOVE_REDUNDANT_SORTS_ENABLED.key -> "true") { |
| 116 | + val df = spark.range(1000).select('id as "key").sort('key.desc).cache() |
| 117 | + val resorted = df.sort('key.desc) |
| 118 | + val sortedAsc = df.sort('key.asc) |
| 119 | + checkNumSorts(df, 0) |
| 120 | + checkNumSorts(resorted, 0) |
| 121 | + checkNumSorts(sortedAsc, 1) |
| 122 | + val result = resorted.collect() |
| 123 | + withSQLConf(SQLConf.REMOVE_REDUNDANT_SORTS_ENABLED.key -> "false") { |
| 124 | + val resorted = df.sort('key.desc) |
| 125 | + checkNumSorts(resorted, 1) |
| 126 | + checkAnswer(resorted, result) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +class RemoveRedundantSortsSuite extends RemoveRedundantSortsSuiteBase |
| 133 | + with DisableAdaptiveExecutionSuite |
| 134 | + |
| 135 | +class RemoveRedundantSortsSuiteAE extends RemoveRedundantSortsSuiteBase |
| 136 | + with EnableAdaptiveExecutionSuite |
0 commit comments