--
-- define i/o functions and the raster type
--
create function raster_in  (text) returns raster
  as external name 'MI_HOME/examples/sequoia2000/src/raster.so' 
    language c; 

create function raster_out (raster) returns text
    as external name 'MI_HOME/examples/sequoia2000/src/raster.so' 
    language c; 

create type raster (internallength = 0, input = raster_in, output = raster_out);

--
-- since rasters contain a large object we have to define an Assign and
-- a Destroy function for them.
--
create function Assign(raster) returns raster as external name
    'MI_HOME/examples/sequoia2000/src/raster.so(raster_assign)'
    language c;
create function Destroy(raster) returns raster as external name
    'MI_HOME/examples/sequoia2000/src/raster.so(raster_destroy)'
    language c;
--
-- functions (including an aggregate) that operate on the raster adt
--
-- clip a raster to a Poly
create function Clip (raster, Poly) returns raster as external name
    'MI_HOME/examples/sequoia2000/src/raster.so(raster_clip)' 
    language c; 

-- clip a raster to a Box
create function Clip (raster, Box) returns raster as external name
    'MI_HOME/examples/sequoia2000/src/raster.so(raster_bclip)' 
    language c; 

create function LowerRes (raster, int) returns raster as external name
    'MI_HOME/examples/sequoia2000/src/raster.so(raster_lowerres)' 
    language c; 
--
-- define the raster average aggregate by defining its 2 component
-- functions then issuing the create aggregate command.
--
create function raster_add (int, raster) returns raster
    as external name 'MI_HOME/examples/sequoia2000/src/raster.so' 
    language c; 
create function raster_avg_final (int) returns raster
    as external name 'MI_HOME/examples/sequoia2000/src/raster.so' 
    language c; 

create aggregate AVG
    (init=nullinit(),			-- there is no initialization action
     iter=raster_add(int,raster),
     final=raster_avg_final(int)); 

--
-- functions operating on the builtin, but undocumented Pnt, Poly
-- and Box types
--
create function make_box (Pnt, double precision) returns Box
    as external name 'MI_HOME/examples/sequoia2000/src/polyfuncs.so' 
    language c; 
create function box_make_poly (Box) returns Poly
    as external name 'MI_HOME/examples/sequoia2000/src/polyfuncs.so' 
    language c; 
--
-- returns the size of the Poly as its area (rather the area of the
-- the Poly's bounding Box
--
create function size (Poly) returns double precision
    as external name 'MI_HOME/examples/sequoia2000/src/polyfuncs.so' 
    language c; 
--
-- Pnt to (degenerate) Poly conversion function
--
create function pt_make_poly(Pnt) returns Poly
    as external name 'MI_HOME/examples/sequoia2000/src/polyfuncs.so'
    language c;

--
-- create the tables.
--
-- for our implementation of the benchmark there are 4 as the Poly
-- data is stored in two separate tables.  the specification for Polys
-- indicates the existence "holes" (we call them islands).  a hole
-- is a smaller Poly inside the larger s.t. all points inside the smaller
-- are not part of the larger.
-- 
-- an alternative approach (which is likely to be faster) would be to
-- encapsulate the holes into a "swiss cheese" Poly adt modifying the
-- operators to take the holes into account.
--
create table rasters (rtime int, location Box, frequency int, data raster); 

create table points (location Pnt, name text); 

create table polygons (landuse int, location Poly); 
create table islands (location Poly);

-- operator bindings
--
-- bind Overlaps to the & operator
--
create binary operator binding & to Overlaps;

--
-- bind Contained to the ^ operator
--
create binary operator binding ^ to Contained; 
--
-- for debugging purposes
--
create function getpid returns integer
as external name '/usr/lib/libc.so.1.8.1'
language c;
