@@ -172,21 +172,18 @@ trait Compiler[F[_]] {
172172 }
173173 }
174174
175- def buildJoinDirectionMap (relations : List [ SqlRelation ] ): Map [String , List [JoinTraverse .Dir ]] = {
175+ def buildJoinDirectionMap (relations : SqlRelation ): Map [String , List [JoinTraverse .Dir ]] = {
176176 def loop (rel : SqlRelation ): JoinTraverse = rel match {
177177 case t @ TableRelationAST (name, aliasOpt) => JoinTraverse .Leaf (t.aliasName)
178178 case t @ SubqueryRelationAST (subquery, alias) => JoinTraverse .Leaf (t.aliasName) // Leaf????
179179 case JoinRelation (left, right, tpe, clause) => JoinTraverse .Join (loop(left), loop(right))
180180 case CrossRelation (left, right) => JoinTraverse .Join (loop(left), loop(right))
181181 }
182182
183- (relations.map(loop _).foldLeft[Option [JoinTraverse ]](None ) {
184- case (None , traverse) => Some (traverse)
185- case (Some (acc), traverse) => Some (JoinTraverse .Join (acc, traverse))
186- }).map(_.directionMap).getOrElse(Map ())
183+ loop(relations).directionMap
187184 }
188185
189- def compileTableRefs (joined : Term [LogicalPlan ], relations : List [ SqlRelation ] ): Map [String , Term [LogicalPlan ]] = {
186+ def compileTableRefs (joined : Term [LogicalPlan ], relations : SqlRelation ): Map [String , Term [LogicalPlan ]] = {
190187 buildJoinDirectionMap(relations).map {
191188 case (name, dirs) =>
192189 name -> dirs.foldLeft(joined) {
@@ -198,11 +195,10 @@ trait Compiler[F[_]] {
198195 }
199196 }
200197
201- def tableContext (joined : Term [LogicalPlan ], relations : List [ SqlRelation ] ): TableContext = {
198+ def tableContext (joined : Term [LogicalPlan ], relations : SqlRelation ): TableContext =
202199 TableContext (Some (joined), compileTableRefs(joined, relations))
203- }
204200
205- def step (relations : List [ SqlRelation ] ): (Option [CompilerM [Term [LogicalPlan ]]] => CompilerM [Term [LogicalPlan ]] => CompilerM [Term [LogicalPlan ]]) = {
201+ def step (relations : SqlRelation ): (Option [CompilerM [Term [LogicalPlan ]]] => CompilerM [Term [LogicalPlan ]] => CompilerM [Term [LogicalPlan ]]) = {
206202 (current : Option [CompilerM [Term [LogicalPlan ]]]) => (next : CompilerM [Term [LogicalPlan ]]) =>
207203 current.map { current =>
208204 for {
@@ -332,90 +328,85 @@ trait Compiler[F[_]] {
332328
333329 val projs = projections.map(_.expr)
334330
335- val stepBuilder = step(relations)
336-
337- if (relations.length == 0 ) for {
338- projs <- projs.map(compile0).sequenceU
339- } yield buildSelectRecord(names, projs)
340- else for {
341- joined <- relations.map(compile0).sequenceU
342-
343- crossed = Foldable [List ].foldLeftM[CompilerM , Term [LogicalPlan ], Term [LogicalPlan ]](
344- joined.tail, joined.head
345- )((left, right) => emit[Term [LogicalPlan ]](LogicalPlan .invoke(Cross , left :: right :: Nil )))
346-
347- rez <- stepBuilder(Some (crossed)) {
348- val filtered = filter map { filter =>
349- for {
350- t <- CompilerState .rootTableReq
351- f <- compile0(filter)
352- } yield Filter (t, f)
353- }
354-
355- stepBuilder(filtered) {
356- val grouped = groupBy map { groupBy =>
331+ relations match {
332+ case None => for {
333+ projs <- projs.map(compile0).sequenceU
334+ } yield buildSelectRecord(names, projs)
335+ case Some (relations) => {
336+ val stepBuilder = step(relations)
337+ stepBuilder(Some (compile0(relations))) {
338+ val filtered = filter map { filter =>
357339 for {
358340 t <- CompilerState .rootTableReq
359- g <- compileArray(groupBy.keys )
360- } yield GroupBy (t, g )
341+ f <- compile0(filter )
342+ } yield Filter (t, f )
361343 }
362344
363- stepBuilder(grouped ) {
364- val having = groupBy.flatMap(_.having) map { having =>
345+ stepBuilder(filtered ) {
346+ val grouped = groupBy map { groupBy =>
365347 for {
366348 t <- CompilerState .rootTableReq
367- h <- compile0(having )
368- } yield Filter (t, h )
349+ g <- compileArray(groupBy.keys )
350+ } yield GroupBy (t, g )
369351 }
370352
371- stepBuilder(having ) {
372- val select = Some {
353+ stepBuilder(grouped ) {
354+ val having = groupBy.flatMap(_.having) map { having =>
373355 for {
374- projs <- projs.map(compile0).sequenceU
375- } yield buildSelectRecord(names, projs)
356+ t <- CompilerState .rootTableReq
357+ h <- compile0(having)
358+ } yield Filter (t, h)
376359 }
377360
378- stepBuilder(select ) {
379- val sort = orderBy map { orderBy =>
361+ stepBuilder(having ) {
362+ val select = Some {
380363 for {
381- t <- CompilerState .rootTableReq
382- s <- compileArray(orderBy.keys.map(_._1)) // TODO: Ascending / descending
383- } yield OrderBy (t, s)
364+ projs <- projs.map(compile0).sequenceU
365+ } yield buildSelectRecord(names, projs)
384366 }
385367
386- stepBuilder(sort ) {
387- // Note: inspecting the name is not the most awesome imaginable way to identify
388- // fields that were introduced to support "oredr by"
389- val synthetic : Option [ String ] => Boolean = {
390- case Some (name) => name.startsWith( " __sd__ " )
391- case None => false
368+ stepBuilder(select ) {
369+ val sort = orderBy map { orderBy =>
370+ for {
371+ t <- CompilerState .rootTableReq
372+ s <- compileArray(orderBy.keys.map(_._1)) // TODO: Ascending / descending
373+ } yield OrderBy (t, s)
392374 }
393-
394- val pruned = if (names.exists(synthetic))
395- Some {
396- for {
397- t <- CompilerState .rootTableReq
398- ns = names.collect {
399- case Some (name) if ! synthetic(Some (name)) => name
400- }
401- ts = ns.map(name => ObjectProject (t, LogicalPlan .constant(Data .Str (name))))
402- } yield buildSelectRecord(ns.map(name => Some (name)), ts)
403- }
404- else None
405-
406- stepBuilder(pruned) {
407- val drop = offset map { offset =>
408- for {
409- t <- CompilerState .rootTableReq
410- } yield Drop (t, LogicalPlan .constant(Data .Int (offset)))
411- }
412375
413- stepBuilder(drop) {
414- (limit map { limit =>
376+ stepBuilder(sort) {
377+ // Note: inspecting the name is not the most awesome imaginable way to identify
378+ // fields that were introduced to support "oredr by"
379+ val synthetic : Option [String ] => Boolean = {
380+ case Some (name) => name.startsWith(" __sd__" )
381+ case None => false
382+ }
383+
384+ val pruned = if (names.exists(synthetic))
385+ Some {
386+ for {
387+ t <- CompilerState .rootTableReq
388+ ns = names.collect {
389+ case Some (name) if ! synthetic(Some (name)) => name
390+ }
391+ ts = ns.map(name => ObjectProject (t, LogicalPlan .constant(Data .Str (name))))
392+ } yield buildSelectRecord(ns.map(name => Some (name)), ts)
393+ }
394+ else None
395+
396+ stepBuilder(pruned) {
397+ val drop = offset map { offset =>
415398 for {
416399 t <- CompilerState .rootTableReq
417- } yield Take (t, LogicalPlan .constant(Data .Int (limit)))
418- }).getOrElse(CompilerState .rootTableReq)
400+ } yield Drop (t, LogicalPlan .constant(Data .Int (offset)))
401+ }
402+
403+ stepBuilder(drop) {
404+ (limit map { limit =>
405+ for {
406+ t <- CompilerState .rootTableReq
407+ } yield Take (t, LogicalPlan .constant(Data .Int (limit)))
408+ }).getOrElse(CompilerState .rootTableReq)
409+ }
419410 }
420411 }
421412 }
@@ -424,7 +415,7 @@ trait Compiler[F[_]] {
424415 }
425416 }
426417 }
427- } yield rez
418+ }
428419
429420 case Subselect (select) => compile0(select)
430421
@@ -512,7 +503,7 @@ trait Compiler[F[_]] {
512503
513504 case SubqueryRelationAST (subquery, _) => compile0(subquery)
514505
515- case JoinRelation (left, right, tpe, clause) =>
506+ case JoinRelation (left, right, tpe, clause) =>
516507 for {
517508 leftName <- CompilerState .freshName(" left" )
518509 rightName <- CompilerState .freshName(" right" )
@@ -521,8 +512,8 @@ trait Compiler[F[_]] {
521512 left0 <- compile0(left)
522513 right0 <- compile0(right)
523514 join <- CompilerState .contextual(
524- tableContext(leftFree, List ( left))
525- ++ tableContext(rightFree, List (right)) ) {
515+ tableContext(leftFree, left) ++ tableContext(rightFree, right )
516+ ) {
526517 for {
527518 tuple <- compileJoin(clause)
528519 } yield LogicalPlan .join(leftFree, rightFree,
0 commit comments