author | František Kučera <franta-hg@frantovo.cz> |
Mon, 21 Feb 2022 01:21:22 +0100 | |
branch | v_0 |
changeset 330 | 70e7eb578cfa |
parent 316 | d7ae02390fac |
permissions | -rw-r--r-- |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
<stránka |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
|
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
246
diff
changeset
|
5 |
<nadpis>Doing projections with Scheme</nadpis> |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
<perex>modifying attribute values and adding new attributes or removing them</perex> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
<m:pořadí-příkladu>01500</m:pořadí-příkladu> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
<text xmlns="http://www.w3.org/1999/xhtml"> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
<p> |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
246
diff
changeset
|
12 |
The <code>relpipe-tr-scheme</code> can not only filter records, |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
but can also modify them and even modify the structure of the relation – add or remove attributes. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
<h2>Sample data</h2> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
<p>We have some CSV file:</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
<m:pre jazyk="text" src="examples/guile-1.csv"/> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
<p>and we convert it to a relation called <code>n</code>:</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
<m:pre jazyk="bash"><![CDATA[cat guile-1.csv \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
| relpipe-in-csv n id integer name string a integer b integer c integer \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
| relpipe-out-tabular]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
<p>which printed as a table looks like this:</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
<m:pre jazyk="text"><![CDATA[n: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
╭──────────────┬───────────────┬─────────────┬─────────────┬─────────────╮ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
│ id (integer) │ name (string) │ a (integer) │ b (integer) │ c (integer) │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
├──────────────┼───────────────┼─────────────┼─────────────┼─────────────┤ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
│ 1 │ first │ 1 │ 2 │ 3 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
│ 2 │ second │ 2 │ 10 │ 1024 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
│ 3 │ third │ 4 │ 4 │ 16 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
╰──────────────┴───────────────┴─────────────┴─────────────┴─────────────╯ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
Record count: 3]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
Because it is annoying to write some code again and again, we will create a shell function and (re)use it later: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
<m:pre jazyk="bash"><![CDATA[sample-data() { |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
cat guile-1.csv \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
| relpipe-in-csv n id integer name string a integer b integer c integer; |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
}]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
Another option is storing the relational data in a file and then reading this file. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
Files are better option, if the transformation is costly and we do not need live/fresh data. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
<h2>Modifying attribute values</h2> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
<p> |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
246
diff
changeset
|
58 |
Then, we can modify such relation using Scheme – e.g. we can make the <code>name</code> uppercase and increase <code>id</code> by 1000: |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
<m:pre jazyk="bash"><![CDATA[sample-data \ |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
246
diff
changeset
|
62 |
| relpipe-tr-scheme \ |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
--relation n \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
--for-each '(set! $name (string-upcase $name) ) (set! $id (+ $id 1000) )' \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
| relpipe-out-tabular]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
<p>So we have:</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
<m:pre jazyk="text"><![CDATA[n: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
╭──────────────┬───────────────┬─────────────┬─────────────┬─────────────╮ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
│ id (integer) │ name (string) │ a (integer) │ b (integer) │ c (integer) │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
├──────────────┼───────────────┼─────────────┼─────────────┼─────────────┤ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
│ 1001 │ FIRST │ 1 │ 2 │ 3 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
│ 1002 │ SECOND │ 2 │ 10 │ 1024 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
│ 1003 │ THIRD │ 4 │ 4 │ 16 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
╰──────────────┴───────────────┴─────────────┴─────────────┴─────────────╯ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
Record count: 3]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
80 |
<h2>Removing attributes</h2> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
The relation on the output might have different structure that the relation on the input. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
We can keep only some of the original attributes: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
87 |
<m:pre jazyk="bash"><![CDATA[sample-data \ |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
246
diff
changeset
|
88 |
| relpipe-tr-scheme \ |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
--relation n \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
--for-each '(set! $name (string-upcase $name) ) (set! $id (+ $id 1000) )' \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
--output-attribute 'id' integer \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
--output-attribute 'name' string \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
| relpipe-out-tabular]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
<p>and have:</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
<m:pre jazyk="text"><![CDATA[n: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
╭──────────────┬───────────────╮ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
│ id (integer) │ name (string) │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
├──────────────┼───────────────┤ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
│ 1001 │ FIRST │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
│ 1002 │ SECOND │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
│ 1003 │ THIRD │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
╰──────────────┴───────────────╯ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
Record count: 3]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
<h2>Adding attributes</h2> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
110 |
If we do not want to completely redefine the structure of the relation, |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
111 |
we can keep all original attributes and just add definitions of some others: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
112 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
113 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
114 |
<m:pre jazyk="bash"><![CDATA[sample-data \ |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
246
diff
changeset
|
115 |
| relpipe-tr-scheme \ |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
116 |
--relation n \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
117 |
--for-each '(define $sum (+ $a $b $c) )' \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
118 |
--output-attribute 'sum' integer \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
119 |
--input-attributes-prepend \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
120 |
| relpipe-out-tabular]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
<p>so we have a completely new attribute containing the sum of <code>a</code>, <code>b</code> and <code>c</code>:</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
123 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
124 |
<m:pre jazyk="text"><![CDATA[n: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
125 |
╭──────────────┬───────────────┬─────────────┬─────────────┬─────────────┬───────────────╮ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
126 |
│ id (integer) │ name (string) │ a (integer) │ b (integer) │ c (integer) │ sum (integer) │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
127 |
├──────────────┼───────────────┼─────────────┼─────────────┼─────────────┼───────────────┤ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
128 |
│ 1 │ first │ 1 │ 2 │ 3 │ 6 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
129 |
│ 2 │ second │ 2 │ 10 │ 1024 │ 1036 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
130 |
│ 3 │ third │ 4 │ 4 │ 16 │ 24 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
131 |
╰──────────────┴───────────────┴─────────────┴─────────────┴─────────────┴───────────────╯ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
132 |
Record count: 3]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
133 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
134 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
135 |
We can change the attribute order by using <code>--input-attributes-append</code> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
136 |
instead of <code>--input-attributes-prepend</code>. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
137 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
138 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
139 |
<h2>Changing the attribute type</h2> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
140 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
141 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
142 |
Each attribute has a data type (integer, string…). |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
143 |
And we can change the type. Of course we have to modify the data, because we can not put e.g. string value into an integer attribute. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
144 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
145 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
146 |
<m:pre jazyk="bash"><![CDATA[sample-data \ |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
246
diff
changeset
|
147 |
| relpipe-tr-scheme \ |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
148 |
--relation n \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
149 |
--for-each '(define $id (string-upcase $name) )' \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
150 |
--output-attribute 'id' string \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
151 |
--output-attribute 'a' integer \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
152 |
--output-attribute 'b' integer \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
153 |
--output-attribute 'c' integer \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
154 |
| relpipe-out-tabular]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
155 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
156 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
157 |
The code above changed the type of <code>id</code> attribute from integer to string |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
158 |
and put uppercase <code>name</code> into it: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
159 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
160 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
161 |
<m:pre jazyk="text"><![CDATA[n: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
162 |
╭─────────────┬─────────────┬─────────────┬─────────────╮ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
163 |
│ id (string) │ a (integer) │ b (integer) │ c (integer) │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
164 |
├─────────────┼─────────────┼─────────────┼─────────────┤ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
165 |
│ FIRST │ 1 │ 2 │ 3 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
166 |
│ SECOND │ 2 │ 10 │ 1024 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
167 |
│ THIRD │ 4 │ 4 │ 16 │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
168 |
╰─────────────┴─────────────┴─────────────┴─────────────╯ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
169 |
Record count: 3]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
170 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
171 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
172 |
<h2>Projection and restriction</h2> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
173 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
174 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
175 |
We can do projection and restriction at the same time, during the same transformation: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
176 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
177 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
178 |
<m:pre jazyk="bash"><![CDATA[sample-data \ |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
246
diff
changeset
|
179 |
| relpipe-tr-scheme \ |
246
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
180 |
--relation n \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
181 |
--for-each '(set! $name (string-upcase $name) ) (set! $id (+ $id 1000) )' \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
182 |
--output-attribute 'id' integer \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
183 |
--output-attribute 'name' string \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
184 |
--where '(= $c (* $a $b) )' \ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
185 |
| relpipe-out-tabular]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
186 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
187 |
<p>and have:</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
188 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
189 |
<m:pre jazyk="bash"><![CDATA[n: |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
190 |
╭──────────────┬───────────────╮ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
191 |
│ id (integer) │ name (string) │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
192 |
├──────────────┼───────────────┤ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
193 |
│ 1003 │ THIRD │ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
194 |
╰──────────────┴───────────────╯ |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
195 |
Record count: 1]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
196 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
197 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
198 |
And if we use <code>expt</code> instead of <code>*</code>, we will get SECOND instead of THIRD. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
199 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
200 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
201 |
<p>The example above has its SQL equivalent:</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
202 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
203 |
<m:pre jazyk="sql"><![CDATA[SELECT |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
204 |
id + 1000 AS id, |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
205 |
upper(name) AS name |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
206 |
FROM n |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
207 |
WHERE c = (a * b);]]></m:pre> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
208 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
209 |
<p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
210 |
The difference is that <m:name/> do not require data to be stored anywhere, |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
211 |
because we (by default) process streams on the fly. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
212 |
Thus one process can generate data, second one can transform them and the third one can convert them to some output format. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
213 |
All processes are running at the same time and without need to cache all data at once. |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
214 |
</p> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
215 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
216 |
</text> |
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
217 |
|
fde0cd94fde6
guile: Doing projections with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
218 |
</stránka> |