Skip to content

Commit

Permalink
feat: [+] #271 unary functions (-) & (!) (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
eruizalo authored Sep 7, 2022
1 parent 86620fa commit c0b4ee0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
19 changes: 8 additions & 11 deletions core/src/main/scala/doric/syntax/BooleanColumns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ private[syntax] trait BooleanColumns {
*/
def not(col: BooleanColumn): BooleanColumn = col.elem.map(f.not).toDC

/**
* Inversion of boolean expression, i.e. NOT.
*
* @group Boolean Type
* @see [[not]]
*/
@inline def !(col: BooleanColumn): BooleanColumn = not(col)

/**
* @group Boolean Type
*/
implicit class BooleanOperationsSyntax(
column: DoricColumn[Boolean]
) {

/**
* Inversion of boolean expression, i.e. NOT.
*
* @group Boolean Type
* @see [[not]]
*/
def unary_! : DoricColumn[Boolean] = not(column)

/**
* Boolean AND
*
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/scala/doric/syntax/NumericColumns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package syntax
import cats.implicits._
import doric.DoricColumn.sparkFunction
import doric.types.{CollectionType, NumericType}
import org.apache.spark.sql.catalyst.expressions.{BRound, FormatNumber, FromUnixTime, Rand, Randn, Round, UnaryMinus}
import org.apache.spark.sql.{Column, functions => f}
import org.apache.spark.sql.catalyst.expressions.{BRound, FormatNumber, FromUnixTime, Rand, Randn, Round, ShiftLeft, ShiftRight, ShiftRightUnsigned}

private[syntax] trait NumericColumns {

Expand Down Expand Up @@ -101,6 +101,14 @@ private[syntax] trait NumericColumns {
column: DoricColumn[T]
) {

/**
* Unary minus, i.e. negate the expression.
*
* @group Numeric Type
*/
def unary_- : DoricColumn[T] =
column.elem.map(x => new Column(UnaryMinus(x.expr))).toDC

/**
* @group Numeric Type
*/
Expand Down
12 changes: 3 additions & 9 deletions core/src/test/scala/doric/syntax/BooleanColumnsSpec.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package doric
package syntax

import org.scalatest.EitherValues
import org.scalatest.matchers.should.Matchers

import org.apache.spark.sql.{functions => f}

class BooleanColumnsSpec
extends DoricTestElements
with EitherValues
with Matchers {
class BooleanColumnsSpec extends DoricTestElements {

describe("Boolean columns") {
import spark.implicits._
Expand All @@ -28,8 +22,8 @@ class BooleanColumnsSpec

it("should be inverted by !") {
df.testColumns("col1")(
c => BoolF.!(colBoolean(c)),
c => f.not(f.col(c)),
c => !colBoolean(c),
c => !f.col(c),
List(Some(false), Some(true), None)
)
}
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/scala/doric/syntax/NumericOperationsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ trait NumericOperationsSpec
f.negate
)
}

it(s"unary (-) function $numTypeStr") {
testDoricSpark[T, T](
List(Some(-1), Some(1), Some(2), None),
List(Some(1), Some(-1), Some(-2), None),
x => -x,
x => -x
)
}
}
}

Expand Down

0 comments on commit c0b4ee0

Please sign in to comment.