Skip to content

Commit

Permalink
array_remove function deals with Column Type
Browse files Browse the repository at this point in the history
  • Loading branch information
Chongguang LIU authored and Chongguang LIU committed Jun 18, 2018
1 parent e65611e commit b6e150c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 4 additions & 1 deletion sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3182,7 +3182,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))
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,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(
Expand All @@ -1131,6 +1131,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, \"\")"),
Expand Down

0 comments on commit b6e150c

Please sign in to comment.