Commit 773dfdcf authored by Pietro Abate's avatar Pietro Abate

[r2004-07-13 22:14:52 by afrisch] Empty log message

Original author: afrisch
Date: 2004-07-13 22:14:53+00:00
parent d8958714
......@@ -199,7 +199,7 @@ ifeq ($(EXPAT), true)
endif
CQL_OBJECTS= query/query.cmo query/query_parse.cmo
CQL_OBJECTS= query/query_aggregates.cmo query/query.cmo query/query_parse.cmo
CQL_OBJECTS_RUN = query/query_run.cmo
VALIDATE_OBJECTS := $(shell for o in $(OBJECTS); do echo $$o; if [ "$$o" = "schema/schema_parser.cmo" ]; then exit 0; fi; done) # all objects until schema_parser.cmo
......
let query_min v =
let (hd,tl) = Value.get_pair v in
Value.fold_sequence
(fun accu x -> if Value.compare x accu < 0 then x else accu)
hd
tl
let query_max v =
let (hd,tl) = Value.get_pair v in
Value.fold_sequence
(fun accu x -> if Value.compare x accu > 0 then x else accu)
hd
tl
let query_sum v =
let s =
Value.fold_sequence
(fun accu x -> Intervals.V.add accu (Value.get_integer x))
Intervals.V.zero
v in
Value.Integer s
let query_avg v =
let n = ref 0 in
let s =
Value.fold_sequence
(fun accu x -> incr n; Intervals.V.add accu (Value.get_integer x))
Intervals.V.zero
v in
Value.Integer (Intervals.V.div s (Intervals.V.from_int !n))
let query_count v =
let n = Value.fold_sequence (fun accu _ -> succ accu) 0 v in
Value.ocaml2cduce_int n
let query_member v =
let (e,p) = Value.get_pair v in
Value.vbool (List.exists (Value.equal e) (Value.get_sequence p))
let query_distinct v =
let s = Value.fold_sequence (fun accu x -> Value.ValueSet.add x accu)
Value.ValueSet.empty v in
Value.sequence (Value.ValueSet.elements s)
(* todo : typage precis pour min et max et distinct_values *)
open Operators;;
open Builtin_defs;;
register_fun "min" (Sequence.plus any) any query_min;;
register_fun "max" (Sequence.plus any) any query_max;;
register_fun "sum" (Sequence.star int) int query_sum;;
register_fun "avg" (Sequence.plus int) int query_avg;;
register_fun "count" Sequence.any int query_count;;
register_fun "member" (Sequence.plus any) bool query_member;;
register_fun "distinct_values" Sequence.any Sequence.any query_distinct;;
......@@ -224,6 +224,10 @@ let get_int = function
| Integer i when Intervals.V.is_int i -> Intervals.V.get_int i
| _ -> raise (Invalid_argument "Value.get_int")
let get_integer = function
| Integer i -> i
| _ -> assert false
let rec is_seq = function
| Pair (_, y) when is_seq y -> true
| Atom a when a = Sequence.nil_atom -> true
......@@ -549,7 +553,11 @@ let rec get_sequence_rev accu v =
let get_sequence_rev v = get_sequence_rev [] v
let rec fold_sequence f accu v =
match normalize v with
| Pair (x,y) -> fold_sequence f (f accu x) y
| _ -> accu
let atom_ascii s =
Atom (Atoms.V.mk_ascii s)
......@@ -625,79 +633,3 @@ let print_utf8 v =
print_string (U.get_str v);
flush stdout
let query_min = function
| Pair(i,p) ->
let rec aux l i = match l with
| Pair(j,r) -> if (compare i j <0) then aux r i else aux r j
| Atom a when a = Sequence.nil_atom -> i
| _ -> assert false
in aux p i
|_ -> assert false
let query_max = function
| Pair(i,p) ->
let rec aux l i = match l with
| Pair(j,r) -> if (compare i j >0) then aux r i else aux r j
| Atom a when a = Sequence.nil_atom -> i
| _ -> assert false
in aux p i
|_ -> assert false
let query_sum = function
| Pair(Integer i,p) ->
let rec aux l i = match l with
| Pair(Integer j,r) -> aux r i+Intervals.V.get_int(j)
| Atom a when a = Sequence.nil_atom -> i
| _ -> assert false
in Integer(Intervals.V.from_int(aux p (Intervals.V.get_int(i))))
|_ -> assert false
let query_avg = function
| Pair(Integer i,p) ->
let rec aux l i n = match l with
| Pair(Integer j,r) -> aux r (i+Intervals.V.get_int(j)) (n+1)
| Atom a when a = Sequence.nil_atom -> i/(n+1)
| _ -> assert false
in Integer(Intervals.V.from_int(aux p (Intervals.V.get_int(i)) 0))
|_ -> assert false
let query_count = function
| Pair(Integer i,p) ->
let rec aux l n = match l with
| Pair(_,r) -> aux r (n+1)
| Atom a when a = Sequence.nil_atom -> n
| _ -> assert false
in Integer(Intervals.V.from_int(aux p 1))
| Atom a when a = Sequence.nil_atom -> Integer (Intervals.V.from_int 0)
|_ -> assert false
let query_member l = match l with
| Pair(e,p) ->
let rec aux l i = match l with
| Pair(j,r) -> if (compare i j =0) then vtrue else aux r i
| Atom a when a = Sequence.nil_atom -> vfalse
| _ -> assert false
in aux p (e)
| Atom a when a = Sequence.nil_atom -> vfalse
|_ -> assert false
let query_distinct = function
| Pair(i,p) ->
let rec aux l1 l2 = match l1 with
| Pair(h,t) -> if (query_member(Pair(h,t)))=vtrue
then (aux t l2) else (aux t (l2@[h]))
| Atom a when a = Sequence.nil_atom -> l2
| _ -> assert false
in sequence(aux (Pair(i,p)) [])
| Atom a when a = Sequence.nil_atom -> (Atom(Sequence.nil_atom))
|_ -> assert false
(* todo : typage precis pour min et max et distinct_values
*)
......@@ -54,6 +54,8 @@ val sequence_rev : t list -> t
val get_sequence : t -> t list
val get_sequence_rev : t -> t list
val fold_sequence: ('a -> t -> 'a) -> 'a -> t -> 'a
val atom_ascii : string -> t
val label_ascii : string -> label
val record : (label * t) list -> t
......@@ -84,6 +86,7 @@ val is_str : t -> bool
val is_seq : t -> bool
val get_int : t -> int
val get_integer : t -> Intervals.V.t
(** @return an associative list of fields from a Record value *)
val get_fields : t -> (Ns.qname * t) list
......@@ -118,12 +121,3 @@ val cduce2ocaml_bigint : t -> Big_int.big_int
val print_utf8: U.t -> unit
(*CQL Aggregats *)
val query_min: t-> t
val query_max: t-> t
val query_sum: t-> t
val query_avg: t-> t
val query_count: t-> t
val query_member: t -> t
val query_distinct: t -> t
......@@ -315,26 +315,3 @@ unary_op_gen "flatten"
register_fun "raise" any Types.empty
(fun v -> raise (Value.CDuceExn v));;
(* CQL agregats *)
register_fun "min" (Sequence.plus any) any
(Value.query_min );;
register_fun "max" (Sequence.plus any) any
(Value.query_max );;
register_fun "sum" (Sequence.plus int) int
(Value.query_sum );;
register_fun "avg" (Sequence.plus int) int
(Value.query_avg );;
register_fun "count" Sequence.any int
(Value.query_count );;
register_fun "member" (Sequence.plus any) bool
(Value.query_member );;
register_fun "distinct_values" (Sequence.star any) Sequence.any
(Value.query_distinct );;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment