Skip to content

Commit 8e9e42b

Browse files
committed
Avoid duplicate code
1 parent 9146913 commit 8e9e42b

File tree

1 file changed

+5
-58
lines changed

1 file changed

+5
-58
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ object FunctionRegistry {
573573
val functionSet: Set[FunctionIdentifier] = builtin.listFunction().toSet
574574

575575
/** See usage above. */
576-
private def expression[T <: Expression](name: String)
576+
private def expression[T <: Expression](name: String, isAliasName: Boolean = false)
577577
(implicit tag: ClassTag[T]): (String, (ExpressionInfo, FunctionBuilder)) = {
578578

579579
// For `RuntimeReplaceable`, skip the constructor with most arguments, which is the main
@@ -619,7 +619,9 @@ object FunctionRegistry {
619619
throw new AnalysisException(invalidArgumentsMsg)
620620
}
621621
try {
622-
f.newInstance(expressions : _*).asInstanceOf[Expression]
622+
val exp = f.newInstance(expressions : _*).asInstanceOf[Expression]
623+
if (isAliasName) exp.setTagValue(exp.FUNC_ALIAS, name)
624+
exp
623625
} catch {
624626
// the exception is an invocation exception. To get a meaningful message, we need the
625627
// cause.
@@ -633,62 +635,7 @@ object FunctionRegistry {
633635

634636
private def expressionWithAlias[T <: Expression](name: String)
635637
(implicit tag: ClassTag[T]): (String, (ExpressionInfo, FunctionBuilder)) = {
636-
637-
// For `RuntimeReplaceable`, skip the constructor with most arguments, which is the main
638-
// constructor and contains non-parameter `child` and should not be used as function builder.
639-
val constructors = if (classOf[RuntimeReplaceable].isAssignableFrom(tag.runtimeClass)) {
640-
val all = tag.runtimeClass.getConstructors
641-
val maxNumArgs = all.map(_.getParameterCount).max
642-
all.filterNot(_.getParameterCount == maxNumArgs)
643-
} else {
644-
tag.runtimeClass.getConstructors
645-
}
646-
// See if we can find a constructor that accepts Seq[Expression]
647-
val varargCtor = constructors.find(_.getParameterTypes.toSeq == Seq(classOf[Seq[_]]))
648-
val builder = (expressions: Seq[Expression]) => {
649-
if (varargCtor.isDefined) {
650-
// If there is an apply method that accepts Seq[Expression], use that one.
651-
try {
652-
varargCtor.get.newInstance(expressions).asInstanceOf[Expression]
653-
} catch {
654-
// the exception is an invocation exception. To get a meaningful message, we need the
655-
// cause.
656-
case e: Exception => throw new AnalysisException(e.getCause.getMessage)
657-
}
658-
} else {
659-
// Otherwise, find a constructor method that matches the number of arguments, and use that.
660-
val params = Seq.fill(expressions.size)(classOf[Expression])
661-
val f = constructors.find(_.getParameterTypes.toSeq == params).getOrElse {
662-
val validParametersCount = constructors
663-
.filter(_.getParameterTypes.forall(_ == classOf[Expression]))
664-
.map(_.getParameterCount).distinct.sorted
665-
val invalidArgumentsMsg = if (validParametersCount.length == 0) {
666-
s"Invalid arguments for function $name"
667-
} else {
668-
val expectedNumberOfParameters = if (validParametersCount.length == 1) {
669-
validParametersCount.head.toString
670-
} else {
671-
validParametersCount.init.mkString("one of ", ", ", " and ") +
672-
validParametersCount.last
673-
}
674-
s"Invalid number of arguments for function $name. " +
675-
s"Expected: $expectedNumberOfParameters; Found: ${params.length}"
676-
}
677-
throw new AnalysisException(invalidArgumentsMsg)
678-
}
679-
try {
680-
val exp = f.newInstance(expressions : _*).asInstanceOf[Expression]
681-
exp.setTagValue(exp.FUNC_ALIAS, name)
682-
exp
683-
} catch {
684-
// the exception is an invocation exception. To get a meaningful message, we need the
685-
// cause.
686-
case e: Exception => throw new AnalysisException(e.getCause.getMessage)
687-
}
688-
}
689-
}
690-
691-
(name, (expressionInfo[T](name), builder))
638+
expression[T](name, true)
692639
}
693640

694641
/**

0 commit comments

Comments
 (0)