-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dbf6b4f
Added MultiNamedExpression
amanomer 3387eef
Removed unused import
amanomer 4476e7e
Added expressionWithAlias
amanomer 77ad53f
Updated try-catch
amanomer 404d829
Updated TC ExplainSuite.scala
amanomer 27f1a7f
TC update
amanomer 2952898
Updated TC in group-by.sql.out
amanomer ab2e422
TC updated udf-group-by.sql
amanomer bb665e4
fixed TC
amanomer 3c87222
validate expression
amanomer 17c91f4
Handled for expressions that take more than 1 parameter
amanomer d07d261
Add meaningfull error message
amanomer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,55 @@ 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]) => { | ||
| val params = classOf[String] +: Seq.fill(expressions.size)(classOf[Expression]) | ||
| val f = constructors.find(_.getParameterTypes.toSeq == params).getOrElse { | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val validParametersCount = constructors | ||
| .filter(_.getParameterTypes.tail.forall(_ == classOf[Expression])) | ||
| .map(_.getParameterCount - 1).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: ${expressions.size}" | ||
| } | ||
| throw new AnalysisException(invalidArgumentsMsg) | ||
| } | ||
| try { | ||
| f.newInstance(name.toString +: 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)) | ||
| } | ||
|
|
||
| /** | ||
| * Creates a function registry lookup entry for cast aliases (SPARK-16730). | ||
| * For example, if name is "int", and dataType is IntegerType, this means int(x) would become | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.