diff options
author | Sébastien Dailly <sebastien@dailly.me> | 2025-03-18 21:49:55 +0100 |
---|---|---|
committer | Sébastien Dailly <sebastien@dailly.me> | 2025-03-18 21:49:55 +0100 |
commit | 0bdc640331b903532fb345930e7078752ba54a2d (patch) | |
tree | 0fecadb244647ec4cb29982cfea4237f51487cad | |
parent | 8b8b730d3ba98d6c9e4e6274844641043b5fefbb (diff) |
Evaluate each predicate in a () blockmain
-rw-r--r-- | lib/analysers/filters.ml | 20 | ||||
-rw-r--r-- | tests/analyser_filters.ml | 8 | ||||
-rw-r--r-- | tests/analyser_query_test.ml | 2 |
3 files changed, 19 insertions, 11 deletions
diff --git a/lib/analysers/filters.ml b/lib/analysers/filters.ml index 7044798..6b1d843 100644 --- a/lib/analysers/filters.ml +++ b/lib/analysers/filters.ml @@ -5,15 +5,23 @@ module Expression = ImportExpression open StdLabels (** Add a list of expressions into the group *) -let rec add_filters : +let add_filters : conf:ImporterSyntax.t -> Chunk.t -> Path.t Expression.T.t list -> unit = fun ~conf group -> function | [] -> () - | hd :: [] -> Chunk.add_expression ~conf group hd - | hd :: tl -> - Chunk.add_expression ~conf group hd; - Chunk.add_string group "\nAND "; - add_filters ~conf group tl + | any -> + let rec f ~conf group = function + | [] -> () + | hd :: [] -> + Chunk.add_expression ~conf group hd; + Chunk.add_string group ")" + | hd :: tl -> + Chunk.add_expression ~conf group hd; + Chunk.add_string group ")\nAND ("; + f ~conf group tl + in + Chunk.add_string group "("; + f ~conf group any type 'a cte_acc = { n : int; diff --git a/tests/analyser_filters.ml b/tests/analyser_filters.ml index 864cab7..ef51e0c 100644 --- a/tests/analyser_filters.ml +++ b/tests/analyser_filters.ml @@ -35,7 +35,7 @@ let simple_filter () = let chunk_predicates = Filters.generate_sql ~conf filter chunk_links in let expected_predicates = Chunk.create () in - Chunk.add_string expected_predicates " WHERE 1=COALESCE('source'.'col_1',0)"; + Chunk.add_string expected_predicates " WHERE (1=COALESCE('source'.'col_1',0))"; Alcotest.(check @@ pair Test_migration.chunk Test_migration.chunk) "Simple predicate" @@ -58,7 +58,7 @@ let multiple_filters () = (* The predicates can be executed in reverse order, but it’s not an issue because they all are applied at the same time in the projection *) Chunk.add_string expected_predicates - " WHERE COALESCE('source'.'col_1','')=?\nAND 1"; + " WHERE (COALESCE('source'.'col_1','')=?)\nAND (1)"; Alcotest.(check @@ pair Test_migration.chunk Test_migration.chunk) "Combined predicate" @@ -109,7 +109,7 @@ let expression_with_group () = "WITH filter0 AS (SELECT source.id, LAST_VALUE('source'.'col_1') OVER \ (PARTITION BY 'source'.'col_1' ORDER BY 'source'.'col_1' RANGE BETWEEN \ UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)=1 AS group_function\n\ - FROM 'source' AS 'source' WHERE 1=COALESCE('source'.'col_1',0))\n"; + FROM 'source' AS 'source' WHERE (1=COALESCE('source'.'col_1',0)))\n"; Alcotest.(check @@ pair Test_migration.chunk Test_migration.chunk) "The predicate expression is inside of the CTE" @@ -130,7 +130,7 @@ let group_with_expression () = Chunk.add_string expected_predicates "\n\ INNER JOIN 'filter0' ON filter0.id = source.id\n\ - WHERE 1=COALESCE('source'.'col_1',0) AND filter0.group_function"; + WHERE (1=COALESCE('source'.'col_1',0)) AND filter0.group_function"; let expected_links = Chunk.create () in Chunk.add_string expected_links diff --git a/tests/analyser_query_test.ml b/tests/analyser_query_test.ml index fd8914b..ed89623 100644 --- a/tests/analyser_query_test.ml +++ b/tests/analyser_query_test.ml @@ -248,7 +248,7 @@ FROM 'source' AS 'source' LEFT JOIN 'other' AS 'other' ON rtrim(upper('source'.'col_1')) = 'other'.'key_other' LEFT JOIN 'last' AS 'last_file' ON rtrim(upper('other'.'col_1')) = 'last_file'.'key_last_file' INNER JOIN 'filter0' ON filter0.id = source.id -WHERE COALESCE('source'.'col_3',0)=0 AND filter0.group_function|} +WHERE (COALESCE('source'.'col_3',0)=0) AND filter0.group_function|} in Alcotest.check Alcotest.string "" expected contents.q |