beliefer commented on a change in pull request #28194: URL: https://github.com/apache/spark/pull/28194#discussion_r417260790
########## File path: sql/core/src/test/scala/org/apache/spark/sql/ExpressionsSchemaSuite.scala ########## @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql + +import java.io.File + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.util.{fileToString, stringToFile} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.tags.ExtendedSQLTest + +// scalastyle:off line.size.limit +/** + * End-to-end test cases for SQL schemas of expression examples. + * The golden result file is "spark/sql/core/src/test/resources/sql-functions/sql-expression-schema.md". + * + * To run the entire test suite: + * {{{ + * build/sbt "sql/test-only *ExpressionsSchemaSuite" + * }}} + * + * To re-generate golden files for entire suite, run: + * {{{ + * SPARK_GENERATE_GOLDEN_FILES=1 build/sbt "sql/test-only *ExpressionsSchemaSuite" + * }}} + * + * For example: + * {{{ + * ... + * @ExpressionDescription( + * usage = "_FUNC_(str, n) - Returns the string which repeats the given string value n times.", + * examples = """ + * Examples: + * > SELECT _FUNC_('123', 2); + * 123123 + * """, + * since = "1.5.0") + * case class StringRepeat(str: Expression, times: Expression) + * ... + * }}} + * + * The format for golden result files look roughly like: + * {{{ + * ... + * | org.apache.spark.sql.catalyst.expressions.StringRepeat | repeat | SELECT repeat('123', 2) | struct<repeat(123, 2):string> | + * ... + * }}} + */ +// scalastyle:on line.size.limit +@ExtendedSQLTest +class ExpressionsSchemaSuite extends QueryTest with SharedSparkSession { + + private val regenerateGoldenFiles: Boolean = System.getenv("SPARK_GENERATE_GOLDEN_FILES") == "1" + + private val baseResourcePath = { + // We use a path based on Spark home for 2 reasons: + // 1. Maven can't get correct resource directory when resources in other jars. + // 2. We test subclasses in the hive-thriftserver module. + val sparkHome = { + assert(sys.props.contains("spark.test.home") || + sys.env.contains("SPARK_HOME"), "spark.test.home or SPARK_HOME is not set.") + sys.props.getOrElse("spark.test.home", sys.env("SPARK_HOME")) + } + + java.nio.file.Paths.get(sparkHome, + "sql", "core", "src", "test", "resources", "sql-functions").toFile + } + + private val resultFile = new File(baseResourcePath, "sql-expression-schema.md") + + /** A single SQL query's SQL and schema. */ + protected case class QueryOutput( + className: String, + funcName: String, + sql: String = "N/A", + schema: String = "N/A") { + override def toString: String = { + s"| $className | $funcName | $sql | $schema |" + } + } + + test("Check schemas for expression examples") { + val exampleRe = """^(.+);\n(?s)(.+)$""".r + val funInfos = spark.sessionState.functionRegistry.listFunction().map { funcId => + spark.sessionState.catalog.lookupFunctionInfo(funcId) + } + + val classFunsMap = funInfos.groupBy(_.getClassName).toSeq.sortBy(_._1) + val outputBuffer = new ArrayBuffer[String] + val outputs = new ArrayBuffer[QueryOutput] + val missingExamples = new ArrayBuffer[String] + + classFunsMap.foreach { kv => + val className = kv._1 + kv._2.foreach { funInfo => + val example = funInfo.getExamples + val funcName = funInfo.getName.replaceAll("\\|", "|") + if (example == "") { + val queryOutput = QueryOutput(className, funcName) + outputBuffer += queryOutput.toString + outputs += queryOutput + missingExamples += funcName + } + + // If expression exists 'Examples' segment, the first element is 'Examples'. Because Review comment: The fundamental purpose of this PR is to double check whether the alias of an expression can be displayed correctly in the schema. Although some expressions have multiple examples, only the first one is output here. https://github.com/apache/spark/blob/133456d2dc809ea7cd03139556998955074dd288/sql/core/src/test/scala/org/apache/spark/sql/ExpressionsSchemaSuite.scala#L123 Although the number of parameters is different, as long as the aliases are the same, there is no need to output one by one. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
