Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progress on implementing the DSL according to Scala 3 idioms
- Loading branch information
Showing
21 changed files
with
228 additions
and
283 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
193 changes: 80 additions & 113 deletions
193
src/main/scala/com/audienceproject/crossbow/DataFrame.scala
Large diffs are not rendered by default.
Oops, something went wrong.
44 changes: 0 additions & 44 deletions
44
src/main/scala/com/audienceproject/crossbow/Implicits.scala
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,51 @@ | ||
package com.audienceproject.crossbow | ||
|
||
import com.audienceproject.crossbow.exceptions.InvalidExpressionException | ||
import com.audienceproject.crossbow.expr.* | ||
|
||
enum JoinType: | ||
case Inner, FullOuter, LeftOuter, RightOuter | ||
|
||
// Column expression. | ||
extension (sc: StringContext) | ||
def $(args: Any*): DataFrame ?=> Expr = Expr.Cell(sc.s(args: _*)) | ||
|
||
extension [T: TypeTag](seq: Seq[T]) | ||
def toDataFrame(names: String*): DataFrame = | ||
if (names.isEmpty) DataFrame.fromSeq[T](seq) | ||
else DataFrame.fromSeq[T](seq).renameColumns(names *) | ||
|
||
// Literal value. | ||
def lit[T: TypeTag](value: T): Expr = Expr.Literal(value) | ||
|
||
given Conversion[Int, Expr] = lit[Int](_) | ||
given Conversion[Long, Expr] = lit[Long](_) | ||
given Conversion[Double, Expr] = lit[Double](_) | ||
given Conversion[Boolean, Expr] = lit[Boolean](_) | ||
given Conversion[String, Expr] = lit[String](_) | ||
|
||
// Index function. | ||
def index(): DataFrame ?=> Expr = Expr.Index() | ||
|
||
// Lambda function. | ||
def lambda[T, R](f: T => R): Expr => Expr = | ||
(expr: Expr) => Expr.Unary(expr, f) | ||
|
||
// Sequence of values. | ||
def seq(exprs: Expr*): Expr = Expr.List(exprs) | ||
|
||
// Aggregators. | ||
def sum(expr: Expr): Expr = expr.typeOf match | ||
case RuntimeType.Int => Expr.Aggregate[Int, Int](expr, _ + _, 0) | ||
case RuntimeType.Long => Expr.Aggregate[Long, Long](expr, _ + _, 0L) | ||
case RuntimeType.Double => Expr.Aggregate[Double, Double](expr, _ + _, 0d) | ||
case _ => throw new InvalidExpressionException("sum", expr) | ||
|
||
def count(): Expr = Expr.Aggregate[Any, Int](lit(1), (_, x) => x + 1, 0) | ||
|
||
def collect(expr: Expr): Expr = Expr.Aggregate[Any, Seq[Any]](expr, (e, seq) => seq :+ e, Vector.empty) | ||
|
||
def one(expr: Expr): Expr = Expr.Aggregate[Any, Any](expr, (elem, _) => elem, null) | ||
|
||
// Custom aggregator. | ||
def reducer[T, U](seed: U)(f: (T, U) => U): Expr => Expr = Expr.Aggregate[T, U](_, f, seed) |
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
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
26 changes: 15 additions & 11 deletions
26
src/main/scala/com/audienceproject/crossbow/schema/Schema.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,38 +1,42 @@ | ||
package com.audienceproject.crossbow.schema | ||
|
||
case class Schema(columns: Seq[Column] = Seq.empty) { | ||
import com.audienceproject.crossbow.expr.RuntimeType | ||
|
||
case class Schema(columns: Seq[Column] = Seq.empty): | ||
|
||
val size: Int = columns.length | ||
|
||
def add(column: Column): Schema = Schema(columns :+ column) | ||
|
||
def get(columnName: String): Column = columns.find(_.name == columnName).getOrElse( | ||
throw new NoSuchElementException(s"Schema does not contain a column with name '$columnName''") | ||
) | ||
def get(columnName: String): Column = | ||
columns.find(_.name == columnName).getOrElse: | ||
throw new NoSuchElementException(s"Schema does not contain a column with name '$columnName''") | ||
|
||
def indexOf(columnName: String): Int = { | ||
def indexOf(columnName: String): Int = | ||
val index = columns.indexWhere(_.name == columnName) | ||
if (index >= 0) index | ||
else throw new NoSuchElementException(s"Schema does not contain a column with name '$columnName''") | ||
} | ||
|
||
private[crossbow] def matchColumns(other: Schema): Seq[(Column, Either[(Int, Int), Either[Int, Int]])] = { | ||
def toRuntimeType: RuntimeType = columns match | ||
case Seq(single) => single.columnType | ||
case _ => RuntimeType.Product(columns.map(_.columnType) *) | ||
|
||
private[crossbow] def matchColumns(other: Schema): Seq[(Column, Either[(Int, Int), Either[Int, Int]])] = | ||
val leftMatches = columns.zipWithIndex.map({ | ||
case (thisCol, i) => | ||
val j = other.columns.indexWhere(_.name == thisCol.name) | ||
if (j >= 0) { | ||
val otherCol = other.columns(j) | ||
if (thisCol.columnType != otherCol.columnType) | ||
throw new IllegalArgumentException(s"Columns $thisCol and $otherCol do not match. Please check schemas.") | ||
throw IllegalArgumentException(s"Columns $thisCol and $otherCol do not match. Please check schemas.") | ||
else (thisCol, Left(i, j)) | ||
} else (thisCol, Right(Left(i))) | ||
}) | ||
val rightMatches = other.columns.zipWithIndex.collect({ | ||
case (otherCol, j) if !columns.exists(_.name == otherCol.name) => (otherCol, Right(Right(j))) | ||
}) | ||
leftMatches ++ rightMatches | ||
} | ||
end matchColumns | ||
|
||
override def toString: String = columns.map(_.toString).mkString("\n") | ||
|
||
} | ||
end 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
Oops, something went wrong.