-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from VirtusLab/conditional-columns
Support conditional columns with `when`
- Loading branch information
Showing
13 changed files
with
318 additions
and
196 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
package org.virtuslab.iskra | ||
|
||
import scala.quoted.* | ||
import types.{DataType, StructType} | ||
import types.{DataType, Encoder, StructType, StructEncoder} | ||
|
||
object UntypedOps: | ||
extension (untyped: UntypedColumn) | ||
def typed[A <: DataType] = Column[A](untyped) | ||
|
||
extension (df: UntypedDataFrame) | ||
transparent inline def typed[A](using encoder: DataType.StructEncoder[A]): DataFrame[?] = ${ typedDataFrameImpl('df, 'encoder) } // TODO: Check schema at runtime? Check if names of columns match? | ||
transparent inline def typed[A](using encoder: StructEncoder[A]): DataFrame[?] = ${ typedDataFrameImpl('df, 'encoder) } // TODO: Check schema at runtime? Check if names of columns match? | ||
|
||
private def typedDataFrameImpl[A : Type](df: Expr[UntypedDataFrame], encoder: Expr[DataType.StructEncoder[A]])(using Quotes) = | ||
private def typedDataFrameImpl[A : Type](df: Expr[UntypedDataFrame], encoder: Expr[StructEncoder[A]])(using Quotes) = | ||
encoder match | ||
case '{ ${e}: DataType.Encoder.Aux[tpe, StructType[t]] } => | ||
case '{ ${e}: Encoder.Aux[tpe, StructType[t]] } => | ||
'{ DataFrame[t](${ df }) } |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.virtuslab.iskra | ||
|
||
import org.apache.spark.sql.{functions => f, Column => UntypedColumn} | ||
import org.virtuslab.iskra.types.{Coerce, DataType, BooleanOptType} | ||
|
||
object When: | ||
class WhenColumn[T <: DataType](untyped: UntypedColumn) extends Column[DataType.Nullable[T]](untyped): | ||
def when[U <: DataType](condition: Column[BooleanOptType], value: Column[U])(using coerce: Coerce[T, U]): WhenColumn[coerce.Coerced] = | ||
WhenColumn(this.untyped.when(condition.untyped, value.untyped)) | ||
def otherwise[U <: DataType](value: Column[U])(using coerce: Coerce[T, U]): Column[coerce.Coerced] = | ||
Column(this.untyped.otherwise(value.untyped)) | ||
|
||
def when[T <: DataType](condition: Column[BooleanOptType], value: Column[T]): WhenColumn[T] = | ||
WhenColumn(f.when(condition.untyped, value.untyped)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.virtuslab.iskra | ||
package functions | ||
|
||
export When.when |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.virtuslab.iskra | ||
package types | ||
|
||
import DataType.{CommonNumericNonNullableType, CommonNumericNullableType, NumericOptType, NumericType} | ||
|
||
trait Coerce[-A <: DataType, -B <: DataType]: | ||
type Coerced <: DataType | ||
|
||
object Coerce: | ||
given sameType[A <: DataType]: Coerce[A, A] with | ||
override type Coerced = A | ||
|
||
given nullable[A <: NumericOptType, B <: NumericOptType]: Coerce[A, B] with | ||
override type Coerced = CommonNumericNullableType[A, B] | ||
|
||
given nonNullable[A <: NumericType, B <: NumericType]: Coerce[A, B] with | ||
override type Coerced = CommonNumericNonNullableType[A, B] |
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
Oops, something went wrong.