|
1 <stránka |
|
2 xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
|
3 xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
|
4 |
|
5 <nadpis>Aggregating data with Guile</nadpis> |
|
6 <perex>counting records and computing sum</perex> |
|
7 <m:pořadí-příkladu>01700</m:pořadí-příkladu> |
|
8 |
|
9 <text xmlns="http://www.w3.org/1999/xhtml"> |
|
10 |
|
11 <p> |
|
12 In <code>relpipe-tr-guile</code> we can generate new records – not only modify records from the input. |
|
13 There is <code>--has-more-records</code> option which – if evaluated as true – says: „read one more record from the Guile context and call me again“. |
|
14 We can also suppress all original records by <code>--where '#f'</code>. |
|
15 And we can also change the structure of the relation (see previous examples). |
|
16 Thus we can iterate through a relation but completely replace its structure and content. |
|
17 </p> |
|
18 |
|
19 <p> |
|
20 What it is good for? We can do aggregations – we can count records, compute sum, maximum, minimum or average value etc. |
|
21 </p> |
|
22 |
|
23 <m:pre jazyk="bash" src="examples/guile-file-count-size-sum.sh"/> |
|
24 |
|
25 <p>Usage example:</p> |
|
26 |
|
27 <m:pre jazyk="text"><![CDATA[$ ./guile-file-count-size-sum.sh /usr/share/icons/oxygen/ |
|
28 filesystem: |
|
29 ╭─────────────────┬───────────────╮ |
|
30 │ count (integer) │ sum (integer) │ |
|
31 ├─────────────────┼───────────────┤ |
|
32 │ 6260 │ 31091700 │ |
|
33 ╰─────────────────┴───────────────╯ |
|
34 Record count: 1]]></m:pre> |
|
35 |
|
36 <p> |
|
37 In SQL same result can be achieved by: |
|
38 </p> |
|
39 |
|
40 <m:pre jazyk="sql"><![CDATA[SELECT |
|
41 count(*) AS count, |
|
42 sum(size) AS sum |
|
43 FROM filesystem;]]></m:pre> |
|
44 |
|
45 <p> |
|
46 This should be possible with <code>relpipe-tr-sql</code> in later versions. |
|
47 SQL is much more declarative and for many cases a better tool. |
|
48 In SQL we describe „how the result should look like“ instead of „how the result should be produced step by step“. |
|
49 </p> |
|
50 |
|
51 <p> |
|
52 One day, there might also be a translator that parses SQL code and generates Guile code, |
|
53 so we could have advantages of both worlds |
|
54 a) concise and declarative syntax of SQL and |
|
55 b) streaming – which means no need for putting all the data in the RAM or on the disk. |
|
56 </p> |
|
57 |
|
58 |
|
59 |
|
60 </text> |
|
61 |
|
62 </stránka> |