Skip to content

Commit

Permalink
LeftJoin with empty time series. Fixed #116 (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenta authored and klangner committed Jun 18, 2018
1 parent 73f5f84 commit f3cfd0b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion shared/src/main/scala/carldata/series/TimeSeries.scala
Expand Up @@ -460,7 +460,12 @@ case class TimeSeries[V](idx: Vector[Instant], ds: Vector[V]) {

@tailrec def joinR(xs: Vector[(Instant, V)],
ys: Vector[(Instant, U)]): Seq[(Instant, (V, U))] = {
if (xs.isEmpty || ys.isEmpty) builder
if (xs.isEmpty && ys.isEmpty) builder
else if (ys.isEmpty) {
xs.foreach(x => builder.append((x._1, (x._2, default))))
builder
}
else if (xs.isEmpty) builder
else {
val x = xs.head
val y = ys.head
Expand Down
33 changes: 33 additions & 0 deletions shared/src/test/scala/carldata/series/TimeSeriesTest.scala
Expand Up @@ -353,6 +353,39 @@ class TimeSeriesTest extends FlatSpec with Matchers {
series1.joinLeft(series2, 0) shouldBe expected
}

it should "left join 2 series, second empty" in {
val now = Instant.now()
val idx1 = Vector(now.plusSeconds(1), now.plusSeconds(2), now.plusSeconds(4), now.plusSeconds(5))
val vs = Vector(1, 4, 6, 8)
val series1 = TimeSeries(idx1, vs)
val expected = TimeSeries(idx1, Vector((1, 0), (4, 0), (6, 0), (8, 0)))

series1.joinLeft(TimeSeries.empty, 0) shouldBe expected
}

it should "left join 2 series, second less elements" in {
val now = Instant.now()
val idx1 = Vector(now.plusSeconds(1), now.plusSeconds(2), now.plusSeconds(4), now.plusSeconds(5))
val idx2 = Vector(now.plusSeconds(2), now.plusSeconds(3), now.plusSeconds(4))
val vs = Vector(1, 4, 6, 8)
val series1 = TimeSeries(idx1, vs)
val series2 = TimeSeries(idx2, vs.tail)
val expected = TimeSeries(idx1, Vector((1, 0), (4, 4), (6, 8), (8, 0)))

series1.joinLeft(series2, 0) shouldBe expected
}

it should "left join 2 series, second more elements" in {
val now = Instant.now()
val idx1 = Vector(now.plusSeconds(1), now.plusSeconds(2), now.plusSeconds(4))
val idx2 = Vector(now.plusSeconds(2), now.plusSeconds(3), now.plusSeconds(4), now.plusSeconds(5))
val vs = Vector(1, 4, 6, 8)
val series1 = TimeSeries(idx1, vs.tail)
val series2 = TimeSeries(idx2, vs)
val expected = TimeSeries(idx1, Vector((4, 0), (6, 1), (8, 6)))

series1.joinLeft(series2, 0) shouldBe expected }

it should "outer join 2 series" in {
val now = Instant.now()
val idx1 = Vector(now.plusSeconds(1), now.plusSeconds(2), now.plusSeconds(4), now.plusSeconds(5))
Expand Down

0 comments on commit f3cfd0b

Please sign in to comment.