Skip to content
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

feat: [+] remaining non aggregate functions (resolves #68) #262

Merged
merged 2 commits into from
Aug 12, 2022
Merged
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
2 changes: 1 addition & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CI/CD:
- .github/**/*.yml

dependencies:
- any: [ '.github/dependabot.yml', 'build.sbt', 'project/**/*', '.scala-steward.conf', '.scalafix.conf', '.scalafmt.conf' ]
- any: [ '.github/dependabot.yml', 'build.sbt', 'project/**/*', '.scala-steward.conf', '.scalafix.conf', '.scalafmt.conf', 'project/build.properties' ]

documentation:
- any: [ 'docs/**/*', 'notebooks/**/*', '*.md' ]
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/scala/doric/syntax/NumericColumns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,21 @@ private[syntax] trait NumericColumns {
* @see [[org.apache.spark.sql.functions.tanh(e:org\.apache\.spark\.sql\.Column)* org.apache.spark.sql.functions.tanh]]
*/
def tanh: DoubleColumn = column.elem.map(f.tanh).toDC

/**
* Unary minus, i.e. negate the expression.
*
* @example {{{
* // Select the amount column and negates all values.
* // Scala:
* df.select( -df("amount") )
* }}}
*
* @todo DayTimeIntervalType & YearMonthIntervalType
* @group Numeric Type
* @see [[org.apache.spark.sql.functions.negate]]
*/
def negate: DoricColumn[T] = column.elem.map(f.negate).toDC
}

/**
Expand Down Expand Up @@ -572,6 +587,15 @@ private[syntax] trait NumericColumns {
def round(scale: IntegerColumn): DoricColumn[T] = (column.elem, scale.elem)
.mapN((c, s) => new Column(Round(c.expr, s.expr)))
.toDC

/**
* Returns col1 if it is not NaN, or col2 if col1 is NaN.
*
* @group Numeric Type
* @see [[org.apache.spark.sql.functions.nanvl]]
*/
def naNvl(col2: DoricColumn[T]): DoricColumn[T] =
(column.elem, col2.elem).mapN(f.nanvl).toDC
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package syntax

import cats.implicits._
import org.apache.spark.sql.Column
import org.apache.spark.sql.{functions => f}
import org.apache.spark.sql.catalyst.expressions.{ShiftLeft, ShiftRight, ShiftRightUnsigned}

private[syntax] trait NumericColumns2_31 {
Expand Down Expand Up @@ -46,6 +47,14 @@ private[syntax] trait NumericColumns2_31 {
(column.elem, numBits.elem)
.mapN((c, n) => new Column(ShiftRightUnsigned(c.expr, n.expr)))
.toDC

/**
* Computes bitwise NOT (~) of a number.
*
* @group Numeric Type
* @see [[org.apache.spark.sql.functions.bitwiseNOT]]
*/
def bitwiseNot: DoricColumn[T] = column.elem.map(f.bitwiseNOT).toDC
alfonsorr marked this conversation as resolved.
Show resolved Hide resolved
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package syntax

import cats.implicits._
import org.apache.spark.sql.Column
import org.apache.spark.sql.{functions => f}
import org.apache.spark.sql.catalyst.expressions.{ShiftLeft, ShiftRight, ShiftRightUnsigned}

private[syntax] trait NumericColumns32 {
Expand Down Expand Up @@ -46,6 +47,14 @@ private[syntax] trait NumericColumns32 {
(column.elem, numBits.elem)
.mapN((c, n) => new Column(ShiftRightUnsigned(c.expr, n.expr)))
.toDC

/**
* Computes bitwise NOT (~) of a number.
*
* @group Numeric Type
* @see [[org.apache.spark.sql.functions.bitwise_not]]
*/
def bitwiseNot: DoricColumn[T] = column.elem.map(f.bitwise_not).toDC
}

}
14 changes: 11 additions & 3 deletions core/src/test/scala/doric/Equalities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@ object Equalities {
}
}

private lazy val tolerance = 0.00001
implicit val eqDouble: Equality[Double] = new Equality[Double] {
override def areEqual(a: Double, b: Any): Boolean = (a, b) match {
case (x: Double, y: Double) => x === y +- 0.00001
case (x: Double, y: Double) => x === y +- tolerance
case _ => false
}
}

implicit val eqFloat: Equality[Float] = new Equality[Float] {
override def areEqual(a: Float, b: Any): Boolean = (a, b) match {
case (x: Float, y: Float) => x === y +- tolerance.toFloat
case _ => false
}
}

implicit val eqBigDecimal: Equality[BigDecimal] = new Equality[BigDecimal] {
override def areEqual(a: BigDecimal, b: Any): Boolean = (a, b) match {
case (x: BigDecimal, y: BigDecimal) => x === y +- 0.00001
case (x: BigDecimal, y: BigDecimal) => x === y +- tolerance
case _ => false
}
}
Expand All @@ -46,7 +54,7 @@ object Equalities {
override def areEqual(a: java.math.BigDecimal, b: Any): Boolean =
(a, b) match {
case (x: java.math.BigDecimal, y: java.math.BigDecimal) =>
x >= (y - 0.00001) && x <= (y + 0.00001)
x >= (y - tolerance) && x <= (y + tolerance)
case _ => false
}
}
Expand Down
5 changes: 2 additions & 3 deletions core/src/test/scala/doric/TypedColumnTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ trait TypedColumnTest extends Matchers with DatasetComparer {
df: DataFrame,
expected: List[Option[T]]
): Unit = {
import Equalities._

val eqCond: BooleanColumn = SparkType[T].dataType match {
case _: MapType =>
Expand Down Expand Up @@ -141,10 +140,10 @@ trait TypedColumnTest extends Matchers with DatasetComparer {
)

if (expected.nonEmpty) {
doricColumns.map {
assert(doricColumns.map {
case Some(x: java.lang.Double) if x.isNaN => None
case x => x
} === expected
} === expected)
}
}

Expand Down
Loading