# AQL - what do you expect as results for these example

**URL:** https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104
**Category:** AQL
**Created:** [16 November 2020 13:08 UTC](https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104 "2020-11-16T13:08:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bna](https://discourse.openehr.org/user_avatar/discourse.openehr.org/bna/32/1994_2.png) [@bna](https://discourse.openehr.org/u/bna)
#### Post date: [16 November 2020 13:08 UTC](https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104/1 "2020-11-16T13:08:49Z")

</div>

# Topic

The following and quite simple AQL will be evaluated with different datasets (A,B,C,D) using either **AND** or **OR**. For each case we have defined alternative and possible resultsets. Look at the examples. Which of the answers do you expect? Are there something missing?

Base AQL to be used for all the examples.

```auto
select o, e from composition c contains (observation o AND/OR evaluation e)

```

Dataset A

```auto
composition
     o1, o2
     e1, e2

```

## Alt A - `AND`

```auto
AND
o1, e1
o1, e2
o2, e1
o2, e2

```

## Alt A - OR1

```auto
OR (alt1)
o1, e1
o1, e2
o2, e1
o2, e2

```

## Alt A - OR2

```auto
OR (alt2)
o1
    e1
o2
    e2

```

# Dataset B

```auto
composition
     o1, o2
     e1

```

## Alt B - AND

```auto
AND
o1, e1
o2, e1

```

## Alt B - OR 1

```auto
OR (alt1)
o1, 
  , e1
o2, 

```

## Alt B - OR 2

```auto
OR (alt2)
o1, e1
o2, e1

```

# Dataset C

```auto
composition
     o1, o2

```

## Alt C - AND

```auto
AND

```

## Alt C - OR1

```auto
OR (alt1)
o1,
o2, 

```

## Alt C - OR2

```auto
OR (alt2)
o1
o2

```

# Dataset D

```auto
composition
     o1
     e1

```

## Alt D - AND

```auto
AND
o1, e1

```

## Alt D - OR 1

```auto
OR (alt1)
o1, e1

```

## Alt D - OR2

```auto
OR (alt2)
o1, 
    e1,

```

---

<div class="post-metadata">

### Author: ![ian.mcnicoll](https://discourse.openehr.org/user_avatar/discourse.openehr.org/ian.mcnicoll/32/4430_2.png) [@ian.mcnicoll](https://discourse.openehr.org/u/ian.mcnicoll)
#### Post date: [16 November 2020 14:36 UTC](https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104/2 "2020-11-16T14:36:25Z")

</div>

> [@bna](#):
>
> ```auto
> composition
> o1, o2
> e1, e2
> 
> ```

Is this 2 compositions or a single composition with all 4 archetypes, sorry being dumb ☹

---

<div class="post-metadata">

### Author: ![thomas.beale](https://discourse.openehr.org/user_avatar/discourse.openehr.org/thomas.beale/32/35_2.png) [@thomas.beale](https://discourse.openehr.org/u/thomas.beale)
#### Post date: [16 November 2020 15:20 UTC](https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104/3 "2020-11-16T15:20:44Z")

</div>

> [@bna](#):
>
> ```auto
> composition
> o1, o2
> e1, e2
> 
> ```

I don’t understand what this construction means - does it mean a Composition containing 4 Entries (i.e. 2 x Obs, 2 x Evals)?

---

<div class="post-metadata">

### Author: ![bna](https://discourse.openehr.org/user_avatar/discourse.openehr.org/bna/32/1994_2.png) [@bna](https://discourse.openehr.org/u/bna)
#### Post date: [16 November 2020 15:35 UTC](https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104/4 "2020-11-16T15:35:26Z")

</div>

Hi  
I will rewrite this post with the syntax and formalism learned from @Seref. Give me a day 🤔

---

<div class="post-metadata">

### Author: ![ian.mcnicoll](https://discourse.openehr.org/user_avatar/discourse.openehr.org/ian.mcnicoll/32/4430_2.png) [@ian.mcnicoll](https://discourse.openehr.org/u/ian.mcnicoll)
#### Post date: [16 November 2020 15:43 UTC](https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104/5 "2020-11-16T15:43:38Z")

</div>

I’m glad I was not the only one who was bit confused - Nordic minimalism? 😉

---

<div class="post-metadata">

### Author: ![Seref](https://discourse.openehr.org/user_avatar/discourse.openehr.org/seref/32/13_2.png) [@Seref](https://discourse.openehr.org/u/Seref)
#### Post date: [16 November 2020 17:58 UTC](https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104/6 "2020-11-16T17:58:16Z")

</div>

@bna here it is (bah, I realised I included C in the select. I won’t make a difference here though…)

 ![image](https://discourse.openehr.org/uploads/default/original/1X/4185854e8ab2a3f31d6313e50e6ba1920c0b3ff7.png)

`A: AND TPQ`  
 ![image](https://discourse.openehr.org/uploads/default/original/1X/1734241ddc4b586568bac8e07480f71632a51c55.png)  
`A: OR TPQ`  
 ![image](https://discourse.openehr.org/uploads/default/original/1X/3bbaf1ed48a6ce06eccf7d7e7eb607f76ab733c7.png)

`B: AND TPQ`  
 ![image](https://discourse.openehr.org/uploads/default/original/1X/de372b731a5fba0966d58931c639285164a47e09.png)  
`B: OR TPQ`  
 ![image](https://discourse.openehr.org/uploads/default/original/1X/9e08ddfd3d75cc7b99eccc419fa0c83ef1f1f950.png)

`C: AND TPQ`  
EMPTY SET  
`C: OR TPQ`  
 ![image](https://discourse.openehr.org/uploads/default/original/1X/0a3f516ca8d0a17cc84a10f7c4194f2a7ba19094.png)

`D: AND TPQ`  
 ![image](https://discourse.openehr.org/uploads/default/original/1X/3008e861bfedf7cebb4e3713158e71afd62f0453.png)  
`D: OR TPQ`  
 ![image](https://discourse.openehr.org/uploads/default/original/1X/2562dd2b50d4fcbfee552f06dad88ee139681676.png)

The permutative behaviour of OR operator is that of UNION:  
Matches of operand 1 + Matches of operand 2 + Matches of operand 1 & 2

There is very little to discuss about the behaviour of AND, it feels natural to everyone. As I mentioned today, OR is a completely different beast, because it is hard to identify rows with the same semantics from a user perspective. Sometimes there is nothing to eliminate: look at the result of OR query on A. All the rows are different and you can’t squash/pack anything there, or throw any rows away.  
Based on the structure of the input tree (actual data) we may have some rows having the same semantics and we can exclude those from the results, but there is none here. @matijap very interested in your view on that particular result set.

I hope I did not mess something up while copying this from paper to screen, I usually use pen and paper to do this.

---

<div class="post-metadata">

### Author: ![thomas.beale](https://discourse.openehr.org/user_avatar/discourse.openehr.org/thomas.beale/32/35_2.png) [@thomas.beale](https://discourse.openehr.org/u/thomas.beale)
#### Post date: [16 November 2020 23:48 UTC](https://discourse.openehr.org/t/aql-what-do-you-expect-as-results-for-these-example/1104/7 "2020-11-16T23:48:20Z")

</div>

I understand the FROM clause as a pattern matcher to filter the _total database_ to obtain the _dataset_ on which the WHERE, then the SELECT are run.

Under this logic, `SELECT o, e FROM composition c CONTAINS (observation o OR evaluation e)` applied to instance structure A would mean the following execution:

- FROM processing: -\> C1
- WHERE processing: ( nothing )
- SELECT processing: -\>

```auto
------
O1, E1
O1, E2
O2, E1
O2, E2
------

```

This is the same answer as @bna’s Alt A - OR1, but only the bottom half only of @Seref’s table ‘A: OR TPQ’. Accordingly, there are no rows of the form `O1, -`, which seems correct to me, because the structure of A does not look like that.

If we imagine a database containing 100 Compositions, including A, B, C and D (call them C1 …C4), being the only ones with Evals or Obs instances, then the query applied to that database will process as follows:

- FROM processing: -\> C1, C2, C3, C4
- SELECT processing: -\>

```auto
------------
C1.O1, C1.E1
C1.O1, C1.E2
C1.O2, C1.E1
C1.O2, C1.E2

C2.O1, C2.E1
C2.O2, C2.E1

C3.O1, -
C3.O2, -

C4.O1, C4.E1
-------------

```

The above is the same as @bna’s, if you choose a A-OR1, B-OR2, C-OR1, and D-OR2.

This approach reduces the permutations by assuming that once OR matching is done, generating a list of _matched top-level container objects_, you just perform normal WHERE and SELECT processing on that list. The use of OR in the first step doesn’t create any ‘cloned views’ containing partial children, which is what some of Bjorn’s and (I think) all of Seref’s do.

This is just one possible AQL semantics which might be useful to think about.
