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 @@ -158,27 +158,23 @@ object FunctionRegistry {
/** See usage above. */
private def expression[T <: Expression](name: String)
(implicit tag: ClassTag[T]): (String, FunctionBuilder) = {
// Use the companion class to find apply methods.
val objectClass = Class.forName(tag.runtimeClass.getName + "$")
val companionObj = objectClass.getDeclaredField("MODULE$").get(null)

// See if we can find an apply that accepts Seq[Expression]
val varargApply = Try(objectClass.getDeclaredMethod("apply", classOf[Seq[_]])).toOption

// See if we can find a constructor that accepts Seq[Expression]
val varargCtor = Try(tag.runtimeClass.getDeclaredConstructor(classOf[Seq[_]])).toOption
val builder = (expressions: Seq[Expression]) => {
if (varargApply.isDefined) {
if (varargCtor.isDefined) {
// If there is an apply method that accepts Seq[Expression], use that one.
varargApply.get.invoke(companionObj, expressions).asInstanceOf[Expression]
varargCtor.get.newInstance(expressions).asInstanceOf[Expression]
} else {
// Otherwise, find an apply method that matches the number of arguments, and use that.
// Otherwise, find an ctor method that matches the number of arguments, and use that.
val params = Seq.fill(expressions.size)(classOf[Expression])
val f = Try(objectClass.getDeclaredMethod("apply", params : _*)) match {
val f = Try(tag.runtimeClass.getDeclaredConstructor(params : _*)) match {
case Success(e) =>
e
case Failure(e) =>
throw new AnalysisException(s"Invalid number of arguments for function $name")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val f = try tag.runtimeClass.getDeclaredConstructor(params: _*) catch {
  case _: NoSuchMethodError =>
    throw new AnalysisException(s"Invalid number of arguments for function $name")
}

😄

f.invoke(companionObj, expressions : _*).asInstanceOf[Expression]
f.newInstance(expressions : _*).asInstanceOf[Expression]
}
}
(name, builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import org.apache.spark.sql.types._
/**
* If an expression wants to be exposed in the function registry (so users can call it with
* "name(arguments...)", the concrete implementation must be a case class whose constructor
* arguments are all Expressions types. In addition, if it needs to support more than one
* constructor, define those constructors explicitly as apply methods in the companion object.
* arguments are all Expressions types.
*
* See [[Substring]] for an example.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ abstract class RDG(seed: Long) extends LeafExpression with Serializable {
/** Generate a random column with i.i.d. uniformly distributed values in [0, 1). */
case class Rand(seed: Long) extends RDG(seed) {
override def eval(input: InternalRow): Double = rng.nextDouble()
}

object Rand {
def apply(): Rand = apply(Utils.random.nextLong())
def this() = this(Utils.random.nextLong())

def apply(seed: Expression): Rand = apply(seed match {
def this(seed: Expression) = this(seed match {
case IntegerLiteral(s) => s
case _ => throw new AnalysisException("Input argument to rand must be an integer literal.")
})
Expand All @@ -63,12 +61,10 @@ object Rand {
/** Generate a random column with i.i.d. gaussian random distribution. */
case class Randn(seed: Long) extends RDG(seed) {
override def eval(input: InternalRow): Double = rng.nextGaussian()
}

object Randn {
def apply(): Randn = apply(Utils.random.nextLong())
def this() = this(Utils.random.nextLong())

def apply(seed: Expression): Randn = apply(seed match {
def this(seed: Expression) = this(seed match {
case IntegerLiteral(s) => s
case _ => throw new AnalysisException("Input argument to rand must be an integer literal.")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.expressions
import java.util.regex.Pattern

import org.apache.spark.sql.catalyst.analysis.UnresolvedException
import org.apache.spark.sql.catalyst.expressions.Substring
import org.apache.spark.sql.catalyst.expressions.codegen._
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.UTF8String
Expand Down Expand Up @@ -225,6 +226,10 @@ case class EndsWith(left: Expression, right: Expression)
case class Substring(str: Expression, pos: Expression, len: Expression)
extends Expression with ExpectsInputTypes {

def this(str: Expression, pos: Expression) = {
this(str, pos, Literal(Integer.MAX_VALUE))
}

override def foldable: Boolean = str.foldable && pos.foldable && len.foldable

override def nullable: Boolean = str.nullable || pos.nullable || len.nullable
Expand Down Expand Up @@ -290,12 +295,6 @@ case class Substring(str: Expression, pos: Expression, len: Expression)
}
}

object Substring {
def apply(str: Expression, pos: Expression): Substring = {
apply(str, pos, Literal(Integer.MAX_VALUE))
}
}

/**
* A function that return the length of the given string expression.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] {
* @param newArgs the new product arguments.
*/
def makeCopy(newArgs: Array[AnyRef]): this.type = attachTree(this, "makeCopy") {
val defaultCtor =
getClass.getConstructors
.find(_.getParameterTypes.size != 0)
.headOption
.getOrElse(sys.error(s"No valid constructor for $nodeName"))
val ctors = getClass.getConstructors.filter(_.getParameterTypes.size != 0)
if (ctors.isEmpty) {
sys.error(s"No valid constructor for $nodeName")
}
val defaultCtor = ctors.maxBy(_.getParameterTypes.size)

try {
CurrentOrigin.withOrigin(origin) {
Expand Down