-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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-23906][SQL] Add built-in UDF TRUNCATE(number) #22419
Changes from all commits
b5365e2
bf7103a
c715694
87cea0b
479b31f
b7e3460
ae7eb73
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 |
---|---|---|
|
@@ -287,6 +287,7 @@ final class Decimal extends Ordered[Decimal] with Serializable { | |
if (doubled > pow10diff || doubled == pow10diff && longVal % 2 != 0) { | ||
longVal += (if (droppedDigits < 0) -1L else 1L) | ||
} | ||
case ROUND_DOWN => | ||
case _ => | ||
sys.error(s"Not supported rounding mode: $roundMode") | ||
} | ||
|
@@ -413,6 +414,7 @@ object Decimal { | |
val ROUND_HALF_EVEN = BigDecimal.RoundingMode.HALF_EVEN | ||
val ROUND_CEILING = BigDecimal.RoundingMode.CEILING | ||
val ROUND_FLOOR = BigDecimal.RoundingMode.FLOOR | ||
val ROUND_DOWN = BigDecimal.RoundingMode.DOWN | ||
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. Need this change, otherwise:
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 guess we need to modify 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 added it and seem do not need change |
||
|
||
/** Maximum number of decimal digits an Int can represent */ | ||
val MAX_INT_DIGITS = 9 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2214,6 +2214,26 @@ object functions { | |
*/ | ||
def radians(columnName: String): Column = radians(Column(columnName)) | ||
|
||
/** | ||
* Returns the value of the column `e` truncated to 0 places. | ||
* | ||
* @group math_funcs | ||
* @since 2.5.0 | ||
*/ | ||
def truncate(e: Column): Column = truncate(e, 0) | ||
|
||
/** | ||
* Returns the value of column `e` truncated to the unit specified by the scale. | ||
* If scale is omitted, then the value of column `e` is truncated to 0 places. | ||
* Scale can be negative to truncate (make zero) scale digits left of the decimal point. | ||
* | ||
* @group math_funcs | ||
* @since 2.5.0 | ||
*/ | ||
def truncate(e: Column, scale: Int): Column = withExpr { | ||
Truncate(e.expr, Literal(scale)) | ||
} | ||
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 need |
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////// | ||
// Misc functions | ||
////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
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 am still preferring to extend
trunc
. Not straightforward to know the difference betweentruncate
andtrunc
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 @wangyum, can you still extend
trunc
?If not, what were the major reasons you decided to separate these? Thanks!
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 don't know we should trunc
StringType
to number or trunc to date.For example:
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.
In that case, its ok to handle the string as date. How about only accepting float, double, and decimal for number truncation?