Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty arrays returns the expected datatype #274

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions core/src/main/scala/doric/syntax/ArrayColumns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package doric
package syntax

import scala.language.higherKinds
import scala.reflect.ClassTag

import cats.data.Kleisli
import cats.implicits._
import doric.types.CollectionType
import doric.types.{CollectionType, LiteralSparkType, SparkType}

import org.apache.spark.sql.{Column, Dataset, functions => f}
import org.apache.spark.sql.catalyst.expressions._
Expand Down Expand Up @@ -58,8 +59,13 @@ private[syntax] trait ArrayColumns {
* @see org.apache.spark.sql.functions.array
* @todo scaladoc link (issue #135)
*/
def array[T](cols: DoricColumn[T]*): ArrayColumn[T] =
cols.toList.traverse(_.elem).map(f.array(_: _*)).toDC
def array[T: SparkType: ClassTag](
cols: DoricColumn[T]*
)(implicit lt: LiteralSparkType[Array[T]]): ArrayColumn[T] =
if (cols.nonEmpty)
cols.toList.traverse(_.elem).map(f.array(_: _*)).toDC
else
lit(Array.empty[T])

/**
* Creates a new list column. The input columns must all have the same data type.
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/scala/doric/syntax/ArrayColumnsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package doric
package syntax

import doric.sem.{ChildColumnNotFound, ColumnTypeError, DoricMultiError, SparkErrorWrapper}
import doric.types.SparkType

import org.apache.spark.sql.{Row, functions => f}
import org.apache.spark.sql.types.{IntegerType, LongType, StringType}
Expand Down Expand Up @@ -300,6 +301,20 @@ class ArrayColumnsSpec extends DoricTestElements {
List(Some(Array("a", "b")))
)
}

it("should work be of the expected type when is empty") {
val df = spark
.range(5)
.select(
array[Long]().as("l"),
array[String]().as("s"),
array[(String, String)]().as("r")
)

df("l").expr.dataType === SparkType[Array[Long]].dataType
df("s").expr.dataType === SparkType[Array[String]].dataType
df("r").expr.dataType === SparkType[Array[(String, String)]].dataType
}
}

describe("list doric function") {
Expand Down