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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
from tests.dialects.test_dialect import Validator
class TestPRQL(Validator):
dialect = "prql"
def test_prql(self):
self.validate_all(
"from x",
write={
"": "SELECT * FROM x",
},
)
self.validate_all(
"from x derive a + 1",
write={
"": "SELECT *, a + 1 FROM x",
},
)
self.validate_all(
"from x derive x = a + 1",
write={
"": "SELECT *, a + 1 AS x FROM x",
},
)
self.validate_all(
"from x derive {a + 1}",
write={
"": "SELECT *, a + 1 FROM x",
},
)
self.validate_all(
"from x derive {x = a + 1, b}",
write={
"": "SELECT *, a + 1 AS x, b FROM x",
},
)
self.validate_all(
"from x derive {x = a + 1, b} select {y = x, 2}",
write={"": "SELECT a + 1 AS y, 2 FROM x"},
)
self.validate_all(
"from x take 10",
write={
"": "SELECT * FROM x LIMIT 10",
},
)
self.validate_all(
"from x take 10 take 5",
write={
"": "SELECT * FROM x LIMIT 5",
},
)
self.validate_all(
"from x filter age > 25",
write={
"": "SELECT * FROM x WHERE age > 25",
},
)
self.validate_all(
"from x derive {x = a + 1, b} filter age > 25",
write={
"": "SELECT *, a + 1 AS x, b FROM x WHERE age > 25",
},
)
self.validate_all(
"from x filter dept != 'IT'",
write={
"": "SELECT * FROM x WHERE dept <> 'IT'",
},
)
self.validate_all(
"from x filter p == 'product' select { a, b }",
write={"": "SELECT a, b FROM x WHERE p = 'product'"},
)
self.validate_all(
"from x filter age > 25 filter age < 27",
write={"": "SELECT * FROM x WHERE age > 25 AND age < 27"},
)
self.validate_all(
"from x filter (age > 25 && age < 27)",
write={"": "SELECT * FROM x WHERE (age > 25 AND age < 27)"},
)
self.validate_all(
"from x filter (age > 25 || age < 27)",
write={"": "SELECT * FROM x WHERE (age > 25 OR age < 27)"},
)
self.validate_all(
"from x filter (age > 25 || age < 22) filter age > 26 filter age < 27",
write={
"": "SELECT * FROM x WHERE ((age > 25 OR age < 22) AND age > 26) AND age < 27",
},
)
self.validate_all(
"from x sort age",
write={
"": "SELECT * FROM x ORDER BY age",
},
)
self.validate_all(
"from x sort {-age}",
write={
"": "SELECT * FROM x ORDER BY age DESC",
},
)
self.validate_all(
"from x sort {age, name}",
write={
"": "SELECT * FROM x ORDER BY age, name",
},
)
self.validate_all(
"from x sort {-age, +name}",
write={
"": "SELECT * FROM x ORDER BY age DESC, name",
},
)
self.validate_all(
"from x append y",
write={
"": "SELECT * FROM x UNION ALL SELECT * FROM y",
},
)
self.validate_all(
"from x remove y",
write={
"": "SELECT * FROM x EXCEPT ALL SELECT * FROM y",
},
)
self.validate_all(
"from x intersect y",
write={"": "SELECT * FROM x INTERSECT ALL SELECT * FROM y"},
)
self.validate_all(
"from x filter a == null filter null != b",
write={
"": "SELECT * FROM x WHERE a IS NULL AND NOT b IS NULL",
},
)
self.validate_all(
"from x filter (a > 1 || null != b || c != null)",
write={
"": "SELECT * FROM x WHERE (a > 1 OR NOT b IS NULL OR NOT c IS NULL)",
},
)
self.validate_all(
"from a aggregate { average x }",
write={
"": "SELECT AVG(x) FROM a",
},
)
self.validate_all(
"from a aggregate { average x, min y, ct = sum z }",
write={
"": "SELECT AVG(x), MIN(y), COALESCE(SUM(z), 0) AS ct FROM a",
},
)
self.validate_all(
"from a aggregate { average x, min y, sum z }",
write={
"": "SELECT AVG(x), MIN(y), COALESCE(SUM(z), 0) FROM a",
},
)
self.validate_all(
"from a aggregate { min y, b = stddev x, max z }",
write={
"": "SELECT MIN(y), STDDEV(x) AS b, MAX(z) FROM a",
},
)
|