-
Notifications
You must be signed in to change notification settings - Fork 138
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
6 changed files
with
165 additions
and
83 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
84 changes: 84 additions & 0 deletions
84
dataset/src/main/scala/frameless/TypedColumnMacroImpl.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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package frameless | ||
|
||
import scala.reflect.macros.whitebox | ||
|
||
private[frameless] object TypedColumnMacroImpl { | ||
|
||
def applyImpl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: whitebox.Context)(x: c.Tree): c.Expr[TypedColumn[T, U]] = { | ||
import c.universe._ | ||
|
||
val t = c.weakTypeOf[T] | ||
val u = c.weakTypeOf[U] | ||
|
||
def buildExpression(path: List[String]): c.Expr[TypedColumn[T, U]] = { | ||
val columnName = path.mkString(".") | ||
|
||
c.Expr[TypedColumn[T, U]](q"new _root_.frameless.TypedColumn[$t, $u]((org.apache.spark.sql.functions.col($columnName)).expr)") | ||
} | ||
|
||
def abort(msg: String) = c.abort(c.enclosingPosition, msg) | ||
|
||
@annotation.tailrec | ||
def path(in: Select, out: List[TermName]): List[TermName] = | ||
in.qualifier match { | ||
case sub: Select => | ||
path(sub, in.name.toTermName :: out) | ||
|
||
case id: Ident => | ||
id.name.toTermName :: in.name.toTermName :: out | ||
|
||
case u => | ||
abort(s"Unsupported selection: $u") | ||
} | ||
|
||
@annotation.tailrec | ||
def check(current: Type, in: List[TermName]): Boolean = in match { | ||
case next :: tail => { | ||
val sym = current.decl(next).asTerm | ||
|
||
if (!sym.isStable) { | ||
abort(s"Stable term expected: ${current}.${next}") | ||
} | ||
|
||
check(sym.info, tail) | ||
} | ||
|
||
case _ => | ||
true | ||
} | ||
|
||
x match { | ||
case fn: Function => fn.body match { | ||
case select: Select if select.name.isTermName => | ||
val expectedRoot: Option[String] = fn.vparams match { | ||
case List(rt) if rt.rhs == EmptyTree => | ||
Option.empty[String] | ||
|
||
case List(rt) => | ||
Some(rt.toString) | ||
|
||
case u => | ||
abort(s"Select expression must have a single parameter: ${u mkString ", "}") | ||
} | ||
|
||
path(select, List.empty) match { | ||
case root :: tail if ( | ||
expectedRoot.forall(_ == root) && check(t, tail)) => { | ||
val colPath = tail.mkString(".") | ||
|
||
c.Expr[TypedColumn[T, U]](q"new _root_.frameless.TypedColumn[$t, $u]((org.apache.spark.sql.functions.col($colPath)).expr)") | ||
} | ||
|
||
case _ => | ||
abort(s"Invalid select expression: $select") | ||
} | ||
|
||
case t => | ||
abort(s"Select expression expected: $t") | ||
} | ||
|
||
case _ => | ||
abort(s"Function expected: $x") | ||
} | ||
} | ||
} |
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,27 @@ | ||
package frameless | ||
|
||
/** Compute the intersection of two types: | ||
* | ||
* - With[A, A] = A | ||
* - With[A, B] = A with B (when A != B) | ||
* | ||
* This type function is needed to prevent IDEs from infering large types | ||
* with shape `A with A with ... with A`. These types could be confusing for | ||
* both end users and IDE's type checkers. | ||
*/ | ||
trait With[A, B] { type Out } | ||
|
||
object With extends LowPrioWith { | ||
implicit def combine[A, B]: Aux[A, B, A with B] = of[A, B, A with B] | ||
} | ||
|
||
private[frameless] sealed trait LowPrioWith { | ||
type Aux[A, B, W] = With[A, B] { type Out = W } | ||
|
||
protected[this] val theInstance = new With[Any, Any] {} | ||
|
||
protected[this] def of[A, B, W]: With[A, B] { type Out = W } = | ||
theInstance.asInstanceOf[Aux[A, B, W]] | ||
|
||
implicit def identity[T]: Aux[T, T, T] = of[T, T, T] | ||
} |
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