select l, l->type[2], l->type
from l in class locus where exists_tag l->Complementation_data



aql select a, a->Variation_External_DB_Keys, a->Variation_External_DB_Keys[2] \
from a in class Locus where exists_tag a->Variation_External_DB_Keys

aql select a, b, c \
from a in class Locus where exists_tag a->Variation_External_DB_Keys, b in a->Variation_External_DB_Keys, c in a->Variation_External_DB_Keys[2]

aql select a, a->Variation_External_DB_Keys[1], \
a->Variation_External_DB_Keys[2], a->Variation_External_DB_Keys[3] \
from a in class Locus where a->Variation_External_DB_Keys[0]



select p, p->1 from p in class person where exists p->height[0]

select p, p->height[3], p->height[2], p->height[1]
from p in class person where exists p->height[0]

////////////////////

select p->height[0], p->height[1], p->height[2], p->height[3]
from p in class person

select p->height[3], p->height[2], p->height[1], p->height[0]
from p in class person


/////////////////////////////////////////////////////////////////

// complex declaration based on an expression (SANgc05249) - 
// lists actors with their height in cm calculated from the imperial measures.
select p, cm
from p in class person
	  where exists p->imperial,
     cm in p->height[imperial][1] * 30 + p->height[imperial][2] * 2.5

// test variable copies (aqlcheck.c:preLocCheck) with those decls
select p, cm, copy
from p in class person
	  where exists p->imperial,
     cm in p->height[imperial][1] * 30 + p->height[imperial][2] * 2.5,
     copy in cm

select m, d
from m in class movie
	  where exists_tag m->release,
     d1 in first (select m->date),
     d2 in d1,
     d in d2


// test variable copies
select m1->cast, m2->director 
from m1 in object("movie", "True Romance"), 
     m2 in m1

// test variable copies - 
// for every movie list all other movies in which one of the characters has appeared
select m, mm 
from m in class movie, 
     mm in m 
        where m->cast[2] = mm->cast[2] and m != mm


// test the IS_EXPR locator types   (SANgc05249)
select c, num
from m in class movie, 
     c in m->cast[2],
     num in count(select m1 from m1 in class movie where m1->cast[2] = c) 
	    where num > 1
order by :num desc

//


// test queries for  SANgc05275
select p, s  from p in class person where p like "*ford*", s in p->spouse

select p, s  from p in class person where p.name like "*ford*", s in p->spouse

select p, s  from p in class person where p->stars_in.name like "*ark*", s in p->spouse

select p, s  from p in class person where p like "*ford*", s in p->stars_in


// class movie is case-sensitive
//
select all class movie."a*"

select all class movie."A*"

// class person isn't
//
select all class person."a*"

select all class person."A*"

//////////////////////

select m, t1->Session from m in class movie, t1 in m->date[0].timestamp


select all class usersession where exists_tag ->session


select m, m->date.timestamp from m in class movie

select m, m.timestamp from m in class movie

select m, m->date, m.timestamp from m in class movie order

select m, t from m in class movie, t in m.timestamp order by :2

select m, t from m in object("movie", "Twelve Monkeys"), t in m.timestamp

//////////////////////

select a, a->height from a in class person

select a, a->height from a in class person, b in a->height[2]

select a, a->height, b from a in class person, b in a->height[2]

select a, a->height[0] from a in class person

select a, a->height[1] from a in class person

select a->height[2] from a in class person where a->height[2] = 5

select a, a->height, a->height[2] from a in class person

select a, a->height, a->height[2] from a in class person where a->height[2] = 5

select a, a->email  from a in class person where exists a->email

// doesn't need to access objects
select a from a in class person where exists_tag a->directed


// shouldn't need re-run to access objects
select a from a in class person where exists a->directed or exists_tag a->directed

select a, a->directed, a->directed.name  from a in class person

select a, a->directed, a->directed.name  from a in class person where exists a->directed or exists_tag a->directed

select a, a->directed, a->directed->title, a->directed.name from a in class person

select a, a->directed, a->directed.name  from a in class person where exists_tag a->directed

select a, a.name.length, a->directed.name  from a in class person where exists_tag a->directed

select a, a->directed[0], a->directed[1]  from a in class person 

select a, a->directed[0], a->directed[1]  from a in class person where exists_tag a->directed

// return first level tags
select a, a->1 from a in class person

select ->0, ->1 from class model

// error 903
select a, a->1 from a in class person where exists_tag a->1

select a, a->1 from a in class person where exists a->1

// error 810
select a, a->2  from a in class person

--------------------------------------------------------------

// syntax error 700
select p from p in class person order by :p->Date_of_Birth

// syntax error 700
select p from p in class person order by
                 :yeardiff(p->Date_of_Birth,today)

// works - people and their age
select p, age 
from p in class person
	  where exists p->Date_of_birth, 
     age in yeardiff(p->Date_of_Birth,today)  
order by :age

----------------------------------------------------------
select all class person

select all from class person

// error 902
select [0], [directed] from class person

select ->0, ->directed from class person

select p, p->directed from p in class person

select ->0 from class person where (->directed = ->stars_in) = true

select ->0, ->directed = ->stars_in from class person

select ->0, ->directed = ->stars_in from class person where ->directed != ->stars_in

select ->0, ->directed = ->stars_in from class person where ->directed and ->stars_in

select ->0, ->directed, ->stars_in, ->directed = ->stars_in from class person where ->directed and ->stars_in

--------------------------------------------------------------
select ->0 from class person where ->stars_in != true

select ->0, ->directed, ->stars_in from class person where  ->directed = ->stars_in

select ->0 from class person where not ->stars_in

select ->0 from class person where not (->stars_in = true)

select ->0, ->stars_in != true from class person      

select ->0, not (->stars_in) from class person      

select ->0, not (->stars_in) from class person where not ->stars_in   

select ->0, ->stars_in from class person where ->stars_in

select ->0, ->stars_in from class person where ->0 like "t*"

select ->0, ->directed, ->stars_in, ->directed != ->stars_in from class person where  ->directed != ->stars_in

select ->0, ->stars_in, ->directed from class person where ->stars_in and ->directed

// show stars_in tag value and it's timestamp
select s, s->stars_in, t, t->session
          from s in class person, 
               t in s->stars_in.timestamp

select ->0, ->stars_in from class person where ->stars_in = "casino"

select ->0, ->real_name, ->height, ->height[2] , ->height[3] from class person

select ->0, ->height[2] + ->height[3] from class person

select ->0, ->height[2] + ->height[3] from class person where exists_tag ->height

select a, a->stars_in[0]  from a in class person

select ->0, ->stars_in[0]  from class person

// result must be empty
@tmp := select all class person where not exists_tag ->stars_in;
select all @tmp where exists_tag :1->stars_in

// result must be empty
@tmp := select all class person where not exists_tag ->stars_in;
select all @tmp:1 where exists_tag ->stars_in

// result must be empty
@tmp := select all class person where not exists_tag ->stars_in;
select p from p in @tmp:1 where exists_tag p->stars_in

==============================

select all class movie where not (->0 like "a*")

===================

@tmp := select ->0, ->date_of_birth from class person;
select :2 from @tmp

@tmp := select ->0, ->date_of_birth from class person;
select p from p in @tmp:2

===================

select all class movie where exists_tag ->cast

// same as above
select all class movie where exists_tag ->cast[0]

// error 903
select all class movie where exists_tag ->cast[1]

// errno 902
select all class movie where exists_tag [cast]

// errno 902
select ->0, [cast] from class movie 

// errno 902
select a[1] from a in class Movie

select ->cast[2] from class movie


select ->0, ->cast, ->cast->stars_in from class Movie where ->cast = "Dennis Hopper"

select ->0, ->cast from class Movie where ->cast = "Dennis Hopper"

select ->0, c::->cast from class Movie where ->cast = "Dennis Hopper"

select ->0, ->stars_in from class Person where ->stars_in = "True Romance"

///////

// don't knwo what this does
select c from a in class person, b in class movie, aa in a->stars_in, c in b->cast[2] where b->cast = a order

// correctly returns nothing (wrong query)
select m, mm from m in class movie, c in m->cast where c[2] = "James Bond", mm in c->stars_in

//********* easy hand optimization with where-clause ******************

// opens all movie objects
select ->0, ->cast from class Movie where ->0 = "True Romance"

// only ever opens one object
select ->0, c from class Movie where ->0 = "True Romance", c in ->cast


//********** Linking classes *****************//

// actors and the characters they've played (takes long)
select a, c from a in class person, b in class movie, aa in a->stars_in, bb in b->cast where bb = a, c in bb[1] order by :a

// actors and the characters they've played (quicker query)
select a, c from a in class person, am in a->stars_in, ama in am->cast where ama = a, c in ama[1] order

//******** designing a nested query **************/

// all character names
select c from m in class movie, a in m->cast, c in a[1]

// actors for a given character name
select a from m in class movie, a in m->cast, c in a[1] where c = "James Bond"

// character names and the number of different actors that played it
select c, count(select a1 from m1 in class movie, a1 in m1->cast, c1 in a1[1] where c1 = c) 
from m in class movie, 
     a in m->cast, 
     c in a[1] order

// characters that have been played by more 2 or more different actors
select ch, num
from m in class movie, 
     actor in m->cast, 
     ch in actor[1],
     num in count(select actor1 from m1 in class movie, actor1 in m1->cast, ch1 in actor1[1] where ch1 = ch) 
	 where num > 1
order by :num desc, :ch

// character names and the number of different movies they appear in
select c, count(select m1 from m1 in class movie where m1->cast[2] = c) 
from m in class movie, 
     c in m->cast[2] order

// characters that appeared in more than 1 movie - without table-variables
select c, num
from m in class movie, 
     c in m->cast[2],
     num in count(select m1 from m1 in class movie where m1->cast[2] = c) 
	    where num > 1
order by :num desc, :c asc

// characters that appeared in more than 1 movie - in 2 parts
@characters := select c, count(select m1 from m1 in class movie where m1->cast[2] = c) 
	       from m in class movie, 
		    c in m->cast[2] order;
select :1, :2 
from @characters 
     where :2 > 1

// row variables - characters that appeared in more than 1 movie
@characters := select c, count(select m1 from m1 in class movie where m1->cast[2] = c) from m in class movie, c in m->cast[2] order;
select row:1, row:2 from row in @characters where row:2 > 1

// column variables - double recursion should loop twice over the table - gives unexpected results.
@characters := select c, count(select m1 from m1 in class movie where m1->cast[2] = c) from m in class movie, c in m->cast[2] order;
select col1, col2 from col1 in @characters:1, col2 in @characters:2 where col2 > 1

// characters that appeared in more than 1 movie listed with the movies they appeared in
@characters := select c, count(select m1 from m1 in class movie where m1->cast[2] = c), m from m in class movie, c in m->cast[2];
select :1, :3 from @characters where :2 > 1 order

// characters that appeared in more than 1 movie listed with the movies, sorted by the movie primary release date
characters := select c, 
		     count(select m1 from m1 in class movie where m1->cast[2] = c), 
		     m, 
		     first(select m->release[date]) 
              from m in class movie, 
		   c in m->cast[2];
select character::row:1, movie::row:3, date::row:4 
from row in @characters 
	    where row:2 > 1 
order by :character, :date 

// same, but without table-variables
select c, m, mdate
from m in class movie, c in m->cast[2],
     num in count(select m1 from m1 in class movie where m1->cast[2] = c)
	    where num > 1,
     mdate in first(select m->release[date])
order by :c, :mdate

//******** James Bond Film Tutorial ************//

// list bond-films (i.e. films where an actor played a character named 'Jamed Bond')
select m
from m in class movie 
       where m->cast[2] = "James Bond"


// list bond-films and all of their actors
select m, m->cast
from m in class movie 
       where m->cast[2] = "James Bond"


// list bond-films and the James Bond actor
select m, a
from m in class movie,
     a in m->cast
       where a[1] = "James Bond"


// list bond films with the James Bond actor and which other movies the bond actor has starred in
select m, a, a->stars_in 
from m in class movie, 
     a in m->cast 
       where a[1] = "James Bond"

// list bond films with the James Bond actor and how many other movies they've been in
select m, a, count(select m1 
	           from m1 in class movie 
			   where m1->cast = a) 
from m in class movie, 
     a in m->cast 
       where a[1] = "James Bond"

// list bond films with their actors and how many non-bond movies they've been in
select m, a, count(select m1 
                   from m1 in class movie, 
			a1 in m1->cast
			   where a1 = a and a1[1] != "James Bond") 
from m in class movie, 
     a in m->cast 
       where a[1] = "James Bond"


// find james bond actors who have starred in films where they didn't play james bond
// and list the other movie and the other character name they've played
select a, otherm, otherc
from m in class movie, 
     a in m->cast 
	  where a[1] = "James Bond", 
     otherm in a->stars_in, a1 in otherm->cast
	    where a = a1,
     otherc in a1[1] 
	  where otherc != "James Bond"

// find bondmovies for which no character M is in the database

// error 933
@bondmovies := select bm 
	       from bm in class movie,
		    ba in bm->cast 
		       where ba[1] = "James Bond";
@withm := select all @bondmovies
		   where ->cast[2] = "M";
@bondmovies diff @withm

// error 933
@bondmovies := select bm 
	       from bm in class movie,
		    ba in bm->cast 
		       where ba[1] = "James Bond";
@withm := select m from m in @bondmovies
		   where m->cast[2] = "M";
@bondmovies diff @withm

// error 932
@bondmovies := select bm 
	       from bm in class movie,
		    ba in bm->cast 
		       where ba[1] = "James Bond";
@withm := select m from m in @bondmovies
		   where m:1->cast[2] = "M";
@bondmovies diff @withm


// error 930
@bondmovies := select bm 
	       from bm in class movie,
		    ba in bm->cast 
		       where ba[1] = "James Bond";
@withm := select all @bondmovies:1
		   where :1->cast[2] = "M";
@bondmovies diff @withm

// correct query version 1
@bondmovies := select bm 
	       from bm in class movie,
		    ba in bm->cast 
		       where ba[1] = "James Bond";
@withm := select m from m in @bondmovies:1
		   where m->cast[2] = "M";
@bondmovies diff @withm

// correct query version 2
@bondmovies := select bm 
	       from bm in class movie,
		    ba in bm->cast 
		       where ba[1] = "James Bond";
@withm := select m:1 from m in @bondmovies
		   where m:1->cast[2] = "M";
@bondmovies diff @withm


// same query, but without table variables
select bm, ma
from bm in class movie,
     ba in bm->cast 
	   where ba[1] = "James Bond",
     ma in ba
	   where ma[1] = "M"

---------------

// list bond movies (by their year and bond-actor) and count the number of bond-prequels and sequels
select first(select m->release[date]), a, count(select m->follows), count(select m->followed_by)
from m in class movie, 
     a in m->cast 
       where a[1] = "James Bond" 
order by :first

// release date of first bond film
@tmp := select first(select m->date), a
from m in class movie, 
     a in m->cast 
       where a[1] = "James Bond" 
order by :1;
select min(@tmp):1

// release date of first bond film
select min(select first(select m->release[date])
from m in class movie, 
     a in m->cast 
       where a[1] = "James Bond" 
order by :1):1


// add up number of prequels and sequels for each bond-movie
select m->release[date], a, count(select m->follows) + count(select m->followed_by)
from m in class movie, 
     a in m->cast
       where a[1] = "James Bond" 
order by :date

// show the recursion over the selectfields as every possible combination
// of prequel with all of its sequels is shown
select m->release[date], m, a, m->follows, m->followed_by 
from m in class movie, 
     a in m->cast 
       where a[1] = "James Bond" 
order by :date


////////////////

// show all actors from bond movies and all their films
select m, m->Cast, m->Cast->Stars_in from m in class movie where m->Cast[2]  = "James Bond"

// actors from bond films that haven't played "James Bond"
@a := select m->Cast from m in class movie where m->Cast[2]  = "James Bond";
@b := select c from m in class movie, c in m->cast, otherm in c->stars_in where c[1] = "James Bond";
@a diff @b

// almost the same query, but it doesn't list "Sophie Marceau", because she doesn't have a character name stored
// for "The world is not enough", so the comparison on the NULL value in that field causes her not to be included.
select othera 
from m in class movie, 
     ba in m->Cast 
	   where ba[1] = "James Bond",
     othera in m->cast 
	       where othera[1] != "James Bond"



--------------

// this query ...
select m, c, otherm from m in class movie, c in m->cast, otherm in c->stars_in

// ... translates to :
select m, m->Cast, m->Cast->Stars_in from m in class movie

----------

select m from m in class movie where m like "A*"

select m from m in class movie where m.name like "A*"


// useless declaration
select m from m in class movie where m like "A*", c in m->cast


select mm::m, m->cast from m in class movie order

select m, mm::m->cast from m in object("movie", "The World is Not Enough")

// should this return an error ??
select mm::m, mm::m->cast from m in object("movie", "The World is Not Enough")

// error 923
select mm::m, mm::m->cast from m in object("movie", "The World is Not Enough") order by :cast

select mm::m, mm::m->directed from m in class actor order by :mm

select mm::m, mm::m->directed from m in class actor order by :mm asc

select mm::m, mm::m->directed from m in class actor order by :mm desc


// should return '1', because there is only one 'directed' per person
select count(select ->directed[0]) from class person

// retuirns 1 with every director
select ->0, count(select ->directed[0]) from class person

// XXXXXX
select ->0, count(select ->directed) from class person

select ->0, count(select ->directed) from class person where exists ->directed

select count(select ->directed) from class person

select count(select ->directed) from class person where exists_tag ->directed


select ->0, ->directed from class person

select ->directed from class person

select ->stars_in from class person order

// all the movies that people star in should be listed in the movie class
@a := select ->stars_in from class person ;
 @b := select all class movie;
 @a diff @b



// returns a single value '1'
select count(select ->directed[0]) from class person where exists_tag ->directed

// returns '1' with every director
select ->0,  count(select ->directed[0]) from class person where exists_tag ->directed

// XXXXXXXXX second column blank???
select ->0, count(select ->directed) from class person

select ->0, count(select ->directed) from class person where exists_tag ->directed

select ->0, count(select ->directed) from class person where exists ->directed

// works
select p, c::count(select p->directed) from p in class person where exists_tag p->directed order by :c, :p

// works now as well
select ->0, c::count(select ->directed) from class person where exists_tag ->directed order by :c

// errno 923
select ->0, c::count(select ->directed) from class person where exists_tag ->directed order by :count

select ->0, count(select ->directed) from class person where exists_tag ->directed order by :count

select ->0, count(select ->directed) from class person where exists_tag ->directed order by :2



--------------------

// shouldn't open any object

@l := select all class movie where exists_tag ->links ;
select all from @l where :1->cast[2] and not exists_tag :1->links

-------------------------

select m from m in class movie where m->cast[0]

select m from m in class movie where not m->cast[0]

select m from m in class movie where m->cast

select m from m in class movie where not m->cast

select m from m in class movie where exists_tag m->cast

select m from m in class movie where not exists_tag m->cast

select ->0, i from class "class", i in ->index where exists i

// compare behaviour - results should be empty
@a := select all class movie where ->cast[0]; 
  @b := select all class movie where not ->cast[0];
    @c := select all class movie;
      @ab := @a union @b;
        @ab diff @c

@a := select all class movie where exists ->cast[0]; 
  @b := select all class movie where ->cast[0];
       @a diff @b

@a := select all class movie where exists_tag ->cast; 
  @b := select all class movie where exists ->cast[0];
       @a diff @b

@a := select all class movie where exists ->cast; 
  @b := select all class movie where ->cast;
       @a diff @b

@a := select all class movie where not exists ->cast; 
  @b := select all class movie where not ->cast;
       @a diff @b

@a := select all class movie where not exists ->cast; 
  @b := select all class movie where ->cast;
       @a intersect @b

@a := select all class movie where not exists ->director; 
  @b := select all class movie where ->director;
       @a intersect @b

//////////

select p->real_name[0] from p in object("person", "tom cruise")

select p->real_name from p in object("person", "tom cruise")

select p, p->real_name from p in object("person", "tom cruise")

select ->0, ->real_name from object("person", "tom cruise")

select p, p->height[2], p->height[3] from p in class person where exists_tag p->height

$a := min(select p, p->height[imperial] from p in class person):2

@t := select p, H::p->height[imperial] from p in class person; 
$a := max @t:H

@t := select p, H::p->height[imperial] from p in class person; 
$a := avg @t:H

// 12 feet to an inch - number of inches times 2.54 gives height in cm - we output in metres
select p, H::(p->height[2]*12+p->height[3])*.0254 from p in class person where exists_tag p->height 

select p, H::(p->height[2]*12+p->height[3])*.0254 from p in class person where exists_tag p->height order by :H

select p, H::(p->height[2]*12+p->height[3])*.0254 from p in class person order by :H

-----------------------------------------------------------------------

// errno 992
select all from classs movie

// errno 700
select all from 

// errno 700
select all from class movie class



