-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
[SPARK-29883][SQL] Implement a helper method for aliasing bool_and() and bool_or() #26712
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) | ||||||||||||||||||||||||||||||||||||||||
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] | ||||||||||||||||||||||||||||||||||||||||
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. Since, we are not validating arguments, queries like 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. We can validate arguments with assert or as used in 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. how is it done in 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. spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala Lines 602 to 620 in ebd83a5
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. Since, in
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. SGTM 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. BTW is it possible to do 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.
No, compilation error. 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. how about 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. It works. Updated in latest commit. cc @cloud-fan |
||||||||||||||||||||||||||||||||||||||||
} 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 | ||||||||||||||||||||||||||||||||||||||||
|
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.