-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23906][SQL] Add built-in UDF TRUNCATE(number) #22419
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 3 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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.spark.sql.catalyst.util | ||
|
|
||
| import java.math.{BigDecimal => JBigDecimal} | ||
|
|
||
| import org.apache.spark.sql.types.Decimal | ||
|
|
||
| object MathUtils { | ||
|
|
||
| /** | ||
| * Returns double type input truncated to scale decimal places. | ||
| */ | ||
| def trunc(input: Double, scale: Int): Double = { | ||
|
||
| trunc(JBigDecimal.valueOf(input), scale).toDouble | ||
| } | ||
|
|
||
| /** | ||
| * Returns float type input truncated to scale decimal places. | ||
| */ | ||
| def trunc(input: Float, scale: Int): Float = { | ||
| trunc(JBigDecimal.valueOf(input), scale).toFloat | ||
| } | ||
|
|
||
| /** | ||
| * Returns decimal type input truncated to scale decimal places. | ||
| */ | ||
| def trunc(input: Decimal, scale: Int): Decimal = { | ||
| trunc(input.toJavaBigDecimal, scale) | ||
| } | ||
|
|
||
| /** | ||
| * Returns BigDecimal type input truncated to scale decimal places. | ||
| */ | ||
| def trunc(input: JBigDecimal, scale: Int): Decimal = { | ||
| // Copy from (https://github.com/apache/hive/blob/release-2.3.0-rc0 | ||
| // /ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTrunc.java#L471-L487) | ||
| val pow = if (scale >= 0) { | ||
| JBigDecimal.valueOf(Math.pow(10, scale)) | ||
| } else { | ||
| JBigDecimal.valueOf(Math.pow(10, Math.abs(scale))) | ||
| } | ||
|
|
||
| val truncatedValue = if (scale > 0) { | ||
| val longValue = input.multiply(pow).longValue() | ||
| JBigDecimal.valueOf(longValue).divide(pow) | ||
| } else if (scale == 0) { | ||
| JBigDecimal.valueOf(input.longValue()) | ||
| } else { | ||
| val longValue = input.divide(pow).longValue() | ||
| JBigDecimal.valueOf(longValue).multiply(pow) | ||
| } | ||
|
|
||
| Decimal(truncatedValue) | ||
| } | ||
| } | ||
| 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.4.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.4.0 | ||
|
||
| */ | ||
| def truncate(e: Column, scale: Int): Column = withExpr { | ||
| Truncate(e.expr, Literal(scale)) | ||
| } | ||
|
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. 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.
Same to
RoundBase. only support foldable:spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala
Line 1076 in c715694