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

Refactor partitionByKey to get clearer error #5527

Merged
merged 1 commit into from
Nov 22, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,8 @@ sealed trait SCollection[T] extends PCollectionWrapper[T] {
* @group collection
*/
def partitionByKey[U](partitionKeys: Set[U])(f: T => U): Map[U, SCollection[T]] = {
val partitionKeysIndexed = partitionKeys.toIndexedSeq

partitionKeysIndexed
.zip(partition(partitionKeys.size, (t: T) => partitionKeysIndexed.indexOf(f(t))))
.toMap
val partitions = partitionKeys.zipWithIndex.toMap
partitionKeys.zip(partition(partitionKeys.size, x => partitions(f(x)))).toMap
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import scala.jdk.CollectionConverters._
import com.spotify.scio.coders.{Beam, Coder, MaterializedCoder}
import com.spotify.scio.options.ScioOptions
import com.spotify.scio.schemas.Schema
import org.apache.beam.sdk.Pipeline.PipelineExecutionException
import org.apache.beam.sdk.coders.{NullableCoder, StringUtf8Coder}

import java.nio.charset.StandardCharsets
Expand Down Expand Up @@ -249,6 +250,16 @@ class SCollectionTest extends PipelineSpec {
m("b") should containInAnyOrder(Seq("b4", "b5"))
m("c") should containInAnyOrder(Seq("c6"))
}

val e = the[PipelineExecutionException] thrownBy {
runWithContext { sc =>
sc
.parallelize(Seq("x"))
.partitionByKey(Set("a", "b", "c"))(_.substring(0, 1))
}
}
e.getCause shouldBe a[NoSuchElementException]
e.getCause.getMessage shouldBe "key not found: x"
}

it should "support hashPartition() based on Object.hashCode()" in {
Expand Down