|
| 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 | +package org.apache.gluten.sql |
| 18 | + |
| 19 | +import org.apache.gluten.backendsapi.BackendsApiManager |
| 20 | +import org.apache.gluten.config.GlutenConfig |
| 21 | +import org.apache.gluten.execution.WholeStageTransformerSuite |
| 22 | + |
| 23 | +import org.apache.spark.sql.catalyst.FunctionIdentifier |
| 24 | +import org.apache.spark.sql.catalyst.expressions.BloomFilterMightContain |
| 25 | +import org.apache.spark.sql.catalyst.expressions.ExpressionInfo |
| 26 | +import org.apache.spark.sql.catalyst.expressions.aggregate.BloomFilterAggregate |
| 27 | +import org.apache.spark.sql.internal.SQLConf |
| 28 | + |
| 29 | +/** |
| 30 | + * Regression tests for https://github.com/apache/gluten/issues/12013. |
| 31 | + * |
| 32 | + * Verifies that `BloomFilterMightContainJointRewriteRule`, registered as a `Rule[LogicalPlan]` via |
| 33 | + * `injectOptimizerRule`, correctly handles whole-stage AQE fallback scenarios where one or both |
| 34 | + * bloom-filter stages revert to vanilla Spark execution. |
| 35 | + */ |
| 36 | +class GlutenBloomFilterFallbackSuite extends WholeStageTransformerSuite { |
| 37 | + protected val resourcePath: String = null |
| 38 | + protected val fileFormat: String = null |
| 39 | + |
| 40 | + import testImplicits._ |
| 41 | + |
| 42 | + private val funcIdBloomFilterAgg = FunctionIdentifier("bloom_filter_agg") |
| 43 | + private val funcIdMightContain = FunctionIdentifier("might_contain") |
| 44 | + |
| 45 | + override def beforeAll(): Unit = { |
| 46 | + super.beforeAll() |
| 47 | + spark.sessionState.functionRegistry.registerFunction( |
| 48 | + funcIdBloomFilterAgg, |
| 49 | + new ExpressionInfo(classOf[BloomFilterAggregate].getName, "bloom_filter_agg"), |
| 50 | + args => |
| 51 | + args.size match { |
| 52 | + case 1 => new BloomFilterAggregate(args(0)) |
| 53 | + case 2 => new BloomFilterAggregate(args(0), args(1)) |
| 54 | + case 3 => new BloomFilterAggregate(args(0), args(1), args(2)) |
| 55 | + case _ => throw new IllegalArgumentException("bloom_filter_agg requires 1-3 arguments") |
| 56 | + } |
| 57 | + ) |
| 58 | + spark.sessionState.functionRegistry.registerFunction( |
| 59 | + funcIdMightContain, |
| 60 | + new ExpressionInfo(classOf[BloomFilterMightContain].getName, "might_contain"), |
| 61 | + args => BloomFilterMightContain(args(0), args(1))) |
| 62 | + } |
| 63 | + |
| 64 | + override def afterAll(): Unit = { |
| 65 | + spark.sessionState.functionRegistry.dropFunction(funcIdBloomFilterAgg) |
| 66 | + spark.sessionState.functionRegistry.dropFunction(funcIdMightContain) |
| 67 | + super.afterAll() |
| 68 | + } |
| 69 | + |
| 70 | + private val veloxBloomFilterMaxNumBits = 4194304L |
| 71 | + |
| 72 | + // GLUTEN-12013: only filter stage falls back (threshold=2). |
| 73 | + // bloom_filter_agg subquery runs natively and produces Velox-format bytes; the filter stage |
| 74 | + // falls back via ExpandFallbackPolicy. The optimizer-level substitution ensures the fallback |
| 75 | + // plan still uses VeloxBloomFilterMightContain so the JVM filter reads Velox-format bytes. |
| 76 | + test("GLUTEN-12013: bloom_filter_agg whole-stage fallback does not corrupt bloom filter bytes") { |
| 77 | + if (BackendsApiManager.getSettings.requireBloomFilterAggMightContainJointFallback()) { |
| 78 | + val table = "bloom_filter_test" |
| 79 | + val numEstimatedItems = 5000000L |
| 80 | + val sqlString = |
| 81 | + s""" |
| 82 | + |SELECT col positive_membership_test |
| 83 | + |FROM $table |
| 84 | + |WHERE might_contain( |
| 85 | + | (SELECT bloom_filter_agg(col, |
| 86 | + | cast($numEstimatedItems as long), |
| 87 | + | cast($veloxBloomFilterMaxNumBits as long)) |
| 88 | + | FROM $table), col) |
| 89 | + |""".stripMargin |
| 90 | + withTempView(table) { |
| 91 | + (Seq(Long.MinValue, 0, Long.MaxValue) ++ (1L to 200000L)) |
| 92 | + .toDF("col") |
| 93 | + .createOrReplaceTempView(table) |
| 94 | + // Threshold=2: FilterExec fallback cost=2 triggers whole-stage fallback; agg cost=1 |
| 95 | + // does not, so Stage 0 runs natively. ANSI off keeps agg cost at 1 on Spark 4.0+. |
| 96 | + withSQLConf( |
| 97 | + GlutenConfig.COLUMNAR_FILTER_ENABLED.key -> "false", |
| 98 | + GlutenConfig.COLUMNAR_WHOLESTAGE_FALLBACK_THRESHOLD.key -> "2", |
| 99 | + SQLConf.ANSI_ENABLED.key -> "false" |
| 100 | + ) { |
| 101 | + val df = spark.sql(sqlString) |
| 102 | + // Must not throw: java.io.IOException: Unexpected Bloom filter version number. |
| 103 | + assert(df.collect().length == 200003) |
| 104 | + // Verify the optimizer rule ran: VeloxBloomFilterMightContain must be present even |
| 105 | + // though Stage 1 executes inside a FallbackNode. |
| 106 | + assert( |
| 107 | + df.queryExecution.optimizedPlan.toString.contains("velox_might_contain"), |
| 108 | + "Expected velox_might_contain in optimized plan -- optimizer rule may not have run" |
| 109 | + ) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // GLUTEN-12013: both stages fall back (threshold=1). |
| 116 | + // Stage 0's inherent transition cost of 1 meets the threshold so ExpandFallbackPolicy |
| 117 | + // promotes it to a whole-stage fallback too. The optimizer rule has already rewritten both |
| 118 | + // sides to Velox variants before ExpandFallbackPolicy captures its snapshot. Even in JVM |
| 119 | + // row-mode, VeloxBloomFilterAggregate produces Velox-format bytes (via JNI) and |
| 120 | + // VeloxBloomFilterMightContain consumes them -- both sides are consistent. |
| 121 | + test("GLUTEN-12013: bloom_filter_agg whole-stage fallback when both stages fall back") { |
| 122 | + if (BackendsApiManager.getSettings.requireBloomFilterAggMightContainJointFallback()) { |
| 123 | + val table = "bloom_filter_test" |
| 124 | + val numEstimatedItems = 5000000L |
| 125 | + val sqlString = |
| 126 | + s""" |
| 127 | + |SELECT col positive_membership_test |
| 128 | + |FROM $table |
| 129 | + |WHERE might_contain( |
| 130 | + | (SELECT bloom_filter_agg(col, |
| 131 | + | cast($numEstimatedItems as long), |
| 132 | + | cast($veloxBloomFilterMaxNumBits as long)) |
| 133 | + | FROM $table), col) |
| 134 | + |""".stripMargin |
| 135 | + withTempView(table) { |
| 136 | + (Seq(Long.MinValue, 0, Long.MaxValue) ++ (1L to 200000L)) |
| 137 | + .toDF("col") |
| 138 | + .createOrReplaceTempView(table) |
| 139 | + // Threshold=1: both stages fall back; both use Velox variants via JNI. |
| 140 | + withSQLConf( |
| 141 | + GlutenConfig.COLUMNAR_FILTER_ENABLED.key -> "false", |
| 142 | + GlutenConfig.COLUMNAR_WHOLESTAGE_FALLBACK_THRESHOLD.key -> "1", |
| 143 | + SQLConf.ANSI_ENABLED.key -> "false" |
| 144 | + ) { |
| 145 | + val df = spark.sql(sqlString) |
| 146 | + // Must not throw: java.io.IOException: Unexpected Bloom filter version number. |
| 147 | + assert(df.collect().length == 200003) |
| 148 | + // Verify the optimizer rule ran on both sides. |
| 149 | + assert( |
| 150 | + df.queryExecution.optimizedPlan.toString.contains("velox_might_contain"), |
| 151 | + "Expected velox_might_contain in optimized plan -- optimizer rule may not have run" |
| 152 | + ) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // GLUTEN-12013: native bloom filter disabled -- early-exit path of the optimizer rule. |
| 159 | + // When spark.gluten.sql.native.bloomFilter=false the rule returns the plan unchanged. |
| 160 | + // BloomFilterAggregate / BloomFilterMightContain remain as vanilla Spark expressions and |
| 161 | + // produce/consume consistent Spark-format bytes. |
| 162 | + test( |
| 163 | + "GLUTEN-12013: native bloom filter disabled skips rewrite and produces correct results") { |
| 164 | + if (BackendsApiManager.getSettings.requireBloomFilterAggMightContainJointFallback()) { |
| 165 | + val table = "bloom_filter_test" |
| 166 | + val numEstimatedItems = 5000000L |
| 167 | + val sqlString = |
| 168 | + s""" |
| 169 | + |SELECT col positive_membership_test |
| 170 | + |FROM $table |
| 171 | + |WHERE might_contain( |
| 172 | + | (SELECT bloom_filter_agg(col, |
| 173 | + | cast($numEstimatedItems as long), |
| 174 | + | cast($veloxBloomFilterMaxNumBits as long)) |
| 175 | + | FROM $table), col) |
| 176 | + |""".stripMargin |
| 177 | + withTempView(table) { |
| 178 | + (Seq(Long.MinValue, 0, Long.MaxValue) ++ (1L to 200000L)) |
| 179 | + .toDF("col") |
| 180 | + .createOrReplaceTempView(table) |
| 181 | + withSQLConf( |
| 182 | + GlutenConfig.COLUMNAR_NATIVE_BLOOMFILTER_ENABLED.key -> "false", |
| 183 | + SQLConf.ANSI_ENABLED.key -> "false" |
| 184 | + ) { |
| 185 | + val df = spark.sql(sqlString) |
| 186 | + assert(df.collect().length == 200003) |
| 187 | + // Verify the rule early-exited: plan must NOT contain Velox variants. |
| 188 | + assert( |
| 189 | + !df.queryExecution.optimizedPlan.toString.contains("velox_might_contain"), |
| 190 | + "Expected vanilla BloomFilterMightContain when native bloom filter is disabled" |
| 191 | + ) |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments