Skip to content

Commit

Permalink
aggregate()
Browse files Browse the repository at this point in the history
  • Loading branch information
shagoon committed Jan 7, 2023
1 parent 7fc8f77 commit 7b8765d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 26 deletions.
11 changes: 6 additions & 5 deletions quill-core/src/main/scala/io/getquill/quotation/Liftables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ trait Liftables extends QuatLiftable {
}

implicit val aggregationOperatorLiftable: Liftable[AggregationOperator] = Liftable[AggregationOperator] {
case AggregationOperator.`min` => q"$pack.AggregationOperator.`min`"
case AggregationOperator.`max` => q"$pack.AggregationOperator.`max`"
case AggregationOperator.`avg` => q"$pack.AggregationOperator.`avg`"
case AggregationOperator.`sum` => q"$pack.AggregationOperator.`sum`"
case AggregationOperator.`size` => q"$pack.AggregationOperator.`size`"
case AggregationOperator.`min` => q"$pack.AggregationOperator.`min`"
case AggregationOperator.`max` => q"$pack.AggregationOperator.`max`"
case AggregationOperator.`avg` => q"$pack.AggregationOperator.`avg`"
case AggregationOperator.`sum` => q"$pack.AggregationOperator.`sum`"
case AggregationOperator.`size` => q"$pack.AggregationOperator.`size`"
case AggregationOperator.`custom` => q"$pack.AggregationOperator.`custom`"
}

implicit val renameableLiftable: Liftable[Renameable] = Liftable[Renameable] {
Expand Down
2 changes: 2 additions & 0 deletions quill-core/src/main/scala/io/getquill/quotation/Parsing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ trait Parsing extends ValueComputation with QuatMaking {
case q"$source.map[$t](($alias) => $body)" if (is[DslQuery[Any]](source)) =>
Map(astParser(source), identParser(alias), astParser(body))

case q"$source.aggregate[$t](($alias) => $body)" if (is[DslQuery[Any]](source)) => Aggregation(AggregationOperator.`custom`, Map(astParser(source), identParser(alias), astParser(body)))

case q"$source.flatMap[$t](($alias) => $body)" if (is[DslQuery[Any]](source)) =>
FlatMap(astParser(source), identParser(alias), astParser(body))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ trait Unliftables extends QuatUnliftable {
}

implicit val aggregationOperatorUnliftable: Unliftable[AggregationOperator] = Unliftable[AggregationOperator] {
case q"$pack.AggregationOperator.`min`" => AggregationOperator.`min`
case q"$pack.AggregationOperator.`max`" => AggregationOperator.`max`
case q"$pack.AggregationOperator.`avg`" => AggregationOperator.`avg`
case q"$pack.AggregationOperator.`sum`" => AggregationOperator.`sum`
case q"$pack.AggregationOperator.`size`" => AggregationOperator.`size`
case q"$pack.AggregationOperator.`min`" => AggregationOperator.`min`
case q"$pack.AggregationOperator.`max`" => AggregationOperator.`max`
case q"$pack.AggregationOperator.`avg`" => AggregationOperator.`avg`
case q"$pack.AggregationOperator.`sum`" => AggregationOperator.`sum`
case q"$pack.AggregationOperator.`size`" => AggregationOperator.`size`
case q"$pack.AggregationOperator.`custom`" => AggregationOperator.`custom`
}

implicit val queryUnliftable: Unliftable[Query] = Unliftable[Query] {
Expand Down
1 change: 1 addition & 0 deletions quill-engine/src/main/scala/io/getquill/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ sealed trait Query[+T] extends QAC[Nothing, T] {
def avg[U >: T](implicit n: Numeric[U]): Option[BigDecimal] = NonQuotedException()
def sum[U >: T](implicit n: Numeric[U]): Option[T] = NonQuotedException()
def size: Long = NonQuotedException()
def aggregate[R](f: T => R): R

def join[A >: T, B](q: Query[B]): JoinQuery[A, B, (A, B)] = NonQuotedException()
def leftJoin[A >: T, B](q: Query[B]): JoinQuery[A, B, (A, Option[B])] = NonQuotedException()
Expand Down
22 changes: 12 additions & 10 deletions quill-engine/src/main/scala/io/getquill/ast/Ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,21 @@ case class GroupBy(query: Ast, alias: Ident, body: Ast) extends Query {
case class Aggregation(operator: AggregationOperator, ast: Ast) extends Query {
def quat =
operator match {
case AggregationOperator.`min` => ast.quat
case AggregationOperator.`max` => ast.quat
case AggregationOperator.`avg` => Quat.Value
case AggregationOperator.`sum` => Quat.Value
case AggregationOperator.`size` => Quat.Value
case AggregationOperator.`min` => ast.quat
case AggregationOperator.`max` => ast.quat
case AggregationOperator.`avg` => Quat.Value
case AggregationOperator.`sum` => Quat.Value
case AggregationOperator.`size` => Quat.Value
case AggregationOperator.`custom` => Quat.Value
}
def bestQuat: Quat =
operator match {
case AggregationOperator.`min` => ast.bestQuat
case AggregationOperator.`max` => ast.bestQuat
case AggregationOperator.`avg` => Quat.Value
case AggregationOperator.`sum` => Quat.Value
case AggregationOperator.`size` => Quat.Value
case AggregationOperator.`min` => ast.bestQuat
case AggregationOperator.`max` => ast.bestQuat
case AggregationOperator.`avg` => Quat.Value
case AggregationOperator.`sum` => Quat.Value
case AggregationOperator.`size` => Quat.Value
case AggregationOperator.`custom` => Quat.Value
}
}

Expand Down
1 change: 1 addition & 0 deletions quill-engine/src/main/scala/io/getquill/ast/Operator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ object AggregationOperator {
case object `avg` extends AggregationOperator
case object `sum` extends AggregationOperator
case object `size` extends AggregationOperator
case object `custom` extends AggregationOperator
}
11 changes: 6 additions & 5 deletions quill-engine/src/main/scala/io/getquill/sql/idiom/SqlIdiom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,12 @@ trait SqlIdiom extends Idiom {
}

implicit val aggregationOperatorTokenizer: Tokenizer[AggregationOperator] = Tokenizer[AggregationOperator] {
case AggregationOperator.`min` => stmt"MIN"
case AggregationOperator.`max` => stmt"MAX"
case AggregationOperator.`avg` => stmt"AVG"
case AggregationOperator.`sum` => stmt"SUM"
case AggregationOperator.`size` => stmt"COUNT"
case AggregationOperator.`min` => stmt"MIN"
case AggregationOperator.`max` => stmt"MAX"
case AggregationOperator.`avg` => stmt"AVG"
case AggregationOperator.`sum` => stmt"SUM"
case AggregationOperator.`size` => stmt"COUNT"
case AggregationOperator.`custom` => stmt""
}

implicit val binaryOperatorTokenizer: Tokenizer[BinaryOperator] = Tokenizer[BinaryOperator] {
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "3.19.0"
ThisBuild / version := "3.19.0.1-SNAPSHOT"

0 comments on commit 7b8765d

Please sign in to comment.