Skip to content
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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3082,7 +3082,10 @@ object functions {
* @since 1.5.0
*/
def array_contains(column: Column, value: Any): Column = withExpr {
ArrayContains(column.expr, Literal(value))
Copy link
Member

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.

Copy link
Member

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).

value match {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thoughts, we should use lit(), e.g., ArrayContains(column.expr, lit(value).expr)? WDYT? @viirya @maropu @HyukjinKwon

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that's what I was thinking too from my glance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chongguang I mean, use just ArrayContains(column.expr, lit(value).expr) instead of value match { .... The lit() should handle literals and columns well.

case c: Column => ArrayContains(column.expr, c.expr)
case _ => ArrayContains(column.expr, Literal(value))
}
}

/**
Expand Down Expand Up @@ -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))
}
}

/**
Expand All @@ -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))
}
}

/**
Expand All @@ -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))
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add another test to use selectExpr, e.g., df.selectExpr("array_contains(a, c)")?


// In hive, this errors because null has no type information
intercept[AnalysisException] {
Expand Down Expand Up @@ -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)),
Expand All @@ -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))
Expand All @@ -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(
Expand All @@ -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)),
Expand Down Expand Up @@ -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(
Expand All @@ -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, \"\")"),
Expand Down