diff --git a/examples/dl2u_error.dl b/examples/dl2u_error.dl new file mode 100644 index 0000000..610cf23 --- /dev/null +++ b/examples/dl2u_error.dl @@ -0,0 +1,5 @@ +source a('A':int, 'B':int). +source b('B':int, 'C':int). +view v('A':int, 'B':int, 'C':int). +-a(A, B) :- a(A, B) , tmp(A, B, C). +tmp(A, B, C) :- a(A, G) , b(G, C) , B = 60 , G = 30. diff --git a/examples/dl2u_name.dl b/examples/dl2u_name.dl new file mode 100644 index 0000000..659ee62 --- /dev/null +++ b/examples/dl2u_name.dl @@ -0,0 +1,4 @@ +source a('A':int, 'B':int). +view v('A':int, 'B':int, 'C':int). +-a(A, B) :- f(B, A). +f(C, D) :- a(D, C). diff --git a/src/ast2sql.ml b/src/ast2sql.ml index d14ff23..e6b387d 100644 --- a/src/ast2sql.ml +++ b/src/ast2sql.ml @@ -2309,9 +2309,6 @@ type grouping_state = | NoneState -module ArgMap = Map.Make(String) - - let update_table_env (head : rterm) (body : term list) (table_env : table_environment) : (table_environment, error) result = let open ResultMonad in let table, args = @@ -2323,42 +2320,10 @@ let update_table_env (head : rterm) (body : term list) (table_env : table_enviro if TableEnv.mem table table_env then return table_env else - args |> List.rev |> foldM (fun (colmns, arg_map) arg -> + args |> List.rev |> foldM (fun columns arg -> match arg with - | NamedVar x -> return (x :: colmns, arg_map |> ArgMap.add x None) + | NamedVar x -> return (x :: columns) | _ -> err @@ InvalidArgInHead { var = arg; error_detail = InRule (head, body) } - ) ([], ArgMap.empty) >>= fun (colmns, arg_map) -> - body |> foldM (fun arg_map term -> - match term with - | Rel (Pred (target, args)) - | Rel (Deltainsert (target, args)) - | Rel (Deltadelete (target, args)) -> - begin match TableEnv.find_opt target table_env with - | None -> - err @@ UnknownTable { table = target; error_detail = InRule (head, body) } - | Some cols -> - List.combine cols args |> foldM (fun arg_map (col, arg) -> - match arg with - | NamedVar x -> - begin match arg_map |> ArgMap.find_opt x with - | None -> err @@ InvalidArgInBody { var = arg; error_detail = InRule (head, body) } - | Some None -> return (arg_map |> ArgMap.add x (Some col)) - | Some (Some col0) when col = col0 -> return arg_map - | Some (Some _) -> err @@ InvalidArgInBody { var = arg; error_detail = InRule (head, body) } - end - - | _ -> - err @@ InvalidArgInBody { var = arg; error_detail = InRule (head, body) } - ) arg_map - end - - | _ -> return arg_map - ) arg_map >>= fun arg_map -> - colmns |> List.rev |> foldM (fun columns col -> - match arg_map |> ArgMap.find_opt col with - | None -> err @@ HeadVariableDoesNotOccurInBody col - | Some None -> err @@ HeadVariableDoesNotOccurInBody col - | Some Some col -> return (col :: columns) ) [] >>= fun columns -> return @@ TableEnv.add table columns table_env diff --git a/test/ast2sql_operation_based_conversion_test.ml b/test/ast2sql_operation_based_conversion_test.ml index 5a401b9..0ade920 100644 --- a/test/ast2sql_operation_based_conversion_test.ml +++ b/test/ast2sql_operation_based_conversion_test.ml @@ -227,14 +227,14 @@ let main () = primary_keys = []; }; expected = String.concat " " [ - "CREATE TEMPORARY TABLE v AS SELECT a_0.AA AS AA, a_0.BB AS BB, b_1.CC AS CC FROM a AS a_0, b AS b_1 WHERE b_1.BB = a_0.BB;"; - "CREATE TEMPORARY TABLE temp0 AS SELECT v_0.AA AS AA, v_0.BB AS BB, 3 AS CC FROM v AS v_0 WHERE v_0.CC = 3 AND v_0.AA <> 4;"; - "CREATE TEMPORARY TABLE temp1 AS SELECT 4 AS AA, temp0.BB AS BB, temp0.CC AS CC FROM temp0 AS temp0;"; - "CREATE TEMPORARY TABLE uv AS SELECT v_0.AA AS AA, v_0.BB AS BB, v_0.CC AS CC FROM v AS v_0 WHERE NOT EXISTS ( SELECT * FROM temp0 AS t WHERE t.AA = v_0.AA AND t.BB = v_0.BB AND t.CC = v_0.CC ) UNION SELECT temp1.AA AS AA, temp1.BB AS BB, temp1.CC AS CC FROM temp1 AS temp1;"; - "CREATE TEMPORARY TABLE temp2 AS SELECT uv_0.BB AS BB, uv_0.CC AS CC FROM uv AS uv_0 WHERE NOT EXISTS ( SELECT * FROM b AS t WHERE t.BB = uv_0.BB AND t.CC = uv_0.CC );"; - "CREATE TEMPORARY TABLE temp3 AS SELECT uv_0.AA AS AA, uv_0.BB AS BB FROM uv AS uv_0 WHERE NOT EXISTS ( SELECT * FROM a AS t WHERE t.AA = uv_0.AA AND t.BB = uv_0.BB );"; - "CREATE TEMPORARY TABLE temp4 AS SELECT b_0.BB AS BB, b_0.CC AS CC FROM b AS b_0, uv AS uv_1 WHERE uv_1.BB = b_0.BB AND NOT EXISTS ( SELECT * FROM uv AS t WHERE t.BB = b_0.BB AND t.CC = b_0.CC );"; - "CREATE TEMPORARY TABLE temp5 AS SELECT a_0.AA AS AA, a_0.BB AS BB FROM a AS a_0 WHERE NOT EXISTS ( SELECT * FROM uv AS t WHERE t.AA = a_0.AA AND t.BB = a_0.BB );"; + "CREATE TEMPORARY TABLE v AS SELECT a_0.AA AS A, a_0.BB AS B, b_1.CC AS C FROM a AS a_0, b AS b_1 WHERE b_1.BB = a_0.BB;"; + "CREATE TEMPORARY TABLE temp0 AS SELECT v_0.A AS A, v_0.B AS B, 3 AS C FROM v AS v_0 WHERE v_0.C = 3 AND v_0.A <> 4;"; + "CREATE TEMPORARY TABLE temp1 AS SELECT 4 AS A, temp0.B AS B, temp0.C AS C FROM temp0 AS temp0;"; + "CREATE TEMPORARY TABLE uv AS SELECT v_0.A AS A, v_0.B AS B, v_0.C AS C FROM v AS v_0 WHERE NOT EXISTS ( SELECT * FROM temp0 AS t WHERE t.A = v_0.A AND t.B = v_0.B AND t.C = v_0.C ) UNION SELECT temp1.A AS A, temp1.B AS B, temp1.C AS C FROM temp1 AS temp1;"; + "CREATE TEMPORARY TABLE temp2 AS SELECT uv_0.B AS BB, uv_0.C AS CC FROM uv AS uv_0 WHERE NOT EXISTS ( SELECT * FROM b AS t WHERE t.BB = uv_0.B AND t.CC = uv_0.C );"; + "CREATE TEMPORARY TABLE temp3 AS SELECT uv_0.A AS AA, uv_0.B AS BB FROM uv AS uv_0 WHERE NOT EXISTS ( SELECT * FROM a AS t WHERE t.AA = uv_0.A AND t.BB = uv_0.B );"; + "CREATE TEMPORARY TABLE temp4 AS SELECT b_0.BB AS BB, b_0.CC AS CC FROM b AS b_0, uv AS uv_1 WHERE uv_1.B = b_0.BB AND NOT EXISTS ( SELECT * FROM uv AS t WHERE t.B = b_0.BB AND t.C = b_0.CC );"; + "CREATE TEMPORARY TABLE temp5 AS SELECT a_0.AA AS AA, a_0.BB AS BB FROM a AS a_0 WHERE NOT EXISTS ( SELECT * FROM uv AS t WHERE t.A = a_0.AA AND t.B = a_0.BB );"; "INSERT INTO b SELECT * FROM temp2;"; "INSERT INTO a SELECT * FROM temp3;"; "DELETE FROM b USING temp4 WHERE b.BB = temp4.BB AND b.CC = temp4.CC;";