# These are queries that test correlated subqueries, and are taken from the
# Hibernate test suite. See this issue:
#   https://github.com/cockroachdb/cockroach/issues/26658

# ------------------------------------------------------------------------------
# Query #1
#   org.hibernate.userguide.collections.UnidirectionalMapTest testLifecycle
# ------------------------------------------------------------------------------
exec-ddl
create table Person (
  id int8 not null,
  primary key (id)
)
----

exec-ddl
create table Phone (
  id int8 not null,
  "number" varchar(255),
  since timestamp,
  type int4,
  primary key (id)
)
----

exec-ddl
create table phone_register (
  phone_id int8 not null,
  person_id int8 not null,
  primary key (phone_id, person_id),
  foreign key (person_id) references Phone (id),
  foreign key (phone_id) references Person (id),
  unique (person_id)
)
----

opt
select
  phoneregis0_.phone_id as phone_id1_2_0_,
  phoneregis0_.person_id as person_i2_2_0_,
  (
    select a10.since
    from Phone a10
    where a10.id=phoneregis0_.person_id
  ) as formula159_0_,
  unidirecti1_.id as id1_1_1_,
  unidirecti1_."number" as number2_1_1_,
  unidirecti1_.since as since3_1_1_,
  unidirecti1_.type as type4_1_1_
from
  phone_register phoneregis0_
inner join Phone unidirecti1_
  on phoneregis0_.person_id=unidirecti1_.id
where phoneregis0_.phone_id=1;
----
project
 ├── columns: phone_id1_2_0_:1!null person_i2_2_0_:2!null formula159_0_:17 id1_1_1_:5!null number2_1_1_:6 since3_1_1_:7 type4_1_1_:8
 ├── key: (5)
 ├── fd: ()-->(1), (5)-->(6-8), (2)==(5), (5)==(2), (7)==(17), (17)==(7)
 ├── inner-join (lookup phone [as=unidirecti1_])
 │    ├── columns: phone_id:1!null person_id:2!null unidirecti1_.id:5!null unidirecti1_.number:6 unidirecti1_.since:7 unidirecti1_.type:8
 │    ├── key columns: [2] = [5]
 │    ├── lookup columns are key
 │    ├── key: (5)
 │    ├── fd: ()-->(1), (5)-->(6-8), (2)==(5), (5)==(2)
 │    ├── scan phone_register [as=phoneregis0_]
 │    │    ├── columns: phone_id:1!null person_id:2!null
 │    │    ├── constraint: /1/2: [/1 - /1]
 │    │    ├── key: (2)
 │    │    └── fd: ()-->(1)
 │    └── filters (true)
 └── projections
      └── unidirecti1_.since:7 [as=formula159_0_:17, outer=(7)]

exec-ddl
drop table phone_register, Person, Phone;
----

# ------------------------------------------------------------------------------
# Query #2
#   org.hibernate.userguide.criteria.CriteriaTest:
#
#   test_criteria_from_fetch_example
#   test_criteria_from_join_example
#   test_criteria_from_multiple_root_example
# ------------------------------------------------------------------------------
exec-ddl
create table Person (
   id int8 not null,
    address varchar(255),
    createdOn timestamp,
    name varchar(255),
    nickName varchar(255),
    version int4 not null,
    primary key (id)
)
----

exec-ddl
create table Person_addresses (
   Person_id int8 not null,
    addresses varchar(255),
    addresses_KEY varchar(255) not null,
    primary key (Person_id, addresses_KEY)
)
----

exec-ddl
create table Phone (
   id int8 not null,
    phone_number varchar(255),
    phone_type varchar(255),
    person_id int8,
    order_id int4,
    primary key (id)
)
----

exec-ddl
create table phone_call (
   id int8 not null,
    duration int4 not null,
    call_timestamp timestamp,
    phone_id int8,
    primary key (id)
)
----

exec-ddl
create table Partner (
   id int8 not null,
    name varchar(255),
    version int4 not null,
    primary key (id)
)
----

opt
select
    phone0_.id as id1_6_0_,
    person1_.id as id1_4_1_,
    phone0_.phone_number as phone_nu2_6_0_,
    phone0_.person_id as person_i4_6_0_,
    phone0_.phone_type as phone_ty3_6_0_,
    addresses2_.Person_id as Person_i1_5_0__,
    addresses2_.addresses as addresse2_5_0__,
    addresses2_.addresses_KEY as addresse3_0__,
    person1_.address as address2_4_1_,
    person1_.createdOn as createdO3_4_1_,
    person1_.name as name4_4_1_,
    person1_.nickName as nickName5_4_1_,
    person1_.version as version6_4_1_,
    addresses2_.Person_id as Person_i1_5_0__,
    addresses2_.addresses as addresse2_5_0__,
    addresses2_.addresses_KEY as addresse3_0__
from
    Phone phone0_
inner join
    Person person1_
        on phone0_.person_id=person1_.id
inner join
    Person_addresses addresses2_
        on person1_.id=addresses2_.Person_id
where
    exists (
        select
            calls3_.id
        from
            phone_call calls3_
        where
            phone0_.id=calls3_.phone_id
    )
----
inner-join (hash)
 ├── columns: id1_6_0_:1!null id1_4_1_:8!null phone_nu2_6_0_:2 person_i4_6_0_:4!null phone_ty3_6_0_:3 person_i1_5_0__:16!null addresse2_5_0__:17 addresse3_0__:18!null address2_4_1_:9 createdo3_4_1_:10 name4_4_1_:11 nickname5_4_1_:12 version6_4_1_:13!null person_i1_5_0__:16!null addresse2_5_0__:17 addresse3_0__:18!null
 ├── key: (1,18)
 ├── fd: (1)-->(2-4), (8)-->(9-13), (16,18)-->(17), (4)==(8,16), (8)==(4,16), (16)==(4,8)
 ├── scan person_addresses [as=addresses2_]
 │    ├── columns: addresses2_.person_id:16!null addresses:17 addresses_key:18!null
 │    ├── key: (16,18)
 │    └── fd: (16,18)-->(17)
 ├── inner-join (lookup person [as=person1_])
 │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 phone0_.person_id:4!null person1_.id:8!null address:9 createdon:10 name:11 nickname:12 version:13!null
 │    ├── key columns: [4] = [8]
 │    ├── lookup columns are key
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-4), (8)-->(9-13), (4)==(8), (8)==(4)
 │    ├── project
 │    │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 phone0_.person_id:4
 │    │    ├── key: (1)
 │    │    ├── fd: (1)-->(2-4)
 │    │    └── inner-join (lookup phone [as=phone0_])
 │    │         ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 phone0_.person_id:4 phone_id:24!null
 │    │         ├── key columns: [24] = [1]
 │    │         ├── lookup columns are key
 │    │         ├── key: (24)
 │    │         ├── fd: (1)-->(2-4), (1)==(24), (24)==(1)
 │    │         ├── distinct-on
 │    │         │    ├── columns: phone_id:24
 │    │         │    ├── grouping columns: phone_id:24
 │    │         │    ├── key: (24)
 │    │         │    └── scan phone_call [as=calls3_]
 │    │         │         └── columns: phone_id:24
 │    │         └── filters (true)
 │    └── filters (true)
 └── filters
      └── person1_.id:8 = addresses2_.person_id:16 [outer=(8,16), constraints=(/8: (/NULL - ]; /16: (/NULL - ]), fd=(8)==(16), (16)==(8)]

opt
select
    phone0_.id as id1_6_,
    phone0_.phone_number as phone_nu2_6_,
    phone0_.person_id as person_i4_6_,
    phone0_.phone_type as phone_ty3_6_
from
    Phone phone0_
inner join
    Person person1_
        on phone0_.person_id=person1_.id
inner join
    Person_addresses addresses2_
        on person1_.id=addresses2_.Person_id
where
    exists (
        select
            calls3_.id
        from
            phone_call calls3_
        where
            phone0_.id=calls3_.phone_id
    )
----
project
 ├── columns: id1_6_:1!null phone_nu2_6_:2 person_i4_6_:4!null phone_ty3_6_:3
 ├── fd: (1)-->(2-4)
 └── inner-join (hash)
      ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 phone0_.person_id:4!null person1_.id:8!null addresses2_.person_id:16!null
      ├── fd: (1)-->(2-4), (4)==(8,16), (8)==(4,16), (16)==(4,8)
      ├── scan person_addresses [as=addresses2_]
      │    └── columns: addresses2_.person_id:16!null
      ├── inner-join (lookup person [as=person1_])
      │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 phone0_.person_id:4!null person1_.id:8!null
      │    ├── key columns: [4] = [8]
      │    ├── lookup columns are key
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4), (4)==(8), (8)==(4)
      │    ├── project
      │    │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 phone0_.person_id:4
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(2-4)
      │    │    └── inner-join (lookup phone [as=phone0_])
      │    │         ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 phone0_.person_id:4 phone_id:24!null
      │    │         ├── key columns: [24] = [1]
      │    │         ├── lookup columns are key
      │    │         ├── key: (24)
      │    │         ├── fd: (1)-->(2-4), (1)==(24), (24)==(1)
      │    │         ├── distinct-on
      │    │         │    ├── columns: phone_id:24
      │    │         │    ├── grouping columns: phone_id:24
      │    │         │    ├── key: (24)
      │    │         │    └── scan phone_call [as=calls3_]
      │    │         │         └── columns: phone_id:24
      │    │         └── filters (true)
      │    └── filters (true)
      └── filters
           └── person1_.id:8 = addresses2_.person_id:16 [outer=(8,16), constraints=(/8: (/NULL - ]; /16: (/NULL - ]), fd=(8)==(16), (16)==(8)]

opt
select
    person0_.id as id1_4_0_,
    partner1_.id as id1_2_1_,
    person0_.address as address2_4_0_,
    person0_.createdOn as createdO3_4_0_,
    person0_.name as name4_4_0_,
    person0_.nickName as nickName5_4_0_,
    person0_.version as version6_4_0_,
    partner1_.name as name2_2_1_,
    partner1_.version as version3_2_1_
from
    Person person0_ cross
join
    Partner partner1_
where
    person0_.address=$1
    and (
        exists (
            select
                phones2_.id
            from
                Phone phones2_
            where
                person0_.id=phones2_.person_id
        )
    )
    and (
        partner1_.name like $2
    )
    and partner1_.version=0
----
inner-join (cross)
 ├── columns: id1_4_0_:1!null id1_2_1_:9!null address2_4_0_:2!null createdo3_4_0_:3 name4_4_0_:4 nickname5_4_0_:5 version6_4_0_:6!null name2_2_1_:10!null version3_2_1_:11!null
 ├── has-placeholder
 ├── key: (1,9)
 ├── fd: ()-->(2,11), (1)-->(3-6), (9)-->(10)
 ├── project
 │    ├── columns: person0_.id:1!null address:2!null createdon:3 person0_.name:4 nickname:5 person0_.version:6!null
 │    ├── has-placeholder
 │    ├── key: (1)
 │    ├── fd: ()-->(2), (1)-->(3-6)
 │    └── inner-join (lookup person [as=person0_])
 │         ├── columns: person0_.id:1!null address:2!null createdon:3 person0_.name:4 nickname:5 person0_.version:6!null person_id:17!null
 │         ├── key columns: [17] = [1]
 │         ├── lookup columns are key
 │         ├── has-placeholder
 │         ├── key: (17)
 │         ├── fd: ()-->(2), (1)-->(3-6), (1)==(17), (17)==(1)
 │         ├── distinct-on
 │         │    ├── columns: person_id:17
 │         │    ├── grouping columns: person_id:17
 │         │    ├── key: (17)
 │         │    └── scan phone [as=phones2_]
 │         │         └── columns: person_id:17
 │         └── filters
 │              └── address:2 = $1 [outer=(2), constraints=(/2: (/NULL - ]), fd=()-->(2)]
 ├── select
 │    ├── columns: partner1_.id:9!null partner1_.name:10!null partner1_.version:11!null
 │    ├── has-placeholder
 │    ├── key: (9)
 │    ├── fd: ()-->(11), (9)-->(10)
 │    ├── scan partner [as=partner1_]
 │    │    ├── columns: partner1_.id:9!null partner1_.name:10 partner1_.version:11!null
 │    │    ├── key: (9)
 │    │    └── fd: (9)-->(10,11)
 │    └── filters
 │         ├── partner1_.name:10 LIKE $2 [outer=(10), constraints=(/10: (/NULL - ])]
 │         └── partner1_.version:11 = 0 [outer=(11), constraints=(/11: [/0 - /0]; tight), fd=()-->(11)]
 └── filters (true)

exec-ddl
drop table Person, Person_addresses, Phone, phone_call, Partner;
----

# ------------------------------------------------------------------------------
# Query #3
#   org.hibernate.userguide.envers.DefaultAuditTest test
# ------------------------------------------------------------------------------
exec-ddl
create table Customer_AUD (
   id int8 not null,
    REV int4 not null,
    REVTYPE int2,
    created_on timestamp,
    firstName varchar(255),
    lastName varchar(255),
    primary key (id, REV)
)
----

opt
select
    defaultaud0_.id as id1_1_,
    defaultaud0_.REV as REV2_1_,
    defaultaud0_.REVTYPE as REVTYPE3_1_,
    defaultaud0_.created_on as created_4_1_,
    defaultaud0_.firstName as firstNam5_1_,
    defaultaud0_.lastName as lastName6_1_
from
    Customer_AUD defaultaud0_
where
    defaultaud0_.REV=(
        select
            max(defaultaud1_.REV)
        from
            Customer_AUD defaultaud1_
        where
            defaultaud1_.REV<=$1
            and defaultaud0_.id=defaultaud1_.id
    )
    and defaultaud0_.REVTYPE<>$2
----
project
 ├── columns: id1_1_:1!null rev2_1_:2!null revtype3_1_:3!null created_4_1_:4 firstnam5_1_:5 lastname6_1_:6
 ├── has-placeholder
 ├── key: (1,2)
 ├── fd: (1,2)-->(3-6)
 └── select
      ├── columns: defaultaud0_.id:1!null defaultaud0_.rev:2!null defaultaud0_.revtype:3!null defaultaud0_.created_on:4 defaultaud0_.firstname:5 defaultaud0_.lastname:6 max:17!null
      ├── has-placeholder
      ├── key: (1,2)
      ├── fd: (1,2)-->(3-6,17), (2)==(17), (17)==(2)
      ├── group-by (hash)
      │    ├── columns: defaultaud0_.id:1!null defaultaud0_.rev:2!null defaultaud0_.revtype:3!null defaultaud0_.created_on:4 defaultaud0_.firstname:5 defaultaud0_.lastname:6 max:17!null
      │    ├── grouping columns: defaultaud0_.id:1!null defaultaud0_.rev:2!null
      │    ├── has-placeholder
      │    ├── key: (1,2)
      │    ├── fd: (1,2)-->(3-6,17)
      │    ├── inner-join (merge)
      │    │    ├── columns: defaultaud0_.id:1!null defaultaud0_.rev:2!null defaultaud0_.revtype:3!null defaultaud0_.created_on:4 defaultaud0_.firstname:5 defaultaud0_.lastname:6 defaultaud1_.id:9!null defaultaud1_.rev:10!null
      │    │    ├── left ordering: +9
      │    │    ├── right ordering: +1
      │    │    ├── has-placeholder
      │    │    ├── key: (2,9,10)
      │    │    ├── fd: (1,2)-->(3-6), (1)==(9), (9)==(1)
      │    │    ├── select
      │    │    │    ├── columns: defaultaud1_.id:9!null defaultaud1_.rev:10!null
      │    │    │    ├── has-placeholder
      │    │    │    ├── key: (9,10)
      │    │    │    ├── ordering: +9
      │    │    │    ├── scan customer_aud [as=defaultaud1_]
      │    │    │    │    ├── columns: defaultaud1_.id:9!null defaultaud1_.rev:10!null
      │    │    │    │    ├── key: (9,10)
      │    │    │    │    └── ordering: +9
      │    │    │    └── filters
      │    │    │         └── defaultaud1_.rev:10 <= $1 [outer=(10), constraints=(/10: (/NULL - ])]
      │    │    ├── select
      │    │    │    ├── columns: defaultaud0_.id:1!null defaultaud0_.rev:2!null defaultaud0_.revtype:3!null defaultaud0_.created_on:4 defaultaud0_.firstname:5 defaultaud0_.lastname:6
      │    │    │    ├── has-placeholder
      │    │    │    ├── key: (1,2)
      │    │    │    ├── fd: (1,2)-->(3-6)
      │    │    │    ├── ordering: +1
      │    │    │    ├── scan customer_aud [as=defaultaud0_]
      │    │    │    │    ├── columns: defaultaud0_.id:1!null defaultaud0_.rev:2!null defaultaud0_.revtype:3 defaultaud0_.created_on:4 defaultaud0_.firstname:5 defaultaud0_.lastname:6
      │    │    │    │    ├── key: (1,2)
      │    │    │    │    ├── fd: (1,2)-->(3-6)
      │    │    │    │    └── ordering: +1
      │    │    │    └── filters
      │    │    │         └── defaultaud0_.revtype:3 != $2 [outer=(3), constraints=(/3: (/NULL - ])]
      │    │    └── filters (true)
      │    └── aggregations
      │         ├── max [as=max:17, outer=(10)]
      │         │    └── defaultaud1_.rev:10
      │         ├── const-agg [as=defaultaud0_.revtype:3, outer=(3)]
      │         │    └── defaultaud0_.revtype:3
      │         ├── const-agg [as=defaultaud0_.created_on:4, outer=(4)]
      │         │    └── defaultaud0_.created_on:4
      │         ├── const-agg [as=defaultaud0_.firstname:5, outer=(5)]
      │         │    └── defaultaud0_.firstname:5
      │         └── const-agg [as=defaultaud0_.lastname:6, outer=(6)]
      │              └── defaultaud0_.lastname:6
      └── filters
           └── defaultaud0_.rev:2 = max:17 [outer=(2,17), constraints=(/2: (/NULL - ]; /17: (/NULL - ]), fd=(2)==(17), (17)==(2)]

exec-ddl
drop table Customer_AUD;
----

# ------------------------------------------------------------------------------
# Query #4
#   org.hibernate.userguide.envers.QueryAuditTest test
# ------------------------------------------------------------------------------
exec-ddl
create table Customer_AUD (
   id int8 not null,
    REV int4 not null,
    REVTYPE int2,
    REVEND int4,
    created_on timestamp,
    firstName varchar(255),
    lastName varchar(255),
    address_id int8,
    primary key (id, REV)
)
----

opt
select
    queryaudit0_.id as id1_3_,
    queryaudit0_.REV as REV2_3_,
    queryaudit0_.REVTYPE as REVTYPE3_3_,
    queryaudit0_.REVEND as REVEND4_3_,
    queryaudit0_.created_on as created_5_3_,
    queryaudit0_.firstName as firstNam6_3_,
    queryaudit0_.lastName as lastName7_3_,
    queryaudit0_.address_id as address_8_3_
from
    Customer_AUD queryaudit0_
where
    queryaudit0_.REVTYPE<>$1
    and queryaudit0_.REV=(
        select
            max(queryaudit1_.REV)
        from
            Customer_AUD queryaudit1_
        where
            queryaudit1_.id=queryaudit0_.id
    )
order by
    queryaudit0_.REV asc
----
sort
 ├── columns: id1_3_:1!null rev2_3_:2!null revtype3_3_:3!null revend4_3_:4 created_5_3_:5 firstnam6_3_:6 lastname7_3_:7 address_8_3_:8
 ├── has-placeholder
 ├── key: (1,2)
 ├── fd: (1,2)-->(3-8)
 ├── ordering: +2
 └── project
      ├── columns: queryaudit0_.id:1!null queryaudit0_.rev:2!null queryaudit0_.revtype:3!null queryaudit0_.revend:4 queryaudit0_.created_on:5 queryaudit0_.firstname:6 queryaudit0_.lastname:7 queryaudit0_.address_id:8
      ├── has-placeholder
      ├── key: (1,2)
      ├── fd: (1,2)-->(3-8)
      └── select
           ├── columns: queryaudit0_.id:1!null queryaudit0_.rev:2!null queryaudit0_.revtype:3!null queryaudit0_.revend:4 queryaudit0_.created_on:5 queryaudit0_.firstname:6 queryaudit0_.lastname:7 queryaudit0_.address_id:8 max:21!null
           ├── has-placeholder
           ├── key: (1,2)
           ├── fd: (1,2)-->(3-8,21), (2)==(21), (21)==(2)
           ├── group-by (hash)
           │    ├── columns: queryaudit0_.id:1!null queryaudit0_.rev:2!null queryaudit0_.revtype:3!null queryaudit0_.revend:4 queryaudit0_.created_on:5 queryaudit0_.firstname:6 queryaudit0_.lastname:7 queryaudit0_.address_id:8 max:21!null
           │    ├── grouping columns: queryaudit0_.id:1!null queryaudit0_.rev:2!null
           │    ├── has-placeholder
           │    ├── key: (1,2)
           │    ├── fd: (1,2)-->(3-8,21)
           │    ├── inner-join (merge)
           │    │    ├── columns: queryaudit0_.id:1!null queryaudit0_.rev:2!null queryaudit0_.revtype:3!null queryaudit0_.revend:4 queryaudit0_.created_on:5 queryaudit0_.firstname:6 queryaudit0_.lastname:7 queryaudit0_.address_id:8 queryaudit1_.id:11!null queryaudit1_.rev:12!null
           │    │    ├── left ordering: +11
           │    │    ├── right ordering: +1
           │    │    ├── has-placeholder
           │    │    ├── key: (2,11,12)
           │    │    ├── fd: (1,2)-->(3-8), (1)==(11), (11)==(1)
           │    │    ├── scan customer_aud [as=queryaudit1_]
           │    │    │    ├── columns: queryaudit1_.id:11!null queryaudit1_.rev:12!null
           │    │    │    ├── key: (11,12)
           │    │    │    └── ordering: +11
           │    │    ├── select
           │    │    │    ├── columns: queryaudit0_.id:1!null queryaudit0_.rev:2!null queryaudit0_.revtype:3!null queryaudit0_.revend:4 queryaudit0_.created_on:5 queryaudit0_.firstname:6 queryaudit0_.lastname:7 queryaudit0_.address_id:8
           │    │    │    ├── has-placeholder
           │    │    │    ├── key: (1,2)
           │    │    │    ├── fd: (1,2)-->(3-8)
           │    │    │    ├── ordering: +1
           │    │    │    ├── scan customer_aud [as=queryaudit0_]
           │    │    │    │    ├── columns: queryaudit0_.id:1!null queryaudit0_.rev:2!null queryaudit0_.revtype:3 queryaudit0_.revend:4 queryaudit0_.created_on:5 queryaudit0_.firstname:6 queryaudit0_.lastname:7 queryaudit0_.address_id:8
           │    │    │    │    ├── key: (1,2)
           │    │    │    │    ├── fd: (1,2)-->(3-8)
           │    │    │    │    └── ordering: +1
           │    │    │    └── filters
           │    │    │         └── queryaudit0_.revtype:3 != $1 [outer=(3), constraints=(/3: (/NULL - ])]
           │    │    └── filters (true)
           │    └── aggregations
           │         ├── max [as=max:21, outer=(12)]
           │         │    └── queryaudit1_.rev:12
           │         ├── const-agg [as=queryaudit0_.revtype:3, outer=(3)]
           │         │    └── queryaudit0_.revtype:3
           │         ├── const-agg [as=queryaudit0_.revend:4, outer=(4)]
           │         │    └── queryaudit0_.revend:4
           │         ├── const-agg [as=queryaudit0_.created_on:5, outer=(5)]
           │         │    └── queryaudit0_.created_on:5
           │         ├── const-agg [as=queryaudit0_.firstname:6, outer=(6)]
           │         │    └── queryaudit0_.firstname:6
           │         ├── const-agg [as=queryaudit0_.lastname:7, outer=(7)]
           │         │    └── queryaudit0_.lastname:7
           │         └── const-agg [as=queryaudit0_.address_id:8, outer=(8)]
           │              └── queryaudit0_.address_id:8
           └── filters
                └── queryaudit0_.rev:2 = max:21 [outer=(2,21), constraints=(/2: (/NULL - ]; /21: (/NULL - ]), fd=(2)==(21), (21)==(2)]

exec-ddl
drop table Customer_AUD;
----

# ------------------------------------------------------------------------------
# Query #5
#   org.hibernate.userguide.hql.HQLTest
#     test_hql_all_subquery_comparison_qualifier_example
#     test_hql_collection_expressions_example_1
#     test_hql_collection_expressions_example_10
#     test_hql_collection_expressions_example_2
#     test_hql_collection_expressions_example_3
#     test_hql_collection_expressions_example_4
#     test_hql_collection_expressions_example_5
#     test_hql_collection_expressions_example_6
#     test_hql_collection_expressions_example_8
#     test_hql_collection_expressions_example_9
#     test_hql_collection_index_operator_example_3
#     test_hql_empty_collection_predicate_example_1
#     test_hql_empty_collection_predicate_example_2
#     test_hql_group_by_example_4
#     test_hql_member_of_collection_predicate_example_1
#     test_hql_member_of_collection_predicate_example_2
#   org.hibernate.jpa.test.criteria.enumcollection.EnumIsMemberTest
#     testQueryEnumCollection
# ------------------------------------------------------------------------------
exec-ddl
create table Phone (
   id int8 not null,
    phone_number varchar(255),
    phone_type varchar(255),
    person_id int8,
    order_id int4,
    primary key (id)
)
----

exec-ddl
create table phone_call (
   id int8 not null,
    duration int4 not null,
    call_timestamp timestamp,
    phone_id int8,
    primary key (id)
)
----

exec-ddl
create table Person (
   id int8 not null,
    address varchar(255),
    createdOn timestamp,
    name varchar(255),
    nickName varchar(255),
    version int4 not null,
    primary key (id)
)
----

exec-ddl
create table Phone_repairTimestamps (
   Phone_id int8 not null,
   repairTimestamps timestamp
)
----

exec-ddl
create table Person_addresses (
    Person_id int8 not null,
    addresses varchar(255),
    addresses_KEY varchar(255) not null,
    primary key (Person_id, addresses_KEY)
)
----

opt
select
    distinct person2_.id as id1_2_,
    person2_.address as address2_2_,
    person2_.createdOn as createdO3_2_,
    person2_.name as name4_2_,
    person2_.nickName as nickName5_2_,
    person2_.version as version6_2_
from
    Phone phone0_
inner join
    phone_call calls1_
        on phone0_.id=calls1_.phone_id
inner join
    Person person2_
        on phone0_.person_id=person2_.id
where
    50>all (
        select
            call3_.duration
        from
            phone_call call3_
        where
            call3_.phone_id=phone0_.id
    )
----
distinct-on
 ├── columns: id1_2_:14!null address2_2_:15 createdo3_2_:16 name4_2_:17 nickname5_2_:18 version6_2_:19!null
 ├── grouping columns: person2_.id:14!null
 ├── key: (14)
 ├── fd: (14)-->(15-19)
 ├── inner-join (hash)
 │    ├── columns: phone0_.id:1!null person_id:4!null calls1_.phone_id:11!null person2_.id:14!null address:15 createdon:16 name:17 nickname:18 version:19!null
 │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
 │    ├── fd: (1)-->(4), (14)-->(15-19), (1)==(11), (11)==(1), (4)==(14), (14)==(4)
 │    ├── scan phone_call [as=calls1_]
 │    │    └── columns: calls1_.phone_id:11
 │    ├── inner-join (hash)
 │    │    ├── columns: phone0_.id:1!null person_id:4!null person2_.id:14!null address:15 createdon:16 name:17 nickname:18 version:19!null
 │    │    ├── multiplicity: left-rows(zero-or-more), right-rows(zero-or-one)
 │    │    ├── key: (1)
 │    │    ├── fd: (1)-->(4), (14)-->(15-19), (4)==(14), (14)==(4)
 │    │    ├── scan person [as=person2_]
 │    │    │    ├── columns: person2_.id:14!null address:15 createdon:16 name:17 nickname:18 version:19!null
 │    │    │    ├── key: (14)
 │    │    │    └── fd: (14)-->(15-19)
 │    │    ├── anti-join (hash)
 │    │    │    ├── columns: phone0_.id:1!null person_id:4
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: (1)-->(4)
 │    │    │    ├── scan phone [as=phone0_]
 │    │    │    │    ├── columns: phone0_.id:1!null person_id:4
 │    │    │    │    ├── key: (1)
 │    │    │    │    └── fd: (1)-->(4)
 │    │    │    ├── select
 │    │    │    │    ├── columns: call3_.duration:23!null call3_.phone_id:25
 │    │    │    │    ├── scan phone_call [as=call3_]
 │    │    │    │    │    └── columns: call3_.duration:23!null call3_.phone_id:25
 │    │    │    │    └── filters
 │    │    │    │         └── (call3_.duration:23 >= 50) IS NOT false [outer=(23)]
 │    │    │    └── filters
 │    │    │         └── call3_.phone_id:25 = phone0_.id:1 [outer=(1,25), constraints=(/1: (/NULL - ]; /25: (/NULL - ]), fd=(1)==(25), (25)==(1)]
 │    │    └── filters
 │    │         └── person_id:4 = person2_.id:14 [outer=(4,14), constraints=(/4: (/NULL - ]; /14: (/NULL - ]), fd=(4)==(14), (14)==(4)]
 │    └── filters
 │         └── phone0_.id:1 = calls1_.phone_id:11 [outer=(1,11), constraints=(/1: (/NULL - ]; /11: (/NULL - ]), fd=(1)==(11), (11)==(1)]
 └── aggregations
      ├── const-agg [as=address:15, outer=(15)]
      │    └── address:15
      ├── const-agg [as=createdon:16, outer=(16)]
      │    └── createdon:16
      ├── const-agg [as=name:17, outer=(17)]
      │    └── name:17
      ├── const-agg [as=nickname:18, outer=(18)]
      │    └── nickname:18
      └── const-agg [as=version:19, outer=(19)]
           └── version:19

opt
select
    phone0_.id as id1_4_,
    phone0_.phone_number as phone_nu2_4_,
    phone0_.person_id as person_i4_4_,
    phone0_.phone_type as phone_ty3_4_
from
    Phone phone0_
where
    (
        select
            max(calls1_.id)
        from
            phone_call calls1_
        where
            phone0_.id=calls1_.phone_id
    )=$1
----
project
 ├── columns: id1_4_:1!null phone_nu2_4_:2 person_i4_4_:4 phone_ty3_4_:3
 ├── has-placeholder
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 └── select
      ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4 max:14!null
      ├── has-placeholder
      ├── key: (1)
      ├── fd: ()-->(14), (1)-->(2-4)
      ├── group-by (hash)
      │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4 max:14!null
      │    ├── grouping columns: phone0_.id:1!null
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4,14)
      │    ├── inner-join (hash)
      │    │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4 calls1_.id:8!null phone_id:11!null
      │    │    ├── multiplicity: left-rows(zero-or-more), right-rows(zero-or-one)
      │    │    ├── key: (8)
      │    │    ├── fd: (1)-->(2-4), (8)-->(11), (1)==(11), (11)==(1)
      │    │    ├── scan phone [as=phone0_]
      │    │    │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4
      │    │    │    ├── key: (1)
      │    │    │    └── fd: (1)-->(2-4)
      │    │    ├── scan phone_call [as=calls1_]
      │    │    │    ├── columns: calls1_.id:8!null phone_id:11
      │    │    │    ├── key: (8)
      │    │    │    └── fd: (8)-->(11)
      │    │    └── filters
      │    │         └── phone0_.id:1 = phone_id:11 [outer=(1,11), constraints=(/1: (/NULL - ]; /11: (/NULL - ]), fd=(1)==(11), (11)==(1)]
      │    └── aggregations
      │         ├── max [as=max:14, outer=(8)]
      │         │    └── calls1_.id:8
      │         ├── const-agg [as=phone_number:2, outer=(2)]
      │         │    └── phone_number:2
      │         ├── const-agg [as=phone_type:3, outer=(3)]
      │         │    └── phone_type:3
      │         └── const-agg [as=person_id:4, outer=(4)]
      │              └── person_id:4
      └── filters
           └── max:14 = $1 [outer=(14), constraints=(/14: (/NULL - ]), fd=()-->(14)]

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    (
        select
            count(phones1_.person_id)
        from
            Phone phones1_
        where
            person0_.id=phones1_.person_id
    )=2
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 └── select
      ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null count:16!null
      ├── key: (1)
      ├── fd: ()-->(16), (1)-->(2-6)
      ├── group-by (hash)
      │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null count:16!null
      │    ├── grouping columns: person0_.id:1!null
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-6,16)
      │    ├── left-join (hash)
      │    │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null person_id:12
      │    │    ├── multiplicity: left-rows(one-or-more), right-rows(zero-or-one)
      │    │    ├── fd: (1)-->(2-6)
      │    │    ├── scan person [as=person0_]
      │    │    │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null
      │    │    │    ├── key: (1)
      │    │    │    └── fd: (1)-->(2-6)
      │    │    ├── scan phone [as=phones1_]
      │    │    │    └── columns: person_id:12
      │    │    └── filters
      │    │         └── person0_.id:1 = person_id:12 [outer=(1,12), constraints=(/1: (/NULL - ]; /12: (/NULL - ]), fd=(1)==(12), (12)==(1)]
      │    └── aggregations
      │         ├── count [as=count:16, outer=(12)]
      │         │    └── person_id:12
      │         ├── const-agg [as=address:2, outer=(2)]
      │         │    └── address:2
      │         ├── const-agg [as=createdon:3, outer=(3)]
      │         │    └── createdon:3
      │         ├── const-agg [as=name:4, outer=(4)]
      │         │    └── name:4
      │         ├── const-agg [as=nickname:5, outer=(5)]
      │         │    └── nickname:5
      │         └── const-agg [as=version:6, outer=(6)]
      │              └── version:6
      └── filters
           └── count:16 = 2 [outer=(16), constraints=(/16: [/2 - /2]; tight), fd=()-->(16)]

opt
select
    phone0_.id as id1_4_,
    phone0_.phone_number as phone_nu2_4_,
    phone0_.person_id as person_i4_4_,
    phone0_.phone_type as phone_ty3_4_
from
    Phone phone0_
where
    (
        select
            min(calls1_.id)
        from
            phone_call calls1_
        where
            phone0_.id=calls1_.phone_id
    )=$1
----
project
 ├── columns: id1_4_:1!null phone_nu2_4_:2 person_i4_4_:4 phone_ty3_4_:3
 ├── has-placeholder
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 └── select
      ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4 min:14!null
      ├── has-placeholder
      ├── key: (1)
      ├── fd: ()-->(14), (1)-->(2-4)
      ├── group-by (hash)
      │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4 min:14!null
      │    ├── grouping columns: phone0_.id:1!null
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-4,14)
      │    ├── inner-join (hash)
      │    │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4 calls1_.id:8!null phone_id:11!null
      │    │    ├── multiplicity: left-rows(zero-or-more), right-rows(zero-or-one)
      │    │    ├── key: (8)
      │    │    ├── fd: (1)-->(2-4), (8)-->(11), (1)==(11), (11)==(1)
      │    │    ├── scan phone [as=phone0_]
      │    │    │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4
      │    │    │    ├── key: (1)
      │    │    │    └── fd: (1)-->(2-4)
      │    │    ├── scan phone_call [as=calls1_]
      │    │    │    ├── columns: calls1_.id:8!null phone_id:11
      │    │    │    ├── key: (8)
      │    │    │    └── fd: (8)-->(11)
      │    │    └── filters
      │    │         └── phone0_.id:1 = phone_id:11 [outer=(1,11), constraints=(/1: (/NULL - ]; /11: (/NULL - ]), fd=(1)==(11), (11)==(1)]
      │    └── aggregations
      │         ├── min [as=min:14, outer=(8)]
      │         │    └── calls1_.id:8
      │         ├── const-agg [as=phone_number:2, outer=(2)]
      │         │    └── phone_number:2
      │         ├── const-agg [as=phone_type:3, outer=(3)]
      │         │    └── phone_type:3
      │         └── const-agg [as=person_id:4, outer=(4)]
      │              └── person_id:4
      └── filters
           └── min:14 = $1 [outer=(14), constraints=(/14: (/NULL - ]), fd=()-->(14)]

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    (
        select
            max(phones1_.order_id)
        from
            Phone phones1_
        where
            person0_.id=phones1_.person_id
    )=0
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 └── select
      ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null max:16!null
      ├── key: (1)
      ├── fd: ()-->(16), (1)-->(2-6)
      ├── group-by (hash)
      │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null max:16!null
      │    ├── grouping columns: person0_.id:1!null
      │    ├── key: (1)
      │    ├── fd: (1)-->(2-6,16)
      │    ├── inner-join (hash)
      │    │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null person_id:12!null order_id:13!null
      │    │    ├── multiplicity: left-rows(zero-or-more), right-rows(zero-or-one)
      │    │    ├── fd: (1)-->(2-6), (1)==(12), (12)==(1)
      │    │    ├── scan person [as=person0_]
      │    │    │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null
      │    │    │    ├── key: (1)
      │    │    │    └── fd: (1)-->(2-6)
      │    │    ├── select
      │    │    │    ├── columns: person_id:12 order_id:13!null
      │    │    │    ├── scan phone [as=phones1_]
      │    │    │    │    └── columns: person_id:12 order_id:13
      │    │    │    └── filters
      │    │    │         └── order_id:13 IS NOT NULL [outer=(13), constraints=(/13: (/NULL - ]; tight)]
      │    │    └── filters
      │    │         └── person0_.id:1 = person_id:12 [outer=(1,12), constraints=(/1: (/NULL - ]; /12: (/NULL - ]), fd=(1)==(12), (12)==(1)]
      │    └── aggregations
      │         ├── max [as=max:16, outer=(13)]
      │         │    └── order_id:13
      │         ├── const-agg [as=address:2, outer=(2)]
      │         │    └── address:2
      │         ├── const-agg [as=createdon:3, outer=(3)]
      │         │    └── createdon:3
      │         ├── const-agg [as=name:4, outer=(4)]
      │         │    └── name:4
      │         ├── const-agg [as=nickname:5, outer=(5)]
      │         │    └── nickname:5
      │         └── const-agg [as=version:6, outer=(6)]
      │              └── version:6
      └── filters
           └── max:16 = 0 [outer=(16), constraints=(/16: [/0 - /0]; tight), fd=()-->(16)]

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    $1::int in (
        select
            phones1_.id
        from
            Phone phones1_
        where
            person0_.id=phones1_.person_id
    )
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── cardinality: [0 - 1]
 ├── has-placeholder
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 └── project
      ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null person_id:12!null
      ├── cardinality: [0 - 1]
      ├── has-placeholder
      ├── key: ()
      ├── fd: ()-->(1-6,12), (1)==(12), (12)==(1)
      └── inner-join (lookup person [as=person0_])
           ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null phones1_.id:9!null person_id:12!null
           ├── key columns: [12] = [1]
           ├── lookup columns are key
           ├── cardinality: [0 - 1]
           ├── has-placeholder
           ├── key: ()
           ├── fd: ()-->(1-6,9,12), (1)==(12), (12)==(1)
           ├── project
           │    ├── columns: phones1_.id:9!null person_id:12
           │    ├── cardinality: [0 - 1]
           │    ├── has-placeholder
           │    ├── key: ()
           │    ├── fd: ()-->(9,12)
           │    └── inner-join (lookup phone [as=phones1_])
           │         ├── columns: phones1_.id:9!null person_id:12 "$1":17!null
           │         ├── flags: disallow merge join
           │         ├── key columns: [17] = [9]
           │         ├── lookup columns are key
           │         ├── cardinality: [0 - 1]
           │         ├── has-placeholder
           │         ├── key: ()
           │         ├── fd: ()-->(9,12,17), (9)==(17), (17)==(9)
           │         ├── values
           │         │    ├── columns: "$1":17
           │         │    ├── cardinality: [1 - 1]
           │         │    ├── has-placeholder
           │         │    ├── key: ()
           │         │    ├── fd: ()-->(17)
           │         │    └── ($1,)
           │         └── filters (true)
           └── filters (true)

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    $1::int=some (
        select
            phones1_.id
        from
            Phone phones1_
        where
            person0_.id=phones1_.person_id
    )
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── cardinality: [0 - 1]
 ├── has-placeholder
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 └── project
      ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null person_id:12!null
      ├── cardinality: [0 - 1]
      ├── has-placeholder
      ├── key: ()
      ├── fd: ()-->(1-6,12), (1)==(12), (12)==(1)
      └── inner-join (lookup person [as=person0_])
           ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null phones1_.id:9!null person_id:12!null
           ├── key columns: [12] = [1]
           ├── lookup columns are key
           ├── cardinality: [0 - 1]
           ├── has-placeholder
           ├── key: ()
           ├── fd: ()-->(1-6,9,12), (1)==(12), (12)==(1)
           ├── project
           │    ├── columns: phones1_.id:9!null person_id:12
           │    ├── cardinality: [0 - 1]
           │    ├── has-placeholder
           │    ├── key: ()
           │    ├── fd: ()-->(9,12)
           │    └── inner-join (lookup phone [as=phones1_])
           │         ├── columns: phones1_.id:9!null person_id:12 "$1":17!null
           │         ├── flags: disallow merge join
           │         ├── key columns: [17] = [9]
           │         ├── lookup columns are key
           │         ├── cardinality: [0 - 1]
           │         ├── has-placeholder
           │         ├── key: ()
           │         ├── fd: ()-->(9,12,17), (9)==(17), (17)==(9)
           │         ├── values
           │         │    ├── columns: "$1":17
           │         │    ├── cardinality: [1 - 1]
           │         │    ├── has-placeholder
           │         │    ├── key: ()
           │         │    ├── fd: ()-->(17)
           │         │    └── ($1,)
           │         └── filters (true)
           └── filters (true)

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    exists (
        select
            phones1_.id
        from
            Phone phones1_
        where
            person0_.id=phones1_.person_id
    )
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 └── inner-join (lookup person [as=person0_])
      ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null person_id:12!null
      ├── key columns: [12] = [1]
      ├── lookup columns are key
      ├── key: (12)
      ├── fd: (1)-->(2-6), (1)==(12), (12)==(1)
      ├── distinct-on
      │    ├── columns: person_id:12
      │    ├── grouping columns: person_id:12
      │    ├── key: (12)
      │    └── scan phone [as=phones1_]
      │         └── columns: person_id:12
      └── filters (true)

opt
select
    phone0_.id as id1_4_,
    phone0_.phone_number as phone_nu2_4_,
    phone0_.person_id as person_i4_4_,
    phone0_.phone_type as phone_ty3_4_
from
    Phone phone0_
where
    $1::date>all (
        select
            repairtime1_.repairTimestamps
        from
            Phone_repairTimestamps repairtime1_
        where
            phone0_.id=repairtime1_.Phone_id
    )
----
anti-join (hash)
 ├── columns: id1_4_:1!null phone_nu2_4_:2 person_i4_4_:4 phone_ty3_4_:3
 ├── immutable, has-placeholder
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── scan phone [as=phone0_]
 │    ├── columns: id:1!null phone_number:2 phone_type:3 person_id:4
 │    ├── key: (1)
 │    └── fd: (1)-->(2-4)
 ├── select
 │    ├── columns: phone_id:8!null repairtimestamps:9
 │    ├── immutable, has-placeholder
 │    ├── scan phone_repairtimestamps [as=repairtime1_]
 │    │    └── columns: phone_id:8!null repairtimestamps:9
 │    └── filters
 │         └── (repairtimestamps:9 >= $1) IS NOT false [outer=(9), immutable]
 └── filters
      └── id:1 = phone_id:8 [outer=(1,8), constraints=(/1: (/NULL - ]; /8: (/NULL - ]), fd=(1)==(8), (8)==(1)]

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    1 in (
        select
            phones1_.order_id
        from
            Phone phones1_
        where
            person0_.id=phones1_.person_id
    )
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 └── inner-join (lookup person [as=person0_])
      ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null person_id:12!null
      ├── key columns: [12] = [1]
      ├── lookup columns are key
      ├── key: (12)
      ├── fd: (1)-->(2-6), (1)==(12), (12)==(1)
      ├── distinct-on
      │    ├── columns: person_id:12
      │    ├── grouping columns: person_id:12
      │    ├── key: (12)
      │    └── select
      │         ├── columns: person_id:12 order_id:13!null
      │         ├── fd: ()-->(13)
      │         ├── scan phone [as=phones1_]
      │         │    └── columns: person_id:12 order_id:13
      │         └── filters
      │              └── order_id:13 = 1 [outer=(13), constraints=(/13: [/1 - /1]; tight), fd=()-->(13)]
      └── filters (true)

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_ cross
join
    Phone phones2_
where
    person0_.id=phones2_.person_id
    and phones2_.order_id = (
        select
            max(phones1_.order_id)
        from
            Phone phones1_
        where
            person0_.id=phones1_.person_id
    )
    and phones2_.phone_type='LAND_LINE'
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── fd: (1)-->(2-6)
 └── select
      ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null phones2_.id:9!null phones2_.order_id:13!null max:23!null
      ├── key: (9)
      ├── fd: (1)-->(2-6), (9)-->(1-6,13,23), (13)==(23), (23)==(13)
      ├── group-by (hash)
      │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null phones2_.id:9!null phones2_.order_id:13 max:23!null
      │    ├── grouping columns: phones2_.id:9!null
      │    ├── key: (9)
      │    ├── fd: (1)-->(2-6), (9)-->(1-6,13,23)
      │    ├── inner-join (hash)
      │    │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null phones2_.id:9!null phones2_.phone_type:11!null phones2_.person_id:12!null phones2_.order_id:13 phones1_.person_id:19!null phones1_.order_id:20!null
      │    │    ├── fd: ()-->(11), (1)-->(2-6), (9)-->(12,13), (1)==(12,19), (12)==(1,19), (19)==(1,12)
      │    │    ├── select
      │    │    │    ├── columns: phones1_.person_id:19 phones1_.order_id:20!null
      │    │    │    ├── scan phone [as=phones1_]
      │    │    │    │    └── columns: phones1_.person_id:19 phones1_.order_id:20
      │    │    │    └── filters
      │    │    │         └── phones1_.order_id:20 IS NOT NULL [outer=(20), constraints=(/20: (/NULL - ]; tight)]
      │    │    ├── inner-join (lookup person [as=person0_])
      │    │    │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null phones2_.id:9!null phones2_.phone_type:11!null phones2_.person_id:12!null phones2_.order_id:13
      │    │    │    ├── key columns: [12] = [1]
      │    │    │    ├── lookup columns are key
      │    │    │    ├── key: (9)
      │    │    │    ├── fd: ()-->(11), (1)-->(2-6), (9)-->(12,13), (1)==(12), (12)==(1)
      │    │    │    ├── select
      │    │    │    │    ├── columns: phones2_.id:9!null phones2_.phone_type:11!null phones2_.person_id:12 phones2_.order_id:13
      │    │    │    │    ├── key: (9)
      │    │    │    │    ├── fd: ()-->(11), (9)-->(12,13)
      │    │    │    │    ├── scan phone [as=phones2_]
      │    │    │    │    │    ├── columns: phones2_.id:9!null phones2_.phone_type:11 phones2_.person_id:12 phones2_.order_id:13
      │    │    │    │    │    ├── key: (9)
      │    │    │    │    │    └── fd: (9)-->(11-13)
      │    │    │    │    └── filters
      │    │    │    │         └── phones2_.phone_type:11 = 'LAND_LINE' [outer=(11), constraints=(/11: [/'LAND_LINE' - /'LAND_LINE']; tight), fd=()-->(11)]
      │    │    │    └── filters (true)
      │    │    └── filters
      │    │         └── person0_.id:1 = phones1_.person_id:19 [outer=(1,19), constraints=(/1: (/NULL - ]; /19: (/NULL - ]), fd=(1)==(19), (19)==(1)]
      │    └── aggregations
      │         ├── max [as=max:23, outer=(20)]
      │         │    └── phones1_.order_id:20
      │         ├── const-agg [as=phones2_.order_id:13, outer=(13)]
      │         │    └── phones2_.order_id:13
      │         ├── const-agg [as=address:2, outer=(2)]
      │         │    └── address:2
      │         ├── const-agg [as=createdon:3, outer=(3)]
      │         │    └── createdon:3
      │         ├── const-agg [as=name:4, outer=(4)]
      │         │    └── name:4
      │         ├── const-agg [as=nickname:5, outer=(5)]
      │         │    └── nickname:5
      │         ├── const-agg [as=version:6, outer=(6)]
      │         │    └── version:6
      │         └── const-agg [as=person0_.id:1, outer=(1)]
      │              └── person0_.id:1
      └── filters
           └── phones2_.order_id:13 = max:23 [outer=(13,23), constraints=(/13: (/NULL - ]; /23: (/NULL - ]), fd=(13)==(23), (23)==(13)]

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    not (exists (select
        phones1_.id
    from
        Phone phones1_
    where
        person0_.id=phones1_.person_id))
----
anti-join (hash)
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 ├── scan person [as=person0_]
 │    ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null
 │    ├── key: (1)
 │    └── fd: (1)-->(2-6)
 ├── scan phone [as=phones1_]
 │    └── columns: person_id:12
 └── filters
      └── person0_.id:1 = person_id:12 [outer=(1,12), constraints=(/1: (/NULL - ]; /12: (/NULL - ]), fd=(1)==(12), (12)==(1)]

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    exists (
        select
            phones1_.id
        from
            Phone phones1_
        where
            person0_.id=phones1_.person_id
    )
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 └── inner-join (lookup person [as=person0_])
      ├── columns: person0_.id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null person_id:12!null
      ├── key columns: [12] = [1]
      ├── lookup columns are key
      ├── key: (12)
      ├── fd: (1)-->(2-6), (1)==(12), (12)==(1)
      ├── distinct-on
      │    ├── columns: person_id:12
      │    ├── grouping columns: person_id:12
      │    ├── key: (12)
      │    └── scan phone [as=phones1_]
      │         └── columns: person_id:12
      └── filters (true)

opt
select
    phone0_.id as id1_4_,
    phone0_.phone_number as phone_nu2_4_,
    phone0_.person_id as person_i4_4_,
    phone0_.phone_type as phone_ty3_4_
from
    Phone phone0_
where
    not (exists (select
        calls1_.id
    from
        phone_call calls1_
    where
        phone0_.id=calls1_.phone_id))
----
anti-join (hash)
 ├── columns: id1_4_:1!null phone_nu2_4_:2 person_i4_4_:4 phone_ty3_4_:3
 ├── key: (1)
 ├── fd: (1)-->(2-4)
 ├── scan phone [as=phone0_]
 │    ├── columns: phone0_.id:1!null phone_number:2 phone_type:3 person_id:4
 │    ├── key: (1)
 │    └── fd: (1)-->(2-4)
 ├── scan phone_call [as=calls1_]
 │    └── columns: phone_id:11
 └── filters
      └── phone0_.id:1 = phone_id:11 [outer=(1,11), constraints=(/1: (/NULL - ]; /11: (/NULL - ]), fd=(1)==(11), (11)==(1)]

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    'Home address' in (
        select
            addresses1_.addresses
        from
            Person_addresses addresses1_
        where
            person0_.id=addresses1_.Person_id
    )
----
project
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 └── inner-join (lookup person [as=person0_])
      ├── columns: id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null person_id:9!null
      ├── key columns: [9] = [1]
      ├── lookup columns are key
      ├── key: (9)
      ├── fd: (1)-->(2-6), (1)==(9), (9)==(1)
      ├── distinct-on
      │    ├── columns: person_id:9!null
      │    ├── grouping columns: person_id:9!null
      │    ├── internal-ordering: +9 opt(10)
      │    ├── key: (9)
      │    └── select
      │         ├── columns: person_id:9!null addresses:10!null
      │         ├── fd: ()-->(10)
      │         ├── ordering: +9 opt(10) [actual: +9]
      │         ├── scan person_addresses [as=addresses1_]
      │         │    ├── columns: person_id:9!null addresses:10
      │         │    └── ordering: +9
      │         └── filters
      │              └── addresses:10 = 'Home address' [outer=(10), constraints=(/10: [/'Home address' - /'Home address']; tight), fd=()-->(10)]
      └── filters (true)

opt
select
    person0_.id as id1_2_,
    person0_.address as address2_2_,
    person0_.createdOn as createdO3_2_,
    person0_.name as name4_2_,
    person0_.nickName as nickName5_2_,
    person0_.version as version6_2_
from
    Person person0_
where
    'Home address' not in  (
        select
            addresses1_.addresses
        from
            Person_addresses addresses1_
        where
            person0_.id=addresses1_.Person_id
    )
----
anti-join (merge)
 ├── columns: id1_2_:1!null address2_2_:2 createdo3_2_:3 name4_2_:4 nickname5_2_:5 version6_2_:6!null
 ├── left ordering: +1
 ├── right ordering: +9
 ├── key: (1)
 ├── fd: (1)-->(2-6)
 ├── scan person [as=person0_]
 │    ├── columns: id:1!null address:2 createdon:3 name:4 nickname:5 version:6!null
 │    ├── key: (1)
 │    ├── fd: (1)-->(2-6)
 │    └── ordering: +1
 ├── select
 │    ├── columns: person_id:9!null addresses:10
 │    ├── ordering: +9
 │    ├── scan person_addresses [as=addresses1_]
 │    │    ├── columns: person_id:9!null addresses:10
 │    │    └── ordering: +9
 │    └── filters
 │         └── (addresses:10 = 'Home address') IS NOT false [outer=(10)]
 └── filters (true)

exec-ddl
drop table Phone, phone_call, Person, Phone_repairTimestamps, Person_addresses;
----

# ------------------------------------------------------------------------------
# Query #6
# ------------------------------------------------------------------------------
exec-ddl
create table EMPLOYEE (
   id int8 not null,
    email varchar(255),
    currentProject_id int8,
    primary key (id)
)
----

exec-ddl
create table Employee_phones (
   Employee_id int8 not null,
    phone_number varchar(255)
)
----

opt
select
    componenti0_.id as id1_0_,
    componenti0_.email as email2_0_,
    componenti0_.currentProject_id as currentP3_0_
from
    EMPLOYEE componenti0_
where
    (
        select
            count(phones1_.Employee_id)
        from
            Employee_phones phones1_
        where
            componenti0_.id=phones1_.Employee_id
    )=1
----
project
 ├── columns: id1_0_:1!null email2_0_:2 currentp3_0_:3
 ├── key: (1)
 ├── fd: (1)-->(2,3)
 └── select
      ├── columns: id:1!null email:2 currentproject_id:3 count:11!null
      ├── key: (1)
      ├── fd: ()-->(11), (1)-->(2,3)
      ├── group-by (hash)
      │    ├── columns: id:1!null email:2 currentproject_id:3 count:11!null
      │    ├── grouping columns: id:1!null
      │    ├── key: (1)
      │    ├── fd: (1)-->(2,3,11)
      │    ├── left-join (hash)
      │    │    ├── columns: id:1!null email:2 currentproject_id:3 employee_id:6
      │    │    ├── multiplicity: left-rows(one-or-more), right-rows(zero-or-one)
      │    │    ├── fd: (1)-->(2,3)
      │    │    ├── scan employee [as=componenti0_]
      │    │    │    ├── columns: id:1!null email:2 currentproject_id:3
      │    │    │    ├── key: (1)
      │    │    │    └── fd: (1)-->(2,3)
      │    │    ├── scan employee_phones [as=phones1_]
      │    │    │    └── columns: employee_id:6!null
      │    │    └── filters
      │    │         └── id:1 = employee_id:6 [outer=(1,6), constraints=(/1: (/NULL - ]; /6: (/NULL - ]), fd=(1)==(6), (6)==(1)]
      │    └── aggregations
      │         ├── count [as=count:11, outer=(6)]
      │         │    └── employee_id:6
      │         ├── const-agg [as=email:2, outer=(2)]
      │         │    └── email:2
      │         └── const-agg [as=currentproject_id:3, outer=(3)]
      │              └── currentproject_id:3
      └── filters
           └── count:11 = 1 [outer=(11), constraints=(/11: [/1 - /1]; tight), fd=()-->(11)]

exec-ddl
drop table EMPLOYEE, Employee_phones;
----

# ------------------------------------------------------------------------------
# Query #7
# ------------------------------------------------------------------------------
exec-ddl
create table Company (
   id int8 not null,
    location_id int8,
    primary key (id)
)
----

exec-ddl
create table Company_Employee (
   Company_id int8 not null,
    employees_id int8 not null,
    primary key (Company_id, employees_id)
)
----

exec-ddl
create table Employee (
   id int8 not null,
    primary key (id)
)
----

exec-ddl
create table Manager (
   id int8 not null,
    primary key (id)
)
----

exec-ddl
create table Location (
   id int8 not null,
    address varchar(255),
    zip int4 not null,
    primary key (id)
)
----

opt
select
    company0_.id as id1_0_0_,
    location3_.id as id1_8_1_,
    company0_.location_id as location2_0_0_,
    location3_.address as address2_8_1_,
    location3_.zip as zip3_8_1_
from
    Company company0_
left outer join
    Location location3_
        on company0_.location_id=location3_.id
where
    not (exists (select
        employee2_.id
    from
        Company_Employee employees1_,
        ( select
            id,
            0 as clazz_
        from
            Employee
        union
        all select
            id,
            1 as clazz_
        from
            Manager ) employee2_
    where
        company0_.id=employees1_.Company_id
        and employees1_.employees_id=employee2_.id))
----
right-join (hash)
 ├── columns: id1_0_0_:1!null id1_8_1_:5 location2_0_0_:2 address2_8_1_:6 zip3_8_1_:7
 ├── key: (1)
 ├── fd: (1)-->(2,5-7), (5)-->(6,7)
 ├── scan location [as=location3_]
 │    ├── columns: location3_.id:5!null address:6 zip:7!null
 │    ├── key: (5)
 │    └── fd: (5)-->(6,7)
 ├── anti-join (hash)
 │    ├── columns: company0_.id:1!null location_id:2
 │    ├── key: (1)
 │    ├── fd: (1)-->(2)
 │    ├── scan company [as=company0_]
 │    │    ├── columns: company0_.id:1!null location_id:2
 │    │    ├── key: (1)
 │    │    └── fd: (1)-->(2)
 │    ├── inner-join (hash)
 │    │    ├── columns: company_id:10!null employees_id:11!null id:22!null
 │    │    ├── fd: (11)==(22), (22)==(11)
 │    │    ├── union-all
 │    │    │    ├── columns: id:22!null
 │    │    │    ├── left columns: employee.id:14
 │    │    │    ├── right columns: manager.id:18
 │    │    │    ├── scan employee
 │    │    │    │    ├── columns: employee.id:14!null
 │    │    │    │    └── key: (14)
 │    │    │    └── scan manager
 │    │    │         ├── columns: manager.id:18!null
 │    │    │         └── key: (18)
 │    │    ├── scan company_employee [as=employees1_]
 │    │    │    ├── columns: company_id:10!null employees_id:11!null
 │    │    │    └── key: (10,11)
 │    │    └── filters
 │    │         └── employees_id:11 = id:22 [outer=(11,22), constraints=(/11: (/NULL - ]; /22: (/NULL - ]), fd=(11)==(22), (22)==(11)]
 │    └── filters
 │         └── company0_.id:1 = company_id:10 [outer=(1,10), constraints=(/1: (/NULL - ]; /10: (/NULL - ]), fd=(1)==(10), (10)==(1)]
 └── filters
      └── location_id:2 = location3_.id:5 [outer=(2,5), constraints=(/2: (/NULL - ]; /5: (/NULL - ]), fd=(2)==(5), (5)==(2)]

exec-ddl
drop table Company, Company_Employee, Employee, Manager, Location;
----

# ------------------------------------------------------------------------------
# Query #8
#   org.hibernate.test.annotations.indexcoll.IndexedCollectionTest
#   testMapKeyOnManyToMany
# ------------------------------------------------------------------------------
exec-ddl
create table News (
   news_id int4 not null,
    detail varchar(255),
    title varchar(255),
    primary key (news_id)
)
----

exec-ddl
create table Newspaper (
   id int4 not null,
    name varchar(255),
    primary key (id)
)
----

exec-ddl
create table Newspaper_News (
   Newspaper_id int4 not null,
    news_news_id int4 not null,
    primary key (Newspaper_id, news_news_id)
)
----

opt
select
    news0_.Newspaper_id as Newspape1_23_0_,
    news0_.news_news_id as news_new2_23_0_,
    (select
        a0.title
    from
        News a0
    where
        a0.news_id=news0_.news_news_id) as formula140_0_,
    news1_.news_id as news_id1_21_1_,
    news1_.detail as detail2_21_1_,
    news1_.title as title3_21_1_
from
    Newspaper_News news0_
inner join
    News news1_
        on news0_.news_news_id=news1_.news_id
where
    news0_.Newspaper_id=1
----
project
 ├── columns: newspape1_23_0_:1!null news_new2_23_0_:2!null formula140_0_:15 news_id1_21_1_:5!null detail2_21_1_:6 title3_21_1_:7
 ├── key: (5)
 ├── fd: ()-->(1), (5)-->(6,7,15), (2)==(5), (5)==(2)
 ├── left-join (lookup news [as=a0])
 │    ├── columns: newspaper_id:1!null news_news_id:2!null news1_.news_id:5!null news1_.detail:6 news1_.title:7 a0.news_id:10 a0.title:12
 │    ├── key columns: [2] = [10]
 │    ├── lookup columns are key
 │    ├── key: (5)
 │    ├── fd: ()-->(1), (5)-->(6,7,10,12), (10)-->(12), (2)==(5), (5)==(2)
 │    ├── inner-join (lookup news [as=news1_])
 │    │    ├── columns: newspaper_id:1!null news_news_id:2!null news1_.news_id:5!null news1_.detail:6 news1_.title:7
 │    │    ├── key columns: [2] = [5]
 │    │    ├── lookup columns are key
 │    │    ├── key: (5)
 │    │    ├── fd: ()-->(1), (5)-->(6,7), (2)==(5), (5)==(2)
 │    │    ├── scan newspaper_news [as=news0_]
 │    │    │    ├── columns: newspaper_id:1!null news_news_id:2!null
 │    │    │    ├── constraint: /1/2: [/1 - /1]
 │    │    │    ├── key: (2)
 │    │    │    └── fd: ()-->(1)
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      └── a0.title:12 [as=formula140_0_:15, outer=(12)]

exec-ddl
drop table News, Newspaper, Newspaper_News;
----

# ------------------------------------------------------------------------------
# Query #9
#   org.hibernate.test.annotations.indexcoll.MapKeyTest testMapKeyOnEmbeddedId
# ------------------------------------------------------------------------------
exec-ddl
create table GenerationGroup (
   id int4 not null,
    age varchar(255),
    culture varchar(255),
    description varchar(255),
    primary key (id)
)
----

exec-ddl
create table GenerationUser (
   id int4 not null,
    primary key (id)
)
----

exec-ddl
create table GenerationUser_GenerationGroup (
   GenerationUser_id int4 not null,
    ref_id int4 not null,
    primary key (GenerationUser_id, ref_id)
)
----

opt
SELECT ref0_.generationuser_id AS generati1_2_0_
      ,ref0_.ref_id AS ref_id2_2_0_
      ,(SELECT a13.age
        FROM generationgroup AS a13
        WHERE a13.id = ref0_.ref_id) AS formula131_0_
      ,(SELECT a15.culture
        FROM generationgroup AS a15
        WHERE a15.id = ref0_.ref_id) AS formula132_0_
      ,(SELECT a13.description
        FROM generationgroup AS a13
        WHERE a13.id = ref0_.ref_id) AS formula133_0_
      ,generation1_.id AS id1_0_1_
      ,generation1_.age AS age2_0_1_
      ,generation1_.culture AS culture3_0_1_
      ,generation1_.description AS descript4_0_1_
FROM generationuser_generationgroup AS ref0_
INNER JOIN generationgroup AS generation1_
  ON ref0_.ref_id = generation1_.id
WHERE ref0_.generationuser_id = 1;
----
project
 ├── columns: generati1_2_0_:1!null ref_id2_2_0_:2!null formula131_0_:29 formula132_0_:30 formula133_0_:31 id1_0_1_:5!null age2_0_1_:6 culture3_0_1_:7 descript4_0_1_:8
 ├── key: (5)
 ├── fd: ()-->(1), (5)-->(6-8,29-31), (2)==(5), (5)==(2)
 ├── left-join (lookup generationgroup [as=a13])
 │    ├── columns: generationuser_id:1!null ref_id:2!null generation1_.id:5!null generation1_.age:6 generation1_.culture:7 generation1_.description:8 a13.id:11 a13.age:12 a15.id:17 a15.culture:19 a13.id:23 a13.description:26
 │    ├── key columns: [2] = [23]
 │    ├── lookup columns are key
 │    ├── key: (5)
 │    ├── fd: ()-->(1), (5)-->(6-8,11,12,17,19,23,26), (11)-->(12), (17)-->(19), (23)-->(26), (2)==(5), (5)==(2)
 │    ├── left-join (lookup generationgroup [as=a15])
 │    │    ├── columns: generationuser_id:1!null ref_id:2!null generation1_.id:5!null generation1_.age:6 generation1_.culture:7 generation1_.description:8 a13.id:11 a13.age:12 a15.id:17 a15.culture:19
 │    │    ├── key columns: [2] = [17]
 │    │    ├── lookup columns are key
 │    │    ├── key: (5)
 │    │    ├── fd: ()-->(1), (5)-->(6-8,11,12,17,19), (11)-->(12), (17)-->(19), (2)==(5), (5)==(2)
 │    │    ├── left-join (lookup generationgroup [as=a13])
 │    │    │    ├── columns: generationuser_id:1!null ref_id:2!null generation1_.id:5!null generation1_.age:6 generation1_.culture:7 generation1_.description:8 a13.id:11 a13.age:12
 │    │    │    ├── key columns: [2] = [11]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── key: (5)
 │    │    │    ├── fd: ()-->(1), (5)-->(6-8,11,12), (11)-->(12), (2)==(5), (5)==(2)
 │    │    │    ├── inner-join (lookup generationgroup [as=generation1_])
 │    │    │    │    ├── columns: generationuser_id:1!null ref_id:2!null generation1_.id:5!null generation1_.age:6 generation1_.culture:7 generation1_.description:8
 │    │    │    │    ├── key columns: [2] = [5]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── key: (5)
 │    │    │    │    ├── fd: ()-->(1), (5)-->(6-8), (2)==(5), (5)==(2)
 │    │    │    │    ├── scan generationuser_generationgroup [as=ref0_]
 │    │    │    │    │    ├── columns: generationuser_id:1!null ref_id:2!null
 │    │    │    │    │    ├── constraint: /1/2: [/1 - /1]
 │    │    │    │    │    ├── key: (2)
 │    │    │    │    │    └── fd: ()-->(1)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── a13.age:12 [as=formula131_0_:29, outer=(12)]
      ├── a15.culture:19 [as=formula132_0_:30, outer=(19)]
      └── a13.description:26 [as=formula133_0_:31, outer=(26)]

exec-ddl
drop table GenerationGroup, GenerationUser, GenerationUser_GenerationGroup;
----

# ------------------------------------------------------------------------------
# Query #10
#   org.hibernate.test.bidi.AuctionTest2 testLazy
# ------------------------------------------------------------------------------
exec-ddl
create table TAuction2 (
   id int8 not null,
    description varchar(255),
    endDatetime timestamp,
    successfulBid int8,
    primary key (id)
)
----

exec-ddl
create table TBid2 (
   id int8 not null,
    amount numeric(31, 19),
    createdDatetime timestamp,
    auctionId int8,
    primary key (id)
)
----

opt
select
    bids0_.auctionId as auctionI4_1_0_,
    bids0_.id as id1_1_0_,
    bids0_.id as id1_1_1_,
    bids0_.amount as amount2_1_1_,
    bids0_.createdDatetime as createdD3_1_1_,
    bids0_.auctionId as auctionI4_1_1_,
    exists(select
        a.id
    from
        TAuction2 a
    where
        a.successfulBid=bids0_.id) as formula41_1_
from
    TBid2 bids0_
where
    bids0_.auctionId=$1
----
project
 ├── columns: auctioni4_1_0_:4!null id1_1_0_:1!null id1_1_1_:1!null amount2_1_1_:2 createdd3_1_1_:3 auctioni4_1_1_:4!null formula41_1_:14!null
 ├── has-placeholder
 ├── key: (1)
 ├── fd: ()-->(4), (1)-->(2,3,14)
 ├── group-by (hash)
 │    ├── columns: bids0_.id:1!null amount:2 createddatetime:3 auctionid:4!null canary_agg:15
 │    ├── grouping columns: bids0_.id:1!null
 │    ├── has-placeholder
 │    ├── key: (1)
 │    ├── fd: ()-->(4), (1)-->(2-4,15)
 │    ├── right-join (hash)
 │    │    ├── columns: bids0_.id:1!null amount:2 createddatetime:3 auctionid:4!null a.id:7 successfulbid:10
 │    │    ├── has-placeholder
 │    │    ├── key: (1,7)
 │    │    ├── fd: ()-->(4), (1)-->(2,3), (7)-->(10)
 │    │    ├── scan tauction2 [as=a]
 │    │    │    ├── columns: a.id:7!null successfulbid:10
 │    │    │    ├── key: (7)
 │    │    │    └── fd: (7)-->(10)
 │    │    ├── select
 │    │    │    ├── columns: bids0_.id:1!null amount:2 createddatetime:3 auctionid:4!null
 │    │    │    ├── has-placeholder
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(4), (1)-->(2,3)
 │    │    │    ├── scan tbid2 [as=bids0_]
 │    │    │    │    ├── columns: bids0_.id:1!null amount:2 createddatetime:3 auctionid:4
 │    │    │    │    ├── key: (1)
 │    │    │    │    └── fd: (1)-->(2-4)
 │    │    │    └── filters
 │    │    │         └── auctionid:4 = $1 [outer=(4), constraints=(/4: (/NULL - ]), fd=()-->(4)]
 │    │    └── filters
 │    │         └── successfulbid:10 = bids0_.id:1 [outer=(1,10), constraints=(/1: (/NULL - ]; /10: (/NULL - ]), fd=(1)==(10), (10)==(1)]
 │    └── aggregations
 │         ├── const-not-null-agg [as=canary_agg:15, outer=(7)]
 │         │    └── a.id:7
 │         ├── const-agg [as=amount:2, outer=(2)]
 │         │    └── amount:2
 │         ├── const-agg [as=createddatetime:3, outer=(3)]
 │         │    └── createddatetime:3
 │         └── const-agg [as=auctionid:4, outer=(4)]
 │              └── auctionid:4
 └── projections
      └── canary_agg:15 IS NOT NULL [as=formula41_1_:14, outer=(15)]

exec-ddl
drop table TAuction2, TBid2;
----

# ------------------------------------------------------------------------------
# Query #11
#   org.hibernate.test.cid.CompositeIdTest
# ------------------------------------------------------------------------------
exec-ddl
CREATE TABLE customer (
  customerid VARCHAR(10) NOT NULL,
  name VARCHAR(100) NOT NULL,
  address VARCHAR(200) NOT NULL,
  PRIMARY KEY (customerid)
);
----

exec-ddl
CREATE TABLE customerorder (
  customerid VARCHAR(10) NOT NULL,
  ordernumber INT4 NOT NULL,
  orderdate DATE NOT NULL,
  PRIMARY KEY (customerid, ordernumber)
);
----

exec-ddl
CREATE TABLE lineitem (
  customerid VARCHAR(10) NOT NULL,
  ordernumber INT4 NOT NULL,
  productid VARCHAR(10) NOT NULL,
  quantity INT4,
  PRIMARY KEY (customerid, ordernumber, productid)
);
----

exec-ddl
CREATE TABLE product (
  productid VARCHAR(10) NOT NULL,
  description VARCHAR(200) NOT NULL,
  cost NUMERIC(19,2),
  numberavailable INT4,
  PRIMARY KEY (productid)
);
----

opt
SELECT
  order0_.customerid AS customer1_1_0_,
  order0_.ordernumber AS ordernum2_1_0_,
  order0_.orderdate AS orderdat3_1_0_,
  (
    SELECT
      sum(li.quantity * p.cost)
    FROM
      lineitem AS li, product AS p
    WHERE
      li.productid = p.productid
      AND li.customerid = order0_.customerid
      AND li.ordernumber = order0_.ordernumber
  )
    AS formula101_0_,
  lineitems1_.customerid AS customer1_2_1_,
  lineitems1_.ordernumber AS ordernum2_2_1_,
  lineitems1_.productid AS producti3_2_1_,
  lineitems1_.customerid AS customer1_2_2_,
  lineitems1_.ordernumber AS ordernum2_2_2_,
  lineitems1_.productid AS producti3_2_2_,
  lineitems1_.quantity AS quantity4_2_2_
FROM
  customerorder AS order0_
  LEFT JOIN lineitem AS lineitems1_
  ON
    order0_.customerid = lineitems1_.customerid
    AND order0_.ordernumber = lineitems1_.ordernumber
WHERE
  order0_.customerid = 'c111' AND order0_.ordernumber = 0;
----
project
 ├── columns: customer1_1_0_:1!null ordernum2_1_0_:2!null orderdat3_1_0_:3!null formula101_0_:26 customer1_2_1_:6 ordernum2_2_1_:7 producti3_2_1_:8 customer1_2_2_:6 ordernum2_2_2_:7 producti3_2_2_:8 quantity4_2_2_:9
 ├── immutable
 ├── key: (8)
 ├── fd: ()-->(1-3,6,7), (8)-->(9,26)
 ├── group-by (hash)
 │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null lineitems1_.customerid:6 lineitems1_.ordernumber:7 lineitems1_.productid:8 lineitems1_.quantity:9 sum:25
 │    ├── grouping columns: lineitems1_.productid:8
 │    ├── immutable
 │    ├── key: (8)
 │    ├── fd: ()-->(1-3,6,7), (8)-->(1-3,6,7,9,25)
 │    ├── right-join (hash)
 │    │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null lineitems1_.customerid:6 lineitems1_.ordernumber:7 lineitems1_.productid:8 lineitems1_.quantity:9 li.customerid:12 li.ordernumber:13 column24:24
 │    │    ├── immutable
 │    │    ├── fd: ()-->(1-3,6,7), (8)-->(9)
 │    │    ├── project
 │    │    │    ├── columns: column24:24 li.customerid:12!null li.ordernumber:13!null
 │    │    │    ├── immutable
 │    │    │    ├── inner-join (hash)
 │    │    │    │    ├── columns: li.customerid:12!null li.ordernumber:13!null li.productid:14!null li.quantity:15 p.productid:18!null cost:20
 │    │    │    │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
 │    │    │    │    ├── key: (12,13,18)
 │    │    │    │    ├── fd: (12-14)-->(15), (18)-->(20), (14)==(18), (18)==(14)
 │    │    │    │    ├── scan lineitem [as=li]
 │    │    │    │    │    ├── columns: li.customerid:12!null li.ordernumber:13!null li.productid:14!null li.quantity:15
 │    │    │    │    │    ├── key: (12-14)
 │    │    │    │    │    └── fd: (12-14)-->(15)
 │    │    │    │    ├── scan product [as=p]
 │    │    │    │    │    ├── columns: p.productid:18!null cost:20
 │    │    │    │    │    ├── key: (18)
 │    │    │    │    │    └── fd: (18)-->(20)
 │    │    │    │    └── filters
 │    │    │    │         └── li.productid:14 = p.productid:18 [outer=(14,18), constraints=(/14: (/NULL - ]; /18: (/NULL - ]), fd=(14)==(18), (18)==(14)]
 │    │    │    └── projections
 │    │    │         └── li.quantity:15 * cost:20 [as=column24:24, outer=(15,20), immutable]
 │    │    ├── left-join (merge)
 │    │    │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null lineitems1_.customerid:6 lineitems1_.ordernumber:7 lineitems1_.productid:8 lineitems1_.quantity:9
 │    │    │    ├── left ordering: +1,+2
 │    │    │    ├── right ordering: +6,+7
 │    │    │    ├── key: (8)
 │    │    │    ├── fd: ()-->(1-3,6,7), (8)-->(9)
 │    │    │    ├── scan customerorder [as=order0_]
 │    │    │    │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null
 │    │    │    │    ├── constraint: /1/2: [/'c111'/0 - /'c111'/0]
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    └── fd: ()-->(1-3)
 │    │    │    ├── scan lineitem [as=lineitems1_]
 │    │    │    │    ├── columns: lineitems1_.customerid:6!null lineitems1_.ordernumber:7!null lineitems1_.productid:8!null lineitems1_.quantity:9
 │    │    │    │    ├── constraint: /6/7/8: [/'c111'/0 - /'c111'/0]
 │    │    │    │    ├── key: (8)
 │    │    │    │    └── fd: ()-->(6,7), (8)-->(9)
 │    │    │    └── filters (true)
 │    │    └── filters
 │    │         ├── li.customerid:12 = order0_.customerid:1 [outer=(1,12), constraints=(/1: (/NULL - ]; /12: (/NULL - ]), fd=(1)==(12), (12)==(1)]
 │    │         └── li.ordernumber:13 = order0_.ordernumber:2 [outer=(2,13), constraints=(/2: (/NULL - ]; /13: (/NULL - ]), fd=(2)==(13), (13)==(2)]
 │    └── aggregations
 │         ├── sum [as=sum:25, outer=(24)]
 │         │    └── column24:24
 │         ├── const-agg [as=order0_.customerid:1, outer=(1)]
 │         │    └── order0_.customerid:1
 │         ├── const-agg [as=order0_.ordernumber:2, outer=(2)]
 │         │    └── order0_.ordernumber:2
 │         ├── const-agg [as=orderdate:3, outer=(3)]
 │         │    └── orderdate:3
 │         ├── const-agg [as=lineitems1_.customerid:6, outer=(6)]
 │         │    └── lineitems1_.customerid:6
 │         ├── const-agg [as=lineitems1_.ordernumber:7, outer=(7)]
 │         │    └── lineitems1_.ordernumber:7
 │         └── const-agg [as=lineitems1_.quantity:9, outer=(9)]
 │              └── lineitems1_.quantity:9
 └── projections
      └── sum:25 [as=formula101_0_:26, outer=(25)]

opt
SELECT
  customer0_.customerid AS customer1_0_0_,
  orders1_.customerid AS customer1_1_1_,
  orders1_.ordernumber AS ordernum2_1_1_,
  lineitems2_.customerid AS customer1_2_2_,
  lineitems2_.ordernumber AS ordernum2_2_2_,
  lineitems2_.productid AS producti3_2_2_,
  product3_.productid AS producti1_3_3_,
  customer0_.name AS name2_0_0_,
  customer0_.address AS address3_0_0_,
  orders1_.orderdate AS orderdat3_1_1_,
  (
    SELECT
      sum(li.quantity * p.cost)
    FROM
      lineitem AS li, product AS p
    WHERE
      li.productid = p.productid
      AND li.customerid = orders1_.customerid
      AND li.ordernumber = orders1_.ordernumber
  )
    AS formula103_1_,
  orders1_.customerid AS customer1_1_0__,
  orders1_.ordernumber AS ordernum2_1_0__,
  orders1_.ordernumber AS ordernum2_0__,
  lineitems2_.quantity AS quantity4_2_2_,
  lineitems2_.customerid AS customer1_2_1__,
  lineitems2_.ordernumber AS ordernum2_2_1__,
  lineitems2_.productid AS producti3_2_1__,
  product3_.description AS descript2_3_3_,
  product3_.cost AS cost3_3_3_,
  product3_.numberavailable AS numberav4_3_3_,
  (
    SELECT
      sum(li.quantity)
    FROM
      lineitem AS li
    WHERE
      li.productid = product3_.productid
  )
    AS formula104_3_
FROM
  customer AS customer0_
  LEFT JOIN customerorder AS orders1_
  ON customer0_.customerid = orders1_.customerid
  LEFT JOIN lineitem AS lineitems2_
  ON
    orders1_.customerid = lineitems2_.customerid
    AND orders1_.ordernumber = lineitems2_.ordernumber
  LEFT JOIN product AS product3_ ON lineitems2_.productid = product3_.productid;
----
project
 ├── columns: customer1_0_0_:1!null customer1_1_1_:6 ordernum2_1_1_:7 customer1_2_2_:11 ordernum2_2_2_:12 producti3_2_2_:13 producti1_3_3_:17 name2_0_0_:2!null address3_0_0_:3!null orderdat3_1_1_:8 formula103_1_:44 customer1_1_0__:6 ordernum2_1_0__:7 ordernum2_0__:7 quantity4_2_2_:14 customer1_2_1__:11 ordernum2_2_1__:12 producti3_2_1__:13 descript2_3_3_:18 cost3_3_3_:19 numberav4_3_3_:20 formula104_3_:45
 ├── immutable
 ├── key: (1,6,7,11-13)
 ├── fd: (1)-->(2,3), (6,7)-->(8), (11-13)-->(14), (17)-->(18-20), (1,6,7,11-13)-->(2,3,8,14,17-20,44,45)
 ├── group-by (hash)
 │    ├── columns: customer0_.customerid:1!null name:2!null address:3!null orders1_.customerid:6 orders1_.ordernumber:7 orderdate:8 lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13 lineitems2_.quantity:14 product3_.productid:17 product3_.description:18 product3_.cost:19 product3_.numberavailable:20 sum:36 sum:43
 │    ├── grouping columns: customer0_.customerid:1!null orders1_.customerid:6 orders1_.ordernumber:7 lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13
 │    ├── immutable
 │    ├── key: (1,6,7,11-13)
 │    ├── fd: (1)-->(2,3), (6,7)-->(8), (11-13)-->(14), (17)-->(18-20), (1,6,7,11-13)-->(2,3,8,14,17-20,36,43)
 │    ├── left-join (hash)
 │    │    ├── columns: customer0_.customerid:1!null name:2!null address:3!null orders1_.customerid:6 orders1_.ordernumber:7 orderdate:8 lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13 lineitems2_.quantity:14 product3_.productid:17 product3_.description:18 product3_.cost:19 product3_.numberavailable:20 sum:36 li.productid:39 li.quantity:40
 │    │    ├── immutable
 │    │    ├── fd: (1)-->(2,3), (6,7)-->(8), (11-13)-->(14), (17)-->(18-20), (1,6,7,11-13)-->(2,3,8,14,17-20,36)
 │    │    ├── group-by (hash)
 │    │    │    ├── columns: customer0_.customerid:1!null name:2!null address:3!null orders1_.customerid:6 orders1_.ordernumber:7 orderdate:8 lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13 lineitems2_.quantity:14 product3_.productid:17 product3_.description:18 product3_.cost:19 product3_.numberavailable:20 sum:36
 │    │    │    ├── grouping columns: customer0_.customerid:1!null orders1_.customerid:6 orders1_.ordernumber:7 lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13
 │    │    │    ├── immutable
 │    │    │    ├── key: (1,6,7,11-13)
 │    │    │    ├── fd: (1)-->(2,3), (6,7)-->(8), (11-13)-->(14), (17)-->(18-20), (1,6,7,11-13)-->(2,3,8,14,17-20,36)
 │    │    │    ├── left-join (hash)
 │    │    │    │    ├── columns: customer0_.customerid:1!null name:2!null address:3!null orders1_.customerid:6 orders1_.ordernumber:7 orderdate:8 lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13 lineitems2_.quantity:14 product3_.productid:17 product3_.description:18 product3_.cost:19 product3_.numberavailable:20 li.customerid:23 li.ordernumber:24 column35:35
 │    │    │    │    ├── immutable
 │    │    │    │    ├── fd: (1)-->(2,3), (6,7)-->(8), (11-13)-->(14), (17)-->(18-20), (1,6,7,11-13)-->(17-20)
 │    │    │    │    ├── left-join (hash)
 │    │    │    │    │    ├── columns: customer0_.customerid:1!null name:2!null address:3!null orders1_.customerid:6 orders1_.ordernumber:7 orderdate:8 lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13 lineitems2_.quantity:14 product3_.productid:17 product3_.description:18 product3_.cost:19 product3_.numberavailable:20
 │    │    │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    │    │    ├── key: (1,6,7,11-13)
 │    │    │    │    │    ├── fd: (1)-->(2,3), (6,7)-->(8), (11-13)-->(14), (17)-->(18-20), (1,6,7,11-13)-->(17-20)
 │    │    │    │    │    ├── left-join (merge)
 │    │    │    │    │    │    ├── columns: customer0_.customerid:1!null name:2!null address:3!null orders1_.customerid:6 orders1_.ordernumber:7 orderdate:8 lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13 lineitems2_.quantity:14
 │    │    │    │    │    │    ├── left ordering: +1
 │    │    │    │    │    │    ├── right ordering: +6
 │    │    │    │    │    │    ├── key: (1,6,7,11-13)
 │    │    │    │    │    │    ├── fd: (1)-->(2,3), (6,7)-->(8), (11-13)-->(14)
 │    │    │    │    │    │    ├── scan customer [as=customer0_]
 │    │    │    │    │    │    │    ├── columns: customer0_.customerid:1!null name:2!null address:3!null
 │    │    │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    │    │    ├── fd: (1)-->(2,3)
 │    │    │    │    │    │    │    └── ordering: +1
 │    │    │    │    │    │    ├── left-join (merge)
 │    │    │    │    │    │    │    ├── columns: orders1_.customerid:6!null orders1_.ordernumber:7!null orderdate:8!null lineitems2_.customerid:11 lineitems2_.ordernumber:12 lineitems2_.productid:13 lineitems2_.quantity:14
 │    │    │    │    │    │    │    ├── left ordering: +6,+7
 │    │    │    │    │    │    │    ├── right ordering: +11,+12
 │    │    │    │    │    │    │    ├── key: (6,7,11-13)
 │    │    │    │    │    │    │    ├── fd: (6,7)-->(8), (11-13)-->(14)
 │    │    │    │    │    │    │    ├── ordering: +6
 │    │    │    │    │    │    │    ├── scan customerorder [as=orders1_]
 │    │    │    │    │    │    │    │    ├── columns: orders1_.customerid:6!null orders1_.ordernumber:7!null orderdate:8!null
 │    │    │    │    │    │    │    │    ├── key: (6,7)
 │    │    │    │    │    │    │    │    ├── fd: (6,7)-->(8)
 │    │    │    │    │    │    │    │    └── ordering: +6,+7
 │    │    │    │    │    │    │    ├── scan lineitem [as=lineitems2_]
 │    │    │    │    │    │    │    │    ├── columns: lineitems2_.customerid:11!null lineitems2_.ordernumber:12!null lineitems2_.productid:13!null lineitems2_.quantity:14
 │    │    │    │    │    │    │    │    ├── key: (11-13)
 │    │    │    │    │    │    │    │    ├── fd: (11-13)-->(14)
 │    │    │    │    │    │    │    │    └── ordering: +11,+12
 │    │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    ├── scan product [as=product3_]
 │    │    │    │    │    │    ├── columns: product3_.productid:17!null product3_.description:18!null product3_.cost:19 product3_.numberavailable:20
 │    │    │    │    │    │    ├── key: (17)
 │    │    │    │    │    │    └── fd: (17)-->(18-20)
 │    │    │    │    │    └── filters
 │    │    │    │    │         └── lineitems2_.productid:13 = product3_.productid:17 [outer=(13,17), constraints=(/13: (/NULL - ]; /17: (/NULL - ]), fd=(13)==(17), (17)==(13)]
 │    │    │    │    ├── project
 │    │    │    │    │    ├── columns: column35:35 li.customerid:23!null li.ordernumber:24!null
 │    │    │    │    │    ├── immutable
 │    │    │    │    │    ├── inner-join (hash)
 │    │    │    │    │    │    ├── columns: li.customerid:23!null li.ordernumber:24!null li.productid:25!null li.quantity:26 p.productid:29!null p.cost:31
 │    │    │    │    │    │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
 │    │    │    │    │    │    ├── key: (23,24,29)
 │    │    │    │    │    │    ├── fd: (23-25)-->(26), (29)-->(31), (25)==(29), (29)==(25)
 │    │    │    │    │    │    ├── scan lineitem [as=li]
 │    │    │    │    │    │    │    ├── columns: li.customerid:23!null li.ordernumber:24!null li.productid:25!null li.quantity:26
 │    │    │    │    │    │    │    ├── key: (23-25)
 │    │    │    │    │    │    │    └── fd: (23-25)-->(26)
 │    │    │    │    │    │    ├── scan product [as=p]
 │    │    │    │    │    │    │    ├── columns: p.productid:29!null p.cost:31
 │    │    │    │    │    │    │    ├── key: (29)
 │    │    │    │    │    │    │    └── fd: (29)-->(31)
 │    │    │    │    │    │    └── filters
 │    │    │    │    │    │         └── li.productid:25 = p.productid:29 [outer=(25,29), constraints=(/25: (/NULL - ]; /29: (/NULL - ]), fd=(25)==(29), (29)==(25)]
 │    │    │    │    │    └── projections
 │    │    │    │    │         └── li.quantity:26 * p.cost:31 [as=column35:35, outer=(26,31), immutable]
 │    │    │    │    └── filters
 │    │    │    │         ├── li.customerid:23 = orders1_.customerid:6 [outer=(6,23), constraints=(/6: (/NULL - ]; /23: (/NULL - ]), fd=(6)==(23), (23)==(6)]
 │    │    │    │         └── li.ordernumber:24 = orders1_.ordernumber:7 [outer=(7,24), constraints=(/7: (/NULL - ]; /24: (/NULL - ]), fd=(7)==(24), (24)==(7)]
 │    │    │    └── aggregations
 │    │    │         ├── sum [as=sum:36, outer=(35)]
 │    │    │         │    └── column35:35
 │    │    │         ├── const-agg [as=name:2, outer=(2)]
 │    │    │         │    └── name:2
 │    │    │         ├── const-agg [as=address:3, outer=(3)]
 │    │    │         │    └── address:3
 │    │    │         ├── const-agg [as=orderdate:8, outer=(8)]
 │    │    │         │    └── orderdate:8
 │    │    │         ├── const-agg [as=lineitems2_.quantity:14, outer=(14)]
 │    │    │         │    └── lineitems2_.quantity:14
 │    │    │         ├── const-agg [as=product3_.productid:17, outer=(17)]
 │    │    │         │    └── product3_.productid:17
 │    │    │         ├── const-agg [as=product3_.description:18, outer=(18)]
 │    │    │         │    └── product3_.description:18
 │    │    │         ├── const-agg [as=product3_.cost:19, outer=(19)]
 │    │    │         │    └── product3_.cost:19
 │    │    │         └── const-agg [as=product3_.numberavailable:20, outer=(20)]
 │    │    │              └── product3_.numberavailable:20
 │    │    ├── scan lineitem [as=li]
 │    │    │    └── columns: li.productid:39!null li.quantity:40
 │    │    └── filters
 │    │         └── li.productid:39 = product3_.productid:17 [outer=(17,39), constraints=(/17: (/NULL - ]; /39: (/NULL - ]), fd=(17)==(39), (39)==(17)]
 │    └── aggregations
 │         ├── sum [as=sum:43, outer=(40)]
 │         │    └── li.quantity:40
 │         ├── const-agg [as=name:2, outer=(2)]
 │         │    └── name:2
 │         ├── const-agg [as=address:3, outer=(3)]
 │         │    └── address:3
 │         ├── const-agg [as=orderdate:8, outer=(8)]
 │         │    └── orderdate:8
 │         ├── const-agg [as=lineitems2_.quantity:14, outer=(14)]
 │         │    └── lineitems2_.quantity:14
 │         ├── const-agg [as=product3_.productid:17, outer=(17)]
 │         │    └── product3_.productid:17
 │         ├── const-agg [as=product3_.description:18, outer=(18)]
 │         │    └── product3_.description:18
 │         ├── const-agg [as=product3_.cost:19, outer=(19)]
 │         │    └── product3_.cost:19
 │         ├── const-agg [as=product3_.numberavailable:20, outer=(20)]
 │         │    └── product3_.numberavailable:20
 │         └── const-agg [as=sum:36, outer=(36)]
 │              └── sum:36
 └── projections
      ├── sum:36 [as=formula103_1_:44, outer=(36)]
      └── sum:43 [as=formula104_3_:45, outer=(43)]

opt
SELECT
  order0_.customerid AS customer1_1_0_,
  order0_.ordernumber AS ordernum2_1_0_,
  order0_.orderdate AS orderdat3_1_0_,
  (
    SELECT
      sum(li.quantity * p.cost)
    FROM
      lineitem AS li, product AS p
    WHERE
      li.productid = p.productid
      AND li.customerid = order0_.customerid
      AND li.ordernumber = order0_.ordernumber
  )
    AS formula105_0_,
  lineitems1_.customerid AS customer1_2_1_,
  lineitems1_.ordernumber AS ordernum2_2_1_,
  lineitems1_.productid AS producti3_2_1_,
  lineitems1_.customerid AS customer1_2_2_,
  lineitems1_.ordernumber AS ordernum2_2_2_,
  lineitems1_.productid AS producti3_2_2_,
  lineitems1_.quantity AS quantity4_2_2_
FROM
  customerorder AS order0_
  LEFT JOIN lineitem AS lineitems1_
  ON
    order0_.customerid = lineitems1_.customerid
    AND order0_.ordernumber = lineitems1_.ordernumber
WHERE
  order0_.customerid = 'c111' AND order0_.ordernumber = 0;
----
project
 ├── columns: customer1_1_0_:1!null ordernum2_1_0_:2!null orderdat3_1_0_:3!null formula105_0_:26 customer1_2_1_:6 ordernum2_2_1_:7 producti3_2_1_:8 customer1_2_2_:6 ordernum2_2_2_:7 producti3_2_2_:8 quantity4_2_2_:9
 ├── immutable
 ├── key: (8)
 ├── fd: ()-->(1-3,6,7), (8)-->(9,26)
 ├── group-by (hash)
 │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null lineitems1_.customerid:6 lineitems1_.ordernumber:7 lineitems1_.productid:8 lineitems1_.quantity:9 sum:25
 │    ├── grouping columns: lineitems1_.productid:8
 │    ├── immutable
 │    ├── key: (8)
 │    ├── fd: ()-->(1-3,6,7), (8)-->(1-3,6,7,9,25)
 │    ├── right-join (hash)
 │    │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null lineitems1_.customerid:6 lineitems1_.ordernumber:7 lineitems1_.productid:8 lineitems1_.quantity:9 li.customerid:12 li.ordernumber:13 column24:24
 │    │    ├── immutable
 │    │    ├── fd: ()-->(1-3,6,7), (8)-->(9)
 │    │    ├── project
 │    │    │    ├── columns: column24:24 li.customerid:12!null li.ordernumber:13!null
 │    │    │    ├── immutable
 │    │    │    ├── inner-join (hash)
 │    │    │    │    ├── columns: li.customerid:12!null li.ordernumber:13!null li.productid:14!null li.quantity:15 p.productid:18!null cost:20
 │    │    │    │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
 │    │    │    │    ├── key: (12,13,18)
 │    │    │    │    ├── fd: (12-14)-->(15), (18)-->(20), (14)==(18), (18)==(14)
 │    │    │    │    ├── scan lineitem [as=li]
 │    │    │    │    │    ├── columns: li.customerid:12!null li.ordernumber:13!null li.productid:14!null li.quantity:15
 │    │    │    │    │    ├── key: (12-14)
 │    │    │    │    │    └── fd: (12-14)-->(15)
 │    │    │    │    ├── scan product [as=p]
 │    │    │    │    │    ├── columns: p.productid:18!null cost:20
 │    │    │    │    │    ├── key: (18)
 │    │    │    │    │    └── fd: (18)-->(20)
 │    │    │    │    └── filters
 │    │    │    │         └── li.productid:14 = p.productid:18 [outer=(14,18), constraints=(/14: (/NULL - ]; /18: (/NULL - ]), fd=(14)==(18), (18)==(14)]
 │    │    │    └── projections
 │    │    │         └── li.quantity:15 * cost:20 [as=column24:24, outer=(15,20), immutable]
 │    │    ├── left-join (merge)
 │    │    │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null lineitems1_.customerid:6 lineitems1_.ordernumber:7 lineitems1_.productid:8 lineitems1_.quantity:9
 │    │    │    ├── left ordering: +1,+2
 │    │    │    ├── right ordering: +6,+7
 │    │    │    ├── key: (8)
 │    │    │    ├── fd: ()-->(1-3,6,7), (8)-->(9)
 │    │    │    ├── scan customerorder [as=order0_]
 │    │    │    │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null
 │    │    │    │    ├── constraint: /1/2: [/'c111'/0 - /'c111'/0]
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    └── fd: ()-->(1-3)
 │    │    │    ├── scan lineitem [as=lineitems1_]
 │    │    │    │    ├── columns: lineitems1_.customerid:6!null lineitems1_.ordernumber:7!null lineitems1_.productid:8!null lineitems1_.quantity:9
 │    │    │    │    ├── constraint: /6/7/8: [/'c111'/0 - /'c111'/0]
 │    │    │    │    ├── key: (8)
 │    │    │    │    └── fd: ()-->(6,7), (8)-->(9)
 │    │    │    └── filters (true)
 │    │    └── filters
 │    │         ├── li.customerid:12 = order0_.customerid:1 [outer=(1,12), constraints=(/1: (/NULL - ]; /12: (/NULL - ]), fd=(1)==(12), (12)==(1)]
 │    │         └── li.ordernumber:13 = order0_.ordernumber:2 [outer=(2,13), constraints=(/2: (/NULL - ]; /13: (/NULL - ]), fd=(2)==(13), (13)==(2)]
 │    └── aggregations
 │         ├── sum [as=sum:25, outer=(24)]
 │         │    └── column24:24
 │         ├── const-agg [as=order0_.customerid:1, outer=(1)]
 │         │    └── order0_.customerid:1
 │         ├── const-agg [as=order0_.ordernumber:2, outer=(2)]
 │         │    └── order0_.ordernumber:2
 │         ├── const-agg [as=orderdate:3, outer=(3)]
 │         │    └── orderdate:3
 │         ├── const-agg [as=lineitems1_.customerid:6, outer=(6)]
 │         │    └── lineitems1_.customerid:6
 │         ├── const-agg [as=lineitems1_.ordernumber:7, outer=(7)]
 │         │    └── lineitems1_.ordernumber:7
 │         └── const-agg [as=lineitems1_.quantity:9, outer=(9)]
 │              └── lineitems1_.quantity:9
 └── projections
      └── sum:25 [as=formula105_0_:26, outer=(25)]

opt
SELECT
  order0_.customerid AS customer1_10_,
  order0_.ordernumber AS ordernum2_10_,
  order0_.orderdate AS orderdat3_10_,
  (
    SELECT
      sum(li.quantity * p.cost)
    FROM
      lineitem AS li, product AS p
    WHERE
      li.productid = p.productid
      AND li.customerid = order0_.customerid
      AND li.ordernumber = order0_.ordernumber
  )
    AS formula273_
FROM
  customerorder AS order0_;
----
project
 ├── columns: customer1_10_:1!null ordernum2_10_:2!null orderdat3_10_:3!null formula273_:20
 ├── immutable
 ├── key: (1,2)
 ├── fd: (1,2)-->(3,20)
 ├── group-by (hash)
 │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null sum:19
 │    ├── grouping columns: order0_.customerid:1!null order0_.ordernumber:2!null
 │    ├── immutable
 │    ├── key: (1,2)
 │    ├── fd: (1,2)-->(3,19)
 │    ├── left-join (hash)
 │    │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null li.customerid:6 li.ordernumber:7 column18:18
 │    │    ├── multiplicity: left-rows(one-or-more), right-rows(zero-or-one)
 │    │    ├── immutable
 │    │    ├── fd: (1,2)-->(3)
 │    │    ├── scan customerorder [as=order0_]
 │    │    │    ├── columns: order0_.customerid:1!null order0_.ordernumber:2!null orderdate:3!null
 │    │    │    ├── key: (1,2)
 │    │    │    └── fd: (1,2)-->(3)
 │    │    ├── project
 │    │    │    ├── columns: column18:18 li.customerid:6!null li.ordernumber:7!null
 │    │    │    ├── immutable
 │    │    │    ├── inner-join (hash)
 │    │    │    │    ├── columns: li.customerid:6!null li.ordernumber:7!null li.productid:8!null quantity:9 p.productid:12!null cost:14
 │    │    │    │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
 │    │    │    │    ├── key: (6,7,12)
 │    │    │    │    ├── fd: (6-8)-->(9), (12)-->(14), (8)==(12), (12)==(8)
 │    │    │    │    ├── scan lineitem [as=li]
 │    │    │    │    │    ├── columns: li.customerid:6!null li.ordernumber:7!null li.productid:8!null quantity:9
 │    │    │    │    │    ├── key: (6-8)
 │    │    │    │    │    └── fd: (6-8)-->(9)
 │    │    │    │    ├── scan product [as=p]
 │    │    │    │    │    ├── columns: p.productid:12!null cost:14
 │    │    │    │    │    ├── key: (12)
 │    │    │    │    │    └── fd: (12)-->(14)
 │    │    │    │    └── filters
 │    │    │    │         └── li.productid:8 = p.productid:12 [outer=(8,12), constraints=(/8: (/NULL - ]; /12: (/NULL - ]), fd=(8)==(12), (12)==(8)]
 │    │    │    └── projections
 │    │    │         └── quantity:9 * cost:14 [as=column18:18, outer=(9,14), immutable]
 │    │    └── filters
 │    │         ├── li.customerid:6 = order0_.customerid:1 [outer=(1,6), constraints=(/1: (/NULL - ]; /6: (/NULL - ]), fd=(1)==(6), (6)==(1)]
 │    │         └── li.ordernumber:7 = order0_.ordernumber:2 [outer=(2,7), constraints=(/2: (/NULL - ]; /7: (/NULL - ]), fd=(2)==(7), (7)==(2)]
 │    └── aggregations
 │         ├── sum [as=sum:19, outer=(18)]
 │         │    └── column18:18
 │         └── const-agg [as=orderdate:3, outer=(3)]
 │              └── orderdate:3
 └── projections
      └── sum:19 [as=formula273_:20, outer=(19)]

exec-ddl
drop table customer, customerorder, lineitem, product
----

# ------------------------------------------------------------------------------
# Query #12
#   org.hibernate.test.criteria.CriteriaQueryTest
# ------------------------------------------------------------------------------
exec-ddl
CREATE TABLE student (
  studentid INT8 NOT NULL,
  name VARCHAR(255) NOT NULL,
  address_city VARCHAR(255),
  address_state VARCHAR(255),
  preferredcoursecode VARCHAR(255),
  PRIMARY KEY (studentid)
);
----

exec-ddl
CREATE TABLE enrolment (
  studentid INT8 NOT NULL,
  coursecode VARCHAR(255) NOT NULL,
  semester INT2 NOT NULL,
  year INT2 NOT NULL,
  PRIMARY KEY (studentid, coursecode)
);
----

opt
SELECT
  this_.studentid AS studenti1_26_0_,
  this_.name AS name2_26_0_,
  this_.address_city AS address_3_26_0_,
  this_.address_state AS address_4_26_0_,
  this_.preferredcoursecode AS preferre5_26_0_
FROM
  student AS this_
WHERE
  EXISTS(
    SELECT
      enrolment_.studentid AS y0_
    FROM
      enrolment AS enrolment_
    WHERE
      enrolment_.year
      = (
          SELECT
            max(maxstudentenrolment_.year) AS y0_
          FROM
            enrolment AS maxstudentenrolment_
          WHERE
            this_.preferredcoursecode = maxstudentenrolment_.coursecode
        )
  );
----
group-by (hash)
 ├── columns: studenti1_26_0_:1!null name2_26_0_:2!null address_3_26_0_:3 address_4_26_0_:4 preferre5_26_0_:5!null
 ├── grouping columns: this_.studentid:1!null
 ├── key: (1)
 ├── fd: (1)-->(2-5)
 ├── select
 │    ├── columns: this_.studentid:1!null name:2!null address_city:3 address_state:4 preferredcoursecode:5!null enrolment_.studentid:8!null enrolment_.coursecode:9!null enrolment_.year:11!null max:20!null
 │    ├── key: (1,8,9)
 │    ├── fd: (1)-->(2-5), (8,9)-->(11), (1,8,9)-->(2-5,11,20), (11)==(20), (20)==(11)
 │    ├── group-by (hash)
 │    │    ├── columns: this_.studentid:1!null name:2!null address_city:3 address_state:4 preferredcoursecode:5!null enrolment_.studentid:8!null enrolment_.coursecode:9!null enrolment_.year:11!null max:20!null
 │    │    ├── grouping columns: this_.studentid:1!null enrolment_.studentid:8!null enrolment_.coursecode:9!null
 │    │    ├── key: (1,8,9)
 │    │    ├── fd: (1)-->(2-5), (8,9)-->(11), (1,8,9)-->(2-5,11,20)
 │    │    ├── inner-join (cross)
 │    │    │    ├── columns: this_.studentid:1!null name:2!null address_city:3 address_state:4 preferredcoursecode:5!null enrolment_.studentid:8!null enrolment_.coursecode:9!null enrolment_.year:11!null maxstudentenrolment_.coursecode:15!null maxstudentenrolment_.year:17!null
 │    │    │    ├── fd: (1)-->(2-5), (8,9)-->(11), (5)==(15), (15)==(5)
 │    │    │    ├── inner-join (hash)
 │    │    │    │    ├── columns: this_.studentid:1!null name:2!null address_city:3 address_state:4 preferredcoursecode:5!null maxstudentenrolment_.coursecode:15!null maxstudentenrolment_.year:17!null
 │    │    │    │    ├── fd: (1)-->(2-5), (5)==(15), (15)==(5)
 │    │    │    │    ├── scan student [as=this_]
 │    │    │    │    │    ├── columns: this_.studentid:1!null name:2!null address_city:3 address_state:4 preferredcoursecode:5
 │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    └── fd: (1)-->(2-5)
 │    │    │    │    ├── scan enrolment [as=maxstudentenrolment_]
 │    │    │    │    │    └── columns: maxstudentenrolment_.coursecode:15!null maxstudentenrolment_.year:17!null
 │    │    │    │    └── filters
 │    │    │    │         └── preferredcoursecode:5 = maxstudentenrolment_.coursecode:15 [outer=(5,15), constraints=(/5: (/NULL - ]; /15: (/NULL - ]), fd=(5)==(15), (15)==(5)]
 │    │    │    ├── scan enrolment [as=enrolment_]
 │    │    │    │    ├── columns: enrolment_.studentid:8!null enrolment_.coursecode:9!null enrolment_.year:11!null
 │    │    │    │    ├── key: (8,9)
 │    │    │    │    └── fd: (8,9)-->(11)
 │    │    │    └── filters (true)
 │    │    └── aggregations
 │    │         ├── max [as=max:20, outer=(17)]
 │    │         │    └── maxstudentenrolment_.year:17
 │    │         ├── const-agg [as=enrolment_.year:11, outer=(11)]
 │    │         │    └── enrolment_.year:11
 │    │         ├── const-agg [as=name:2, outer=(2)]
 │    │         │    └── name:2
 │    │         ├── const-agg [as=address_city:3, outer=(3)]
 │    │         │    └── address_city:3
 │    │         ├── const-agg [as=address_state:4, outer=(4)]
 │    │         │    └── address_state:4
 │    │         └── const-agg [as=preferredcoursecode:5, outer=(5)]
 │    │              └── preferredcoursecode:5
 │    └── filters
 │         └── enrolment_.year:11 = max:20 [outer=(11,20), constraints=(/11: (/NULL - ]; /20: (/NULL - ]), fd=(11)==(20), (20)==(11)]
 └── aggregations
      ├── const-agg [as=name:2, outer=(2)]
      │    └── name:2
      ├── const-agg [as=address_city:3, outer=(3)]
      │    └── address_city:3
      ├── const-agg [as=address_state:4, outer=(4)]
      │    └── address_state:4
      └── const-agg [as=preferredcoursecode:5, outer=(5)]
           └── preferredcoursecode:5

exec-ddl
drop table student, enrolment
----

# ------------------------------------------------------------------------------
# Query #13
#   org.hibernate.test.subselectfetch.SubselectFetchWithFormulaTest
#   TODO(andyk): Need to decorrelate LeftJoin -> Project complex.
# ------------------------------------------------------------------------------
exec-ddl
CREATE TABLE t_name (id INT4 NOT NULL, c_name VARCHAR(255), PRIMARY KEY (id));
----

opt
SELECT
  this_.id AS id1_0_0_,
  this_.c_name AS c_name2_0_0_,
  (SELECT length(this_.c_name) FROM t_name WHERE this_.id = t_name.id)
    AS formula0_0_
FROM
  t_name AS this_;
----
project
 ├── columns: id1_0_0_:1!null c_name2_0_0_:2 formula0_0_:10
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(2), (2)-->(10)
 ├── scan t_name [as=this_]
 │    ├── columns: this_.id:1!null this_.c_name:2
 │    ├── key: (1)
 │    └── fd: (1)-->(2)
 └── projections
      └── length(this_.c_name:2) [as=formula0_0_:10, outer=(2), immutable]

exec-ddl
drop table t_name
----
