-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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-49306][SQL] Create new SQL functions 'zeroifnull' and 'nullifzero' #47817
Changes from 2 commits
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
|
||
package org.apache.spark.sql | ||
|
||
import org.apache.spark.{SPARK_REVISION, SPARK_VERSION_SHORT} | ||
import org.apache.spark.{SPARK_REVISION, SPARK_VERSION_SHORT, SparkNumberFormatException} | ||
import org.apache.spark.sql.catalyst.expressions.Hex | ||
import org.apache.spark.sql.catalyst.parser.ParseException | ||
import org.apache.spark.sql.functions._ | ||
|
@@ -285,6 +285,48 @@ class MiscFunctionsSuite extends QueryTest with SharedSparkSession { | |
assert(df.selectExpr("random(1)").collect() != null) | ||
assert(df.select(random(lit(1))).collect() != null) | ||
} | ||
|
||
test("SPARK-49306 nullifzero and zeroifnull functions") { | ||
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 test suite is for misc functions mostly. Could you move expression tests to 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. Sure, it turns out all these were end to end tests so I just moved them all to |
||
val df = Seq((1, 2, 3)).toDF("a", "b", "c") | ||
checkAnswer(df.selectExpr("nullifzero(0)"), Row(null)) | ||
checkAnswer(df.selectExpr("nullifzero(cast(0 as tinyint))"), Row(null)) | ||
checkAnswer(df.selectExpr("nullifzero(cast(0 as bigint))"), Row(null)) | ||
checkAnswer(df.selectExpr("nullifzero('0')"), Row(null)) | ||
checkAnswer(df.selectExpr("nullifzero(0.0)"), Row(null)) | ||
checkAnswer(df.selectExpr("nullifzero(1)"), Row(1)) | ||
checkAnswer(df.selectExpr("nullifzero(null)"), Row(null)) | ||
var expr = "nullifzero('abc')" | ||
checkError( | ||
exception = intercept[SparkNumberFormatException] { | ||
checkAnswer(df.selectExpr(expr), Row(null)) | ||
}, | ||
errorClass = "CAST_INVALID_INPUT", | ||
parameters = Map( | ||
"expression" -> "'abc'", | ||
"sourceType" -> "\"STRING\"", | ||
"targetType" -> "\"BIGINT\"", | ||
"ansiConfig" -> "\"spark.sql.ansi.enabled\"" | ||
), | ||
context = ExpectedContext("", "", 0, expr.length - 1, expr)) | ||
|
||
checkAnswer(df.selectExpr("zeroifnull(null)"), Row(0)) | ||
checkAnswer(df.selectExpr("zeroifnull(1)"), Row(1)) | ||
checkAnswer(df.selectExpr("zeroifnull(cast(1 as tinyint))"), Row(1)) | ||
checkAnswer(df.selectExpr("zeroifnull(cast(1 as bigint))"), Row(1)) | ||
expr = "zeroifnull('abc')" | ||
checkError( | ||
exception = intercept[SparkNumberFormatException] { | ||
checkAnswer(df.selectExpr(expr), Row(null)) | ||
}, | ||
errorClass = "CAST_INVALID_INPUT", | ||
parameters = Map( | ||
"expression" -> "'abc'", | ||
"sourceType" -> "\"STRING\"", | ||
"targetType" -> "\"BIGINT\"", | ||
"ansiConfig" -> "\"spark.sql.ansi.enabled\"" | ||
), | ||
context = ExpectedContext("", "", 0, expr.length - 1, expr)) | ||
} | ||
} | ||
|
||
object ReflectClass { | ||
|
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.
should probably add them into functions.scala and functions.py. could be done in a separate PR tho.
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.
Thanks, I will add DataFrame support in a separate PR :)