|
| 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.expressions |
| 19 | + |
| 20 | +import scala.collection.parallel.immutable.ParVector |
| 21 | + |
| 22 | +import org.apache.spark.SparkFunSuite |
| 23 | +import org.apache.spark.sql.catalyst.FunctionIdentifier |
| 24 | +import org.apache.spark.sql.catalyst.expressions.ExpressionInfo |
| 25 | +import org.apache.spark.sql.execution.HiveResult.hiveResultString |
| 26 | +import org.apache.spark.sql.internal.SQLConf |
| 27 | +import org.apache.spark.sql.test.SharedSparkSession |
| 28 | + |
| 29 | +class ExpressionInfoSuite extends SparkFunSuite with SharedSparkSession { |
| 30 | + |
| 31 | + test("Replace _FUNC_ in ExpressionInfo") { |
| 32 | + val info = spark.sessionState.catalog.lookupFunctionInfo(FunctionIdentifier("upper")) |
| 33 | + assert(info.getName === "upper") |
| 34 | + assert(info.getClassName === "org.apache.spark.sql.catalyst.expressions.Upper") |
| 35 | + assert(info.getUsage === "upper(str) - Returns `str` with all characters changed to uppercase.") |
| 36 | + assert(info.getExamples.contains("> SELECT upper('SparkSql');")) |
| 37 | + assert(info.getSince === "1.0.1") |
| 38 | + assert(info.getNote === "") |
| 39 | + assert(info.getExtended.contains("> SELECT upper('SparkSql');")) |
| 40 | + } |
| 41 | + |
| 42 | + test("group info in ExpressionInfo") { |
| 43 | + val info = spark.sessionState.catalog.lookupFunctionInfo(FunctionIdentifier("sum")) |
| 44 | + assert(info.getGroup === "agg_funcs") |
| 45 | + |
| 46 | + Seq("agg_funcs", "array_funcs", "datetime_funcs", "json_funcs", "map_funcs", "window_funcs") |
| 47 | + .foreach { groupName => |
| 48 | + val info = new ExpressionInfo( |
| 49 | + "testClass", null, "testName", null, "", "", "", groupName, "", "") |
| 50 | + assert(info.getGroup === groupName) |
| 51 | + } |
| 52 | + |
| 53 | + val errMsg = intercept[IllegalArgumentException] { |
| 54 | + val invalidGroupName = "invalid_group_funcs" |
| 55 | + new ExpressionInfo("testClass", null, "testName", null, "", "", "", invalidGroupName, "", "") |
| 56 | + }.getMessage |
| 57 | + assert(errMsg.contains("'group' is malformed in the expression [testName].")) |
| 58 | + } |
| 59 | + |
| 60 | + test("error handling in ExpressionInfo") { |
| 61 | + val errMsg1 = intercept[IllegalArgumentException] { |
| 62 | + val invalidNote = " invalid note" |
| 63 | + new ExpressionInfo("testClass", null, "testName", null, "", "", invalidNote, "", "", "") |
| 64 | + }.getMessage |
| 65 | + assert(errMsg1.contains("'note' is malformed in the expression [testName].")) |
| 66 | + |
| 67 | + val errMsg2 = intercept[IllegalArgumentException] { |
| 68 | + val invalidSince = "-3.0.0" |
| 69 | + new ExpressionInfo("testClass", null, "testName", null, "", "", "", "", invalidSince, "") |
| 70 | + }.getMessage |
| 71 | + assert(errMsg2.contains("'since' is malformed in the expression [testName].")) |
| 72 | + |
| 73 | + val errMsg3 = intercept[IllegalArgumentException] { |
| 74 | + val invalidDeprecated = " invalid deprecated" |
| 75 | + new ExpressionInfo("testClass", null, "testName", null, "", "", "", "", "", invalidDeprecated) |
| 76 | + }.getMessage |
| 77 | + assert(errMsg3.contains("'deprecated' is malformed in the expression [testName].")) |
| 78 | + } |
| 79 | + |
| 80 | + test("using _FUNC_ instead of function names in examples") { |
| 81 | + val exampleRe = "(>.*;)".r |
| 82 | + val setStmtRe = "(?i)^(>\\s+set\\s+).+".r |
| 83 | + val ignoreSet = Set( |
| 84 | + // Examples for CaseWhen show simpler syntax: |
| 85 | + // `CASE WHEN ... THEN ... WHEN ... THEN ... END` |
| 86 | + "org.apache.spark.sql.catalyst.expressions.CaseWhen", |
| 87 | + // _FUNC_ is replaced by `locate` but `locate(... IN ...)` is not supported |
| 88 | + "org.apache.spark.sql.catalyst.expressions.StringLocate", |
| 89 | + // _FUNC_ is replaced by `%` which causes a parsing error on `SELECT %(2, 1.8)` |
| 90 | + "org.apache.spark.sql.catalyst.expressions.Remainder", |
| 91 | + // Examples demonstrate alternative names, see SPARK-20749 |
| 92 | + "org.apache.spark.sql.catalyst.expressions.Length") |
| 93 | + spark.sessionState.functionRegistry.listFunction().foreach { funcId => |
| 94 | + val info = spark.sessionState.catalog.lookupFunctionInfo(funcId) |
| 95 | + val className = info.getClassName |
| 96 | + withClue(s"Expression class '$className'") { |
| 97 | + val exprExamples = info.getOriginalExamples |
| 98 | + if (!exprExamples.isEmpty && !ignoreSet.contains(className)) { |
| 99 | + assert(exampleRe.findAllIn(exprExamples).toIterable |
| 100 | + .filter(setStmtRe.findFirstIn(_).isEmpty) // Ignore SET commands |
| 101 | + .forall(_.contains("_FUNC_"))) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + test("check outputs of expression examples") { |
| 108 | + def unindentAndTrim(s: String): String = { |
| 109 | + s.replaceAll("\n\\s+", "\n").trim |
| 110 | + } |
| 111 | + val beginSqlStmtRe = " > ".r |
| 112 | + val endSqlStmtRe = ";\n".r |
| 113 | + def checkExampleSyntax(example: String): Unit = { |
| 114 | + val beginStmtNum = beginSqlStmtRe.findAllIn(example).length |
| 115 | + val endStmtNum = endSqlStmtRe.findAllIn(example).length |
| 116 | + assert(beginStmtNum === endStmtNum, |
| 117 | + "The number of ` > ` does not match to the number of `;`") |
| 118 | + } |
| 119 | + val exampleRe = """^(.+);\n(?s)(.+)$""".r |
| 120 | + val ignoreSet = Set( |
| 121 | + // One of examples shows getting the current timestamp |
| 122 | + "org.apache.spark.sql.catalyst.expressions.UnixTimestamp", |
| 123 | + // Random output without a seed |
| 124 | + "org.apache.spark.sql.catalyst.expressions.Rand", |
| 125 | + "org.apache.spark.sql.catalyst.expressions.Randn", |
| 126 | + "org.apache.spark.sql.catalyst.expressions.Shuffle", |
| 127 | + "org.apache.spark.sql.catalyst.expressions.Uuid", |
| 128 | + // The example calls methods that return unstable results. |
| 129 | + "org.apache.spark.sql.catalyst.expressions.CallMethodViaReflection") |
| 130 | + |
| 131 | + val parFuncs = new ParVector(spark.sessionState.functionRegistry.listFunction().toVector) |
| 132 | + parFuncs.foreach { funcId => |
| 133 | + // Examples can change settings. We clone the session to prevent tests clashing. |
| 134 | + val clonedSpark = spark.cloneSession() |
| 135 | + // Coalescing partitions can change result order, so disable it. |
| 136 | + clonedSpark.sessionState.conf.setConf(SQLConf.COALESCE_PARTITIONS_ENABLED, false) |
| 137 | + val info = clonedSpark.sessionState.catalog.lookupFunctionInfo(funcId) |
| 138 | + val className = info.getClassName |
| 139 | + if (!ignoreSet.contains(className)) { |
| 140 | + withClue(s"Function '${info.getName}', Expression class '$className'") { |
| 141 | + val example = info.getExamples |
| 142 | + checkExampleSyntax(example) |
| 143 | + example.split(" > ").toList.foreach { |
| 144 | + case exampleRe(sql, output) => |
| 145 | + val df = clonedSpark.sql(sql) |
| 146 | + val actual = unindentAndTrim( |
| 147 | + hiveResultString(df.queryExecution.executedPlan).mkString("\n")) |
| 148 | + val expected = unindentAndTrim(output) |
| 149 | + assert(actual === expected) |
| 150 | + case _ => |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments