Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,16 @@ object functions {
ArrayFilter(column.expr, createLambda(f))
}

/**
* Returns an array of elements for which a predicate holds in a given array.
*
* @group collection_funcs
* @since 3.0.0
*/
def filter(column: Column, f: (Column, Column) => Column): Column = withExpr {
ArrayFilter(column.expr, createLambda(f))
}

/**
* Applies a binary operator to an initial state and all elements in the array,
* and reduces this to a single state. The final state is converted into the final result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public void testFilter() {
null
)
);
checkAnswer(
arrDf.select(filter(col("x"), (x, i) -> x.plus(i).equalTo(10))),
toRows(
makeArray(9, 8, 7),
makeArray(7),
JavaTestUtils.<Integer>makeArray(),
null
)
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,12 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSparkSession {
Row(Seq("b", "c")),
Row(Seq.empty),
Row(null)))
checkAnswer(df.select(filter(col("s"), (x, i) => i % 2 === 0)),
Seq(
Row(Seq("c", "b")),
Row(Seq("b", "c")),
Row(Seq.empty),
Row(null)))
}

// Test with local relation, the Project will be evaluated without codegen
Expand Down