annotate src/sqlcache.sml @ 2243:da7d026d1a94

Fix possible formula simplification bug with extra formula' type.
author Ziv Scully <ziv@mit.edu>
date Mon, 20 Jul 2015 19:49:13 -0700
parents 88cc0f44c940
children e4a7e3cd6f11
rev   line source
ziv@2235 1 structure Sqlcache (* DEBUG: add back :> SQLCACHE. *) = struct
ziv@2209 2
ziv@2209 3 open Mono
ziv@2209 4
ziv@2209 5 structure IS = IntBinarySet
ziv@2209 6 structure IM = IntBinaryMap
ziv@2213 7 structure SK = struct type ord_key = string val compare = String.compare end
ziv@2213 8 structure SS = BinarySetFn(SK)
ziv@2213 9 structure SM = BinaryMapFn(SK)
ziv@2213 10 structure SIMM = MultimapFn(structure KeyMap = SM structure ValSet = IS)
ziv@2209 11
ziv@2216 12 (* Filled in by [cacheWrap] during [Sqlcache]. *)
ziv@2213 13 val ffiInfo : {index : int, params : int} list ref = ref []
ziv@2209 14
ziv@2227 15 fun resetFfiInfo () = ffiInfo := []
ziv@2227 16
ziv@2213 17 fun getFfiInfo () = !ffiInfo
ziv@2213 18
ziv@2215 19 (* Some FFIs have writing as their only effect, which the caching records. *)
ziv@2215 20 val ffiEffectful =
ziv@2223 21 (* ASK: how can this be less hard-coded? *)
ziv@2215 22 let
ziv@2215 23 val fs = SS.fromList ["htmlifyInt_w",
ziv@2215 24 "htmlifyFloat_w",
ziv@2215 25 "htmlifyString_w",
ziv@2215 26 "htmlifyBool_w",
ziv@2215 27 "htmlifyTime_w",
ziv@2215 28 "attrifyInt_w",
ziv@2215 29 "attrifyFloat_w",
ziv@2215 30 "attrifyString_w",
ziv@2215 31 "attrifyChar_w",
ziv@2215 32 "urlifyInt_w",
ziv@2215 33 "urlifyFloat_w",
ziv@2215 34 "urlifyString_w",
ziv@2215 35 "urlifyBool_w",
ziv@2215 36 "urlifyChannel_w"]
ziv@2215 37 in
ziv@2215 38 fn (m, f) => Settings.isEffectful (m, f)
ziv@2215 39 andalso not (m = "Basis" andalso SS.member (fs, f))
ziv@2215 40 end
ziv@2215 41
ziv@2234 42 val cache = ref LruCache.cache
ziv@2233 43 fun setCache c = cache := c
ziv@2233 44 fun getCache () = !cache
ziv@2233 45
ziv@2215 46
ziv@2215 47 (* Effect analysis. *)
ziv@2215 48
ziv@2216 49 (* Makes an exception for [EWrite] (which is recorded when caching). *)
ziv@2230 50 fun effectful doPrint (effs : IS.set) (inFunction : bool) (bound : int) : exp -> bool =
ziv@2215 51 (* If result is true, expression is definitely effectful. If result is
ziv@2215 52 false, then expression is definitely not effectful if effs is fully
ziv@2215 53 populated. The intended pattern is to use this a number of times equal
ziv@2215 54 to the number of declarations in a file, Bellman-Ford style. *)
ziv@2234 55 (* TODO: make incrementing of the number of bound variables cleaner,
ziv@2234 56 probably by using [MonoUtil] instead of all this. *)
ziv@2215 57 let
ziv@2215 58 (* DEBUG: remove printing when done. *)
ziv@2215 59 fun tru msg = if doPrint then (print (msg ^ "\n"); true) else true
ziv@2215 60 val rec eff' =
ziv@2215 61 (* ASK: is there a better way? *)
ziv@2215 62 fn EPrim _ => false
ziv@2215 63 (* We don't know if local functions have effects when applied. *)
ziv@2215 64 | ERel idx => if inFunction andalso idx >= bound
ziv@2215 65 then tru ("rel" ^ Int.toString idx) else false
ziv@2215 66 | ENamed name => if IS.member (effs, name) then tru "named" else false
ziv@2215 67 | ECon (_, _, NONE) => false
ziv@2215 68 | ECon (_, _, SOME e) => eff e
ziv@2215 69 | ENone _ => false
ziv@2215 70 | ESome (_, e) => eff e
ziv@2215 71 | EFfi (m, f) => if ffiEffectful (m, f) then tru "ffi" else false
ziv@2215 72 | EFfiApp (m, f, _) => if ffiEffectful (m, f) then tru "ffiapp" else false
ziv@2215 73 (* ASK: we're calling functions effectful if they have effects when
ziv@2215 74 applied or if the function expressions themselves have effects.
ziv@2215 75 Is that okay? *)
ziv@2215 76 (* This is okay because the values we ultimately care about aren't
ziv@2215 77 functions, and this is a conservative approximation, anyway. *)
ziv@2215 78 | EApp (eFun, eArg) => effectful doPrint effs true bound eFun orelse eff eArg
ziv@2215 79 | EAbs (_, _, _, e) => effectful doPrint effs inFunction (bound+1) e
ziv@2215 80 | EUnop (_, e) => eff e
ziv@2215 81 | EBinop (_, _, e1, e2) => eff e1 orelse eff e2
ziv@2215 82 | ERecord xs => List.exists (fn (_, e, _) => eff e) xs
ziv@2215 83 | EField (e, _) => eff e
ziv@2215 84 (* If any case could be effectful, consider it effectful. *)
ziv@2215 85 | ECase (e, xs, _) => eff e orelse List.exists (fn (_, e) => eff e) xs
ziv@2215 86 | EStrcat (e1, e2) => eff e1 orelse eff e2
ziv@2215 87 (* ASK: how should we treat these three? *)
ziv@2215 88 | EError _ => tru "error"
ziv@2215 89 | EReturnBlob _ => tru "blob"
ziv@2215 90 | ERedirect _ => tru "redirect"
ziv@2215 91 (* EWrite is a special exception because we record writes when caching. *)
ziv@2215 92 | EWrite _ => false
ziv@2215 93 | ESeq (e1, e2) => eff e1 orelse eff e2
ziv@2215 94 (* TODO: keep context of which local variables aren't effectful? Only
ziv@2215 95 makes a difference for function expressions, though. *)
ziv@2215 96 | ELet (_, _, eBind, eBody) => eff eBind orelse
ziv@2215 97 effectful doPrint effs inFunction (bound+1) eBody
ziv@2215 98 | EClosure (_, es) => List.exists eff es
ziv@2215 99 (* TODO: deal with EQuery. *)
ziv@2215 100 | EQuery _ => tru "query"
ziv@2215 101 | EDml _ => tru "dml"
ziv@2215 102 | ENextval _ => tru "nextval"
ziv@2215 103 | ESetval _ => tru "setval"
ziv@2215 104 | EUnurlify (e, _, _) => eff e
ziv@2215 105 (* ASK: how should we treat this? *)
ziv@2215 106 | EJavaScript _ => tru "javascript"
ziv@2215 107 (* ASK: these are all effectful, right? *)
ziv@2215 108 | ESignalReturn _ => tru "signalreturn"
ziv@2215 109 | ESignalBind _ => tru "signalbind"
ziv@2215 110 | ESignalSource _ => tru "signalsource"
ziv@2215 111 | EServerCall _ => tru "servercall"
ziv@2215 112 | ERecv _ => tru "recv"
ziv@2215 113 | ESleep _ => tru "sleep"
ziv@2215 114 | ESpawn _ => tru "spawn"
ziv@2215 115 and eff = fn (e', _) => eff' e'
ziv@2215 116 in
ziv@2215 117 eff
ziv@2215 118 end
ziv@2215 119
ziv@2215 120 (* TODO: test this. *)
ziv@2215 121 val effectfulMap =
ziv@2215 122 let
ziv@2215 123 fun doVal ((_, name, _, e, _), effMap) =
ziv@2215 124 if effectful false effMap false 0 e
ziv@2215 125 then IS.add (effMap, name)
ziv@2215 126 else effMap
ziv@2215 127 val doDecl =
ziv@2215 128 fn (DVal v, effMap) => doVal (v, effMap)
ziv@2215 129 (* Repeat the list of declarations a number of times equal to its size. *)
ziv@2215 130 | (DValRec vs, effMap) =>
ziv@2215 131 List.foldl doVal effMap (List.concat (List.map (fn _ => vs) vs))
ziv@2215 132 (* ASK: any other cases? *)
ziv@2215 133 | (_, effMap) => effMap
ziv@2215 134 in
ziv@2215 135 MonoUtil.File.fold {typ = #2, exp = #2, decl = doDecl} IS.empty
ziv@2215 136 end
ziv@2215 137
ziv@2215 138
ziv@2216 139 (* Boolean formula normalization. *)
ziv@2216 140
ziv@2234 141 datatype junctionType = Conj | Disj
ziv@2216 142
ziv@2216 143 datatype 'atom formula =
ziv@2216 144 Atom of 'atom
ziv@2216 145 | Negate of 'atom formula
ziv@2234 146 | Combo of junctionType * 'atom formula list
ziv@2216 147
ziv@2243 148 (* Guaranteed to have all negation pushed to the atoms. *)
ziv@2243 149 datatype 'atom formula' =
ziv@2243 150 Atom' of 'atom
ziv@2243 151 | Combo' of junctionType * 'atom formula' list
ziv@2243 152
ziv@2234 153 val flipJt = fn Conj => Disj | Disj => Conj
ziv@2216 154
ziv@2236 155 fun concatMap f xs = List.concat (map f xs)
ziv@2216 156
ziv@2216 157 val rec cartesianProduct : 'a list list -> 'a list list =
ziv@2216 158 fn [] => [[]]
ziv@2236 159 | (xs :: xss) => concatMap (fn ys => concatMap (fn x => [x :: ys]) xs)
ziv@2236 160 (cartesianProduct xss)
ziv@2216 161
ziv@2218 162 (* Pushes all negation to the atoms.*)
ziv@2218 163 fun pushNegate (negate : 'atom -> 'atom) (negating : bool) =
ziv@2243 164 fn Atom x => Atom' (if negating then negate x else x)
ziv@2218 165 | Negate f => pushNegate negate (not negating) f
ziv@2243 166 | Combo (j, fs) => Combo' (if negating then flipJt j else j, map (pushNegate negate negating) fs)
ziv@2218 167
ziv@2218 168 val rec flatten =
ziv@2243 169 fn Combo' (_, [f]) => flatten f
ziv@2243 170 | Combo' (j, fs) =>
ziv@2243 171 Combo' (j, List.foldr (fn (f, acc) =>
ziv@2243 172 case f of
ziv@2243 173 Combo' (j', fs') =>
ziv@2243 174 if j = j' orelse length fs' = 1
ziv@2243 175 then fs' @ acc
ziv@2243 176 else f :: acc
ziv@2243 177 | _ => f :: acc)
ziv@2243 178 []
ziv@2243 179 (map flatten fs))
ziv@2218 180 | f => f
ziv@2218 181
ziv@2243 182 (* [simplify] operates on the desired normal form. E.g., if [junc] is [Disj],
ziv@2243 183 consider the list of lists to be a disjunction of conjunctions. *)
ziv@2237 184 fun normalize' (simplify : 'a list list -> 'a list list)
ziv@2235 185 (junc : junctionType) =
ziv@2216 186 let
ziv@2235 187 fun norm junc =
ziv@2237 188 simplify
ziv@2243 189 o (fn Atom' x => [[x]]
ziv@2243 190 | Combo' (j, fs) =>
ziv@2235 191 let
ziv@2236 192 val fss = map (norm junc) fs
ziv@2235 193 in
ziv@2236 194 if j = junc
ziv@2236 195 then List.concat fss
ziv@2236 196 else map List.concat (cartesianProduct fss)
ziv@2235 197 end)
ziv@2216 198 in
ziv@2235 199 norm junc
ziv@2216 200 end
ziv@2216 201
ziv@2237 202 fun normalize simplify negate junc =
ziv@2243 203 normalize' simplify junc
ziv@2235 204 o flatten
ziv@2235 205 o pushNegate negate false
ziv@2216 206
ziv@2221 207 fun mapFormula mf =
ziv@2221 208 fn Atom x => Atom (mf x)
ziv@2221 209 | Negate f => Negate (mapFormula mf f)
ziv@2235 210 | Combo (j, fs) => Combo (j, map (mapFormula mf) fs)
ziv@2216 211
ziv@2230 212
ziv@2215 213 (* SQL analysis. *)
ziv@2213 214
ziv@2240 215 structure CmpKey = struct
ziv@2235 216
ziv@2235 217 type ord_key = Sql.cmp
ziv@2235 218
ziv@2235 219 val compare =
ziv@2235 220 fn (Sql.Eq, Sql.Eq) => EQUAL
ziv@2235 221 | (Sql.Eq, _) => LESS
ziv@2235 222 | (_, Sql.Eq) => GREATER
ziv@2235 223 | (Sql.Ne, Sql.Ne) => EQUAL
ziv@2235 224 | (Sql.Ne, _) => LESS
ziv@2235 225 | (_, Sql.Ne) => GREATER
ziv@2235 226 | (Sql.Lt, Sql.Lt) => EQUAL
ziv@2235 227 | (Sql.Lt, _) => LESS
ziv@2235 228 | (_, Sql.Lt) => GREATER
ziv@2235 229 | (Sql.Le, Sql.Le) => EQUAL
ziv@2235 230 | (Sql.Le, _) => LESS
ziv@2235 231 | (_, Sql.Le) => GREATER
ziv@2235 232 | (Sql.Gt, Sql.Gt) => EQUAL
ziv@2235 233 | (Sql.Gt, _) => LESS
ziv@2235 234 | (_, Sql.Gt) => GREATER
ziv@2235 235 | (Sql.Ge, Sql.Ge) => EQUAL
ziv@2235 236
ziv@2235 237 end
ziv@2235 238
ziv@2216 239 val rec chooseTwos : 'a list -> ('a * 'a) list =
ziv@2216 240 fn [] => []
ziv@2216 241 | x :: ys => map (fn y => (x, y)) ys @ chooseTwos ys
ziv@2213 242
ziv@2237 243 fun removeRedundant madeRedundantBy zs =
ziv@2237 244 let
ziv@2237 245 fun removeRedundant' (xs, ys) =
ziv@2237 246 case xs of
ziv@2237 247 [] => ys
ziv@2237 248 | x :: xs' =>
ziv@2237 249 removeRedundant' (xs',
ziv@2237 250 if List.exists (fn y => madeRedundantBy (x, y)) (xs' @ ys)
ziv@2237 251 then ys
ziv@2237 252 else x :: ys)
ziv@2237 253 in
ziv@2237 254 removeRedundant' (zs, [])
ziv@2237 255 end
ziv@2237 256
ziv@2216 257 datatype atomExp =
ziv@2216 258 QueryArg of int
ziv@2216 259 | DmlRel of int
ziv@2216 260 | Prim of Prim.t
ziv@2216 261 | Field of string * string
ziv@2216 262
ziv@2216 263 structure AtomExpKey : ORD_KEY = struct
ziv@2216 264
ziv@2234 265 type ord_key = atomExp
ziv@2216 266
ziv@2234 267 val compare =
ziv@2234 268 fn (QueryArg n1, QueryArg n2) => Int.compare (n1, n2)
ziv@2234 269 | (QueryArg _, _) => LESS
ziv@2234 270 | (_, QueryArg _) => GREATER
ziv@2234 271 | (DmlRel n1, DmlRel n2) => Int.compare (n1, n2)
ziv@2234 272 | (DmlRel _, _) => LESS
ziv@2234 273 | (_, DmlRel _) => GREATER
ziv@2234 274 | (Prim p1, Prim p2) => Prim.compare (p1, p2)
ziv@2234 275 | (Prim _, _) => LESS
ziv@2234 276 | (_, Prim _) => GREATER
ziv@2234 277 | (Field (t1, f1), Field (t2, f2)) =>
ziv@2234 278 case String.compare (t1, t2) of
ziv@2234 279 EQUAL => String.compare (f1, f2)
ziv@2234 280 | ord => ord
ziv@2216 281
ziv@2216 282 end
ziv@2216 283
ziv@2216 284 structure UF = UnionFindFn(AtomExpKey)
ziv@2234 285
ziv@2235 286 structure ConflictMaps = struct
ziv@2235 287
ziv@2235 288 structure TK = TripleKeyFn(structure I = CmpKey
ziv@2235 289 structure J = OptionKeyFn(AtomExpKey)
ziv@2235 290 structure K = OptionKeyFn(AtomExpKey))
ziv@2235 291 structure TS = BinarySetFn(TK)
ziv@2235 292
ziv@2235 293 val toKnownEquality =
ziv@2235 294 (* [NONE] here means unkown. Anything that isn't a comparison between two
ziv@2235 295 knowns shouldn't be used, and simply dropping unused terms is okay in
ziv@2235 296 disjunctive normal form. *)
ziv@2235 297 fn (Sql.Eq, SOME e1, SOME e2) => SOME (e1, e2)
ziv@2235 298 | _ => NONE
ziv@2235 299
ziv@2235 300 val equivClasses : (Sql.cmp * atomExp option * atomExp option) list -> atomExp list list =
ziv@2235 301 UF.classes
ziv@2235 302 o List.foldl UF.union' UF.empty
ziv@2235 303 o List.mapPartial toKnownEquality
ziv@2235 304
ziv@2235 305 fun addToEqs (eqs, n, e) =
ziv@2235 306 case IM.find (eqs, n) of
ziv@2235 307 (* Comparing to a constant is probably better than comparing to a
ziv@2235 308 variable? Checking that existing constants match a new ones is
ziv@2235 309 handled by [accumulateEqs]. *)
ziv@2235 310 SOME (Prim _) => eqs
ziv@2235 311 | _ => IM.insert (eqs, n, e)
ziv@2235 312
ziv@2235 313 val accumulateEqs =
ziv@2235 314 (* [NONE] means we have a contradiction. *)
ziv@2235 315 fn (_, NONE) => NONE
ziv@2235 316 | ((Prim p1, Prim p2), eqso) =>
ziv@2235 317 (case Prim.compare (p1, p2) of
ziv@2235 318 EQUAL => eqso
ziv@2235 319 | _ => NONE)
ziv@2235 320 | ((QueryArg n, Prim p), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
ziv@2235 321 | ((QueryArg n, DmlRel r), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
ziv@2235 322 | ((Prim p, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
ziv@2235 323 | ((DmlRel r, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
ziv@2235 324 (* TODO: deal with equalities between [DmlRel]s and [Prim]s.
ziv@2235 325 This would involve guarding the invalidation with a check for the
ziv@2235 326 relevant comparisons. *)
ziv@2235 327 | (_, eqso) => eqso
ziv@2235 328
ziv@2235 329 val eqsOfClass : atomExp list -> atomExp IM.map option =
ziv@2235 330 List.foldl accumulateEqs (SOME IM.empty)
ziv@2235 331 o chooseTwos
ziv@2235 332
ziv@2235 333 fun toAtomExps rel (cmp, e1, e2) =
ziv@2235 334 let
ziv@2235 335 val qa =
ziv@2235 336 (* Here [NONE] means unkown. *)
ziv@2235 337 fn Sql.SqConst p => SOME (Prim p)
ziv@2235 338 | Sql.Field tf => SOME (Field tf)
ziv@2235 339 | Sql.Inj (EPrim p, _) => SOME (Prim p)
ziv@2235 340 | Sql.Inj (ERel n, _) => SOME (rel n)
ziv@2235 341 (* We can't deal with anything else, e.g., CURRENT_TIMESTAMP
ziv@2235 342 becomes Sql.Unmodeled, which becomes NONE here. *)
ziv@2235 343 | _ => NONE
ziv@2235 344 in
ziv@2235 345 (cmp, qa e1, qa e2)
ziv@2235 346 end
ziv@2235 347
ziv@2235 348 fun negateCmp (cmp, e1, e2) =
ziv@2235 349 (case cmp of
ziv@2235 350 Sql.Eq => Sql.Ne
ziv@2235 351 | Sql.Ne => Sql.Eq
ziv@2235 352 | Sql.Lt => Sql.Ge
ziv@2235 353 | Sql.Le => Sql.Gt
ziv@2235 354 | Sql.Gt => Sql.Le
ziv@2235 355 | Sql.Ge => Sql.Lt,
ziv@2235 356 e1, e2)
ziv@2235 357
ziv@2235 358 val markQuery : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
ziv@2235 359 (Sql.cmp * atomExp option * atomExp option) formula =
ziv@2235 360 mapFormula (toAtomExps QueryArg)
ziv@2235 361
ziv@2235 362 val markDml : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
ziv@2235 363 (Sql.cmp * atomExp option * atomExp option) formula =
ziv@2235 364 mapFormula (toAtomExps DmlRel)
ziv@2235 365 (* No eqs should have key conflicts because no variable is in two
ziv@2235 366 equivalence classes, so the [#1] could be [#2]. *)
ziv@2235 367
ziv@2235 368 val mergeEqs : (atomExp IntBinaryMap.map option list
ziv@2235 369 -> atomExp IntBinaryMap.map option) =
ziv@2235 370 List.foldr (fn (SOME eqs, SOME acc) => SOME (IM.unionWith #1 (eqs, acc)) | _ => NONE)
ziv@2235 371 (SOME IM.empty)
ziv@2235 372
ziv@2239 373 val simplify =
ziv@2239 374 map TS.listItems
ziv@2239 375 o removeRedundant (fn (x, y) => TS.isSubset (y, x))
ziv@2239 376 o map (fn xs => TS.addList (TS.empty, xs))
ziv@2239 377
ziv@2235 378 fun dnf (fQuery, fDml) =
ziv@2239 379 normalize simplify negateCmp Disj (Combo (Conj, [markQuery fQuery, markDml fDml]))
ziv@2235 380
ziv@2235 381 val conflictMaps = List.mapPartial (mergeEqs o map eqsOfClass o equivClasses) o dnf
ziv@2235 382
ziv@2235 383 end
ziv@2235 384
ziv@2235 385 val conflictMaps = ConflictMaps.conflictMaps
ziv@2213 386
ziv@2216 387 val rec sqexpToFormula =
ziv@2234 388 fn Sql.SqTrue => Combo (Conj, [])
ziv@2234 389 | Sql.SqFalse => Combo (Disj, [])
ziv@2216 390 | Sql.SqNot e => Negate (sqexpToFormula e)
ziv@2216 391 | Sql.Binop (Sql.RCmp c, e1, e2) => Atom (c, e1, e2)
ziv@2234 392 | Sql.Binop (Sql.RLop l, p1, p2) => Combo (case l of Sql.And => Conj | Sql.Or => Disj,
ziv@2216 393 [sqexpToFormula p1, sqexpToFormula p2])
ziv@2216 394 (* ASK: any other sqexps that can be props? *)
ziv@2216 395 | _ => raise Match
ziv@2213 396
ziv@2218 397 fun renameTables tablePairs =
ziv@2216 398 let
ziv@2216 399 fun renameString table =
ziv@2216 400 case List.find (fn (_, t) => table = t) tablePairs of
ziv@2216 401 NONE => table
ziv@2216 402 | SOME (realTable, _) => realTable
ziv@2216 403 val renameSqexp =
ziv@2216 404 fn Sql.Field (table, field) => Sql.Field (renameString table, field)
ziv@2216 405 | e => e
ziv@2218 406 fun renameAtom (cmp, e1, e2) = (cmp, renameSqexp e1, renameSqexp e2)
ziv@2216 407 in
ziv@2218 408 mapFormula renameAtom
ziv@2216 409 end
ziv@2218 410
ziv@2218 411 val rec queryToFormula =
ziv@2234 412 fn Sql.Query1 {Where = NONE, ...} => Combo (Conj, [])
ziv@2218 413 | Sql.Query1 {From = tablePairs, Where = SOME e, ...} =>
ziv@2218 414 renameTables tablePairs (sqexpToFormula e)
ziv@2234 415 | Sql.Union (q1, q2) => Combo (Disj, [queryToFormula q1, queryToFormula q2])
ziv@2216 416
ziv@2218 417 fun valsToFormula (table, vals) =
ziv@2234 418 Combo (Conj, map (fn (field, v) => Atom (Sql.Eq, Sql.Field (table, field), v)) vals)
ziv@2218 419
ziv@2216 420 val rec dmlToFormula =
ziv@2221 421 fn Sql.Insert (table, vals) => valsToFormula (table, vals)
ziv@2218 422 | Sql.Delete (table, wher) => renameTables [(table, "T")] (sqexpToFormula wher)
ziv@2218 423 | Sql.Update (table, vals, wher) =>
ziv@2218 424 let
ziv@2221 425 val fWhere = sqexpToFormula wher
ziv@2221 426 val fVals = valsToFormula (table, vals)
ziv@2237 427 val modifiedFields = SS.addList (SS.empty, map #1 vals)
ziv@2221 428 (* TODO: don't use field name hack. *)
ziv@2221 429 val markField =
ziv@2237 430 fn e as Sql.Field (t, v) => if SS.member (modifiedFields, v)
ziv@2237 431 then Sql.Field (t, v ^ "'")
ziv@2237 432 else e
ziv@2221 433 | e => e
ziv@2221 434 val mark = mapFormula (fn (cmp, e1, e2) => (cmp, markField e1, markField e2))
ziv@2218 435 in
ziv@2218 436 renameTables [(table, "T")]
ziv@2234 437 (Combo (Disj, [Combo (Conj, [fVals, mark fWhere]),
ziv@2234 438 Combo (Conj, [mark fVals, fWhere])]))
ziv@2218 439 end
ziv@2213 440
ziv@2213 441 val rec tablesQuery =
ziv@2216 442 fn Sql.Query1 {From = tablePairs, ...} => SS.fromList (map #1 tablePairs)
ziv@2216 443 | Sql.Union (q1, q2) => SS.union (tablesQuery q1, tablesQuery q2)
ziv@2213 444
ziv@2213 445 val tableDml =
ziv@2216 446 fn Sql.Insert (tab, _) => tab
ziv@2216 447 | Sql.Delete (tab, _) => tab
ziv@2216 448 | Sql.Update (tab, _, _) => tab
ziv@2213 449
ziv@2213 450
ziv@2213 451 (* Program instrumentation. *)
ziv@2213 452
ziv@2234 453 val varName =
ziv@2234 454 let
ziv@2234 455 val varNumber = ref 0
ziv@2234 456 in
ziv@2234 457 fn s => (varNumber := !varNumber + 1; s ^ Int.toString (!varNumber))
ziv@2234 458 end
ziv@2234 459
ziv@2233 460 val {check, store, flush, ...} = getCache ()
ziv@2233 461
ziv@2230 462 val dummyLoc = ErrorMsg.dummySpan
ziv@2216 463
ziv@2230 464 fun stringExp s = (EPrim (Prim.String (Prim.Normal, s)), dummyLoc)
ziv@2230 465
ziv@2230 466 val stringTyp = (TFfi ("Basis", "string"), dummyLoc)
ziv@2213 467
ziv@2213 468 val sequence =
ziv@2213 469 fn (exp :: exps) =>
ziv@2213 470 let
ziv@2230 471 val loc = dummyLoc
ziv@2213 472 in
ziv@2213 473 List.foldl (fn (e', seq) => ESeq ((seq, loc), (e', loc))) exp exps
ziv@2213 474 end
ziv@2213 475 | _ => raise Match
ziv@2213 476
ziv@2215 477 (* Always increments negative indices because that's what we need later. *)
ziv@2215 478 fun incRelsBound bound inc =
ziv@2215 479 MonoUtil.Exp.mapB
ziv@2215 480 {typ = fn x => x,
ziv@2215 481 exp = fn level =>
ziv@2215 482 (fn ERel n => ERel (if n >= level orelse n < 0 then n + inc else n)
ziv@2215 483 | x => x),
ziv@2215 484 bind = fn (level, MonoUtil.Exp.RelE _) => level + 1 | (level, _) => level}
ziv@2215 485 bound
ziv@2215 486
ziv@2215 487 val incRels = incRelsBound 0
ziv@2213 488
ziv@2223 489 fun cacheWrap (query, i, urlifiedRel0, resultTyp, args) =
ziv@2213 490 let
ziv@2223 491 val () = ffiInfo := {index = i, params = length args} :: !ffiInfo
ziv@2230 492 val loc = dummyLoc
ziv@2223 493 (* We ensure before this step that all arguments aren't effectful.
ziv@2227 494 by turning them into local variables as needed. *)
ziv@2230 495 val argsInc = map (incRels 1) args
ziv@2233 496 val check = (check (i, args), dummyLoc)
ziv@2233 497 val store = (store (i, argsInc, urlifiedRel0), dummyLoc)
ziv@2223 498 val rel0 = (ERel 0, loc)
ziv@2213 499 in
ziv@2223 500 ECase (check,
ziv@2223 501 [((PNone stringTyp, loc),
ziv@2234 502 (ELet (varName "q", resultTyp, query, (ESeq (store, rel0), loc)), loc)),
ziv@2234 503 ((PSome (stringTyp, (PVar (varName "hit", stringTyp), loc)), loc),
ziv@2223 504 (* Boolean is false because we're not unurlifying from a cookie. *)
ziv@2223 505 (EUnurlify (rel0, resultTyp, false), loc))],
ziv@2223 506 {disc = stringTyp, result = resultTyp})
ziv@2213 507 end
ziv@2213 508
ziv@2213 509 fun fileMapfold doExp file start =
ziv@2213 510 case MonoUtil.File.mapfold {typ = Search.return2,
ziv@2213 511 exp = fn x => (fn s => Search.Continue (doExp x s)),
ziv@2213 512 decl = Search.return2} file start of
ziv@2213 513 Search.Continue x => x
ziv@2213 514 | Search.Return _ => raise Match
ziv@2213 515
ziv@2213 516 fun fileMap doExp file = #1 (fileMapfold (fn x => fn _ => (doExp x, ())) file ())
ziv@2213 517
ziv@2221 518 fun factorOutNontrivial text =
ziv@2221 519 let
ziv@2230 520 val loc = dummyLoc
ziv@2221 521 fun strcat (e1, e2) = (EStrcat (e1, e2), loc)
ziv@2221 522 val chunks = Sql.chunkify text
ziv@2221 523 val (newText, newVariables) =
ziv@2221 524 (* Important that this is foldr (to oppose foldl below). *)
ziv@2221 525 List.foldr
ziv@2221 526 (fn (chunk, (qText, newVars)) =>
ziv@2221 527 (* Variable bound to the head of newBs will have the lowest index. *)
ziv@2221 528 case chunk of
ziv@2221 529 Sql.Exp (e as (EPrim _, _)) => (strcat (e, qText), newVars)
ziv@2221 530 | Sql.Exp e =>
ziv@2221 531 let
ziv@2221 532 val n = length newVars
ziv@2221 533 in
ziv@2221 534 (* This is the (n + 1)th new variable, so there are
ziv@2221 535 already n new variables bound, so we increment
ziv@2221 536 indices by n. *)
ziv@2221 537 (strcat ((ERel (~(n+1)), loc), qText), incRels n e :: newVars)
ziv@2221 538 end
ziv@2221 539 | Sql.String s => (strcat (stringExp s, qText), newVars))
ziv@2221 540 (stringExp "", [])
ziv@2221 541 chunks
ziv@2221 542 fun wrapLets e' =
ziv@2221 543 (* Important that this is foldl (to oppose foldr above). *)
ziv@2234 544 List.foldl (fn (v, e') => ELet (varName "sqlArg", stringTyp, v, (e', loc)))
ziv@2221 545 e'
ziv@2221 546 newVariables
ziv@2221 547 val numArgs = length newVariables
ziv@2221 548 in
ziv@2221 549 (newText, wrapLets, numArgs)
ziv@2221 550 end
ziv@2221 551
ziv@2215 552 fun addChecking file =
ziv@2213 553 let
ziv@2223 554 fun doExp (queryInfo as (tableToIndices, indexToQueryNumArgs, index)) =
ziv@2223 555 fn e' as EQuery {query = origQueryText,
ziv@2223 556 sqlcacheInfo = urlifiedRel0,
ziv@2223 557 state = resultTyp,
ziv@2223 558 initial, body, tables, exps} =>
ziv@2213 559 let
ziv@2221 560 val (newQueryText, wrapLets, numArgs) = factorOutNontrivial origQueryText
ziv@2215 561 (* Increment once for each new variable just made. *)
ziv@2221 562 val queryExp = incRels numArgs
ziv@2215 563 (EQuery {query = newQueryText,
ziv@2223 564 sqlcacheInfo = urlifiedRel0,
ziv@2223 565 state = resultTyp,
ziv@2215 566 initial = initial,
ziv@2215 567 body = body,
ziv@2215 568 tables = tables,
ziv@2223 569 exps = exps},
ziv@2230 570 dummyLoc)
ziv@2215 571 val (EQuery {query = queryText, ...}, _) = queryExp
ziv@2235 572 (* DEBUG *)
ziv@2221 573 val () = Print.preface ("sqlcache> ", (MonoPrint.p_exp MonoEnv.empty queryText))
ziv@2230 574 val args = List.tabulate (numArgs, fn n => (ERel n, dummyLoc))
ziv@2213 575 fun bind x f = Option.mapPartial f x
ziv@2215 576 fun guard b x = if b then x else NONE
ziv@2215 577 (* DEBUG: set first boolean argument to true to turn on printing. *)
ziv@2215 578 fun safe bound = not o effectful true (effectfulMap file) false bound
ziv@2213 579 val attempt =
ziv@2213 580 (* Ziv misses Haskell's do notation.... *)
ziv@2215 581 guard (safe 0 queryText andalso safe 0 initial andalso safe 2 body) (
ziv@2216 582 bind (Sql.parse Sql.query queryText) (fn queryParsed =>
ziv@2223 583 SOME (wrapLets (cacheWrap (queryExp, index, urlifiedRel0, resultTyp, args)),
ziv@2218 584 (SS.foldr (fn (tab, qi) => SIMM.insert (qi, tab, index))
ziv@2218 585 tableToIndices
ziv@2218 586 (tablesQuery queryParsed),
ziv@2223 587 IM.insert (indexToQueryNumArgs, index, (queryParsed, numArgs)),
ziv@2223 588 index + 1))))
ziv@2213 589 in
ziv@2213 590 case attempt of
ziv@2213 591 SOME pair => pair
ziv@2213 592 | NONE => (e', queryInfo)
ziv@2213 593 end
ziv@2213 594 | e' => (e', queryInfo)
ziv@2213 595 in
ziv@2223 596 fileMapfold (fn exp => fn state => doExp state exp) file (SIMM.empty, IM.empty, 0)
ziv@2213 597 end
ziv@2213 598
ziv@2235 599 structure Invalidations = struct
ziv@2235 600
ziv@2235 601 val loc = dummyLoc
ziv@2235 602
ziv@2235 603 val optionAtomExpToExp =
ziv@2235 604 fn NONE => (ENone stringTyp, loc)
ziv@2235 605 | SOME e => (ESome (stringTyp,
ziv@2235 606 (case e of
ziv@2235 607 DmlRel n => ERel n
ziv@2235 608 | Prim p => EPrim p
ziv@2235 609 (* TODO: make new type containing only these two. *)
ziv@2235 610 | _ => raise Match,
ziv@2235 611 loc)),
ziv@2235 612 loc)
ziv@2235 613
ziv@2235 614 fun eqsToInvalidation numArgs eqs =
ziv@2235 615 let
ziv@2235 616 fun inv n = if n < 0 then [] else IM.find (eqs, n) :: inv (n - 1)
ziv@2235 617 in
ziv@2235 618 inv (numArgs - 1)
ziv@2235 619 end
ziv@2235 620
ziv@2235 621 (* Tests if [ys] makes [xs] a redundant cache invalidation. [NONE] here
ziv@2235 622 represents unknown, which means a wider invalidation. *)
ziv@2235 623 val rec madeRedundantBy : atomExp option list * atomExp option list -> bool =
ziv@2235 624 fn ([], []) => true
ziv@2237 625 | (_ :: xs, NONE :: ys) => madeRedundantBy (xs, ys)
ziv@2235 626 | (SOME x :: xs, SOME y :: ys) => (case AtomExpKey.compare (x, y) of
ziv@2235 627 EQUAL => madeRedundantBy (xs, ys)
ziv@2235 628 | _ => false)
ziv@2235 629 | _ => false
ziv@2235 630
ziv@2235 631 fun eqss (query, dml) = conflictMaps (queryToFormula query, dmlToFormula dml)
ziv@2235 632
ziv@2235 633 fun invalidations ((query, numArgs), dml) =
ziv@2235 634 (map (map optionAtomExpToExp)
ziv@2237 635 o removeRedundant madeRedundantBy
ziv@2235 636 o map (eqsToInvalidation numArgs)
ziv@2235 637 o eqss)
ziv@2235 638 (query, dml)
ziv@2235 639
ziv@2235 640 end
ziv@2235 641
ziv@2235 642 val invalidations = Invalidations.invalidations
ziv@2235 643
ziv@2235 644 (* DEBUG *)
ziv@2235 645 val gunk : ((Sql.query * int) * Sql.dml) list ref = ref []
ziv@2216 646
ziv@2223 647 fun addFlushing (file, (tableToIndices, indexToQueryNumArgs, _)) =
ziv@2213 648 let
ziv@2221 649 val flushes = List.concat o
ziv@2233 650 map (fn (i, argss) => map (fn args => flush (i, args)) argss)
ziv@2213 651 val doExp =
ziv@2221 652 fn EDml (origDmlText, failureMode) =>
ziv@2213 653 let
ziv@2221 654 val (newDmlText, wrapLets, numArgs) = factorOutNontrivial origDmlText
ziv@2221 655 val dmlText = incRels numArgs newDmlText
ziv@2221 656 val dmlExp = EDml (dmlText, failureMode)
ziv@2235 657 (* DEBUG *)
ziv@2221 658 val () = Print.preface ("sqlcache> ", (MonoPrint.p_exp MonoEnv.empty dmlText))
ziv@2221 659 val invs =
ziv@2216 660 case Sql.parse Sql.dml dmlText of
ziv@2218 661 SOME dmlParsed =>
ziv@2221 662 map (fn i => (case IM.find (indexToQueryNumArgs, i) of
ziv@2221 663 SOME queryNumArgs =>
ziv@2235 664 (* DEBUG *)
ziv@2235 665 (gunk := (queryNumArgs, dmlParsed) :: !gunk;
ziv@2235 666 (i, invalidations (queryNumArgs, dmlParsed)))
ziv@2221 667 (* TODO: fail more gracefully. *)
ziv@2221 668 | NONE => raise Match))
ziv@2221 669 (SIMM.findList (tableToIndices, tableDml dmlParsed))
ziv@2221 670 (* TODO: fail more gracefully. *)
ziv@2221 671 | NONE => raise Match
ziv@2213 672 in
ziv@2221 673 wrapLets (sequence (flushes invs @ [dmlExp]))
ziv@2213 674 end
ziv@2213 675 | e' => e'
ziv@2213 676 in
ziv@2235 677 (* DEBUG *)
ziv@2235 678 gunk := [];
ziv@2213 679 fileMap doExp file
ziv@2213 680 end
ziv@2213 681
ziv@2221 682 val inlineSql =
ziv@2221 683 let
ziv@2221 684 val doExp =
ziv@2221 685 (* TODO: EQuery, too? *)
ziv@2221 686 (* ASK: should this live in [MonoOpt]? *)
ziv@2221 687 fn EDml ((ECase (disc, cases, {disc = dTyp, ...}), loc), failureMode) =>
ziv@2221 688 let
ziv@2221 689 val newCases = map (fn (p, e) => (p, (EDml (e, failureMode), loc))) cases
ziv@2221 690 in
ziv@2221 691 ECase (disc, newCases, {disc = dTyp, result = (TRecord [], loc)})
ziv@2221 692 end
ziv@2221 693 | e => e
ziv@2221 694 in
ziv@2221 695 fileMap doExp
ziv@2221 696 end
ziv@2221 697
ziv@2213 698 fun go file =
ziv@2213 699 let
ziv@2235 700 (* TODO: do something nicer than [Sql] being in one of two modes. *)
ziv@2227 701 val () = (resetFfiInfo (); Sql.sqlcacheMode := true)
ziv@2221 702 val file' = addFlushing (addChecking (inlineSql file))
ziv@2215 703 val () = Sql.sqlcacheMode := false
ziv@2213 704 in
ziv@2221 705 file'
ziv@2213 706 end
ziv@2213 707
ziv@2209 708 end