Skip to content

Commit

Permalink
fix: [~] str interpolator now prints empty if null column (issue #191) (
Browse files Browse the repository at this point in the history
  • Loading branch information
eruizalo committed Mar 29, 2022
1 parent ae94977 commit a74e078
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions core/src/main/scala/doric/syntax/Interpolators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ package syntax
trait Interpolators {
implicit class doricStringInterpolator(val sc: StringContext) {
def ds(columns: StringColumn*): StringColumn = {
val literals: Seq[DoricColumn[String]] = sc.parts.map(_.lit)
val literals: Seq[StringColumn] = sc.parts.map(_.lit)

val cols: Seq[DoricColumn[String]] = literals
val cols: Seq[StringColumn] = literals
.zipAll(columns, "".lit, "".lit)
.flatMap { case (a, b) =>
List(a, b)
}

concat(cols: _*)
concatWs("".lit, cols: _*)
}
}
}
17 changes: 15 additions & 2 deletions core/src/test/scala/doric/syntax/InterpolatorsSpec.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package doric
package syntax

import org.apache.spark.sql.{functions => f}
import org.scalatest.EitherValues
import org.scalatest.matchers.should.Matchers

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

class InterpolatorsSpec
extends DoricTestElements
with EitherValues
Expand Down Expand Up @@ -34,6 +33,20 @@ class InterpolatorsSpec
)
)
}

it("should work if null columns") {
val dfNull = List("value 1", null)
.toDF(colName)
dfNull.testColumns2(colName, "Column has value:")(
(c, str) => ds"${str.lit} -->${colString(c)}",
(c, str) =>
f.concat(f.lit(str), f.lit(" -->"), f.coalesce(f.col(c), f.lit(""))),
List(
Some("Column has value: -->value 1"),
Some("Column has value: -->")
)
)
}
}

}

0 comments on commit a74e078

Please sign in to comment.