comparison src/monoize.sml @ 1357:a0f0823a54a0

Allow SQL aggregation over nullable types
author Adam Chlipala <adam@chlipala.net>
date Tue, 21 Dec 2010 17:01:51 -0500
parents b4480a56cab7
children 32c8a3509369
comparison
equal deleted inserted replaced
1356:977901cb52cc 1357:a0f0823a54a0
2635 t) => 2635 t) =>
2636 let 2636 let
2637 val default = 2637 val default =
2638 case #1 t of 2638 case #1 t of
2639 L.CFfi ("Basis", s) => 2639 L.CFfi ("Basis", s) =>
2640 (case s of 2640 SOME (case s of
2641 "int" => "0" 2641 "int" => "0"
2642 | "float" => "0.0" 2642 | "float" => "0.0"
2643 | "string" => "''" 2643 | "string" => "''"
2644 | "time" => "0" 2644 | "time" => "0"
2645 | _ => raise Fail "Illegal type of sql_aggregate [1]") 2645 | _ => raise Fail "Illegal type of sql_aggregate [1]")
2646 | L.CApp ((L.CFfi ("Basis", "option"), _), _) => NONE
2646 | _ => raise Fail "Illegal type of sql_aggregate [2]" 2647 | _ => raise Fail "Illegal type of sql_aggregate [2]"
2647 2648
2648 val s = (L'.TFfi ("Basis", "string"), loc) 2649 val s = (L'.TFfi ("Basis", "string"), loc)
2649 fun sc s = (L'.EPrim (Prim.String s), loc) 2650 fun sc s = (L'.EPrim (Prim.String s), loc)
2651
2652 val main = strcat [(L'.ERel 1, loc),
2653 sc "(",
2654 (L'.ERel 0, loc),
2655 sc ")"]
2656
2657 val main = case default of
2658 NONE => main
2659 | SOME default =>
2660 strcat [sc "COALESCE(",
2661 main,
2662 sc ("," ^ default ^ ")")]
2650 in 2663 in
2651 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), 2664 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
2652 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), 2665 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), main), loc)), loc),
2653 strcat [sc "COALESCE(",
2654 (L'.ERel 1, loc),
2655 sc "(",
2656 (L'.ERel 0, loc),
2657 sc (")," ^ default ^ ")")]), loc)), loc),
2658 fm) 2666 fm)
2659 end 2667 end
2660 2668
2661 | L.ECApp ((L.EFfi ("Basis", "sql_count_col"), _), _) => 2669 | L.ECApp ((L.EFfi ("Basis", "sql_count_col"), _), _) =>
2662 ((L'.EPrim (Prim.String "COUNT"), loc), 2670 ((L'.EPrim (Prim.String "COUNT"), loc),
2663 fm) 2671 fm)
2664 2672
2665 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm) 2673 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
2666 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm) 2674 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
2675 | L.ECApp ((L.EFfi ("Basis", "sql_summable_option"), _), _) =>
2676 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
2677 (L'.ERecord [], loc)), loc),
2678 fm)
2667 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) => 2679 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
2668 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), 2680 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
2669 (L'.EPrim (Prim.String "AVG"), loc)), loc), 2681 (L'.EPrim (Prim.String "AVG"), loc)), loc),
2670 fm) 2682 fm)
2671 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) => 2683 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
2677 | L.EFfi ("Basis", "sql_arith_float") => ((L'.ERecord [], loc), fm) 2689 | L.EFfi ("Basis", "sql_arith_float") => ((L'.ERecord [], loc), fm)
2678 2690
2679 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm) 2691 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
2680 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm) 2692 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
2681 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm) 2693 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
2694 | L.ECApp ((L.EFfi ("Basis", "sql_maxable_option"), _), _) =>
2695 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
2696 (L'.ERecord [], loc)), loc),
2697 fm)
2682 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) => 2698 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
2683 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), 2699 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
2684 (L'.EPrim (Prim.String "MAX"), loc)), loc), 2700 (L'.EPrim (Prim.String "MAX"), loc)), loc),
2685 fm) 2701 fm)
2686 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) => 2702 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>