Skip to content

Commit

Permalink
Merge pull request #51 from hiroshi-cl/fix/dl2u_tmp_error
Browse files Browse the repository at this point in the history
#46 2-1の修正: ast2sqlでソースのカラム名を伝搬させず述語のカラム名をそのまま使うようにした
  • Loading branch information
hiroshi-cl authored Oct 31, 2024
2 parents ec0e384 + 4b7cee9 commit a67ac06
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 45 deletions.
5 changes: 5 additions & 0 deletions examples/dl2u_error.dl
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions examples/dl2u_name.dl
Original file line number Diff line number Diff line change
@@ -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).
39 changes: 2 additions & 37 deletions src/ast2sql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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

Expand Down
16 changes: 8 additions & 8 deletions test/ast2sql_operation_based_conversion_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;";
Expand Down

0 comments on commit a67ac06

Please sign in to comment.