From a74e0785a083bc529977b44731cd16b413f10f9b Mon Sep 17 00:00:00 2001 From: Eduardo Ruiz Alonso Date: Tue, 29 Mar 2022 09:51:10 +0200 Subject: [PATCH] fix: [~] str interpolator now prints empty if null column (issue #191) (#199) --- .../main/scala/doric/syntax/Interpolators.scala | 6 +++--- .../scala/doric/syntax/InterpolatorsSpec.scala | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/doric/syntax/Interpolators.scala b/core/src/main/scala/doric/syntax/Interpolators.scala index efeab86a9..191951387 100644 --- a/core/src/main/scala/doric/syntax/Interpolators.scala +++ b/core/src/main/scala/doric/syntax/Interpolators.scala @@ -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: _*) } } } diff --git a/core/src/test/scala/doric/syntax/InterpolatorsSpec.scala b/core/src/test/scala/doric/syntax/InterpolatorsSpec.scala index e3562569f..0396f8e6a 100644 --- a/core/src/test/scala/doric/syntax/InterpolatorsSpec.scala +++ b/core/src/test/scala/doric/syntax/InterpolatorsSpec.scala @@ -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 @@ -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: -->") + ) + ) + } } }