-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.rel
71 lines (56 loc) · 1.99 KB
/
query.rel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// *****************************************************************
// Sales Engineering Query Library
//
// Tools for querying and poking around RAI database relations
//
// INSTALL this source: query.rel
// *****************************************************************
module se_query
// Sample the first "limit" rows in relation "D".
// Useful for exploring newly loaded CSV data.
@outline
def head[D, limit in Int] =
table[c, row, v: lined_csv[D](c, row, v) and row <= limit]
// Sample the last "limit" rows in relation "D".
// Useful for exploring newly loaded CSV data.
@outline
def tail[D, limit in Int] =
table[c, row, v: lined_csv[D](c, row, v) and
row > count[x: D(_, x, _)] - limit]
// Count rows in CSV data file
@outline
def rowcount[D] =
count[row: lined_csv[D](_, row, _)]
// Count attributes in CSV data file
@outline
def attrcount[D] =
count[attr: lined_csv[D](attr, _, _)]
// Display both count of rows and count of attributes (columns) in CSV data file
@inline
def shape[D] =
{:rows, rowcount[D];
:cols, attrcount[D]}
// helper function for `super_table`
@outline
def _make_table[R, Col_names](col_name,
row_i,
val) {
pivot[Col_names](col_i, col_name) and
pivot[sort[R][row_i]](col_i, val)
from col_i
}
// print relation in table format
// with specified custom column names
// for now, the order of columns can't be controlled
@outline
def super_table[R, Col_names] = table[_make_table[R, Col_names]]
// Combine values from multiple GNF columns
// into single relation.
// Duplicate tuples (values) are removed as this function
// doesn't preserve neither GNF key nor
// column names. But it does preserve arity of the values.
@inline
def union_multi_gnf_columns[R, ColNames] {
{xs...: R(ColNames, _, xs...)}
}
end