-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-21117][SQL] Built-in SQL Function Support - WIDTH_BUCKET #18323
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 7 commits
20fe567
cb682a9
3a0eaf7
3a5d46e
7407541
fda866f
5c3ecee
9e2cabb
507fcfb
099db6d
01af62b
0355284
0940a49
e0478f5
2e2b2ca
ea5e4ae
31ee943
44ef7df
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 |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | |
| import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen._ | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.util.NumberConverter | ||
| import org.apache.spark.sql.catalyst.util.{MathUtils, NumberConverter} | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.unsafe.types.UTF8String | ||
|
|
||
|
|
@@ -1186,3 +1186,51 @@ case class BRound(child: Expression, scale: Expression) | |
| with Serializable with ImplicitCastInputTypes { | ||
| def this(child: Expression) = this(child, Literal(0)) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the bucket number into which | ||
| * the value of this expression would fall after being evaluated. | ||
|
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. nit: how about the format and the rephrasing below? |
||
| * | ||
| * @param expr is the expression for which the histogram is being created | ||
|
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. is this comment correct? How about "the expression for which the bucket number in the histogram would return"?
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. |
||
| * @param minValue is an expression that resolves | ||
| * to the minimum end point of the acceptable range for expr | ||
| * @param maxValue is an expression that resolves | ||
| * to the maximum end point of the acceptable range for expr | ||
| * @param numBucket is an An expression that resolves to | ||
|
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.
|
||
| * a constant indicating the number of buckets | ||
| */ | ||
| // scalastyle:off line.size.limit | ||
| @ExpressionDescription( | ||
| usage = "_FUNC_(expr, min_value, max_value, num_bucket) - Returns an long between 0 and `num_buckets`+1 by mapping the `expr` into buckets defined by the range [`min_value`, `max_value`].", | ||
|
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. Nit:
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.
|
||
| extended = """ | ||
|
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.
|
||
| Examples: | ||
| > SELECT _FUNC_(5.35, 0.024, 10.06, 5); | ||
| 3 | ||
| """) | ||
| // scalastyle:on line.size.limit | ||
| case class WidthBucket( | ||
| expr: Expression, | ||
| minValue: Expression, | ||
| maxValue: Expression, | ||
| numBucket: Expression) extends QuaternaryExpression with ImplicitCastInputTypes { | ||
|
|
||
| override def children: Seq[Expression] = Seq(expr, minValue, maxValue, numBucket) | ||
| override def inputTypes: Seq[AbstractDataType] = Seq(DoubleType, DoubleType, DoubleType, LongType) | ||
| override def dataType: DataType = LongType | ||
| override def nullable: Boolean = true | ||
|
|
||
| override def nullSafeEval(ex: Any, min: Any, max: Any, num: Any): Any = { | ||
|
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 should not use nullSafeEval. See the answers I got from Oracle. |
||
| MathUtils.widthBucket( | ||
| ex.asInstanceOf[Double], | ||
|
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. What happened if the input is not a constant, but an foldable expression?
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. foldable expression is correct. |
||
| min.asInstanceOf[Double], | ||
| max.asInstanceOf[Double], | ||
| num.asInstanceOf[Long]) | ||
| } | ||
|
|
||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
|
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. As you override |
||
| val mathUtils = MathUtils.getClass.getName.stripSuffix("$") | ||
| nullSafeCodeGen(ctx, ev, (ex, min, max, num) => | ||
| s"${ev.value} = $mathUtils.widthBucket($ex, $min, $max, $num);" | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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 org.apache.spark.sql.AnalysisException | ||
|
|
||
| object MathUtils { | ||
|
|
||
| /** | ||
| * Returns the bucket number into which | ||
| * the value of this expression would fall after being evaluated. | ||
| * | ||
| * @param expr id the expression for which the histogram is being created | ||
|
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. id -> is
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. nit: id -> is |
||
| * @param minValue is an expression that resolves | ||
| * to the minimum end point of the acceptable range for expr | ||
| * @param maxValue is an expression that resolves | ||
| * to the maximum end point of the acceptable range for expr | ||
| * @param numBucket is an An expression that resolves to | ||
| * a constant indicating the number of buckets | ||
| * @return Returns an long between 0 and numBucket+1 by mapping the expr into buckets defined by | ||
| * the range [minValue, maxValue]. For example: | ||
| * widthBucket(0, 1, 1, 1) -> 0, widthBucket(20, 1, 1, 1) -> 2. | ||
|
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 remove these examples in the description, they are just corner cases. My previous comment was just to make sure both ends should be included. |
||
| */ | ||
| def widthBucket(expr: Double, minValue: Double, maxValue: Double, numBucket: Long): Long = { | ||
|
|
||
| if (numBucket <= 0) { | ||
| throw new AnalysisException(s"The num of bucket must be greater than 0, but got ${numBucket}") | ||
|
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. This check needs to be moved to |
||
| } | ||
|
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. Do we consider minValue == maxValue and numBucket > 1 valid input or not?
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. If
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 know, but my question is: should the possible result when minValue == maxValue only be 0, 1, or 2? e.g. should |
||
|
|
||
| val lower: Double = Math.min(minValue, maxValue) | ||
| val upper: Double = Math.max(minValue, maxValue) | ||
|
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. Does other databases allow max value to appear first? i.e.
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. Yes, Oracle support it. |
||
|
|
||
| val result: Long = if (expr < lower) { | ||
|
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. // an underflow bucket numbered 0
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. Added 2 test case: |
||
| 0 | ||
| } else if (expr >= upper) { | ||
| numBucket + 1L | ||
|
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. // an overflow bucket numbered num_buckets+1
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. Added 2 test case: |
||
| } else { | ||
| (numBucket.toDouble * (expr - lower) / (upper - lower) + 1).toLong | ||
|
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. what if upper == lower? |
||
| } | ||
|
|
||
| if (minValue > maxValue) (numBucket - result) + 1 else result | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets | |
| import com.google.common.math.LongMath | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.analysis.TypeCoercion.ImplicitTypeCasts.implicitCast | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
|
|
@@ -644,4 +645,36 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| checkEvaluation(BRound(-0.35, 1), -0.4) | ||
| checkEvaluation(BRound(-35, -1), -40) | ||
| } | ||
|
|
||
| test("width_bucket") { | ||
| def test( | ||
| expr: Double, | ||
| minValue: Double, | ||
| maxValue: Double, | ||
| numBucket: Long, | ||
| expected: Long): Unit = { | ||
| checkEvaluation(WidthBucket(Literal.create(expr, DoubleType), | ||
| Literal.create(minValue, DoubleType), | ||
| Literal.create(maxValue, DoubleType), | ||
| Literal.create(numBucket, LongType)), | ||
| expected) | ||
| } | ||
|
|
||
| test(5.35, 0.024, 10.06, 5, 3) | ||
|
|
||
| test(3.14, 0, 4, 3, 3) | ||
| test(2, 0, 4, 3, 2) | ||
| test(-1, 0, 3.2, 4, 0) | ||
|
|
||
| test(3.14, 4, 0, 3, 1) | ||
| test(2, 4, 0, 3, 2) | ||
| test(-1, 3.2, 0, 4, 5) | ||
|
|
||
| intercept[AnalysisException]{ | ||
|
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. also add test cases for null input and wrong input type
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. Please capture the error message and verify it. |
||
| WidthBucket(Literal.create(1.0, DoubleType), | ||
| Literal.create(1.0, DoubleType), | ||
| Literal.create(2.0, DoubleType), | ||
| Literal.create(-1L, LongType)).eval() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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 org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.util.MathUtils._ | ||
|
|
||
| class MathUtilsSuite extends SparkFunSuite { | ||
|
|
||
| test("widthBucket") { | ||
| assert(widthBucket(5.35, 0.024, 10.06, 5) === 3) | ||
| assert(widthBucket(0, 1, 1, 1) === 0) | ||
| assert(widthBucket(20, 1, 1, 1) === 2) | ||
|
|
||
| // Test https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2137.htm#OLADM717 | ||
| // WIDTH_BUCKET(credit_limit, 100, 5000, 10) | ||
| assert(widthBucket(500, 100, 5000, 10) === 1) | ||
| assert(widthBucket(2300, 100, 5000, 10) === 5) | ||
| assert(widthBucket(3500, 100, 5000, 10) === 7) | ||
| assert(widthBucket(1200, 100, 5000, 10) === 3) | ||
| assert(widthBucket(1400, 100, 5000, 10) === 3) | ||
| assert(widthBucket(700, 100, 5000, 10) === 2) | ||
| assert(widthBucket(5000, 100, 5000, 10) === 11) | ||
| assert(widthBucket(1800, 100, 5000, 10) === 4) | ||
| assert(widthBucket(400, 100, 5000, 10) === 1) | ||
|
|
||
| intercept[AnalysisException]{ | ||
| assert(widthBucket(100, 100, 5000, -1) === 1) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,3 +92,8 @@ select abs(-3.13), abs('-2.19'); | |
|
|
||
| -- positive/negative | ||
| select positive('-1.11'), positive(-1.11), negative('-1.11'), negative(-1.11); | ||
|
|
||
| -- width_bucket | ||
|
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. Do we need end-to-end tests here? I think we already cover these cases in other test suites.
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. Instead, we need to add a test for sql queries using this function. |
||
| select width_bucket(5.35, 0.024, 10.06, 5); | ||
| select width_bucket(5.35, 0.024, 10.06, -5); | ||
|
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. add a case for wrong input type: |
||
| select width_bucket(null, 0.024, 10.06, 5); | ||
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 can remove this based on your current code.