-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 55 additions & 30 deletions
85
core/src/main/scala/com/github/mrpowers/spark/fast/tests/DatasetUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,74 @@ | ||
package com.github.mrpowers.spark.fast.tests | ||
|
||
import org.apache.spark.sql.catalyst.encoders.AgnosticEncoders.OptionEncoder | ||
import org.apache.spark.sql.catalyst.encoders.{AgnosticEncoder, ExpressionEncoder, RowEncoder} | ||
import org.apache.spark.sql.expressions.Window | ||
import org.apache.spark.sql.functions._ | ||
import org.apache.spark.sql.{DataFrame, Dataset, Encoder, Row} | ||
import org.apache.spark.sql.types.StructType | ||
import org.apache.spark.sql.{DataFrame, Dataset, Encoder, Encoders, Row, TypedColumn} | ||
|
||
import scala.reflect.ClassTag | ||
import scala.reflect.runtime.universe.TypeTag | ||
|
||
private object DatasetUtils { | ||
implicit class DatasetOps[T: ClassTag](ds: Dataset[T]) { | ||
implicit class DatasetOps[T: ClassTag: TypeTag](ds: Dataset[T]) { | ||
def zipWithIndex(indexName: String): DataFrame = ds | ||
.orderBy() | ||
.withColumn(indexName, row_number().over(Window.orderBy(monotonically_increasing_id()))) | ||
.select(ds.columns.map(col) :+ col(indexName): _*) | ||
|
||
def joinPair( | ||
other: Dataset[T], | ||
primaryKeys: Seq[String] | ||
): Dataset[(T, T)] = { | ||
if (primaryKeys.nonEmpty) { | ||
ds | ||
.as("l") | ||
.joinWith(other.as("r"), primaryKeys.map(k => col(s"l.$k") === col(s"r.$k")).reduce(_ && _)) | ||
/** | ||
* Check if the primary key is actually unique | ||
*/ | ||
def isKeyUnique(primaryKey: Seq[String]): Boolean = | ||
ds.select(primaryKey.map(col): _*).distinct.count == ds.count | ||
|
||
def outerJoinWith[P: ClassTag: TypeTag]( | ||
other: Dataset[P], | ||
primaryKeys: Seq[String], | ||
outerJoinType: String = "full" | ||
): Dataset[(Option[T], Option[P])] = { | ||
val (ds1, ds2, key) = if (primaryKeys.nonEmpty) { | ||
(ds, other, primaryKeys) | ||
} else { | ||
val indexName = s"index_${java.util.UUID.randomUUID}" | ||
val columns = ds.columns | ||
val joined = ds | ||
.zipWithIndex(indexName) | ||
.alias("l") | ||
.join(other.zipWithIndex(indexName).alias("r"), indexName) | ||
|
||
val encoder: Encoder[T] = ds.encoder | ||
val leftCols = columns.map(n => col(s"l.$n")) | ||
val rightCols = columns.map(n => col(s"r.$n")) | ||
val (pair1, pair2) = | ||
if (columns.length == 1 && !(implicitly[ClassTag[T]].runtimeClass == classOf[Row])) | ||
(leftCols.head, rightCols.head) | ||
else | ||
(struct(leftCols: _*), struct(rightCols: _*)) | ||
|
||
joined | ||
.select( | ||
pair1.as("l").as[T](encoder), | ||
pair2.as("r").as[T](encoder) | ||
) | ||
(ds.zipWithIndex(indexName), other.zipWithIndex(indexName), Seq(indexName)) | ||
} | ||
|
||
val joined = ds1 | ||
.as("l") | ||
.join(ds2.as("r"), key, s"${outerJoinType}_outer") | ||
|
||
joined.select(encoderToOptionTypedCol[T]("l", ds.schema, key), encoderToOptionTypedCol[P]("r", other.schema, key)) | ||
} | ||
} | ||
|
||
private def encoderToOptionTypedCol[P: ClassTag: TypeTag]( | ||
colName: String, | ||
schema: StructType, | ||
key: Seq[String] | ||
): TypedColumn[Any, Option[P]] = { | ||
val columns = schema.names.map(n => col(s"$colName.$n")) | ||
val isRowType = implicitly[ClassTag[P]].runtimeClass == classOf[Row] | ||
val unTypedColumn = | ||
if (columns.length == 1 && !isRowType) | ||
columns.head | ||
else | ||
when(key.map(k => col(s"$colName.$k").isNull).reduce(_ && _), lit(null)).otherwise(struct(columns: _*)) | ||
|
||
val enc: Encoder[Option[P]] = if (isRowType) { | ||
ExpressionEncoder(OptionEncoder(RowEncoder.encoderFor(schema).asInstanceOf[AgnosticEncoder[P]])) | ||
} else { | ||
ExpressionEncoder() | ||
} | ||
unTypedColumn.as(colName).as[Option[P]](enc) | ||
} | ||
|
||
def encoderToRowCol( | ||
colName: String, | ||
schema: StructType | ||
): TypedColumn[Any, Row] = { | ||
val columns = schema.names.map(n => col(s"$colName.$n")) | ||
struct(columns: _*).as(colName).as[Row](Encoders.row(schema)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters