-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-29883][SQL] Implement a helper method for aliasing bool_and() and bool_or() #26712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
dbf6b4f
3387eef
4476e7e
77ad53f
404d829
27f1a7f
2952898
ab2e422
bb665e4
3c87222
17c91f4
d07d261
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -313,11 +313,11 @@ object FunctionRegistry { | |||||||||||||||||||||||||||||||||||||||
| expression[CollectList]("collect_list"), | ||||||||||||||||||||||||||||||||||||||||
| expression[CollectSet]("collect_set"), | ||||||||||||||||||||||||||||||||||||||||
| expression[CountMinSketchAgg]("count_min_sketch"), | ||||||||||||||||||||||||||||||||||||||||
| expression[BoolAnd]("every"), | ||||||||||||||||||||||||||||||||||||||||
| expression[BoolAnd]("bool_and"), | ||||||||||||||||||||||||||||||||||||||||
| expression[BoolOr]("any"), | ||||||||||||||||||||||||||||||||||||||||
| expression[BoolOr]("some"), | ||||||||||||||||||||||||||||||||||||||||
| expression[BoolOr]("bool_or"), | ||||||||||||||||||||||||||||||||||||||||
| expressionWithAlias[BoolAnd]("every"), | ||||||||||||||||||||||||||||||||||||||||
| expressionWithAlias[BoolAnd]("bool_and"), | ||||||||||||||||||||||||||||||||||||||||
| expressionWithAlias[BoolOr]("any"), | ||||||||||||||||||||||||||||||||||||||||
| expressionWithAlias[BoolOr]("some"), | ||||||||||||||||||||||||||||||||||||||||
| expressionWithAlias[BoolOr]("bool_or"), | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // string functions | ||||||||||||||||||||||||||||||||||||||||
| expression[Ascii]("ascii"), | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -590,12 +590,12 @@ object FunctionRegistry { | |||||||||||||||||||||||||||||||||||||||
| val builder = (expressions: Seq[Expression]) => { | ||||||||||||||||||||||||||||||||||||||||
| if (varargCtor.isDefined) { | ||||||||||||||||||||||||||||||||||||||||
| // If there is an apply method that accepts Seq[Expression], use that one. | ||||||||||||||||||||||||||||||||||||||||
| Try(varargCtor.get.newInstance(expressions).asInstanceOf[Expression]) match { | ||||||||||||||||||||||||||||||||||||||||
| case Success(e) => e | ||||||||||||||||||||||||||||||||||||||||
| case Failure(e) => | ||||||||||||||||||||||||||||||||||||||||
| // the exception is an invocation exception. To get a meaningful message, we need the | ||||||||||||||||||||||||||||||||||||||||
| // cause. | ||||||||||||||||||||||||||||||||||||||||
| throw new AnalysisException(e.getCause.getMessage) | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| varargCtor.get.newInstance(expressions).asInstanceOf[Expression] | ||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||
| // the exception is an invocation exception. To get a meaningful message, we need the | ||||||||||||||||||||||||||||||||||||||||
| // cause. | ||||||||||||||||||||||||||||||||||||||||
| case e: Exception => throw new AnalysisException(e.getCause.getMessage) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| // Otherwise, find a constructor method that matches the number of arguments, and use that. | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -618,19 +618,36 @@ object FunctionRegistry { | |||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| throw new AnalysisException(invalidArgumentsMsg) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| Try(f.newInstance(expressions : _*).asInstanceOf[Expression]) match { | ||||||||||||||||||||||||||||||||||||||||
| case Success(e) => e | ||||||||||||||||||||||||||||||||||||||||
| case Failure(e) => | ||||||||||||||||||||||||||||||||||||||||
| // the exception is an invocation exception. To get a meaningful message, we need the | ||||||||||||||||||||||||||||||||||||||||
| // cause. | ||||||||||||||||||||||||||||||||||||||||
| throw new AnalysisException(e.getCause.getMessage) | ||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| f.newInstance(expressions : _*).asInstanceOf[Expression] | ||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||
| // the exception is an invocation exception. To get a meaningful message, we need the | ||||||||||||||||||||||||||||||||||||||||
| // cause. | ||||||||||||||||||||||||||||||||||||||||
| case e: Exception => throw new AnalysisException(e.getCause.getMessage) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| (name, (expressionInfo[T](name), builder)) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| private def expressionWithAlias[T <: Expression](name: String) | ||||||||||||||||||||||||||||||||||||||||
| (implicit tag: ClassTag[T]): (String, (ExpressionInfo, FunctionBuilder)) = { | ||||||||||||||||||||||||||||||||||||||||
| val constructors = tag.runtimeClass.getConstructors | ||||||||||||||||||||||||||||||||||||||||
| .filter(_.getParameterTypes.head == classOf[String]) | ||||||||||||||||||||||||||||||||||||||||
| assert(constructors.length == 1) | ||||||||||||||||||||||||||||||||||||||||
| val builder = (expressions: Seq[Expression]) => { | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| constructors.head.newInstance(name.toString, expressions.head).asInstanceOf[Expression] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| val params = Seq.fill(expressions.size)(classOf[Expression]) | |
| val f = constructors.find(_.getParameterTypes.toSeq == params).getOrElse { | |
| val validParametersCount = constructors | |
| .filter(_.getParameterTypes.forall(_ == classOf[Expression])) | |
| .map(_.getParameterCount).distinct.sorted | |
| val invalidArgumentsMsg = if (validParametersCount.length == 0) { | |
| s"Invalid arguments for function $name" | |
| } else { | |
| val expectedNumberOfParameters = if (validParametersCount.length == 1) { | |
| validParametersCount.head.toString | |
| } else { | |
| validParametersCount.init.mkString("one of ", ", ", " and ") + | |
| validParametersCount.last | |
| } | |
| s"Invalid number of arguments for function $name. " + | |
| s"Expected: $expectedNumberOfParameters; Found: ${params.length}" | |
| } | |
| throw new AnalysisException(invalidArgumentsMsg) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since, in expressionWithAlias we are always passing expressions.head to function's constructor. We can use assert statement
...
val builder = (expressions: Seq[Expression]) => {
assert(expressions.size == 1,
s"Invalid number of arguments for function $name. " +
s"Expected: 1; Found: ${expressions.size}")
assert(expressions.head == classOf[Expression],
s"Invalid arguments for function $name")
try {
constructors.head.newInstance(name.toString, expressions.head).asInstanceOf[Expression]
}
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW is it possible to do newInstance(name.toString, expressions: _*)? Then it can work for other expressions that take more than 1 parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to do newInstance(name.toString, expressions: _*)?
No, compilation error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about newInstance((name.toString +: expressions): _*)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works. Updated in latest commit. cc @cloud-fan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @amanomer . I'm wondering if this change is required in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reformatting of try-catch block can be raised in different PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I think this is fine and cleaner, so think it's OK to change here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are similar try-catch block format on other files too, which can be reformatted like this.