From 98493698e781358ac1f0283f2aeb7305a406b7e4 Mon Sep 17 00:00:00 2001 From: Jolanrensen Date: Wed, 13 Jul 2022 13:15:28 +0200 Subject: [PATCH] added seqOf functions and tests --- .../org/jetbrains/kotlinx/spark/api/Seq.kt | 40 +++++++++++++++++++ .../jetbrains/kotlinx/spark/api/ApiTest.kt | 12 ++++++ 2 files changed, 52 insertions(+) create mode 100644 kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/Seq.kt 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) }