Skip to content

Commit be589b3

Browse files
prestonphimarios
authored andcommitted
Add column method 'unary_!' (#312)
1 parent c763176 commit be589b3

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

dataset/src/main/scala/frameless/TypedColumn.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ abstract class AbstractTypedColumn[T, U]
189189
def +(u: U)(implicit n: CatalystNumeric[U]): ThisType[T, U] =
190190
typed(self.untyped.plus(u))
191191

192+
/**
193+
* Inversion of boolean expression, i.e. NOT.
194+
* {{{
195+
* // Select rows that are not active (isActive === false)
196+
* df.filter( !df('isActive) )
197+
* }}}
198+
*
199+
* apache/spark
200+
*/
201+
def unary_!(implicit i0: U <:< Boolean): ThisType[T, Boolean] =
202+
typed(!untyped)
203+
192204
/** Unary minus, i.e. negate the expression.
193205
* {{{
194206
* // Select the amount column and negates all values.

dataset/src/test/scala/frameless/ColumnTests.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java.time.Instant
44

55
import org.scalacheck.Prop._
66
import org.scalacheck.{Arbitrary, Gen, Prop}
7+
import org.scalatest.Matchers._
78
import shapeless.test.illTyped
89

910
import scala.math.Ordering.Implicits._
@@ -357,4 +358,20 @@ class ColumnTests extends TypedDatasetSuite {
357358
check(forAll(prop[Vector[Vector[String]], Vector[Vector[BigDecimal]]] _))
358359
}
359360

361+
test("unary_!") {
362+
val ds = TypedDataset.create((true, false) :: Nil)
363+
364+
val rs = ds.select(!ds('_1), !ds('_2)).collect().run().head
365+
val expected = (false, true)
366+
367+
rs shouldEqual expected
368+
}
369+
370+
test("unary_! with non-boolean columns should not compile") {
371+
val ds = TypedDataset.create((1, "a", 2.0) :: Nil)
372+
373+
"ds.select(!ds('_1))" shouldNot typeCheck
374+
"ds.select(!ds('_2))" shouldNot typeCheck
375+
"ds.select(!ds('_3))" shouldNot typeCheck
376+
}
360377
}

0 commit comments

Comments
 (0)