Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public String getClassName() {
}

public String getUsage() {
return usage;
return replaceFunctionName(usage);
}

public String getName() {
return name;
}

public String getExtended() {
return extended;
return replaceFunctionName(extended);
}

public String getSince() {
Expand All @@ -57,7 +57,7 @@ public String getArguments() {
}

public String getExamples() {
return examples;
return replaceFunctionName(examples);
}

public String getNote() {
Expand Down Expand Up @@ -150,4 +150,12 @@ public ExpressionInfo(String className, String db, String name, String usage, St
// simply pass the `extended` as `arguments` and an empty string for `examples`.
this(className, db, name, usage, extended, "", "", "", "");
}

private String replaceFunctionName(String usage) {
if (usage == null) {
return "N/A.";
} else {
return usage.replaceAll("_FUNC_", name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ case class DescribeFunctionCommand(
schema.toAttributes
}

private def replaceFunctionName(usage: String, functionName: String): String = {
if (usage == null) {
"N/A."
} else {
usage.replaceAll("_FUNC_", functionName)
}
}

override def run(sparkSession: SparkSession): Seq[Row] = {
// Hard code "<>", "!=", "between", and "case" for now as there is no corresponding functions.
functionName.funcName.toLowerCase(Locale.ROOT) match {
Expand Down Expand Up @@ -148,11 +140,11 @@ case class DescribeFunctionCommand(
val result =
Row(s"Function: $name") ::
Row(s"Class: ${info.getClassName}") ::
Row(s"Usage: ${replaceFunctionName(info.getUsage, info.getName)}") :: Nil
Row(s"Usage: ${info.getUsage}") :: Nil

if (isExtended) {
result :+
Row(s"Extended Usage:${replaceFunctionName(info.getExtended, info.getName)}")
Row(s"Extended Usage:${info.getExtended}")
} else {
result
}
Expand Down
12 changes: 12 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.apache.spark.sql
import java.math.BigDecimal

import org.apache.spark.sql.api.java._
import org.apache.spark.sql.catalyst.FunctionIdentifier
import org.apache.spark.sql.catalyst.plans.logical.Project
import org.apache.spark.sql.execution.QueryExecution
import org.apache.spark.sql.execution.columnar.InMemoryRelation
Expand Down Expand Up @@ -523,4 +524,15 @@ class UDFSuite extends QueryTest with SharedSQLContext {

assert(spark.range(2).select(nonDeterministicJavaUDF()).distinct().count() == 2)
}

test("Replace _FUNC_ in UDF ExpressionInfo") {
val info = spark.sessionState.catalog.lookupFunctionInfo(FunctionIdentifier("upper"))
assert(info.getName === "upper")
assert(info.getClassName === "org.apache.spark.sql.catalyst.expressions.Upper")
assert(info.getUsage === "upper(str) - Returns `str` with all characters changed to uppercase.")
assert(info.getExamples.contains("> SELECT upper('SparkSql');"))
assert(info.getSince === "1.0.1")
assert(info.getNote === "")
assert(info.getExtended.contains("> SELECT upper('SparkSql');"))
}
}