Skip to content

Commit

Permalink
Allow columns with types on empty dataframes
Browse files Browse the repository at this point in the history
  • Loading branch information
Johs Kristoffersen committed Feb 2, 2021
1 parent 57f037c commit 86bfe4e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/main/scala/com/audienceproject/crossbow/DataFrame.scala
Expand Up @@ -175,9 +175,7 @@ class DataFrame private(private val columnData: Vector[Array[_]], val schema: Sc
val eval = expr.compile(this)
val ord = Order.getOrdering(eval.typeOf, givenOrderings)
val indices = Array.tabulate(rowCount)(identity)
Sorting.quickSort[Int](indices)(new Ordering[Int] {
override def compare(x: Int, y: Int): Int = ord.compare(eval(x), eval(y))
})
Sorting.quickSort[Int](indices)((x: Int, y: Int) => ord.compare(eval(x), eval(y)))
slice(indices.toIndexedSeq, if (givenOrderings.isEmpty) Some(expr) else None)
}
}
Expand Down Expand Up @@ -338,8 +336,7 @@ object DataFrame {
* @return new DataFrame
*/
def fromSeq[T: ru.TypeTag](data: Seq[T]): DataFrame = {
if (data.isEmpty) empty()
else {

val dataType = Types.toInternalType(ru.typeOf[T])
dataType match {
case ProductType(elementTypes@_*) =>
Expand All @@ -351,7 +348,7 @@ object DataFrame {
val col = convert(data, dataType)
new DataFrame(Vector(col), Schema(List(new Column("_0", dataType))))
}
}

}

/**
Expand Down
@@ -0,0 +1,13 @@
package com.audienceproject.crossbow.core

import com.audienceproject.crossbow.{DataFrame, expr}
import com.audienceproject.crossbow.schema.{Column, Schema}
import org.scalatest.funsuite.AnyFunSuite

class ConstuctionTest extends AnyFunSuite {
test("construct from typed empty seq") {
val df = DataFrame.fromSeq(Seq.empty[(Int,Long)])
assertResult(2)(df.numColumns)
assertResult(Schema(Seq(Column("_0",expr.IntType),Column("_1",expr.LongType))))(df.schema)
}
}

0 comments on commit 86bfe4e

Please sign in to comment.