From 33dfd07b96aabed6b928d226d0be3237da3b5e0f Mon Sep 17 00:00:00 2001 From: ygrek Date: Tue, 17 Apr 2018 17:16:32 +0300 Subject: [PATCH] sql: improve single-row detection (fix #47) --- lib/syntax.ml | 17 +++++++++++------ test/misc.sql | 3 +++ test/out/function.xml | 2 +- test/out/misc.xml | 10 ++++++++-- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/syntax.ml b/lib/syntax.ml index fa9230f..12ceb39 100644 --- a/lib/syntax.ml +++ b/lib/syntax.ml @@ -38,13 +38,18 @@ let get_params_q e = in loop [] e |> List.rev +let rec is_singular = function +| Value _ +| Param _ -> true +| Column _ -> false +| Fun (func,args) -> + (* grouping function of zero or single parameter or function of all singular values *) + (Type.is_grouping func && List.length args <= 1) || List.for_all is_singular args +| Select _ -> false (* ? *) +| Inserted _ -> false (* ? *) + let test_all_grouping columns = - let test = function - (* grouping function of zero or single parameter *) - | Expr (Fun (func,args),_) when Type.is_grouping func && List.length args <= 1 -> true - | _ -> false - in - List.for_all test columns + List.for_all (function Expr (e,_) -> is_singular e | All | AllOf _ -> false) columns let cross = List.fold_left Schema.cross [] diff --git a/test/misc.sql b/test/misc.sql index eec33a7..79576ab 100644 --- a/test/misc.sql +++ b/test/misc.sql @@ -46,3 +46,6 @@ INSERT INTO test VALUES (1, 'one'), (2, 'two'), (3, 'three'); + +-- @issue47 +SELECT count(*) > 0 FROM test; diff --git a/test/out/function.xml b/test/out/function.xml index bdabad2..f0e135a 100644 --- a/test/out/function.xml +++ b/test/out/function.xml @@ -5,7 +5,7 @@ - + diff --git a/test/out/misc.xml b/test/out/misc.xml index 63f9771..a184853 100644 --- a/test/out/misc.xml +++ b/test/out/misc.xml @@ -1,7 +1,7 @@ - + @@ -108,7 +108,7 @@ - + @@ -122,4 +122,10 @@ + + + + + + -- 2.11.4.GIT