Skip to content

Commit

Permalink
Issue-272: Add String Comparison operators (hablapps#355)
Browse files Browse the repository at this point in the history
* Issue-272: Add String Comparison operators

* Issue-272: Fix formatting
  • Loading branch information
Amalicia authored May 16, 2023
1 parent 8eef33d commit 619e228
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
25 changes: 25 additions & 0 deletions core/src/main/scala/doric/syntax/StringColumns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package doric
package syntax

import cats.implicits._
import doric.DoricColumn.sparkFunction
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.types.{DataType, StructType}
import org.apache.spark.sql.{Column, functions => f}
Expand Down Expand Up @@ -689,5 +690,29 @@ protected trait StringColumns {
.map(x => new Column(JsonTuple(x.map(_.expr))))
.toDC
}

/**
* @group Comparable Type
*/
def <(other: StringColumn): BooleanColumn =
sparkFunction[String, Boolean](s, other, _ < _)

/**
* @group Comparable Type
*/
def <=(other: StringColumn): BooleanColumn =
sparkFunction[String, Boolean](s, other, _ <= _)

/**
* @group Comparable Type
*/
def >(other: StringColumn): BooleanColumn =
sparkFunction[String, Boolean](s, other, _ > _)

/**
* @group Comparable Type
*/
def >=(other: StringColumn): BooleanColumn =
sparkFunction[String, Boolean](s, other, _ >= _)
}
}
40 changes: 40 additions & 0 deletions core/src/test/scala/doric/syntax/StringColumnsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1048,4 +1048,44 @@ class StringColumnsSpec extends DoricTestElements {
}
}

describe("comparison operators") {
import spark.implicits._

val df =
List(("abc", "xyz"), ("abc", "abc"), ("xyz", "abc"), ("xyz", "xyz"))
.toDF("col1", "col2")

it("> should work as spark > function") {
df.testColumns2("col1", "col2")(
(col1, col2) => colString(col1) > colString(col2),
(col1, col2) => f.col(col1) > f.col(col2),
List(false, false, true, false).map(Option(_))
)
}

it(">= should work as spark >= function") {
df.testColumns2("col1", "col2")(
(col1, col2) => colString(col1) >= colString(col2),
(col1, col2) => f.col(col1) >= f.col(col2),
List(false, true, true, true).map(Option(_))
)
}

it("< should work as spark < function") {
df.testColumns2("col1", "col2")(
(col1, col2) => colString(col1) < colString(col2),
(col1, col2) => f.col(col1) < f.col(col2),
List(true, false, false, false).map(Option(_))
)
}

it("<= should work as spark <= function") {
df.testColumns2("col1", "col2")(
(col1, col2) => colString(col1) <= colString(col2),
(col1, col2) => f.col(col1) <= f.col(col2),
List(true, true, false, true).map(Option(_))
)
}
}

}

0 comments on commit 619e228

Please sign in to comment.