|
| 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.analysis |
| 19 | + |
| 20 | +import org.apache.spark.SparkConf |
| 21 | +import org.apache.spark.sql.SparkSessionExtensions |
| 22 | +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan |
| 23 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 24 | +import org.apache.spark.sql.test.SharedSparkSession |
| 25 | + |
| 26 | +class AnalysisConfOverrideSuite extends SharedSparkSession { |
| 27 | + |
| 28 | + override protected def sparkConf: SparkConf = { |
| 29 | + super.sparkConf |
| 30 | + .set("spark.sql.extensions", "com.databricks.sql.ConfOverrideValidationExtensions") |
| 31 | + } |
| 32 | + |
| 33 | + override def beforeAll(): Unit = { |
| 34 | + super.beforeAll() |
| 35 | + spark.sql("SELECT id as a FROM range(10)").createTempView("table") |
| 36 | + spark.sql("SELECT id as a, (id + 1) as b FROM range(10)").createTempView("table2") |
| 37 | + } |
| 38 | + |
| 39 | + override def afterAll(): Unit = { |
| 40 | + spark.catalog.dropTempView("table") |
| 41 | + spark.catalog.dropTempView("table2") |
| 42 | + super.afterAll() |
| 43 | + } |
| 44 | + |
| 45 | + private def testOverride(testName: String)(f: (String, String) => Unit): Unit = { |
| 46 | + test(testName) { |
| 47 | + val key = "spark.sql.catalog.x.y" |
| 48 | + val value = "true" |
| 49 | + withSQLConf(key -> value) { |
| 50 | + f |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + testOverride("simple plan") { case (key, value) => |
| 56 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 57 | + spark.sql("SELECT * FROM TaBlE") |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + testOverride("CTE") { case (key, value) => |
| 62 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 63 | + spark.sql( |
| 64 | + """WITH cte AS (SELECT * FROM TaBlE) |
| 65 | + |SELECT * FROM cte |
| 66 | + |""".stripMargin) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + testOverride("Subquery") { case (key, value) => |
| 71 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 72 | + spark.sql( |
| 73 | + """ |
| 74 | + |SELECT * FROM TaBlE WHERE a in (SELECT a FROM table2) |
| 75 | + |""".stripMargin) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + testOverride("View") { case (key, value) => |
| 80 | + withTable("test_table", "test_table2") { |
| 81 | + spark.sql("CREATE TABLE test_table AS SELECT id as a FROM range(10)") |
| 82 | + spark.sql("CREATE TABLE test_table2 AS SELECT id as a, (id + 1) as b FROM range(10)") |
| 83 | + withView("test_view") { |
| 84 | + spark.sql("CREATE VIEW test_view AS " + |
| 85 | + "SELECT * FROM test_table WHERE a in (SELECT a FROM test_table2)") |
| 86 | + |
| 87 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 88 | + spark.sql("SELECT * FROM test_view") |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + testOverride("user defined SQL functions") { case (key, value) => |
| 95 | + withTable("test_table", "test_table2") { |
| 96 | + spark.sql("CREATE TABLE test_table AS SELECT id as a FROM range(10)") |
| 97 | + spark.sql("CREATE TABLE test_table2 AS SELECT id as a, (id + 1) as b FROM range(10)") |
| 98 | + withUserDefinedFunction("f1" -> true, "f2" -> false, "f3" -> false) { |
| 99 | + spark.sql( |
| 100 | + """CREATE OR REPLACE TEMPORARY FUNCTION f1() RETURNS TABLE (a bigint) |
| 101 | + |RETURN SELECT * FROM test_table WHERE a in (SELECT a FROM test_table2) |
| 102 | + |""".stripMargin |
| 103 | + ) |
| 104 | + spark.sql( |
| 105 | + """CREATE OR REPLACE FUNCTION f2() RETURNS TABLE (a bigint) |
| 106 | + |RETURN SELECT * FROM test_table WHERE a in (SELECT a FROM test_table2) |
| 107 | + |""".stripMargin |
| 108 | + ) |
| 109 | + spark.sql( |
| 110 | + """CREATE OR REPLACE FUNCTION f3(in bigint) RETURNS (out bigint) |
| 111 | + |RETURN in + 1 |
| 112 | + |""".stripMargin |
| 113 | + ) |
| 114 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 115 | + spark.sql("SELECT * FROM f1()") |
| 116 | + } |
| 117 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 118 | + spark.sql("SELECT * FROM f2()") |
| 119 | + } |
| 120 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 121 | + spark.sql("SELECT f3(1)") |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + testOverride("user defined SQL functions - test conf disabled") { case (key, value) => |
| 128 | + withTable("test_table", "test_table2") { |
| 129 | + spark.sql("CREATE TABLE test_table AS SELECT id as a FROM range(10)") |
| 130 | + spark.sql("CREATE TABLE test_table2 AS SELECT id as a, (id + 1) as b FROM range(10)") |
| 131 | + // turn the flag off to maintain former behavior |
| 132 | + withSQLConf("spark.sql.analyzer.sqlFunctionResolution.applyConfOverrides" -> "false") { |
| 133 | + withUserDefinedFunction("f1" -> true, "f2" -> false, "f3" -> false) { |
| 134 | + spark.sql( |
| 135 | + """CREATE OR REPLACE TEMPORARY FUNCTION f1() RETURNS TABLE (a bigint) |
| 136 | + |RETURN SELECT * FROM test_table WHERE a in (SELECT a FROM test_table2) |
| 137 | + |""".stripMargin |
| 138 | + ) |
| 139 | + spark.sql( |
| 140 | + """CREATE OR REPLACE FUNCTION f2() RETURNS TABLE (a bigint) |
| 141 | + |RETURN SELECT * FROM test_table WHERE a in (SELECT a FROM test_table2) |
| 142 | + |""".stripMargin |
| 143 | + ) |
| 144 | + spark.sql( |
| 145 | + """CREATE OR REPLACE FUNCTION f3(in bigint) RETURNS (out bigint) |
| 146 | + |RETURN in + 1 |
| 147 | + |""".stripMargin |
| 148 | + ) |
| 149 | + intercept[AssertionError] { |
| 150 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 151 | + spark.sql("SELECT * FROM f1()") |
| 152 | + } |
| 153 | + } |
| 154 | + intercept[AssertionError] { |
| 155 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 156 | + spark.sql("SELECT * FROM f2()") |
| 157 | + } |
| 158 | + } |
| 159 | + intercept[AssertionError] { |
| 160 | + ValidateConfOverrideRule.withConfValidationEnabled(key, value) { |
| 161 | + spark.sql("SELECT f3(1)") |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/** Utility singleton object to orchestrate the test. */ |
| 171 | +object ValidateConfOverrideRule { |
| 172 | + private var confToCheck: Option[(String, String)] = None |
| 173 | + private var isCalled: Boolean = false |
| 174 | + |
| 175 | + def withConfValidationEnabled(key: String, value: String)(f: => Unit): Unit = { |
| 176 | + try { |
| 177 | + confToCheck = Some(key -> value) |
| 178 | + f |
| 179 | + assert(isCalled, "The rule was enabled, but not called. This is a test setup error.") |
| 180 | + } finally { |
| 181 | + isCalled = false |
| 182 | + confToCheck = None |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +class ValidateConfOverrideRule extends Rule[LogicalPlan] { |
| 188 | + override def apply(plan: LogicalPlan): LogicalPlan = { |
| 189 | + ValidateConfOverrideRule.confToCheck.foreach { case (k, v) => |
| 190 | + assert(conf.getConfString(k) == v, |
| 191 | + s"The feature wasn't enabled within plan:\n$plan") |
| 192 | + ValidateConfOverrideRule.isCalled = true |
| 193 | + } |
| 194 | + plan |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +class ConfOverrideValidationExtensions extends (SparkSessionExtensions => Unit) { |
| 199 | + override def apply(extensions: SparkSessionExtensions): Unit = { |
| 200 | + extensions.injectResolutionRule(_ => new ValidateConfOverrideRule) |
| 201 | + } |
| 202 | +} |
0 commit comments