jhc-systems / sqlest

Write SQL in Scala
https://jhc-systems.github.io/sqlest/latest/api/
Apache License 2.0
30 stars 17 forks source link

Add ROLLUP(), CUBE() and GROUPING SETS() & update tests. #1

Closed DavidGregory084 closed 10 years ago

DavidGregory084 commented 10 years ago

Accepting contributions?

DavidGregory084 commented 10 years ago

So I think we decided that we need: case class Group[A] implicit conversions between Column[A] and subtypes to Group[A] and subtypes. TupleGroup

DavidGregory084 commented 10 years ago

If I'm right about point 2), this means defining implicit functions to create Group objects from Columns? e.g.

implicit def scalarFunctionColumn2ScalarFunctionGroup(col: ScalarFunctionColumn[A]) = 
  ScalarFunctionGroup[A](<params>)

Then groupBy can be a List[Group[A]]?

brendanator commented 10 years ago

We certainly are - thanks!

When a grouping functions is used outside of a group by clause It would be better to fail to compile time instead of throwing an exception at run time

The way to do this is to introduce a new type Group and change the groupBy function signature to take Groups instead of Columns

Have a look a Order and OrderSyntax for an example of how to implement the implicit conversion for a Column.

You will also need to create TupleGroup as a subclass of Group

trait Group
case class ColumnGroup(column: Column[_]) extends Group
case class TupleGroup(columns: List[Column[_]]) extends Group

with implicit conversions from all sizes of tuples of columns to TupleGroup

brendanator commented 10 years ago

I don't think you need different conversions for each Column subclass. You only need to know which columns are being grouped by, not their types

DavidGregory084 commented 10 years ago

I didn't do quite as you suggested as I couldn't get nested grouping operators to work unless Groups could contain other Groups...I hope that is okay.

Very pleased to see this!

[error]  found   : sqlest.ast.FunctionGroup
[error]  required: sqlest.ast.AliasedColumn[_]
[error]       select(sum(MyTable.col1).as("sum"), rollUp(MyTable.col1))
[error]
brendanator commented 10 years ago

This is really good. I've added a few comments to your commit but only very minor changes

Good work on making it not compile when you use a FunctionGroup inside a select clause

Don't worry about the nested group operators, it's possible but tricky to get this to work. I'll come back and have a look at it later

brendanator commented 10 years ago

Thanks David - great work!