Skip to content

Commit

Permalink
continue
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus committed Dec 26, 2021
1 parent 13f551a commit 480f8b1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ object Normalize extends StatelessTransformer {

override def apply(q: Query): Query =
trace"Avoid Capture and Normalize $q into:" andReturn
norm(Dealias(AvoidAliasConflict(q)))
norm(q)
//norm(Dealias(AvoidAliasConflict(q)))

private def traceNorm[T](label: String) =
trace[T](s"${label} (Normalize)", 1, Normalizations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ object Messages {
sealed trait TraceType { def value: String }
object TraceType {
case object SqlNormalizations extends TraceType { val value = "sql" }
case object SqlQuery extends TraceType { val value = "query" }
case object ExpandDistinct extends TraceType { val value = "distinct" }
case object Normalizations extends TraceType { val value = "norm" }
case object Standard extends TraceType { val value = "standard" }
Expand All @@ -96,7 +97,7 @@ object Messages {
// This kind of trace is always on by default and does not need to be enabled by the user.
case object Warning extends TraceType { val value = "warning" }

def values: List[TraceType] = List(Standard, SqlNormalizations, Normalizations, NestedQueryExpansion, AvoidAliasConflict, ReifyLiftings, PatMatch, Quotation, RepropagateQuats, RenameProperties, Warning, ShealthLeaf, ApplyMap, ExpandDistinct)
def values: List[TraceType] = List(Standard, SqlNormalizations, Normalizations, NestedQueryExpansion, AvoidAliasConflict, ReifyLiftings, PatMatch, Quotation, RepropagateQuats, RenameProperties, Warning, ShealthLeaf, ApplyMap, ExpandDistinct, SqlQuery)
}

val qprint = new AstPrinter(traceOpinions, traceAstSimple, Messages.traceQuats)
Expand Down
30 changes: 19 additions & 11 deletions quill-sql-portable/src/main/scala/io/getquill/sql/SqlQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import io.getquill.ast._
import io.getquill.context.sql.norm.{ ExpandSelection, FlattenGroupByAggregation }
import io.getquill.norm.BetaReduction
import io.getquill.quat.Quat
import io.getquill.util.Messages.fail
import io.getquill.util.Interpolator
import io.getquill.util.Messages.{ TraceType, fail }
import io.getquill.{ Literal, PseudoAst }

case class OrderByCriteria(ast: Ast, ordering: PropertyOrdering)
Expand Down Expand Up @@ -74,16 +75,19 @@ object TakeDropFlatten {

object SqlQuery {

val interp = new Interpolator(TraceType.SqlQuery, 1)
import interp._

def apply(query: Ast): SqlQuery =
query match {
case Union(a, b) => SetOperationSqlQuery(apply(a), UnionOperation, apply(b))(query.quat)
case UnionAll(a, b) => SetOperationSqlQuery(apply(a), UnionAllOperation, apply(b))(query.quat)
case UnaryOperation(op, q: Query) => UnaryOperationSqlQuery(op, apply(q))(query.quat)
case _: Operation | _: Value => FlattenSqlQuery(select = List(SelectValue(query)))(query.quat)
case Map(q, a, b) if a == b => apply(q)
case TakeDropFlatten(q, limit, offset) => flatten(q, "x").copy(limit = limit, offset = offset)(q.quat)
case q: Query => flatten(q, "x")
case infix: Infix => flatten(infix, "x")
case Union(a, b) => trace"Apply Union for $query" andReturn SetOperationSqlQuery(apply(a), UnionOperation, apply(b))(query.quat)
case UnionAll(a, b) => trace"Apply UnionAll for $query" andReturn SetOperationSqlQuery(apply(a), UnionAllOperation, apply(b))(query.quat)
case UnaryOperation(op, q: Query) => trace"Apply Unary for $query" andReturn UnaryOperationSqlQuery(op, apply(q))(query.quat)
case _: Operation | _: Value => trace"Apply Flatten $query" andReturn FlattenSqlQuery(select = List(SelectValue(query)))(query.quat)
case Map(q, a, b) if a == b => trace"Apply Map for $query" andReturn apply(q)
case TakeDropFlatten(q, limit, offset) => trace"Apply Take/Drop for $query" andReturn flatten(q, "x").copy(limit = limit, offset = offset)(q.quat)
case q: Query => trace"Apply Query flatten for $query" andReturn flatten(q, "x")
case infix: Infix => trace"Apply Infix flatten for $query" andReturn flatten(infix, "x")
case other => fail(s"Query not properly normalized. Please open a bug report. Ast: '$other'")
}

Expand All @@ -97,11 +101,15 @@ object SqlQuery {
case FlatMap(q @ (_: Query | _: Infix), Ident(alias, _), p: Query) =>
val source = this.source(q, alias)
val (nestedContexts, finalFlatMapBody) = flattenContexts(p)
(source +: nestedContexts, finalFlatMapBody)
trace"Flatten Contexts for $query. Add source $source resulting in:" andReturn {
(source +: nestedContexts, finalFlatMapBody)
}
case FlatMap(q @ (_: Query | _: Infix), Ident(alias, _), p: Infix) =>
fail(s"Infix can't be use as a `flatMap` body. $query")
case other =>
(List.empty, other)
trace"Flatten Contexts for $query yielded Empty List" andReturn {
(List.empty, other)
}
}

private def flatten(sources: List[FromContext], finalFlatMapBody: Ast, alias: String): FlattenSqlQuery = {
Expand Down

0 comments on commit 480f8b1

Please sign in to comment.