-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24371] [SQL] Added isInCollection in DataFrame API for Scala and Java. #21416
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
Changes from 1 commit
da10307
730b19b
6ff2806
286a468
1332406
fed2846
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -394,6 +394,14 @@ class ColumnExpressionSuite extends QueryTest with SharedSQLContext { | |
| checkAnswer(df.filter($"b".isin("z", "y")), | ||
| df.collect().toSeq.filter(r => r.getString(1) == "z" || r.getString(1) == "y")) | ||
|
|
||
| // Auto casting should work with mixture of different types in collections | ||
| checkAnswer(df.filter($"a".isin(1.toShort, "2")), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 1 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isin("3", 2.toLong)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isin(3, "1")), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 1)) | ||
|
|
||
| val df2 = Seq((1, Seq(1)), (2, Seq(2)), (3, Seq(3))).toDF("a", "b") | ||
|
|
||
| val e = intercept[AnalysisException] { | ||
|
|
@@ -407,29 +415,9 @@ class ColumnExpressionSuite extends QueryTest with SharedSQLContext { | |
|
|
||
| test("isInCollection: Scala Collection") { | ||
| val df = Seq((1, "x"), (2, "y"), (3, "z")).toDF("a", "b") | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(1, 2))), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 1 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(3, 2))), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 2)) | ||
| // Test with different types of collections | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(3, 1))), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 1)) | ||
|
|
||
| // Auto casting should work with mixture of different types in collections | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(1.toShort, "2"))), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 1 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq("3", 2.toLong))), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(3, "1"))), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 1)) | ||
|
|
||
| checkAnswer(df.filter($"b".isInCollection(Seq("y", "x"))), | ||
| df.collect().toSeq.filter(r => r.getString(1) == "y" || r.getString(1) == "x")) | ||
| checkAnswer(df.filter($"b".isInCollection(Seq("z", "x"))), | ||
| df.collect().toSeq.filter(r => r.getString(1) == "z" || r.getString(1) == "x")) | ||
| checkAnswer(df.filter($"b".isInCollection(Seq("z", "y"))), | ||
| df.collect().toSeq.filter(r => r.getString(1) == "z" || r.getString(1) == "y")) | ||
|
|
||
| // Test with different types of collections | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(1, 2).toSet)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 1 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(3, 2).toArray)), | ||
|
|
@@ -450,29 +438,9 @@ class ColumnExpressionSuite extends QueryTest with SharedSQLContext { | |
|
|
||
| test("isInCollection: Java Collection") { | ||
|
Contributor
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. As stated up above, maybe this would make sense to do in Java, but your call.
Member
Author
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. I totally agree with you that we should have tests natively in Java instead of converting the types to Java in Scala and hope the best that it will work in Java. Let's do it in the followup PR. |
||
| val df = Seq((1, "x"), (2, "y"), (3, "z")).toDF("a", "b") | ||
|
Contributor
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. same thing here. just run a single test case.
Member
Author
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. Done. |
||
| // Test with different types of collections | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(1, 2).asJava)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 1 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(3, 2).asJava)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(3, 1).asJava)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 1)) | ||
|
|
||
| // Auto casting should work with mixture of different types in collections | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(1.toShort, "2").asJava)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 1 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq("3", 2.toLong).asJava)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(3, "1").asJava)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 3 || r.getInt(0) == 1)) | ||
|
|
||
| checkAnswer(df.filter($"b".isInCollection(Seq("y", "x").asJava)), | ||
| df.collect().toSeq.filter(r => r.getString(1) == "y" || r.getString(1) == "x")) | ||
| checkAnswer(df.filter($"b".isInCollection(Seq("z", "x").asJava)), | ||
| df.collect().toSeq.filter(r => r.getString(1) == "z" || r.getString(1) == "x")) | ||
| checkAnswer(df.filter($"b".isInCollection(Seq("z", "y").asJava)), | ||
| df.collect().toSeq.filter(r => r.getString(1) == "z" || r.getString(1) == "y")) | ||
|
|
||
| // Test with different types of collections | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(1, 2).toSet.asJava)), | ||
| df.collect().toSeq.filter(r => r.getInt(0) == 1 || r.getInt(0) == 2)) | ||
| checkAnswer(df.filter($"a".isInCollection(Seq(3, 1).toList.asJava)), | ||
|
|
||
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.
can we simplify the test cases? you are just testing this api as a wrapper. you don't need to run so many queries for type coercion.
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.
Done.