|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.comet.exec |
| 21 | + |
| 22 | +import org.apache.spark.sql.CometTestBase |
| 23 | +import org.apache.spark.sql.comet.CometNativeScanExec |
| 24 | +import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanExec |
| 25 | + |
| 26 | +import org.apache.comet.CometConf |
| 27 | + |
| 28 | +/** Tests for Dynamic Partition Pruning (DPP) with native DataFusion scan. */ |
| 29 | +class CometDPPSuite extends CometTestBase { |
| 30 | + |
| 31 | + test("DPP with native_datafusion scan - basic join") { |
| 32 | + withSQLConf( |
| 33 | + CometConf.COMET_ENABLED.key -> "true", |
| 34 | + CometConf.COMET_EXEC_ENABLED.key -> "true", |
| 35 | + CometConf.COMET_NATIVE_SCAN_IMPL.key -> "native_datafusion") { |
| 36 | + withTempPath { dir => |
| 37 | + spark |
| 38 | + .range(10000) |
| 39 | + .selectExpr("id", "id % 100 as dim_key", "rand() as value") |
| 40 | + .write |
| 41 | + .mode("overwrite") |
| 42 | + .parquet(s"${dir.getCanonicalPath}/fact") |
| 43 | + spark |
| 44 | + .range(100) |
| 45 | + .selectExpr("id", "'name_' || id as name") |
| 46 | + .where("id < 10") |
| 47 | + .write |
| 48 | + .mode("overwrite") |
| 49 | + .parquet(s"${dir.getCanonicalPath}/dim") |
| 50 | + |
| 51 | + spark.read.parquet(s"${dir.getCanonicalPath}/fact").createOrReplaceTempView("fact") |
| 52 | + spark.read.parquet(s"${dir.getCanonicalPath}/dim").createOrReplaceTempView("dim") |
| 53 | + |
| 54 | + val df = spark.sql("SELECT f.*, d.name FROM fact f JOIN dim d ON f.dim_key = d.id") |
| 55 | + val result = df.collect() |
| 56 | + |
| 57 | + assert(result.forall(row => row.getLong(1) < 10)) |
| 58 | + |
| 59 | + val plan = df.queryExecution.executedPlan |
| 60 | + val hasNativeScan = plan.collect { case _: CometNativeScanExec => true }.nonEmpty || |
| 61 | + plan |
| 62 | + .collect { case a: AdaptiveSparkPlanExec => |
| 63 | + a.executedPlan.collect { case _: CometNativeScanExec => true }.nonEmpty |
| 64 | + } |
| 65 | + .exists(identity) |
| 66 | + assert(hasNativeScan, "Expected CometNativeScanExec in plan") |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + test("DPP auto-selection for queries with dynamic pruning filters") { |
| 72 | + withSQLConf( |
| 73 | + CometConf.COMET_ENABLED.key -> "true", |
| 74 | + CometConf.COMET_EXEC_ENABLED.key -> "true", |
| 75 | + CometConf.COMET_NATIVE_SCAN_IMPL.key -> "auto") { |
| 76 | + withTempPath { dir => |
| 77 | + spark |
| 78 | + .range(1000) |
| 79 | + .selectExpr("id", "id % 10 as dim_key") |
| 80 | + .write |
| 81 | + .mode("overwrite") |
| 82 | + .parquet(s"${dir.getCanonicalPath}/fact") |
| 83 | + spark |
| 84 | + .range(10) |
| 85 | + .selectExpr("id", "'name_' || id as name") |
| 86 | + .where("id < 5") |
| 87 | + .write |
| 88 | + .mode("overwrite") |
| 89 | + .parquet(s"${dir.getCanonicalPath}/dim") |
| 90 | + |
| 91 | + spark.read.parquet(s"${dir.getCanonicalPath}/fact").createOrReplaceTempView("fact2") |
| 92 | + spark.read.parquet(s"${dir.getCanonicalPath}/dim").createOrReplaceTempView("dim2") |
| 93 | + |
| 94 | + val result = spark |
| 95 | + .sql("SELECT f.*, d.name FROM fact2 f JOIN dim2 d ON f.dim_key = d.id") |
| 96 | + .collect() |
| 97 | + assert(result.forall(row => row.getLong(1) < 5)) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + test("DPP reduces output rows significantly") { |
| 103 | + withSQLConf( |
| 104 | + CometConf.COMET_ENABLED.key -> "true", |
| 105 | + CometConf.COMET_EXEC_ENABLED.key -> "true", |
| 106 | + CometConf.COMET_NATIVE_SCAN_IMPL.key -> "native_datafusion") { |
| 107 | + withTempPath { dir => |
| 108 | + val factRows = 100000 |
| 109 | + val dimRows = 1000 |
| 110 | + val selectivity = 0.01 |
| 111 | + |
| 112 | + spark |
| 113 | + .range(factRows) |
| 114 | + .selectExpr("id", s"id % $dimRows as dim_key", "rand() as value") |
| 115 | + .write |
| 116 | + .mode("overwrite") |
| 117 | + .parquet(s"${dir.getCanonicalPath}/fact") |
| 118 | + spark |
| 119 | + .range(dimRows) |
| 120 | + .selectExpr("id", "'name_' || id as name") |
| 121 | + .where(s"id < ${(dimRows * selectivity).toInt}") |
| 122 | + .write |
| 123 | + .mode("overwrite") |
| 124 | + .parquet(s"${dir.getCanonicalPath}/dim") |
| 125 | + |
| 126 | + spark.read.parquet(s"${dir.getCanonicalPath}/fact").createOrReplaceTempView("fact3") |
| 127 | + spark.read.parquet(s"${dir.getCanonicalPath}/dim").createOrReplaceTempView("dim3") |
| 128 | + |
| 129 | + val count = |
| 130 | + spark.sql("SELECT f.*, d.name FROM fact3 f JOIN dim3 d ON f.dim_key = d.id").count() |
| 131 | + val expectedMax = (factRows * selectivity * 2).toLong |
| 132 | + assert(count <= expectedMax, s"Expected at most $expectedMax rows with DPP, got $count") |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + test("DPP with multiple join conditions") { |
| 138 | + withSQLConf( |
| 139 | + CometConf.COMET_ENABLED.key -> "true", |
| 140 | + CometConf.COMET_EXEC_ENABLED.key -> "true", |
| 141 | + CometConf.COMET_NATIVE_SCAN_IMPL.key -> "native_datafusion") { |
| 142 | + withTempPath { dir => |
| 143 | + spark |
| 144 | + .range(1000) |
| 145 | + .selectExpr("id", "id % 10 as key1", "id % 5 as key2", "rand() as value") |
| 146 | + .write |
| 147 | + .mode("overwrite") |
| 148 | + .parquet(s"${dir.getCanonicalPath}/fact") |
| 149 | + spark |
| 150 | + .range(10) |
| 151 | + .selectExpr("id as key1", "'dim1_' || id as name1") |
| 152 | + .where("id < 3") |
| 153 | + .write |
| 154 | + .mode("overwrite") |
| 155 | + .parquet(s"${dir.getCanonicalPath}/dim1") |
| 156 | + spark |
| 157 | + .range(5) |
| 158 | + .selectExpr("id as key2", "'dim2_' || id as name2") |
| 159 | + .where("id < 2") |
| 160 | + .write |
| 161 | + .mode("overwrite") |
| 162 | + .parquet(s"${dir.getCanonicalPath}/dim2") |
| 163 | + |
| 164 | + spark.read.parquet(s"${dir.getCanonicalPath}/fact").createOrReplaceTempView("fact_multi") |
| 165 | + spark.read.parquet(s"${dir.getCanonicalPath}/dim1").createOrReplaceTempView("dim1") |
| 166 | + spark.read.parquet(s"${dir.getCanonicalPath}/dim2").createOrReplaceTempView("dim2") |
| 167 | + |
| 168 | + val result = spark |
| 169 | + .sql(""" |
| 170 | + SELECT f.*, d1.name1, d2.name2 FROM fact_multi f |
| 171 | + JOIN dim1 d1 ON f.key1 = d1.key1 JOIN dim2 d2 ON f.key2 = d2.key2 |
| 172 | + """) |
| 173 | + .collect() |
| 174 | + assert(result.forall(row => row.getLong(1) < 3 && row.getLong(2) < 2)) |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments