From 524f16241af0a22661cf6ccf8e8641d437d0930d Mon Sep 17 00:00:00 2001 From: Hiroshi Date: Wed, 9 Oct 2024 00:11:20 +0900 Subject: [PATCH 1/3] =?UTF-8?q?ast2sql=E3=81=A7=E3=82=BD=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E3=81=AE=E3=82=AB=E3=83=A9=E3=83=A0=E5=90=8D=E3=82=92=E4=BC=9D?= =?UTF-8?q?=E6=90=AC=E3=81=95=E3=81=9B=E3=81=9A=E8=BF=B0=E8=AA=9E=E3=81=AE?= =?UTF-8?q?=E3=82=AB=E3=83=A9=E3=83=A0=E5=90=8D=E3=82=92=E3=81=9D=E3=81=AE?= =?UTF-8?q?=E3=81=BE=E3=81=BE=E4=BD=BF=E3=81=86=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E3=81=97=E3=81=9F=20#46?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/dl2u_error.dl | 5 +++ examples/dl2u_name.dl | 4 +++ src/ast2sql.ml | 36 ++----------------- ...ast2sql_operation_based_conversion_test.ml | 16 ++++----- 4 files changed, 19 insertions(+), 42 deletions(-) create mode 100644 examples/dl2u_error.dl create mode 100644 examples/dl2u_name.dl 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 22fb365..368abfa 100644 --- a/src/ast2sql.ml +++ b/src/ast2sql.ml @@ -2323,42 +2323,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 colmns arg -> match arg with - | NamedVar x -> return (x :: colmns, arg_map |> ArgMap.add x None) + | NamedVar x -> return (x :: colmns) | _ -> 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;"; From b57832eba9f05cedc1cc1385783df35fff6bc528 Mon Sep 17 00:00:00 2001 From: Hiroshi Date: Sun, 27 Oct 2024 22:44:56 +0900 Subject: [PATCH 2/3] =?UTF-8?q?ArgMap=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ast2sql.ml | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ast2sql.ml b/src/ast2sql.ml index 4643b2a..167d29d 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 = From 4b7cee940b7cc2494e57a982fde83b1a281a4f5e Mon Sep 17 00:00:00 2001 From: Hiroshi Date: Sun, 27 Oct 2024 22:45:43 +0900 Subject: [PATCH 3/3] =?UTF-8?q?typo=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ast2sql.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ast2sql.ml b/src/ast2sql.ml index 167d29d..e6b387d 100644 --- a/src/ast2sql.ml +++ b/src/ast2sql.ml @@ -2320,9 +2320,9 @@ 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 -> + args |> List.rev |> foldM (fun columns arg -> match arg with - | NamedVar x -> return (x :: colmns) + | NamedVar x -> return (x :: columns) | _ -> err @@ InvalidArgInHead { var = arg; error_detail = InRule (head, body) } ) [] >>= fun columns -> return @@ TableEnv.add table columns table_env