Skip to content

Commit ec78438

Browse files
sarutakDavies Liu
authored andcommitted
[SPARK-8686] [SQL] DataFrame should support where with expression represented by String
DataFrame supports `filter` function with two types of argument, `Column` and `String`. But `where` doesn't. Author: Kousuke Saruta <[email protected]> Closes apache#7063 from sarutak/SPARK-8686 and squashes the following commits: 180f9a4 [Kousuke Saruta] Added test d61aec4 [Kousuke Saruta] Add "where" method with String argument to DataFrame
1 parent 77da5be commit ec78438

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

sql/core/src/main/scala/org/apache/spark/sql/DataFrame.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,18 @@ class DataFrame private[sql](
714714
*/
715715
def where(condition: Column): DataFrame = filter(condition)
716716

717+
/**
718+
* Filters rows using the given SQL expression.
719+
* {{{
720+
* peopleDf.where("age > 15")
721+
* }}}
722+
* @group dfops
723+
* @since 1.5.0
724+
*/
725+
def where(conditionExpr: String): DataFrame = {
726+
filter(Column(new SqlParser().parseExpression(conditionExpr)))
727+
}
728+
717729
/**
718730
* Groups the [[DataFrame]] using the specified columns, so we can run aggregation on them.
719731
* See [[GroupedData]] for all the available aggregate functions.

sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ class DataFrameSuite extends QueryTest {
160160
testData.collect().filter(_.getInt(0) > 90).toSeq)
161161
}
162162

163+
test("filterExpr using where") {
164+
checkAnswer(
165+
testData.where("key > 50"),
166+
testData.collect().filter(_.getInt(0) > 50).toSeq)
167+
}
168+
163169
test("repartition") {
164170
checkAnswer(
165171
testData.select('key).repartition(10).select('key),

0 commit comments

Comments
 (0)