-
Notifications
You must be signed in to change notification settings - Fork 15
/
hybrid.smli
365 lines (344 loc) · 10.5 KB
/
hybrid.smli
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
(*
* Licensed to Julian Hyde under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Julian Hyde licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*
* Tests queries that are to execute mostly in Calcite.
*)
Sys.set ("printLength", 64);
> val it = () : unit
Sys.set ("stringDepth", ~1);
> val it = () : unit
(*) Query in Hybrid mode (100% Calcite)
Sys.set ("hybrid", true);
> val it = () : unit
from e in scott.emp
where e.deptno = 20
yield e.empno;
> val it = [7369,7566,7788,7876,7902] : int list
Sys.plan();
> val it =
> "calcite(plan LogicalProject(empno=[$2])
> LogicalFilter(condition=[=($1, 20)])
> LogicalProject(comm=[$6], deptno=[$7], empno=[$0], ename=[$1], hiredate=[$4], job=[$2], mgr=[$3], sal=[$5])
> JdbcTableScan(table=[[scott, EMP]])
> )"
> : string
(*) Query in Hybrid mode (20% Morel -> 80% Calcite)
from e in (List.filter (fn e2 => e2.empno < 7700) scott.emp)
where e.deptno = 20
yield e.empno;
> val it = [7369,7566] : int list
Sys.plan();
> val it =
> "calcite(plan LogicalProject(empno=[$2])
> LogicalFilter(condition=[=($1, 20)])
> LogicalTableFunctionScan(invocation=[morelTable('#filter List (fn e2 => #empno e2 < 7700) (#emp scott)', '{
> \"fields\": [
> {
> \"type\": \"REAL\",
> \"nullable\": false,
> \"name\": \"comm\"
> },
> {
> \"type\": \"INTEGER\",
> \"nullable\": false,
> \"name\": \"deptno\"
> },
> {
> \"type\": \"INTEGER\",
> \"nullable\": false,
> \"name\": \"empno\"
> },
> {
> \"type\": \"VARCHAR\",
> \"nullable\": false,
> \"precision\": -1,
> \"name\": \"ename\"
> },
> {
> \"type\": \"VARCHAR\",
> \"nullable\": false,
> \"precision\": -1,
> \"name\": \"hiredate\"
> },
> {
> \"type\": \"VARCHAR\",
> \"nullable\": false,
> \"precision\": -1,
> \"name\": \"job\"
> },
> {
> \"type\": \"INTEGER\",
> \"nullable\": false,
> \"name\": \"mgr\"
> },
> {
> \"type\": \"REAL\",
> \"nullable\": false,
> \"name\": \"sal\"
> }
> ],
> \"nullable\": false
> }')], rowType=[RecordType(REAL comm, INTEGER deptno, INTEGER empno, VARCHAR ename, VARCHAR hiredate, VARCHAR job, INTEGER mgr, REAL sal)])
> )"
> : string
(*) Query in Hybrid mode (99% Calcite; there is a variable but it is not
(*) referenced by Calcite code)
let
val twenty = 20
in
from e in scott.emp
where e.deptno = 20
yield e.empno
end;
> val it = [7369,7566,7788,7876,7902] : int list
Sys.plan();
> val it =
> "calcite(plan LogicalProject(empno=[$2])
> LogicalFilter(condition=[=($1, 20)])
> LogicalProject(comm=[$6], deptno=[$7], empno=[$0], ename=[$1], hiredate=[$4], job=[$2], mgr=[$3], sal=[$5])
> JdbcTableScan(table=[[scott, EMP]])
> )"
> : string
(*) Query in Hybrid mode (90% Calcite; Calcite code references a variable
(*) and an expression from the enclosing environment)
let
val ten = 7 + 3
val deptNos = ten :: 20 :: 30 :: [40]
in
from e in scott.emp
where e.deptno = List.nth (deptNos, 1)
yield e.empno + 13 mod ten
end;
> val it = [7372,7569,7791,7879,7905] : int list
Sys.plan();
> val it =
> "let(matchCode0 match(ten, apply2(fnValue +, constant(7), constant(3))), resultCode calcite(plan LogicalProject($f0=[+($2, MOD(13, morelScalar('ten', '{
> \"type\": \"INTEGER\",
> \"nullable\": false
> }')))])
> LogicalFilter(condition=[=($1, morelScalar('#nth List (ten :: 20 :: 30 :: [40], 1)', '{
> \"type\": \"INTEGER\",
> \"nullable\": false
> }'))])
> LogicalProject(comm=[$6], deptno=[$7], empno=[$0], ename=[$1], hiredate=[$4], job=[$2], mgr=[$3], sal=[$5])
> JdbcTableScan(table=[[scott, EMP]])
> ))"
> : string
(*) Query in Hybrid mode (90% Calcite; Calcite code references a function
(*) from the enclosing environment)
let
fun double x = x * 2
in
from d in scott.dept
yield double d.deptno
end;
> val it = [20,40,60,80] : int list
Sys.plan();
> val it =
> "calcite(plan LogicalProject($f0=[*($0, 2)])
> JdbcTableScan(table=[[scott, DEPT]])
> )"
> : string
(*) Query on an empty list whose element is not referenced
from e in []
yield {a = 1, b = true};
> val it = [] : {a:int, b:bool} list
Sys.plan();
> val it = "calcite(plan LogicalValues(tuples=[[]])
> )" : string
(*) Equivalent, using map
map (fn e => {a = 1, b = true}) [];
> val it = [] : {a:int, b:bool} list
Sys.plan();
> val it =
> "apply(fnCode apply(fnValue List.map, argCode match(e, tuple(constant(1), constant(true)))), argCode calcite(plan LogicalValues(tuples=[[]])
> ))"
> : string
(*) Query on an empty list whose element is referenced
from e in []
yield {a = 1 + e, b = true};
> val it = [] : {a:int, b:bool} list
Sys.plan();
> val it =
> "calcite(plan LogicalProject(a=[+(1, $0)], b=[true])
> LogicalValues(tuples=[[]])
> )"
> : string
(*) Equivalent, using map
map (fn e => {a = 1 + e, b = true}) [];
> val it = [] : {a:int, b:bool} list
Sys.plan();
> val it =
> "apply(fnCode apply(fnValue List.map, argCode match(e, tuple(apply2(fnValue +, constant(1), get(name e)), constant(true)))), argCode calcite(plan LogicalValues(tuples=[[]])
> ))"
> : string
(*) Query on a singleton list whose element is not referenced
from e in [0]
yield {a = 1, b = true};
> val it = [{a=1,b=true}] : {a:int, b:bool} list
Sys.plan();
> val it = "calcite(plan LogicalValues(tuples=[[{ 1, true }]])
> )" : string
(*) Equivalent, using map
map (fn e => {a = 1, b = true}) [0];
> val it = [{a=1,b=true}] : {a:int, b:bool} list
Sys.plan();
> val it =
> "apply(fnCode apply(fnValue List.map, argCode match(e, tuple(constant(1), constant(true)))), argCode calcite(plan LogicalValues(tuples=[[{ 0 }]])
> ))"
> : string
(*) Simple query with scan, filter, project
from e in scott.emp where e.deptno = 30 yield e.empno;
> val it = [7499,7521,7654,7698,7844,7900] : int list
Sys.plan();
> val it =
> "calcite(plan LogicalProject(empno=[$2])
> LogicalFilter(condition=[=($1, 30)])
> LogicalProject(comm=[$6], deptno=[$7], empno=[$0], ename=[$1], hiredate=[$4], job=[$2], mgr=[$3], sal=[$5])
> JdbcTableScan(table=[[scott, EMP]])
> )"
> : string
(*) Equivalent #1; result and plan should be the same
let
val emps = #emp scott
in
from e in emps
where e.deptno = 30
yield e.empno
end;
> val it = [7499,7521,7654,7698,7844,7900] : int list
Sys.plan();
> val it =
> "calcite(plan LogicalProject(empno=[$2])
> LogicalFilter(condition=[=($1, 30)])
> LogicalProject(comm=[$6], deptno=[$7], empno=[$0], ename=[$1], hiredate=[$4], job=[$2], mgr=[$3], sal=[$5])
> JdbcTableScan(table=[[scott, EMP]])
> )"
> : string
(*) Equivalent #2; result and plan should be the same
let
val emps = #emp scott
val thirty = 30
in
from e in emps
where e.deptno = thirty
yield e.empno
end;
> val it = [7499,7521,7654,7698,7844,7900] : int list
Sys.plan();
> val it =
> "calcite(plan LogicalProject(empno=[$2])
> LogicalFilter(condition=[=($1, 30)])
> LogicalProject(comm=[$6], deptno=[$7], empno=[$0], ename=[$1], hiredate=[$4], job=[$2], mgr=[$3], sal=[$5])
> JdbcTableScan(table=[[scott, EMP]])
> )"
> : string
(*) Equivalent #3; result and plan should be the same
map (fn e => (#empno e))
(List.filter (fn e => (#deptno e) = 30) (#emp scott));
> val it = [7499,7521,7654,7698,7844,7900] : int list
Sys.plan();
> val it =
> "apply(fnCode apply(fnValue List.map, argCode match(e_1, apply(fnValue nth:2, argCode get(name e)))), argCode apply(fnCode apply(fnValue List.filter, argCode match(e, apply2(fnValue =, apply(fnValue nth:1, argCode get(name e)), constant(30)))), argCode calcite(plan LogicalProject(comm=[$6], deptno=[$7], empno=[$0], ename=[$1], hiredate=[$4], job=[$2], mgr=[$3], sal=[$5])
> JdbcTableScan(table=[[scott, EMP]])
> )))"
> : string
(*) Union followed by group
(*
from x in (from e in scott.emp yield e.deptno)
union (from d in scott.dept yield d.deptno)
group x compute c = count;
*)
(*) Recursive query
(*
let
fun descendants level oldEmps newEmps =
let
val pred =
if (exists prev) then
fn e => e.mgr in (from p in newEmps
yield p.empno)
else
fn e => e.mgr = 0
val nextEmps = from e in scott.emp
where (pred e)
yield {level, e.empno}
in
if (exists nextEmps) then
descendants (level + 1) (oldEmps union newEmps) nextEmps
else
(oldEmps union newEmps)
end
in
end;
*)
(*) Recursive query, 2
(*
let
fun descendants () =
(from e in scott.emp
where e.mgr = 0
yield {e, level = 0})
union
(from d in descendants (),
e in scott.emp
where e.mgr = d.e.empno
yield {e, level = d.level + 1});
in
descendants ()
end;
*)
(*) Recursive query, 3
Sys.set ("hybrid", false);
> val it = () : unit
let
fun descendants2 descendants newDescendants =
if null newDescendants then
descendants
else
descendants2 (descendants union newDescendants)
(from d in newDescendants,
e in scott.emp
where e.mgr = d.e.empno
yield {e, level = d.level + 1})
in
from d in descendants2 []
(from e in scott.emp
where e.mgr = 0
yield {e, level = 0})
yield {d.e.empno, d.e.mgr, d.e.ename, d.level}
end;
> val it =
> [{empno=7839,ename="KING",level=0,mgr=0},
> {empno=7566,ename="JONES",level=1,mgr=7839},
> {empno=7698,ename="BLAKE",level=1,mgr=7839},
> {empno=7782,ename="CLARK",level=1,mgr=7839},
> {empno=7788,ename="SCOTT",level=2,mgr=7566},
> {empno=7902,ename="FORD",level=2,mgr=7566},
> {empno=7499,ename="ALLEN",level=2,mgr=7698},
> {empno=7521,ename="WARD",level=2,mgr=7698},
> {empno=7654,ename="MARTIN",level=2,mgr=7698},
> {empno=7844,ename="TURNER",level=2,mgr=7698},
> {empno=7900,ename="JAMES",level=2,mgr=7698},
> {empno=7934,ename="MILLER",level=2,mgr=7782},
> {empno=7876,ename="ADAMS",level=3,mgr=7788},
> {empno=7369,ename="SMITH",level=3,mgr=7902}]
> : {empno:int, ename:string, level:int, mgr:int} list
"end";
> val it = "end" : string
(*) End hybrid.smli