-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR provides experimental support for SQL queries with grouping and aggregation. These require the mixing normaliser (mixing_norm=on in the configuration file). The result of grouping over a relation is represented as a finite map, which in Links is treated as a list of (grouping key, associated subrelation) pairs. Aggregation can then be applied groupwise to a finite map to obtain again a relation. Such Links queries are translated to SQL queries using group by and aggregates. The following operations are provided in the prelude and can be used in grouping queries: ``` groupBy; fun : ((a) -b-> c, [a]) -b-> [(c, [a])] ``` `groupBy(f, l)` takes a list `l` and a grouping criterion `f`: it applies `f` to each item in the list to obtain its grouping key, and finally returns a finite map, i.e. a list of pairs associating to each grouping key the list of items of the original list `l` that share the same grouping key. e.g. `groupBy(fun (x) { (is_even = even(x)) }, [1, 2, 3]) = [((is_even = true), [2]), ((is_even = false), [1,3])]` ``` concatMapKey; fun : ((a) -b-> [c], [(a, [_])]) -b-> [c] ``` `concatMapKey` works like `concatMap`, but it takes as input a finite map rather than any list; it performs comprehension over the (deduplicated) key set of the input map (for this reason, the output type of the finite map argument is irrelevant). ``` lookupG; fun : (a, [(a, [b])]) -> [b] ``` Given a key `k` and a finite map `m`, `lookupG(k,m)` returns the list of values associated by the map `m` to the key `k`. ``` aggBy; fun : ([(a, [b])], ([b]) -c-> d) -c-> [(a, d)] ``` `aggBy(m, f)` takes a finite map `m` and an aggregation criterion `f`: its purpose is to apply the aggregation to the input map on a key by key basis This is where our LINQ infrastructure gets hacky: we can only support f when it is in the form: `fun (t) { (outlabel1 = agg1(for (x <- t) [x.inlabel1]), ..., outlabeln = aggn(for (x <- t) [x.inlabeln])) }` Where `agg1, ... aggn` are certain aggregation functions defined in the prelude: `sum, sumf, avg, avgF, min_list, minF_list, max_list, maxF_list` Any other use of `aggBy` will NOT be databaseable. Example queries can be found in the database/grouping.links test file (which requires tables created by database/grouping-create.links)
- Loading branch information
Showing
27 changed files
with
1,179 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.