Skip to content

Commit

Permalink
add magic set dsl source.
Browse files Browse the repository at this point in the history
  • Loading branch information
stechu committed Jan 11, 2017
1 parent 08c67aa commit e6badf8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 19 additions & 3 deletions dsl/CosetteParser.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Syntax and Parser for Cosette.
> | DIden String String -- a.b
> | BinOp ValueExpr String ValueExpr -- a.b + b.c etc
> | Constant String -- constant variable
> | VQE QueryExpr -- query expressions
> | Agg String ValueExpr -- aggregation function
> deriving (Eq, Show)

=== predicate
Expand Down Expand Up @@ -93,12 +95,14 @@ TODO: add Left Join, Semi join etc to table expression

=== query expression

TODO: currently, grouping only supports columns rather than arbitrary value expressions

> data QueryExpr = Select
> {qSelectList :: [SelectItem]
> ,qFrom :: Maybe [TableRef]
> ,qWhere :: Maybe Predicate
> ,qDistinct:: Bool
> }
> ,qGroup :: Maybe [ValueExpr]
> ,qDistinct:: Bool}
> | UnionAll QueryExpr QueryExpr
> deriving (Eq, Show)

Expand Down Expand Up @@ -132,6 +136,8 @@ term

> term :: [String] -> Parser ValueExpr
> term blackList = try dIden
> <|> (VQE <$> queryExpr)
> <|> (Agg <$> identifier <*> parens dIden)
> <|> num
> <|> constant
> <|> parens (valueExpr [])
Expand Down Expand Up @@ -271,6 +277,14 @@ TODO: for now, only base relations can be unioned in from clause.
> fromList :: Parser [TableRef]
> fromList = keyword_ "from" *> commaSep1 fromItem

=== grouping clause

> groupby :: Parser ()
> groupby = keyword_ "group" *> keyword_ "by"

> groupList :: Parser [ValueExpr]
> groupList = groupby *> commaSep1 dIden

=== queryExpr

Query without distinct
Expand All @@ -280,6 +294,7 @@ Query without distinct
> <$> selectList
> <*> optionMaybe fromList
> <*> optionMaybe whereClause
> <*> optionMaybe groupList
> <*> (do return False)

Query with distinct
Expand All @@ -289,6 +304,7 @@ Query with distinct
> <$> distSelectList
> <*> optionMaybe fromList
> <*> optionMaybe whereClause
> <*> optionMaybe groupList
> <*> (do return True)

Query expression
Expand Down Expand Up @@ -477,6 +493,7 @@ Parse cosette program
> makeSelect = Select {qSelectList = []
> ,qFrom = Nothing
> ,qWhere = Nothing
> ,qGroup = Nothing
> ,qDistinct = False}

> makeTest :: (Eq a, Show a) => Parser a -> (String,a) -> H.Test
Expand All @@ -485,4 +502,3 @@ Parse cosette program
> case gote of
> Left e -> H.assertFailure $ show e
> Right got -> H.assertEqual src expected got

4 changes: 2 additions & 2 deletions dsl/examples/countbug.cos
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ query q1
query q2
`select x.pnum as xp
from parts x, (select y.pnum as suppnum, count(y.shipdate) as ct
from supply y where shipdate < 10
group by y.pnum) temp
from supply y where shipdate < 10
group by y.pnum) temp
where x.qoh = temp.ct AND x.pnum = temp.suppnum`;

verify q1 q2;

0 comments on commit e6badf8

Please sign in to comment.