aboutsummaryrefslogtreecommitdiff
path: root/lib/expression/query.ml
blob: 5bd914a030188809a6f95bfec7676c1c6378f7e3 (plain)
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
(** 
 This module create an sql query from an expression.
 *)

open StdLabels

(** This type is used in the query builder (see [query_of_expression] just
    below in order to tell if we need to bind the parameters in the query, or
    if we can use plain literal as is (with some risk at the execution time. *)
type _ binded_query =
  | BindParam : ImportCSV.DataType.t Queue.t binded_query
  | NoParam : unit binded_query

module QueryParameter = struct
  (** Internaly, we need to keep a different type for the Literal chunks
    (which requires to be quoted), and raw (which should be given as is to the
    sql engine) 

    The Raw can be generated from both BindParam or NoParam queries. *)
  type t =
    | Literal
    | Queue of ImportCSV.DataType.t Queue.t
    | Raw of t

  (** Wrap the given parameter mode into the raw mode *)
  let raw : t -> t = function
    | Raw t -> Raw t
    | Literal -> Raw Literal
    | Queue q -> Raw (Queue q)

  (** Nest the parameter in order to use it inside another function call. 

      The rule is to get out of the Raw mode as soon as we dive into another
      one function. *)
  let nest : t -> t = function
    | Raw t -> t
    | other -> other
end

module TypeBuilder =
  Compose.Expression
    (Type_of)
    (struct
      let v = ignore
    end)

module Query = TypeBuilder.Make (struct
  type 'a repr = Format.formatter -> nested:QueryParameter.t -> unit
  type 'a obs = Format.formatter -> nested:QueryParameter.t -> unit
  type 'a path_repr = Format.formatter -> 'a -> unit

  let observe : 'a Type_of.obs * 'a repr -> 'a obs =
   fun (_, x) formatter ~nested ->
    let () = x formatter ~nested in
    Format.pp_print_flush formatter ()

  (** Unify an external reference with a given type, using the COALESCE
        function *)
  let unify :
      with_:Type_of.t ->
      nested:QueryParameter.t ->
      Format.formatter ->
      'a Type_of.obs * 'a repr ->
      unit =
   fun ~with_ ~nested format (type_of, expr) ->
    match (type_of, with_) with
    | ImportDataTypes.Types.Extern, Number
    | ImportDataTypes.Types.Extern, Extern ->
        Format.fprintf format "COALESCE(%a,0)"
          (fun f expr -> expr f ~nested)
          expr
    | ImportDataTypes.Types.Extern, String ->
        Format.fprintf format "COALESCE(%a,'')"
          (fun f expr -> expr f ~nested)
          expr
    | _, Float ->
        Format.fprintf format "CAST(%a AS REAL)"
          (fun f expr -> expr f ~nested)
          expr
    | _, _ -> expr ~nested format

  let empty : 'a Type_of.obs -> 'a repr =
   fun type_of formatter ~nested ->
    ignore type_of;
    ignore nested;
    Format.fprintf formatter "''"

  let expr : 'a Type_of.obs * 'a repr -> 'a Type_of.obs -> 'a repr =
   fun expr type_of formatter ~nested ->
    ignore type_of;
    Format.fprintf formatter "(";
    (snd expr) ~nested formatter;
    Format.fprintf formatter ")"

  let literal : string -> 'a Type_of.obs -> 'a repr =
   fun l type_of formatter ~nested ->
    ignore type_of;
    match nested with
    | QueryParameter.Literal ->
        (* If the text is a true literal, we insert it directly. This is
           only called from the [query_of_expression] function *)
        Format.fprintf formatter "'%s'" l
    | QueryParameter.Queue queue ->
        Format.fprintf formatter "?";
        Queue.add (ImportCSV.DataType.Content l) queue
    | QueryParameter.Raw _ -> Format.fprintf formatter "%s" l

  let integer : string -> 'a Type_of.obs -> 'a repr =
   fun l type_of formatter ~nested ->
    ignore type_of;
    ignore nested;
    Format.fprintf formatter "%s" l

  let path : 'b path_repr -> 'b -> 'a Type_of.obs -> 'a repr =
   fun repr p type_of formatter ~nested ->
    ignore nested;
    ignore type_of;
    repr formatter p

  let concat : ('a Type_of.obs * 'a repr) list -> 'a Type_of.obs -> 'a repr =
   fun expression type_of formatter ~nested ->
    ignore type_of;
    let nested' = QueryParameter.nest nested in

    Format.pp_print_list
      ~pp_sep:(fun f () -> Format.fprintf f " || ")
      (unify ~with_:ImportDataTypes.Types.String ~nested:nested')
      formatter expression

  let print_expression :
      ?sep:string ->
      QueryParameter.t ->
      Format.formatter ->
      ('a Type_of.obs * 'a repr) list ->
      unit =
   fun ?(sep = ", ") nested formatter expression ->
    (Format.pp_print_list
       ~pp_sep:(fun f () -> Format.fprintf f "%s" sep)
       (fun f v -> (snd v) f ~nested))
      formatter expression

  (** Format the partition expression. This function is used internally and
      only form the expression inside the clause. *)
  let group_windows :
      QueryParameter.t ->
      Format.formatter ->
      ('a Type_of.obs * 'a repr) list
      * ('a Type_of.obs * 'a repr) list
      * string option ->
      unit =
   fun nested formatter (expressions, order, range) ->
    match (expressions, order) with
    | [], _ -> ()
    | _, [] ->
        Format.fprintf formatter " OVER (PARTITION BY %a%a)"
          (print_expression nested) expressions
          (Format.pp_print_option (fun f v -> Format.fprintf f "%s" v))
          range
    | _, _ ->
        Format.fprintf formatter " OVER (PARTITION BY %a ORDER BY %a%a)"
          (print_expression nested) expressions (print_expression nested) order
          (Format.pp_print_option (fun f v -> Format.fprintf f "%s" v))
          range

  let window :
      ('a Type_of.obs * 'a repr) T.window ->
      ('a Type_of.obs * 'a repr) list ->
      ('a Type_of.obs * 'a repr) list ->
      'a Type_of.obs ->
      'a repr =
   fun name expressions order type_of formatter ~nested ->
    ignore type_of;
    let nested' = QueryParameter.nest nested in

    (* By default, the range is defined like this

       [RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE NO OTHERS]

       this only build a range until the current row, but in some cases (min,
       last), we want to scan the whole group in order to evaluate the value to
       keep.
    *)
    let range = " RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" in
    match name with
    | T.Min expr ->
        Format.fprintf formatter "FIRST_VALUE(%a)%a "
          (fun f v -> (snd v) f ~nested:nested')
          expr (group_windows nested')
          (expressions, order, Some range)
    | T.Max expr ->
        Format.fprintf formatter "LAST_VALUE(%a)%a"
          (fun f v -> (snd v) f ~nested:nested')
          expr (group_windows nested')
          (expressions, order, Some range)
    | T.Counter ->
        (* If no order is given, return the number of elements in the
           whole group *)
        let operator =
          match order with
          | [] -> "COUNT"
          | _ -> "ROW_NUMBER"
        in
        Format.fprintf formatter "%s()%a" operator (group_windows nested')
          (expressions, order, None)
    | T.Previous expr ->
        Format.fprintf formatter "LAG(%a)%a"
          (fun f v -> (snd v) f ~nested:nested')
          expr (group_windows nested') (expressions, order, None)
    | T.Sum expr ->
        Format.fprintf formatter "SUM(%a)%a"
          (fun f v -> (snd v) f ~nested:nested')
          expr (group_windows nested') (expressions, order, None)

  let nvl : ('a Type_of.obs * 'a repr) list -> 'a Type_of.obs -> 'a repr =
   fun expression type_of formatter ~nested ->
    ignore type_of;
    let nested' = QueryParameter.nest nested in
    Format.fprintf formatter "COALESCE(%a)" (print_expression nested')
      expression

  let join :
      string -> ('a Type_of.obs * 'a repr) list -> 'a Type_of.obs -> 'a repr =
   fun sep expression type_of formatter ~nested ->
    ignore type_of;
    let nested' = QueryParameter.nest nested in

    (* Directly call the literal function for the first argument *)
    Format.fprintf formatter "CONCAT(%a, %a)"
      (fun f v -> (literal v ImportDataTypes.Types.String) f ~nested:nested')
      sep (print_expression nested') expression

  let boperator :
      T.binary_operator ->
      'a Type_of.obs * 'a repr ->
      'a Type_of.obs * 'a repr ->
      'a Type_of.obs ->
      'a repr =
   fun name e1 e2 type_of formatter ~nested ->
    ignore type_of;
    (* When dividing, we need to be sure that the type is a float,
       otherwise SQL will truncate the result *)
    let with_ =
      match name with
      | T.Division -> ImportDataTypes.Types.Float
      | _ -> fst e2
    in

    let nested' = QueryParameter.nest nested in
    Format.fprintf formatter "%a%s%a"
      (unify ~with_ ~nested:nested')
      e1
      (* The operator *)
      (T.name_of_operator name)
      (unify ~with_:(fst e1) ~nested:nested')
      e2

  let gequality :
      T.binary_operator ->
      'a Type_of.obs * 'a repr ->
      ('a Type_of.obs * 'a repr) list ->
      'a Type_of.obs ->
      'a repr =
   fun name e1 group type_of ->
    ignore type_of;
    let group_type = List.map ~f:fst group in
    fun formatter ~nested ->
      let nested' = QueryParameter.nest nested in
      let op_name =
        match name with
        | T.Equal -> " IN("
        | T.Different -> " NOT IN("
        | _ -> ""
      in

      Format.fprintf formatter "%a%s%a)"
        (unify ~with_:(Type_of.group' group_type) ~nested:nested')
        e1 op_name (print_expression nested') group

  let exprs expressions formatter ~nested =
    (* Literal expression, starting from now, all the quoted string are
       directly given to the sql engine *)
    let nested' = QueryParameter.raw nested in

    Format.fprintf formatter "(%a)"
      (print_expression ~sep:" " nested')
      expressions

  let rec funct :
      string -> ('a Type_of.obs * 'a repr) list -> 'a Type_of.obs -> 'a repr =
   fun name expressions type_of formatter ~nested ->
    ignore type_of;
    let nested' = QueryParameter.nest nested in
    match name with
    | "expr" ->
        (* Raw expression are parsed directly *)
        exprs expressions formatter ~nested
    | "if" ->
        (* The if is renamed into IIF *)
        funct "IIF" expressions type_of formatter ~nested
    | _ ->
        (* Default case *)
        Format.fprintf formatter "%s(%a)" name (print_expression nested')
          expressions

  let function' :
      T.funct -> ('a Type_of.obs * 'a repr) list -> 'a Type_of.obs -> 'a repr =
   fun name expressions type_of formatter ~nested ->
    ignore type_of;
    let nested' = QueryParameter.nest nested in
    match name with
    | Upper | Trim ->
        Format.fprintf formatter "%s(%a)" (T.name_of_function name)
          (print_expression nested') expressions
end)

module M = Sym.M (Query)

let query_of_expression :
    type b.
    b binded_query ->
    Format.formatter ->
    (Format.formatter -> 'a -> unit) ->
    'a T.t ->
    b =
 fun parameter formatter printer expr ->
  let repr = M.eval ~path_repr:printer expr in
  match parameter with
  | BindParam ->
      let p = Queue.create () in
      let parameter = QueryParameter.Queue p in
      Query.observe repr formatter ~nested:parameter;
      p
  | NoParam ->
      Query.observe repr formatter ~nested:Literal;
      ()