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

Add Comparison Operators to Date Columns #230

Merged
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
27 changes: 26 additions & 1 deletion core/src/main/scala/doric/syntax/DateColumns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package doric
package syntax

import cats.implicits._
import doric.DoricColumn.sparkFunction
import doric.types.{DateType, SparkType}
import java.sql.Date

import java.sql.Date
import org.apache.spark.sql.{Column, functions => f}
import org.apache.spark.sql.catalyst.expressions.{AddMonths, DateAdd, DateFormatClass, DateSub, MonthsBetween, NextDay, TruncDate, TruncTimestamp}

Expand All @@ -23,6 +24,30 @@ private[syntax] trait DateColumns {
column: DoricColumn[T]
) {

/**
* @group Comparable Type
*/
def >=(other: DoricColumn[T]): BooleanColumn =
sparkFunction[T, Boolean](column, other, _ >= _)

/**
* @group Comparable Type
*/
def <=(other: DoricColumn[T]): BooleanColumn =
sparkFunction[T, Boolean](column, other, _ <= _)

/**
* @group Comparable Type
*/
def >(other: DoricColumn[T]): BooleanColumn =
sparkFunction[T, Boolean](column, other, _ > _)

/**
* @group Comparable Type
*/
def <(other: DoricColumn[T]): BooleanColumn =
sparkFunction[T, Boolean](column, other, _ < _)

/**
* Adds to the Date or Timestamp column the number of months
*
Expand Down
44 changes: 44 additions & 0 deletions core/src/test/scala/doric/DateColumnTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package doric

import doric.types.{DateType, SparkType}
import org.apache.spark.sql.DataFrame
import org.scalatest.funspec.AnyFunSpecLike

import scala.reflect.ClassTag

trait DateColumnTest extends AnyFunSpecLike with TypedColumnTest {

def df: DataFrame

def test[T: DateType: SparkType: ClassTag](tagName: String): Unit = {

describe(s"Date type $tagName") {
it(">") {
test[T, T, Boolean](tagName, (a, b) => a > b)
}
it(">=") {
test[T, T, Boolean](tagName, (a, b) => a >= b)
}
it("<") {
test[T, T, Boolean](tagName, (a, b) => a < b)
}
it("<=") {
test[T, T, Boolean](tagName, (a, b) => a <= b)
}
}
}

def test[T1: SparkType: ClassTag, T2: SparkType: ClassTag, O: SparkType](
tagName: String,
f: (DoricColumn[T1], DoricColumn[T2]) => DoricColumn[O]
): Unit =
df.validateColumnType(
f(
col[T1](getName(tagName)),
col[T2](getName(tagName))
)
)

def getName(tagName: String, pos: Int = 1): String =
s"col_${tagName}_$pos"
}
22 changes: 19 additions & 3 deletions core/src/test/scala/doric/syntax/DateColumnsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@ import java.sql.{Date, Timestamp}
import java.time.{Instant, LocalDate}
import org.scalatest.EitherValues
import org.scalatest.matchers.should.Matchers

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

class DateColumnsSpec
extends DoricTestElements
with EitherValues
with Matchers {
with Matchers
with DateColumnTest {

import spark.implicits._

def df: DataFrame =
List(
(
Date.valueOf("2022-05-27"),
new Timestamp(1653669106840L)
)
).toDF(
getName("date"),
getName("timestamp")
)

test[Date]("date")
test[Timestamp]("timestamp")

describe("currentDate doric function") {
import spark.implicits._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ package doric
package syntax

import java.sql.Date
import java.time.LocalDate
import java.time.{Instant, LocalDate}
import org.scalatest.EitherValues
import org.scalatest.matchers.should.Matchers

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

class DateColumns3xSpec
extends DoricTestElements
with EitherValues
with Matchers {
with Matchers
with DateColumnTest {

import spark.implicits._

def df: DataFrame =
List(
(
LocalDate.parse("2022-05-27"),
Instant.ofEpochMilli(1653669106840L)
)
).toDF(
getName("localDate"),
getName("instant")
)

test[LocalDate]("localDate")
test[Instant]("instant")

describe("addMonths doric function with column") {
import spark.implicits._
Expand Down