-
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-24574][SQL] array_contains, array_position, array_remove and element_at functions deal with Column type #21581
Changes from 6 commits
27733f9
28aa515
e65611e
b6e150c
eabdb6e
8440d5b
f8c5b43
ddd94f7
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 |
---|---|---|
|
@@ -3082,7 +3082,10 @@ object functions { | |
* @since 1.5.0 | ||
*/ | ||
def array_contains(column: Column, value: Any): Column = withExpr { | ||
ArrayContains(column.expr, Literal(value)) | ||
value match { | ||
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. On second thoughts, we should use 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. Yup, that's what I was thinking too from my glance. 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, I think so. 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. +1 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. @chongguang I mean, use just |
||
case c: Column => ArrayContains(column.expr, c.expr) | ||
case _ => ArrayContains(column.expr, Literal(value)) | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -3146,7 +3149,10 @@ object functions { | |
* @since 2.4.0 | ||
*/ | ||
def array_position(column: Column, value: Any): Column = withExpr { | ||
ArrayPosition(column.expr, Literal(value)) | ||
value match { | ||
case c: Column => ArrayPosition(column.expr, c.expr) | ||
case _ => ArrayPosition(column.expr, Literal(value)) | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -3157,7 +3163,10 @@ object functions { | |
* @since 2.4.0 | ||
*/ | ||
def element_at(column: Column, value: Any): Column = withExpr { | ||
ElementAt(column.expr, Literal(value)) | ||
value match { | ||
case c: Column => ElementAt(column.expr, c.expr) | ||
case _ => ElementAt(column.expr, Literal(value)) | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -3175,7 +3184,10 @@ object functions { | |
* @since 2.4.0 | ||
*/ | ||
def array_remove(column: Column, element: Any): Column = withExpr { | ||
ArrayRemove(column.expr, Literal(element)) | ||
element match { | ||
case c: Column => ArrayRemove(column.expr, c.expr) | ||
case _ => ArrayRemove(column.expr, Literal(element)) | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -558,9 +558,9 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
|
||
test("array contains function") { | ||
val df = Seq( | ||
(Seq[Int](1, 2), "x"), | ||
(Seq[Int](), "x") | ||
).toDF("a", "b") | ||
(Seq[Int](1, 2), "x", 1), | ||
(Seq[Int](), "x", 1) | ||
).toDF("a", "b", "c") | ||
|
||
// Simple test cases | ||
checkAnswer( | ||
|
@@ -571,6 +571,10 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
df.selectExpr("array_contains(a, 1)"), | ||
Seq(Row(true), Row(false)) | ||
) | ||
checkAnswer( | ||
df.select(array_contains(df("a"), df("c"))), | ||
Seq(Row(true), Row(false)) | ||
) | ||
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. Can you add another test to use |
||
|
||
// In hive, this errors because null has no type information | ||
intercept[AnalysisException] { | ||
|
@@ -785,9 +789,9 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
|
||
test("array position function") { | ||
val df = Seq( | ||
(Seq[Int](1, 2), "x"), | ||
(Seq[Int](), "x") | ||
).toDF("a", "b") | ||
(Seq[Int](1, 2), "x", 1), | ||
(Seq[Int](), "x", 1) | ||
).toDF("a", "b", "c") | ||
|
||
checkAnswer( | ||
df.select(array_position(df("a"), 1)), | ||
|
@@ -797,7 +801,10 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
df.selectExpr("array_position(a, 1)"), | ||
Seq(Row(1L), Row(0L)) | ||
) | ||
|
||
checkAnswer( | ||
df.select(array_position(df("a"), df("c"))), | ||
Seq(Row(1L), Row(0L)) | ||
) | ||
checkAnswer( | ||
df.select(array_position(df("a"), null)), | ||
Seq(Row(null), Row(null)) | ||
|
@@ -824,10 +831,10 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
|
||
test("element_at function") { | ||
val df = Seq( | ||
(Seq[String]("1", "2", "3")), | ||
(Seq[String](null, "")), | ||
(Seq[String]()) | ||
).toDF("a") | ||
(Seq[String]("1", "2", "3"), 1), | ||
(Seq[String](null, ""), -1), | ||
(Seq[String](), 2) | ||
).toDF("a", "b") | ||
|
||
intercept[Exception] { | ||
checkAnswer( | ||
|
@@ -845,6 +852,10 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
df.select(element_at(df("a"), 4)), | ||
Seq(Row(null), Row(null), Row(null)) | ||
) | ||
checkAnswer( | ||
df.select(element_at(df("a"), df("b"))), | ||
Seq(Row("1"), Row(""), Row(null)) | ||
) | ||
|
||
checkAnswer( | ||
df.select(element_at(df("a"), 1)), | ||
|
@@ -1112,10 +1123,10 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
|
||
test("array remove") { | ||
val df = Seq( | ||
(Array[Int](2, 1, 2, 3), Array("a", "b", "c", "a"), Array("", "")), | ||
(Array.empty[Int], Array.empty[String], Array.empty[String]), | ||
(null, null, null) | ||
).toDF("a", "b", "c") | ||
(Array[Int](2, 1, 2, 3), Array("a", "b", "c", "a"), Array("", ""), 2), | ||
(Array.empty[Int], Array.empty[String], Array.empty[String], 2), | ||
(null, null, null, 2) | ||
).toDF("a", "b", "c", "d") | ||
checkAnswer( | ||
df.select(array_remove($"a", 2), array_remove($"b", "a"), array_remove($"c", "")), | ||
Seq( | ||
|
@@ -1124,6 +1135,14 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
Row(null, null, null)) | ||
) | ||
|
||
checkAnswer( | ||
df.select(array_remove($"a", $"d")), | ||
Seq( | ||
Row(Seq(1, 3)), | ||
Row(Seq.empty[Int]), | ||
Row(null)) | ||
) | ||
|
||
checkAnswer( | ||
df.selectExpr("array_remove(a, 2)", "array_remove(b, \"a\")", | ||
"array_remove(c, \"\")"), | ||
|
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.
Other similar expressions are
ArrayPosition
,ElementAt
,ArrayRemove
.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.
Oh, I just found @maropu has pointed it out in #21581 (comment).