-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-8218][SQL] Add binary log math function #6725
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 1 commit
f373bac
c795342
c6c187f
21c3bfd
605574d
ebc9929
23c54a3
5b39c02
3d75bfc
1750034
0634ef7
db7dc38
bc89597
8cf37b7
6089d11
beed631
fd01863
102070d
bf96bd9
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 |
|---|---|---|
|
|
@@ -202,3 +202,14 @@ case class Pow(left: Expression, right: Expression) | |
| """ | ||
| } | ||
| } | ||
|
|
||
| case class Logarithm(left: Expression, right: Expression) | ||
|
Contributor
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 seem to be doing this throughout the file, but it seems pretty confusing to me to be using
Contributor
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. +1
Contributor
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. Oh - one thing is that left/right is coming from BinaryExpression
Contributor
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. Yeah, that is what I meant saying it wasn't worth whatever code reuse we are getting. The other option would be to name the arguments and have
Contributor
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. Inheriting from |
||
| extends BinaryMathExpression((c1, c2) => math.log(c2) / math.log(c1), "LOG") { | ||
|
Contributor
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. If we need to override the
Member
Author
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. I think Due to support the case when the left is null in that 10 base logarithm is applied, to override
Contributor
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. Okay, but this is confusing. Is it this lambda that is being used for evaluation or the
Member
Author
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 should be ok because there is other binary math expression
Contributor
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. I would argue that that function is also implemented in a confusing way. We should not shoehorn things into the class hierarchy if its going to result hard to follow code. I'd rather we have small amounts of code duplication.
Member
Author
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. OK. Sounds reasonable. I think I can refactor this part of codes a little. |
||
| override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = { | ||
| defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.log($c2) / java.lang.Math.log($c1)") + s""" | ||
|
Contributor
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. let's generate two versions of the code, i.e. if there is only one argument, generate the old version directly. When there are two arguments, generate the new version.
Member
Author
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. do you mean, when there is only one argument,
Contributor
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. yes, exactly.
Contributor
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. if left == EulerNumber, can we generate the code to just use java.lang.Math.log? |
||
| if (Double.valueOf(${ev.primitive}).isNaN()) { | ||
| ${ev.isNull} = true; | ||
| } | ||
| """ | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1299,6 +1299,70 @@ object functions { | |
| */ | ||
| def toRadians(columnName: String): Column = toRadians(Column(columnName)) | ||
|
|
||
| /** | ||
| * Returns the first argument-base logarithm of the second argument. | ||
| * | ||
| * @group math_funcs | ||
| * @since 1.4.0 | ||
| */ | ||
| def logarithm(l: Column, r: Column): Column = Logarithm(l.expr, r.expr) | ||
|
Contributor
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. let's name the 1st argument as "base", and the second as "a". and also only keep the following 2 versions: log(base: Double, a: Column) log(base: Double, a: String)
Contributor
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 I think it is OK to just name this log, and we should move them to where log is right now. |
||
|
|
||
| /** | ||
| * Returns the first argument-base logarithm of the second argument. | ||
| * | ||
| * @group math_funcs | ||
| * @since 1.4.0 | ||
| */ | ||
| def logarithm(l: Column, rightName: String): Column = logarithm(l, Column(rightName)) | ||
|
|
||
| /** | ||
| * Returns the first argument-base logarithm of the second argument. | ||
| * | ||
| * @group math_funcs | ||
| * @since 1.4.0 | ||
| */ | ||
| def logarithm(leftName: String, r: Column): Column = logarithm(Column(leftName), r) | ||
|
|
||
| /** | ||
| * Returns the first argument-base logarithm of the second argument. | ||
| * | ||
| * @group math_funcs | ||
| * @since 1.4.0 | ||
| */ | ||
| def logarithm(leftName: String, rightName: String): Column = | ||
| logarithm(Column(leftName), Column(rightName)) | ||
|
|
||
| /** | ||
| * Returns the first argument-base logarithm of the second argument. | ||
| * | ||
| * @group math_funcs | ||
| * @since 1.4.0 | ||
| */ | ||
| def logarithm(l: Column, r: Double): Column = logarithm(l, lit(r).expr) | ||
|
|
||
| /** | ||
| * Returns the first argument-base logarithm of the second argument. | ||
| * | ||
| * @group math_funcs | ||
| * @since 1.4.0 | ||
| */ | ||
| def logarithm(leftName: String, r: Double): Column = logarithm(Column(leftName), r) | ||
|
|
||
| /** | ||
| * Returns the first argument-base logarithm of the second argument. | ||
| * | ||
| * @group math_funcs | ||
| * @since 1.4.0 | ||
| */ | ||
| def logarithm(l: Double, r: Column): Column = logarithm(lit(l).expr, r) | ||
|
|
||
| /** | ||
| * Returns the first argument-base logarithm of the second argument. | ||
| * | ||
| * @group math_funcs | ||
| * @since 1.4.0 | ||
| */ | ||
| def logarithm(l: Double, rightName: String): Column = logarithm(l, Column(rightName)) | ||
|
|
||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
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.
we should name this log to make it compatible. (you'd need to make the logarithm expression itself being able to handle both)