|
1 |
| -(* open Core *) |
2 |
| -(* open Castor *) |
3 |
| -(* open Ast *) |
4 |
| -(* open Schema *) |
5 |
| -(* open Collections *) |
6 |
| -(* module P = Pred.Infix *) |
7 |
| -(* module A = Abslayout *) |
8 |
| -(* open Match.Query *) |
9 |
| -(* open Egraph_matcher *) |
10 |
| - |
11 |
| -(* let elim_join_nest g _ = *) |
12 |
| -(* let%bind root, join = M.any_join g in *) |
13 |
| -(* let scope = Fresh.name Global.fresh "s%d" in *) |
14 |
| -(* let pred = Pred.scoped (schema r1) scope pred in *) |
15 |
| -(* let lhs = *) |
16 |
| -(* let scalars = *) |
17 |
| -(* Schema.schema r1 |> Schema.scoped scope |> List.map ~f:C.scalar_name *) |
18 |
| -(* in *) |
19 |
| -(* A.tuple scalars Cross *) |
20 |
| -(* and rhs = A.filter pred r2 in *) |
21 |
| -(* return (root, C.list join.r1 @@ A.tuple [ lhs; rhs ] Cross) *) |
22 |
| - |
23 |
| -(* let elim_join_nest = of_func elim_join_nest ~name:"elim-join-nest" *) |
24 |
| - |
25 |
| -(* let elim_join_hash r = *) |
26 |
| -(* let open Option.Let_syntax in *) |
27 |
| -(* let%bind pred, r1, r2 = to_join r in *) |
28 |
| -(* match pred with *) |
29 |
| -(* | `Binop (Eq, kl, kr) -> *) |
30 |
| -(* let join_scope = Fresh.name Global.fresh "s%d" *) |
31 |
| -(* and hash_scope = Fresh.name Global.fresh "s%d" *) |
32 |
| -(* and r1_schema = schema r1 in *) |
33 |
| -(* let key_name = Fresh.name Global.fresh "k%d" in *) |
34 |
| -(* let layout = *) |
35 |
| -(* let slist = *) |
36 |
| -(* let r1_schema = Schema.scoped join_scope r1_schema in *) |
37 |
| -(* r1_schema @ schema r2 |> Select_list.of_names *) |
38 |
| -(* in *) |
39 |
| -(* A.dep_join r1 join_scope @@ A.select slist *) |
40 |
| -(* @@ A.hash_idx *) |
41 |
| -(* (A.dedup @@ A.select [ (kr, key_name) ] r2) *) |
42 |
| -(* hash_scope *) |
43 |
| -(* (A.filter *) |
44 |
| -(* (`Binop *) |
45 |
| -(* (Eq, `Name (Name.create ~scope:hash_scope key_name), kr)) *) |
46 |
| -(* r2) *) |
47 |
| -(* [ Pred.scoped r1_schema join_scope kl ] *) |
48 |
| -(* in *) |
49 |
| -(* Some layout *) |
50 |
| -(* | _ -> None *) |
51 |
| - |
52 |
| -(* let elim_join_hash = of_func elim_join_hash ~name:"elim-join-hash" *) |
53 |
| - |
54 |
| -(* let elim_join_filter r = *) |
55 |
| -(* let open Option.Let_syntax in *) |
56 |
| -(* let%map pred, r1, r2 = to_join r in *) |
57 |
| -(* A.filter pred (A.join (`Bool true) r1 r2) *) |
58 |
| - |
59 |
| -(* let elim_join_filter = of_func elim_join_filter ~name:"elim-join-filter" *) |
| 1 | +open Core |
| 2 | +module Subst = Castor.Subst |
| 3 | +module Schema = Castor.Schema |
| 4 | +module Name = Castor.Name |
| 5 | +module Free = Castor.Free |
| 6 | +module Abslayout = Castor.Abslayout |
| 7 | +module Pred = Castor.Pred |
| 8 | +module Select_list = Castor.Select_list |
| 9 | +module Fresh = Castor.Fresh |
| 10 | +module Global = Castor.Global |
| 11 | +module Egraph = Castor.Egraph |
| 12 | +module P = Pred.Infix |
| 13 | +module A = Abslayout |
| 14 | +open Egraph_matcher |
| 15 | + |
| 16 | +let elim_join_nest g _ = |
| 17 | + let%map root, join = M.any_join g in |
| 18 | + let lhs_schema = G.schema g join.r1 in |
| 19 | + let pred = |
| 20 | + Subst.subst_pred |
| 21 | + (List.map lhs_schema ~f:(fun n -> (n, `Name (Name.zero n))) |
| 22 | + |> Map.of_alist_exn (module Name)) |
| 23 | + join.pred |
| 24 | + in |
| 25 | + let lhs = C.tuple g (List.map lhs_schema ~f:(C.scalar_name g)) Cross |
| 26 | + and rhs = C.filter g pred (of_annot g @@ Subst.incr @@ to_annot g join.r2) in |
| 27 | + (root, C.depjoin g { d_lhs = join.r1; d_rhs = C.tuple g [ lhs; rhs ] Cross }) |
| 28 | + |
| 29 | +let elim_join_hash g _ = |
| 30 | + let%bind root, join = M.any_join g in |
| 31 | + match join.pred with |
| 32 | + | `Binop (Eq, kl, kr) -> |
| 33 | + let r1_schema = G.schema g join.r1 in |
| 34 | + let kl = |
| 35 | + Subst.incr_pred kl |
| 36 | + |> Subst.subst_pred |
| 37 | + (Map.of_alist_exn (module Name) |
| 38 | + @@ List.map r1_schema ~f:(fun n -> (n, `Name (Name.zero n)))) |
| 39 | + in |
| 40 | + let layout = |
| 41 | + let slist = |
| 42 | + Select_list.of_names |
| 43 | + (List.map ~f:Name.zero r1_schema @ G.schema g join.r2) |
| 44 | + in |
| 45 | + C.depjoin g |
| 46 | + { |
| 47 | + d_lhs = join.r1; |
| 48 | + d_rhs = |
| 49 | + C.select g slist |
| 50 | + @@ C.hash_idx g |
| 51 | + { |
| 52 | + hi_keys = |
| 53 | + C.dedup g |
| 54 | + @@ C.select g [ (kr, "key") ] |
| 55 | + @@ of_annot g @@ Subst.incr @@ to_annot g join.r2; |
| 56 | + hi_values = |
| 57 | + C.filter g |
| 58 | + (`Binop |
| 59 | + (Eq, `Name (Name.zero @@ Name.create "key"), kr)) |
| 60 | + @@ of_annot g @@ Subst.incr @@ Subst.incr |
| 61 | + @@ to_annot g join.r2; |
| 62 | + hi_lookup = [ kl ]; |
| 63 | + hi_key_layout = None; |
| 64 | + }; |
| 65 | + } |
| 66 | + in |
| 67 | + return (root, layout) |
| 68 | + | _ -> empty |
| 69 | + |
| 70 | +let elim_join_filter g _ = |
| 71 | + let%map root, join = M.any_join g in |
| 72 | + (root, C.filter g join.pred (C.join g (`Bool true) join.r1 join.r2)) |
60 | 73 |
|
61 | 74 | (* let hoist_join_param_filter r = *)
|
62 | 75 | (* let open Option.Let_syntax in *)
|
|
163 | 176 | (* * return *)
|
164 | 177 | (* * @@ A.filter (Pred.conjoin hoist) *)
|
165 | 178 | (* * @@ A.join (Pred.conjoin keep) r1 r2 *\) *)
|
| 179 | + |
| 180 | +let () = |
| 181 | + Ops.register elim_join_nest "elim-join-nest"; |
| 182 | + Ops.register elim_join_hash "elim-join-hash"; |
| 183 | + Ops.register elim_join_hash "elim-join-filter" |
0 commit comments