|
| 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.sources |
| 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.execution.exchange.ShuffleExchangeExec |
| 23 | +import org.apache.spark.sql.internal.SQLConf |
| 24 | +import org.apache.spark.sql.internal.StaticSQLConf.CATALOG_IMPLEMENTATION |
| 25 | +import org.apache.spark.sql.test.{SharedSparkSession, SQLTestUtils} |
| 26 | + |
| 27 | +class ExchangePushDownThroughAggregateWithoutHiveSupportSuite |
| 28 | + extends ExchangePushDownThroughAggregateSuite |
| 29 | + with SharedSparkSession |
| 30 | + with DisableAdaptiveExecutionSuite { |
| 31 | + |
| 32 | + protected override def beforeAll(): Unit = { |
| 33 | + super.beforeAll() |
| 34 | + assert(spark.sparkContext.conf.get(CATALOG_IMPLEMENTATION) == "in-memory") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class ExchangePushDownThroughAggregateWithoutHiveSupportSuiteAE |
| 39 | + extends ExchangePushDownThroughAggregateSuite |
| 40 | + with SharedSparkSession |
| 41 | + with EnableAdaptiveExecutionSuite { |
| 42 | + |
| 43 | + protected override def beforeAll(): Unit = { |
| 44 | + super.beforeAll() |
| 45 | + assert(spark.sparkContext.conf.get(CATALOG_IMPLEMENTATION) == "in-memory") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +abstract class ExchangePushDownThroughAggregateSuite extends QueryTest |
| 50 | + with SQLTestUtils with AdaptiveSparkPlanHelper { |
| 51 | + |
| 52 | +// protected override def beforeAll(): Unit = { |
| 53 | +// super.beforeAll() |
| 54 | +// assert(spark.sparkContext.conf.get(CATALOG_IMPLEMENTATION) == "in-memory") |
| 55 | +// } |
| 56 | + |
| 57 | + import testImplicits._ |
| 58 | + |
| 59 | + private lazy val df1 = |
| 60 | + (0 until 50).map(i => (i % 2, i % 4, i % 8)).toDF("i", "j", "k").as("df1") |
| 61 | + private lazy val df2 = |
| 62 | + (0 until 50).map(i => (i % 3, i % 6, i % 9)).toDF("i", "j", "k").as("df2") |
| 63 | + private lazy val df3 = |
| 64 | + (0 until 50).map(i => (i % 3, i % 6, i % 9)).toDF("l", "m", "n").as("df3") |
| 65 | + |
| 66 | + private def checkExchangePushDown(query: String, |
| 67 | + shuffleCountIfEnable: Int, |
| 68 | + shuffleCountIfDisable: Int): Unit = { |
| 69 | + |
| 70 | + def checkExchangePushDownResult(query: String, enabled: Boolean, |
| 71 | + expectedNumShuffle: Int): DataFrame = { |
| 72 | + val df = sql(query) |
| 73 | + df.collect() |
| 74 | + |
| 75 | + val plan = df.queryExecution.executedPlan |
| 76 | + |
| 77 | + // scalastyle:off println |
| 78 | + println(s"query: ${query},\nenabled: ${enabled},\nplan: ${plan}") |
| 79 | + // scalastyle:on println |
| 80 | + |
| 81 | + val shuffles = collect(plan) { case s: ShuffleExchangeExec => s } |
| 82 | + assert(shuffles.length == expectedNumShuffle) |
| 83 | + df |
| 84 | + } |
| 85 | + |
| 86 | + withSQLConf(SQLConf.EXCHANGE_PUSH_DOWN_THROUGH_AGGREGATE_ENABLED.key -> "true") { |
| 87 | + val result = checkExchangePushDownResult(query, true, shuffleCountIfEnable) |
| 88 | + |
| 89 | + withSQLConf(SQLConf.EXCHANGE_PUSH_DOWN_THROUGH_AGGREGATE_ENABLED.key -> "false") { |
| 90 | + val result2 = checkExchangePushDownResult(query, false, shuffleCountIfDisable) |
| 91 | + checkAnswer(result, result2) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + test("Exchange push down through aggregate - basic test") { |
| 98 | + withTable("bucket_table1", "normal_table1", "normal_table2") { |
| 99 | + df1.write.format("parquet").bucketBy(8, "i"). |
| 100 | + saveAsTable("bucket_table1") |
| 101 | + df2.write.format("parquet").saveAsTable("normal_table1") |
| 102 | + df3.write.format("parquet").saveAsTable("normal_table2") |
| 103 | + // df1.write.format("parquet").saveAsTable("t3") |
| 104 | + |
| 105 | + Seq( |
| 106 | +// ( |
| 107 | +// """ |
| 108 | +// |select * from (select i, j, k from |
| 109 | +// |(select distinct i, j, k from |
| 110 | +// |(select i, j, k from bucket_table1 cluster by j) |
| 111 | +// | t2 ) t1 cluster by j) t0 order by i, j, k |
| 112 | +// |""".stripMargin, 1, 2), |
| 113 | + |
| 114 | +// ( |
| 115 | +// """ |
| 116 | +// |select * from (select i, j, k from |
| 117 | +// |(select distinct i, j, k from bucket_table1 |
| 118 | +// | t2 ) t1 cluster by j ) t0 order by i, j, k |
| 119 | +// |""".stripMargin, 1, 1), |
| 120 | + |
| 121 | + // basic test for bucket table |
| 122 | + ( |
| 123 | + """ |
| 124 | + |select l, i, j, k from |
| 125 | + |normal_table2 t0 left join (select distinct i, j, k from bucket_table1 |
| 126 | + |) t1 on t0.l = t1.j |
| 127 | + |""".stripMargin, 2, 2), |
| 128 | + ( |
| 129 | + """ |
| 130 | + |select l, i, j, k from |
| 131 | + |normal_table2 t0 left join ( |
| 132 | + |select i, j, k, count(*) as c from bucket_table1 group by 1, 2, 3 |
| 133 | + |) t1 on t0.l = t1.c |
| 134 | + |""".stripMargin, 2, 2), // No support since clustered on c instead of i, j, k |
| 135 | + // with filter |
| 136 | + ( |
| 137 | + """ |
| 138 | + |select l, i, j, k from |
| 139 | + |normal_table2 t0 left join (select distinct i, j, k from bucket_table1 |
| 140 | + |where i > 0) t1 on t0.l = t1.j |
| 141 | + |""".stripMargin, 2, 2), |
| 142 | + // with one aggregate function |
| 143 | + ( |
| 144 | + """ |
| 145 | + |select l, i, j, k, mi from |
| 146 | + |normal_table2 t0 left join (select distinct i, j, k, max(i) as mi from bucket_table1 |
| 147 | + |where i > 0 group by 1, 2, 3) t1 on t0.l = t1.j |
| 148 | + |""".stripMargin, 2, 2), |
| 149 | + // with 2 aggregate function |
| 150 | + ( |
| 151 | + """ |
| 152 | + |select l, i, j, k, mi, cnt from |
| 153 | + |normal_table2 t0 left join |
| 154 | + |(select distinct i, j, k, max(i) as mi, count(1) as cnt from bucket_table1 |
| 155 | + |where i > 0 group by 1, 2, 3) t1 on t0.l = t1.j |
| 156 | + |""".stripMargin, 2, 2), |
| 157 | + |
| 158 | + |
| 159 | + // basic test for normal table |
| 160 | + ( |
| 161 | + """ |
| 162 | + |select l, i, j, k from |
| 163 | + |normal_table2 t0 left join (select distinct i, j, k from normal_table1 |
| 164 | + |) t1 on t0.l = t1.j |
| 165 | + |""".stripMargin, 2, 3), |
| 166 | + // with filter |
| 167 | + ( |
| 168 | + """ |
| 169 | + |select l, i, j, k from |
| 170 | + |normal_table2 t0 left join (select distinct i, j, k from normal_table1 |
| 171 | + |where i > 0) t1 on t0.l = t1.j |
| 172 | + |""".stripMargin, 2, 3), |
| 173 | + // with one aggregate function |
| 174 | + ( |
| 175 | + """ |
| 176 | + |select l, i, j, k, mi from |
| 177 | + |normal_table2 t0 left join (select distinct i, j, k, max(i) as mi from normal_table1 |
| 178 | + |where i > 0 group by 1, 2, 3) t1 on t0.l = t1.j |
| 179 | + |""".stripMargin, 2, 3), |
| 180 | + // with 2 aggregate function |
| 181 | + ( |
| 182 | + """ |
| 183 | + |select l, i, j, k, mi, cnt from |
| 184 | + |normal_table2 t0 left join |
| 185 | + |(select distinct i, j, k, max(i) as mi, count(1) as cnt from normal_table1 |
| 186 | + |where i > 0 group by 1, 2, 3) t1 on t0.l = t1.j |
| 187 | + |""".stripMargin, 2, 3) |
| 188 | + ).foreach { case (query, shuffleCountIfEnable, shuffleCountIfDisable) => |
| 189 | + withSQLConf(SQLConf.AUTO_BUCKETED_SCAN_ENABLED.key -> "true", |
| 190 | + SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1") { |
| 191 | + checkExchangePushDown(query, shuffleCountIfEnable, shuffleCountIfDisable) |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments