Skip to content
Snippets Groups Projects
Commit fe767395 authored by 岑玉海's avatar 岑玉海 Committed by Herman van Hovell
Browse files

[SPARK-17429][SQL] use ImplicitCastInputTypes with function Length

## What changes were proposed in this pull request?
select length(11);
select length(2.0);
these sql will return errors, but hive is ok.
this PR will support casting input types implicitly for function length
the correct result is:
select length(11) return 2
select length(2.0) return 3

Author: 岑玉海 <261810726@qq.com>
Author: cenyuhai <cenyuhai@didichuxing.com>

Closes #15014 from cenyuhai/SPARK-17429.
parent d403562e
No related branches found
No related tags found
No related merge requests found
...@@ -1057,7 +1057,7 @@ case class Substring(str: Expression, pos: Expression, len: Expression) ...@@ -1057,7 +1057,7 @@ case class Substring(str: Expression, pos: Expression, len: Expression)
@ExpressionDescription( @ExpressionDescription(
usage = "_FUNC_(str | binary) - Returns the length of str or number of bytes in binary data.", usage = "_FUNC_(str | binary) - Returns the length of str or number of bytes in binary data.",
extended = "> SELECT _FUNC_('Spark SQL');\n 9") extended = "> SELECT _FUNC_('Spark SQL');\n 9")
case class Length(child: Expression) extends UnaryExpression with ExpectsInputTypes { case class Length(child: Expression) extends UnaryExpression with ImplicitCastInputTypes {
override def dataType: DataType = IntegerType override def dataType: DataType = IntegerType
override def inputTypes: Seq[AbstractDataType] = Seq(TypeCollection(StringType, BinaryType)) override def inputTypes: Seq[AbstractDataType] = Seq(TypeCollection(StringType, BinaryType))
......
...@@ -330,7 +330,8 @@ class StringFunctionsSuite extends QueryTest with SharedSQLContext { ...@@ -330,7 +330,8 @@ class StringFunctionsSuite extends QueryTest with SharedSQLContext {
} }
test("string / binary length function") { test("string / binary length function") {
val df = Seq(("123", Array[Byte](1, 2, 3, 4), 123)).toDF("a", "b", "c") val df = Seq(("123", Array[Byte](1, 2, 3, 4), 123, 2.0f, 3.015))
.toDF("a", "b", "c", "d", "e")
checkAnswer( checkAnswer(
df.select(length($"a"), length($"b")), df.select(length($"a"), length($"b")),
Row(3, 4)) Row(3, 4))
...@@ -339,9 +340,10 @@ class StringFunctionsSuite extends QueryTest with SharedSQLContext { ...@@ -339,9 +340,10 @@ class StringFunctionsSuite extends QueryTest with SharedSQLContext {
df.selectExpr("length(a)", "length(b)"), df.selectExpr("length(a)", "length(b)"),
Row(3, 4)) Row(3, 4))
intercept[AnalysisException] { checkAnswer(
df.selectExpr("length(c)") // int type of the argument is unacceptable df.selectExpr("length(c)", "length(d)", "length(e)"),
} Row(3, 3, 5)
)
} }
test("initcap function") { test("initcap function") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment