diff --git a/kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/Seq.kt b/kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/Seq.kt new file mode 100644 index 00000000..2cb44ca2 --- /dev/null +++ b/kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/Seq.kt @@ -0,0 +1,40 @@ +package org.jetbrains.kotlinx.spark.api + +import scala.collection.immutable.`Seq$`.`MODULE$` as Seq +import scala.collection.immutable.Seq as Seq +import scala.collection.mutable.`Seq$`.`MODULE$` as MutableSeq +import scala.collection.mutable.Seq as MutableSeq + +/** + * Returns a new empty immutable Seq. + */ +fun emptySeq(): Seq = Seq.empty() as Seq + +/** + * Returns a new immutable Seq with the given elements. + */ +fun seqOf(vararg elements: T): Seq = + if (elements.isEmpty()) + emptySeq() + else + Seq.newBuilder().apply { + for (it in elements) + `$plus$eq`(it) + }.result() as Seq + +/** + * Returns a new mutable Seq with the given elements. + */ +fun emptyMutableSeq(): MutableSeq = MutableSeq.empty() as MutableSeq + +/** + * Returns a new mutable Seq with the given elements. + */ +fun mutableSeqOf(vararg elements: T): MutableSeq = + if (elements.isEmpty()) + emptyMutableSeq() + else + MutableSeq.newBuilder().apply { + for (it in elements) + `$plus$eq`(it) + }.result() as MutableSeq diff --git a/kotlin-spark-api/src/test/kotlin/org/jetbrains/kotlinx/spark/api/ApiTest.kt b/kotlin-spark-api/src/test/kotlin/org/jetbrains/kotlinx/spark/api/ApiTest.kt index c3362e91..20044807 100644 --- a/kotlin-spark-api/src/test/kotlin/org/jetbrains/kotlinx/spark/api/ApiTest.kt +++ b/kotlin-spark-api/src/test/kotlin/org/jetbrains/kotlinx/spark/api/ApiTest.kt @@ -33,6 +33,18 @@ class ApiTest : ShouldSpec({ context("miscellaneous integration tests") { withSpark(props = mapOf("spark.sql.codegen.comments" to true)) { + should("Create Seqs") { + spark.createDataset(seqOf(1, 2, 3), encoder()) + .collectAsList() shouldBe listOf(1, 2, 3) + + + seqOf(1, 2, 3) shouldBe seqOf(1, 2, 3) + mutableSeqOf(1, 2, 3) shouldBe mutableSeqOf(1, 2, 3) + + seqOf() shouldBe emptySeq() + mutableSeqOf() shouldBe emptyMutableSeq() + } + @OptIn(ExperimentalStdlibApi::class) should("broadcast variables") { val largeList = (1..15).map { SomeClass(a = (it..15).toList().toIntArray(), b = it) }