exec-ddl
CREATE TABLE public.region
(
    r_regionkey int PRIMARY KEY,
    r_name char(25) NOT NULL,
    r_comment varchar(152)
);
----
TABLE region
 ├── r_regionkey int not null
 ├── r_name string not null
 ├── r_comment string
 └── INDEX primary
      └── r_regionkey int not null

exec-ddl
CREATE TABLE public.nation
(
    n_nationkey int PRIMARY KEY,
    n_name char(25) NOT NULL,
    n_regionkey int NOT NULL,
    n_comment varchar(152),
    INDEX n_rk (n_regionkey ASC),
    CONSTRAINT nation_fkey_region FOREIGN KEY (n_regionkey) references public.region (r_regionkey)
);
----
TABLE nation
 ├── n_nationkey int not null
 ├── n_name string not null
 ├── n_regionkey int not null
 ├── n_comment string
 ├── INDEX primary
 │    └── n_nationkey int not null
 ├── INDEX n_rk
 │    ├── n_regionkey int not null
 │    └── n_nationkey int not null
 └── FOREIGN KEY (n_regionkey) REFERENCES t.public.region (r_regionkey)

exec-ddl
CREATE TABLE public.supplier
(
    s_suppkey int PRIMARY KEY,
    s_name char(25) NOT NULL,
    s_address varchar(40) NOT NULL,
    s_nationkey int NOT NULL,
    s_phone char(15) NOT NULL,
    s_acctbal float NOT NULL,
    s_comment varchar(101) NOT NULL,
    INDEX s_nk (s_nationkey ASC),
    CONSTRAINT supplier_fkey_nation FOREIGN KEY (s_nationkey) references public.nation (n_nationkey)
);
----
TABLE supplier
 ├── s_suppkey int not null
 ├── s_name string not null
 ├── s_address string not null
 ├── s_nationkey int not null
 ├── s_phone string not null
 ├── s_acctbal float not null
 ├── s_comment string not null
 ├── INDEX primary
 │    └── s_suppkey int not null
 ├── INDEX s_nk
 │    ├── s_nationkey int not null
 │    └── s_suppkey int not null
 └── FOREIGN KEY (s_nationkey) REFERENCES t.public.nation (n_nationkey)

exec-ddl
CREATE TABLE public.part
(
    p_partkey int PRIMARY KEY,
    p_name varchar(55) NOT NULL,
    p_mfgr char(25) NOT NULL,
    p_brand char(10) NOT NULL,
    p_type varchar(25) NOT NULL,
    p_size int NOT NULL,
    p_container char(10) NOT NULL,
    p_retailprice float NOT NULL,
    p_comment varchar(23) NOT NULL
);
----
TABLE part
 ├── p_partkey int not null
 ├── p_name string not null
 ├── p_mfgr string not null
 ├── p_brand string not null
 ├── p_type string not null
 ├── p_size int not null
 ├── p_container string not null
 ├── p_retailprice float not null
 ├── p_comment string not null
 └── INDEX primary
      └── p_partkey int not null

exec-ddl
CREATE TABLE public.partsupp
(
    ps_partkey int NOT NULL,
    ps_suppkey int NOT NULL,
    ps_availqty int NOT NULL,
    ps_supplycost float NOT NULL,
    ps_comment varchar(199) NOT NULL,
    PRIMARY KEY (ps_partkey, ps_suppkey),
    INDEX ps_sk (ps_suppkey ASC),
    CONSTRAINT partsupp_fkey_part FOREIGN KEY (ps_partkey) references public.part (p_partkey),
    CONSTRAINT partsupp_fkey_supplier FOREIGN KEY (ps_suppkey) references public.supplier (s_suppkey)
);
----
TABLE partsupp
 ├── ps_partkey int not null
 ├── ps_suppkey int not null
 ├── ps_availqty int not null
 ├── ps_supplycost float not null
 ├── ps_comment string not null
 ├── INDEX primary
 │    ├── ps_partkey int not null
 │    └── ps_suppkey int not null
 ├── INDEX ps_sk
 │    ├── ps_suppkey int not null
 │    └── ps_partkey int not null
 ├── FOREIGN KEY (ps_partkey) REFERENCES t.public.part (p_partkey)
 └── FOREIGN KEY (ps_suppkey) REFERENCES t.public.supplier (s_suppkey)

exec-ddl
CREATE TABLE public.customer
(
    c_custkey int PRIMARY KEY,
    c_name varchar(25) NOT NULL,
    c_address varchar(40) NOT NULL,
    c_nationkey int NOT NULL NOT NULL,
    c_phone char(15) NOT NULL,
    c_acctbal float NOT NULL,
    c_mktsegment char(10) NOT NULL,
    c_comment varchar(117) NOT NULL,
    INDEX c_nk (c_nationkey ASC),
    CONSTRAINT customer_fkey_nation FOREIGN KEY (c_nationkey) references public.nation (n_nationkey)
);
----
TABLE customer
 ├── c_custkey int not null
 ├── c_name string not null
 ├── c_address string not null
 ├── c_nationkey int not null
 ├── c_phone string not null
 ├── c_acctbal float not null
 ├── c_mktsegment string not null
 ├── c_comment string not null
 ├── INDEX primary
 │    └── c_custkey int not null
 ├── INDEX c_nk
 │    ├── c_nationkey int not null
 │    └── c_custkey int not null
 └── FOREIGN KEY (c_nationkey) REFERENCES t.public.nation (n_nationkey)

exec-ddl
CREATE TABLE public.orders
(
    o_orderkey int PRIMARY KEY,
    o_custkey int NOT NULL,
    o_orderstatus char(1) NOT NULL,
    o_totalprice float NOT NULL,
    o_orderdate date NOT NULL,
    o_orderpriority char(15) NOT NULL,
    o_clerk char(15) NOT NULL,
    o_shippriority int NOT NULL,
    o_comment varchar(79) NOT NULL,
    INDEX o_ck (o_custkey ASC),
    INDEX o_od (o_orderdate ASC),
    CONSTRAINT orders_fkey_customer FOREIGN KEY (o_custkey) references public.customer (c_custkey)
);
----
TABLE orders
 ├── o_orderkey int not null
 ├── o_custkey int not null
 ├── o_orderstatus string not null
 ├── o_totalprice float not null
 ├── o_orderdate date not null
 ├── o_orderpriority string not null
 ├── o_clerk string not null
 ├── o_shippriority int not null
 ├── o_comment string not null
 ├── INDEX primary
 │    └── o_orderkey int not null
 ├── INDEX o_ck
 │    ├── o_custkey int not null
 │    └── o_orderkey int not null
 ├── INDEX o_od
 │    ├── o_orderdate date not null
 │    └── o_orderkey int not null
 └── FOREIGN KEY (o_custkey) REFERENCES t.public.customer (c_custkey)

exec-ddl
CREATE TABLE public.lineitem
(
    l_orderkey int NOT NULL,
    l_partkey int NOT NULL,
    l_suppkey int NOT NULL,
    l_linenumber int NOT NULL,
    l_quantity float NOT NULL,
    l_extendedprice float NOT NULL,
    l_discount float NOT NULL,
    l_tax float NOT NULL,
    l_returnflag char(1) NOT NULL,
    l_linestatus char(1) NOT NULL,
    l_shipdate date NOT NULL,
    l_commitdate date NOT NULL,
    l_receiptdate date NOT NULL,
    l_shipinstruct char(25) NOT NULL,
    l_shipmode char(10) NOT NULL,
    l_comment varchar(44) NOT NULL,
    PRIMARY KEY (l_orderkey, l_linenumber),
    INDEX l_ok (l_orderkey ASC),
    INDEX l_pk (l_partkey ASC),
    INDEX l_sk (l_suppkey ASC),
    INDEX l_sd (l_shipdate ASC),
    INDEX l_cd (l_commitdate ASC),
    INDEX l_rd (l_receiptdate ASC),
    INDEX l_pk_sk (l_partkey ASC, l_suppkey ASC),
    INDEX l_sk_pk (l_suppkey ASC, l_partkey ASC),
    CONSTRAINT lineitem_fkey_orders FOREIGN KEY (l_orderkey) references public.orders (o_orderkey),
    CONSTRAINT lineitem_fkey_part FOREIGN KEY (l_partkey) references public.part (p_partkey),
    CONSTRAINT lineitem_fkey_supplier FOREIGN KEY (l_suppkey) references public.supplier (s_suppkey),
    CONSTRAINT lineitem_fkey_partsupp FOREIGN KEY (l_partkey, l_suppkey) references public.partsupp (ps_partkey, ps_suppkey)
);
----
TABLE lineitem
 ├── l_orderkey int not null
 ├── l_partkey int not null
 ├── l_suppkey int not null
 ├── l_linenumber int not null
 ├── l_quantity float not null
 ├── l_extendedprice float not null
 ├── l_discount float not null
 ├── l_tax float not null
 ├── l_returnflag string not null
 ├── l_linestatus string not null
 ├── l_shipdate date not null
 ├── l_commitdate date not null
 ├── l_receiptdate date not null
 ├── l_shipinstruct string not null
 ├── l_shipmode string not null
 ├── l_comment string not null
 ├── INDEX primary
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── INDEX l_ok
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── INDEX l_pk
 │    ├── l_partkey int not null
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── INDEX l_sk
 │    ├── l_suppkey int not null
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── INDEX l_sd
 │    ├── l_shipdate date not null
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── INDEX l_cd
 │    ├── l_commitdate date not null
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── INDEX l_rd
 │    ├── l_receiptdate date not null
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── INDEX l_pk_sk
 │    ├── l_partkey int not null
 │    ├── l_suppkey int not null
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── INDEX l_sk_pk
 │    ├── l_suppkey int not null
 │    ├── l_partkey int not null
 │    ├── l_orderkey int not null
 │    └── l_linenumber int not null
 ├── FOREIGN KEY (l_orderkey) REFERENCES t.public.orders (o_orderkey)
 ├── FOREIGN KEY (l_partkey) REFERENCES t.public.part (p_partkey)
 ├── FOREIGN KEY (l_suppkey) REFERENCES t.public.supplier (s_suppkey)
 └── FOREIGN KEY (l_partkey, l_suppkey) REFERENCES t.public.partsupp (ps_partkey, ps_suppkey)

# --------------------------------------------------
# Q1
# Pricing Summary Report
# Reports the amount of business that was billed, shipped, and returned.
#
# Provides a summary pricing report for all lineitems shipped as of a given
# date. The date is within 60 - 120 days of the greatest ship date contained in
# the database. The query lists totals for extended price, discounted extended
# price, discounted extended price plus tax, average quantity, average extended
# price, and average discount. These aggregates are grouped by RETURNFLAG and
# LINESTATUS, and listed in ascending order of RETURNFLAG and LINESTATUS. A
# count of the number of lineitems in each group is included.
# --------------------------------------------------
opt
SELECT
    l_returnflag,
    l_linestatus,
    sum(l_quantity) AS sum_qty,
    sum(l_extendedprice) AS sum_base_price,
    sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price,
    sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge,
    avg(l_quantity) AS avg_qty,
    avg(l_extendedprice) AS avg_price,
    avg(l_discount) AS avg_disc,
    count(*) AS count_order
FROM
    lineitem
WHERE
    l_shipdate <= DATE '1998-12-01' - INTERVAL '90' DAY
GROUP BY
    l_returnflag,
    l_linestatus
ORDER BY
    l_returnflag,
    l_linestatus;
----
group-by
 ├── columns: l_returnflag:9(string!null) l_linestatus:10(string!null) sum_qty:17(float) sum_base_price:18(float) sum_disc_price:20(float) sum_charge:22(float) avg_qty:23(float) avg_price:24(float) avg_disc:25(float) count_order:26(int)
 ├── grouping columns: l_returnflag:9(string!null) l_linestatus:10(string!null)
 ├── key: (9,10)
 ├── fd: (9,10)-->(17,18,20,22-26)
 ├── ordering: +9,+10
 ├── sort
 │    ├── columns: l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_returnflag:9(string!null) l_linestatus:10(string!null) column19:19(float) column21:21(float)
 │    ├── ordering: +9,+10
 │    └── project
 │         ├── columns: column19:19(float) column21:21(float) l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_returnflag:9(string!null) l_linestatus:10(string!null)
 │         ├── select
 │         │    ├── columns: l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_tax:8(float!null) l_returnflag:9(string!null) l_linestatus:10(string!null) l_shipdate:11(date!null)
 │         │    ├── scan lineitem
 │         │    │    └── columns: l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_tax:8(float!null) l_returnflag:9(string!null) l_linestatus:10(string!null) l_shipdate:11(date!null)
 │         │    └── filters
 │         │         └── l_shipdate <= '1998-09-02' [type=bool, outer=(11), constraints=(/11: (/NULL - /'1998-09-02']; tight)]
 │         └── projections
 │              ├── l_extendedprice * (1.0 - l_discount) [type=float, outer=(6,7)]
 │              └── (l_extendedprice * (1.0 - l_discount)) * (l_tax + 1.0) [type=float, outer=(6-8)]
 └── aggregations
      ├── sum [type=float, outer=(5)]
      │    └── variable: l_quantity [type=float]
      ├── sum [type=float, outer=(6)]
      │    └── variable: l_extendedprice [type=float]
      ├── sum [type=float, outer=(19)]
      │    └── variable: column19 [type=float]
      ├── sum [type=float, outer=(21)]
      │    └── variable: column21 [type=float]
      ├── avg [type=float, outer=(5)]
      │    └── variable: l_quantity [type=float]
      ├── avg [type=float, outer=(6)]
      │    └── variable: l_extendedprice [type=float]
      ├── avg [type=float, outer=(7)]
      │    └── variable: l_discount [type=float]
      └── count-rows [type=int]

# --------------------------------------------------
# Q2
# Minimum Cost Supplier
# Finds which supplier should be selected to place an order for a given part in
# a given region.
#
# Finds, in a given region, for each part of a certain type and size, the
# supplier who can supply it at minimum cost. If several suppliers in that
# region offer the desired part type and size at the same (minimum) cost, the
# query lists the parts from suppliers with the 100 highest account balances.
# For each supplier, the query lists the supplier's account balance, name and
# nation; the part's number and manufacturer; the supplier's address, phone
# number and comment information.
#
# TODO:
#   1. Join ordering
#   2. Push down equivalent column comparisons
#   3. Allow Select to be pushed below RowNumber used to add key column
#   4. Add decorrelation rule for RowNumber/RowKey
# --------------------------------------------------
opt
SELECT
    s_acctbal,
    s_name,
    n_name,
    p_partkey,
    p_mfgr,
    s_address,
    s_phone,
    s_comment
FROM
    part,
    supplier,
    partsupp,
    nation,
    region
WHERE
    p_partkey = ps_partkey
    AND s_suppkey = ps_suppkey
    AND p_size = 15
    AND p_type LIKE '%BRASS'
    AND s_nationkey = n_nationkey
    AND n_regionkey = r_regionkey
    AND r_name = 'EUROPE'
    AND ps_supplycost = (
        SELECT
            min(ps_supplycost)
        FROM
            partsupp,
            supplier,
            nation,
            region
        WHERE
            p_partkey = ps_partkey
            AND s_suppkey = ps_suppkey
            AND s_nationkey = n_nationkey
            AND n_regionkey = r_regionkey
            AND r_name = 'EUROPE'
    )
ORDER BY
    s_acctbal DESC,
    n_name,
    s_name,
    p_partkey
LIMIT 100;
----
project
 ├── columns: s_acctbal:15(float) s_name:11(string) n_name:23(string) p_partkey:1(int) p_mfgr:3(string) s_address:12(string) s_phone:14(string) s_comment:16(string)
 ├── cardinality: [0 - 100]
 ├── fd: (1)-->(3)
 ├── ordering: -15,+23,+11,+1
 └── limit
      ├── columns: p_partkey:1(int) p_mfgr:3(string) s_name:11(string) s_address:12(string) s_phone:14(string) s_acctbal:15(float) s_comment:16(string) ps_partkey:17(int!null) ps_suppkey:18(int!null) ps_supplycost:20(float!null) n_name:23(string) min:48(float!null)
      ├── internal-ordering: -15,+23,+11,+(1|17)
      ├── cardinality: [0 - 100]
      ├── key: (17,18)
      ├── fd: (1)-->(3), (17,18)-->(1,3,11,12,14-16,20,23,48), (1)==(17), (17)==(1), (18)-->(11,12,14-16,23), (20)==(48), (48)==(20)
      ├── ordering: -15,+23,+11,+(1|17) [actual: -15,+23,+11,+1]
      ├── sort
      │    ├── columns: p_partkey:1(int) p_mfgr:3(string) s_name:11(string) s_address:12(string) s_phone:14(string) s_acctbal:15(float) s_comment:16(string) ps_partkey:17(int!null) ps_suppkey:18(int!null) ps_supplycost:20(float!null) n_name:23(string) min:48(float!null)
      │    ├── key: (17,18)
      │    ├── fd: (1)-->(3), (17,18)-->(1,3,11,12,14-16,20,23,48), (1)==(17), (17)==(1), (18)-->(11,12,14-16,23), (20)==(48), (48)==(20)
      │    ├── ordering: -15,+23,+11,+(1|17) [actual: -15,+23,+11,+1]
      │    └── select
      │         ├── columns: p_partkey:1(int) p_mfgr:3(string) s_name:11(string) s_address:12(string) s_phone:14(string) s_acctbal:15(float) s_comment:16(string) ps_partkey:17(int!null) ps_suppkey:18(int!null) ps_supplycost:20(float!null) n_name:23(string) min:48(float!null)
      │         ├── key: (17,18)
      │         ├── fd: (1)-->(3), (17,18)-->(1,3,11,12,14-16,20,23,48), (1)==(17), (17)==(1), (18)-->(11,12,14-16,23), (20)==(48), (48)==(20)
      │         ├── group-by
      │         │    ├── columns: p_partkey:1(int) p_mfgr:3(string) s_name:11(string) s_address:12(string) s_phone:14(string) s_acctbal:15(float) s_comment:16(string) ps_partkey:17(int!null) ps_suppkey:18(int!null) ps_supplycost:20(float) n_name:23(string) min:48(float)
      │         │    ├── grouping columns: ps_partkey:17(int!null) ps_suppkey:18(int!null)
      │         │    ├── key: (17,18)
      │         │    ├── fd: (1)-->(3), (17,18)-->(1,3,11,12,14-16,20,23,48), (1)==(17), (17)==(1), (18)-->(11,12,14-16,23)
      │         │    ├── inner-join
      │         │    │    ├── columns: p_partkey:1(int!null) p_mfgr:3(string!null) p_type:5(string!null) p_size:6(int!null) s_suppkey:10(int!null) s_name:11(string!null) s_address:12(string!null) s_nationkey:13(int!null) s_phone:14(string!null) s_acctbal:15(float!null) s_comment:16(string!null) ps_partkey:17(int!null) ps_suppkey:18(int!null) ps_supplycost:20(float!null) n_nationkey:22(int!null) n_name:23(string!null) n_regionkey:24(int!null) r_regionkey:26(int!null) r_name:27(string!null) ps_partkey:29(int!null) ps_suppkey:30(int!null) ps_supplycost:32(float!null) s_suppkey:34(int!null) s_nationkey:37(int!null) n_nationkey:41(int!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │         │    │    ├── key: (18,29,34)
      │         │    │    ├── fd: ()-->(6,27,46), (1)-->(3,5), (10)-->(11-16), (17,18)-->(20), (22)-->(23,24), (24)==(26), (26)==(24), (10)==(18), (18)==(10), (13)==(22), (22)==(13), (1)==(17,29), (17)==(1,29), (29,30)-->(32), (34)-->(37), (41)-->(43), (43)==(45), (45)==(43), (37)==(41), (41)==(37), (30)==(34), (34)==(30), (29)==(1,17)
      │         │    │    ├── inner-join
      │         │    │    │    ├── columns: ps_partkey:29(int!null) ps_suppkey:30(int!null) ps_supplycost:32(float!null) s_suppkey:34(int!null) s_nationkey:37(int!null) n_nationkey:41(int!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │         │    │    │    ├── key: (29,34)
      │         │    │    │    ├── fd: ()-->(46), (29,30)-->(32), (34)-->(37), (41)-->(43), (43)==(45), (45)==(43), (37)==(41), (41)==(37), (30)==(34), (34)==(30)
      │         │    │    │    ├── scan partsupp
      │         │    │    │    │    ├── columns: ps_partkey:29(int!null) ps_suppkey:30(int!null) ps_supplycost:32(float!null)
      │         │    │    │    │    ├── key: (29,30)
      │         │    │    │    │    └── fd: (29,30)-->(32)
      │         │    │    │    ├── inner-join
      │         │    │    │    │    ├── columns: s_suppkey:34(int!null) s_nationkey:37(int!null) n_nationkey:41(int!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │         │    │    │    │    ├── key: (34)
      │         │    │    │    │    ├── fd: ()-->(46), (34)-->(37), (41)-->(43), (43)==(45), (45)==(43), (37)==(41), (41)==(37)
      │         │    │    │    │    ├── scan supplier@s_nk
      │         │    │    │    │    │    ├── columns: s_suppkey:34(int!null) s_nationkey:37(int!null)
      │         │    │    │    │    │    ├── key: (34)
      │         │    │    │    │    │    └── fd: (34)-->(37)
      │         │    │    │    │    ├── inner-join (lookup nation@n_rk)
      │         │    │    │    │    │    ├── columns: n_nationkey:41(int!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │         │    │    │    │    │    ├── key columns: [45] = [43]
      │         │    │    │    │    │    ├── key: (41)
      │         │    │    │    │    │    ├── fd: ()-->(46), (41)-->(43), (43)==(45), (45)==(43)
      │         │    │    │    │    │    ├── select
      │         │    │    │    │    │    │    ├── columns: r_regionkey:45(int!null) r_name:46(string!null)
      │         │    │    │    │    │    │    ├── key: (45)
      │         │    │    │    │    │    │    ├── fd: ()-->(46)
      │         │    │    │    │    │    │    ├── scan region
      │         │    │    │    │    │    │    │    ├── columns: r_regionkey:45(int!null) r_name:46(string!null)
      │         │    │    │    │    │    │    │    ├── key: (45)
      │         │    │    │    │    │    │    │    └── fd: (45)-->(46)
      │         │    │    │    │    │    │    └── filters
      │         │    │    │    │    │    │         └── r_name = 'EUROPE' [type=bool, outer=(46), constraints=(/46: [/'EUROPE' - /'EUROPE']; tight), fd=()-->(46)]
      │         │    │    │    │    │    └── filters (true)
      │         │    │    │    │    └── filters
      │         │    │    │    │         └── s_nationkey = n_nationkey [type=bool, outer=(37,41), constraints=(/37: (/NULL - ]; /41: (/NULL - ]), fd=(37)==(41), (41)==(37)]
      │         │    │    │    └── filters
      │         │    │    │         └── s_suppkey = ps_suppkey [type=bool, outer=(30,34), constraints=(/30: (/NULL - ]; /34: (/NULL - ]), fd=(30)==(34), (34)==(30)]
      │         │    │    ├── inner-join
      │         │    │    │    ├── columns: p_partkey:1(int!null) p_mfgr:3(string!null) p_type:5(string!null) p_size:6(int!null) s_suppkey:10(int!null) s_name:11(string!null) s_address:12(string!null) s_nationkey:13(int!null) s_phone:14(string!null) s_acctbal:15(float!null) s_comment:16(string!null) ps_partkey:17(int!null) ps_suppkey:18(int!null) ps_supplycost:20(float!null) n_nationkey:22(int!null) n_name:23(string!null) n_regionkey:24(int!null) r_regionkey:26(int!null) r_name:27(string!null)
      │         │    │    │    ├── key: (17,18)
      │         │    │    │    ├── fd: ()-->(6,27), (1)-->(3,5), (10)-->(11-16), (17,18)-->(20), (22)-->(23,24), (24)==(26), (26)==(24), (10)==(18), (18)==(10), (13)==(22), (22)==(13), (1)==(17), (17)==(1)
      │         │    │    │    ├── inner-join
      │         │    │    │    │    ├── columns: s_suppkey:10(int!null) s_name:11(string!null) s_address:12(string!null) s_nationkey:13(int!null) s_phone:14(string!null) s_acctbal:15(float!null) s_comment:16(string!null) ps_partkey:17(int!null) ps_suppkey:18(int!null) ps_supplycost:20(float!null) n_nationkey:22(int!null) n_name:23(string!null) n_regionkey:24(int!null) r_regionkey:26(int!null) r_name:27(string!null)
      │         │    │    │    │    ├── key: (17,18)
      │         │    │    │    │    ├── fd: ()-->(27), (10)-->(11-16), (17,18)-->(20), (22)-->(23,24), (24)==(26), (26)==(24), (10)==(18), (18)==(10), (13)==(22), (22)==(13)
      │         │    │    │    │    ├── scan partsupp
      │         │    │    │    │    │    ├── columns: ps_partkey:17(int!null) ps_suppkey:18(int!null) ps_supplycost:20(float!null)
      │         │    │    │    │    │    ├── key: (17,18)
      │         │    │    │    │    │    └── fd: (17,18)-->(20)
      │         │    │    │    │    ├── inner-join
      │         │    │    │    │    │    ├── columns: s_suppkey:10(int!null) s_name:11(string!null) s_address:12(string!null) s_nationkey:13(int!null) s_phone:14(string!null) s_acctbal:15(float!null) s_comment:16(string!null) n_nationkey:22(int!null) n_name:23(string!null) n_regionkey:24(int!null) r_regionkey:26(int!null) r_name:27(string!null)
      │         │    │    │    │    │    ├── key: (10)
      │         │    │    │    │    │    ├── fd: ()-->(27), (22)-->(23,24), (24)==(26), (26)==(24), (10)-->(11-16), (13)==(22), (22)==(13)
      │         │    │    │    │    │    ├── scan supplier
      │         │    │    │    │    │    │    ├── columns: s_suppkey:10(int!null) s_name:11(string!null) s_address:12(string!null) s_nationkey:13(int!null) s_phone:14(string!null) s_acctbal:15(float!null) s_comment:16(string!null)
      │         │    │    │    │    │    │    ├── key: (10)
      │         │    │    │    │    │    │    └── fd: (10)-->(11-16)
      │         │    │    │    │    │    ├── inner-join (lookup nation)
      │         │    │    │    │    │    │    ├── columns: n_nationkey:22(int!null) n_name:23(string!null) n_regionkey:24(int!null) r_regionkey:26(int!null) r_name:27(string!null)
      │         │    │    │    │    │    │    ├── key columns: [22] = [22]
      │         │    │    │    │    │    │    ├── key: (22)
      │         │    │    │    │    │    │    ├── fd: ()-->(27), (22)-->(23,24), (24)==(26), (26)==(24)
      │         │    │    │    │    │    │    ├── inner-join (lookup nation@n_rk)
      │         │    │    │    │    │    │    │    ├── columns: n_nationkey:22(int!null) n_regionkey:24(int!null) r_regionkey:26(int!null) r_name:27(string!null)
      │         │    │    │    │    │    │    │    ├── key columns: [26] = [24]
      │         │    │    │    │    │    │    │    ├── key: (22)
      │         │    │    │    │    │    │    │    ├── fd: ()-->(27), (22)-->(24), (24)==(26), (26)==(24)
      │         │    │    │    │    │    │    │    ├── select
      │         │    │    │    │    │    │    │    │    ├── columns: r_regionkey:26(int!null) r_name:27(string!null)
      │         │    │    │    │    │    │    │    │    ├── key: (26)
      │         │    │    │    │    │    │    │    │    ├── fd: ()-->(27)
      │         │    │    │    │    │    │    │    │    ├── scan region
      │         │    │    │    │    │    │    │    │    │    ├── columns: r_regionkey:26(int!null) r_name:27(string!null)
      │         │    │    │    │    │    │    │    │    │    ├── key: (26)
      │         │    │    │    │    │    │    │    │    │    └── fd: (26)-->(27)
      │         │    │    │    │    │    │    │    │    └── filters
      │         │    │    │    │    │    │    │    │         └── r_name = 'EUROPE' [type=bool, outer=(27), constraints=(/27: [/'EUROPE' - /'EUROPE']; tight), fd=()-->(27)]
      │         │    │    │    │    │    │    │    └── filters (true)
      │         │    │    │    │    │    │    └── filters (true)
      │         │    │    │    │    │    └── filters
      │         │    │    │    │    │         └── s_nationkey = n_nationkey [type=bool, outer=(13,22), constraints=(/13: (/NULL - ]; /22: (/NULL - ]), fd=(13)==(22), (22)==(13)]
      │         │    │    │    │    └── filters
      │         │    │    │    │         └── s_suppkey = ps_suppkey [type=bool, outer=(10,18), constraints=(/10: (/NULL - ]; /18: (/NULL - ]), fd=(10)==(18), (18)==(10)]
      │         │    │    │    ├── select
      │         │    │    │    │    ├── columns: p_partkey:1(int!null) p_mfgr:3(string!null) p_type:5(string!null) p_size:6(int!null)
      │         │    │    │    │    ├── key: (1)
      │         │    │    │    │    ├── fd: ()-->(6), (1)-->(3,5)
      │         │    │    │    │    ├── scan part
      │         │    │    │    │    │    ├── columns: p_partkey:1(int!null) p_mfgr:3(string!null) p_type:5(string!null) p_size:6(int!null)
      │         │    │    │    │    │    ├── key: (1)
      │         │    │    │    │    │    └── fd: (1)-->(3,5,6)
      │         │    │    │    │    └── filters
      │         │    │    │    │         ├── p_size = 15 [type=bool, outer=(6), constraints=(/6: [/15 - /15]; tight), fd=()-->(6)]
      │         │    │    │    │         └── p_type LIKE '%BRASS' [type=bool, outer=(5), constraints=(/5: (/NULL - ])]
      │         │    │    │    └── filters
      │         │    │    │         └── p_partkey = ps_partkey [type=bool, outer=(1,17), constraints=(/1: (/NULL - ]; /17: (/NULL - ]), fd=(1)==(17), (17)==(1)]
      │         │    │    └── filters
      │         │    │         └── p_partkey = ps_partkey [type=bool, outer=(1,29), constraints=(/1: (/NULL - ]; /29: (/NULL - ]), fd=(1)==(29), (29)==(1)]
      │         │    └── aggregations
      │         │         ├── min [type=float, outer=(32)]
      │         │         │    └── variable: ps_supplycost [type=float]
      │         │         ├── const-agg [type=string, outer=(11)]
      │         │         │    └── variable: s_name [type=string]
      │         │         ├── const-agg [type=string, outer=(12)]
      │         │         │    └── variable: s_address [type=string]
      │         │         ├── const-agg [type=string, outer=(14)]
      │         │         │    └── variable: s_phone [type=string]
      │         │         ├── const-agg [type=float, outer=(15)]
      │         │         │    └── variable: s_acctbal [type=float]
      │         │         ├── const-agg [type=string, outer=(16)]
      │         │         │    └── variable: s_comment [type=string]
      │         │         ├── const-agg [type=float, outer=(20)]
      │         │         │    └── variable: ps_supplycost [type=float]
      │         │         ├── const-agg [type=string, outer=(23)]
      │         │         │    └── variable: n_name [type=string]
      │         │         ├── const-agg [type=string, outer=(3)]
      │         │         │    └── variable: p_mfgr [type=string]
      │         │         └── const-agg [type=int, outer=(1)]
      │         │              └── variable: p_partkey [type=int]
      │         └── filters
      │              └── ps_supplycost = min [type=bool, outer=(20,48), constraints=(/20: (/NULL - ]; /48: (/NULL - ]), fd=(20)==(48), (48)==(20)]
      └── const: 100 [type=int]

# --------------------------------------------------
# Q3
# Shipping Priority
# Retrieves the 10 unshipped orders with the highest value.
#
# Retrieves the shipping priority and potential revenue, defined as the sum of
# l_extendedprice * (1-l_discount), of the orders having the largest revenue
# among those that had not been shipped as of a given date. Orders are listed in
# decreasing order of revenue. If more than 10 unshipped orders exist, only the
# 10 orders with the largest revenue are listed.
# --------------------------------------------------
opt
SELECT
    l_orderkey,
    sum(l_extendedprice * (1 - l_discount)) AS revenue,
    o_orderdate,
    o_shippriority
FROM
    customer,
    orders,
    lineitem
WHERE
    c_mktsegment = 'BUILDING'
    AND c_custkey = o_custkey
    AND l_orderkey = o_orderkey
    AND o_orderDATE < DATE '1995-03-15'
    AND l_shipdate > DATE '1995-03-15'
GROUP BY
    l_orderkey,
    o_orderdate,
    o_shippriority
ORDER BY
    revenue DESC,
    o_orderdate
LIMIT 10;
----
limit
 ├── columns: l_orderkey:18(int!null) revenue:35(float) o_orderdate:13(date) o_shippriority:16(int)
 ├── internal-ordering: -35,+13
 ├── cardinality: [0 - 10]
 ├── key: (18)
 ├── fd: (18)-->(13,16,35)
 ├── ordering: -35,+13
 ├── sort
 │    ├── columns: o_orderdate:13(date) o_shippriority:16(int) l_orderkey:18(int!null) sum:35(float)
 │    ├── key: (18)
 │    ├── fd: (18)-->(13,16,35)
 │    ├── ordering: -35,+13
 │    └── group-by
 │         ├── columns: o_orderdate:13(date) o_shippriority:16(int) l_orderkey:18(int!null) sum:35(float)
 │         ├── grouping columns: l_orderkey:18(int!null)
 │         ├── key: (18)
 │         ├── fd: (18)-->(13,16,35)
 │         ├── project
 │         │    ├── columns: column34:34(float) o_orderdate:13(date!null) o_shippriority:16(int!null) l_orderkey:18(int!null)
 │         │    ├── fd: (18)-->(13,16)
 │         │    ├── inner-join (lookup lineitem)
 │         │    │    ├── columns: c_custkey:1(int!null) c_mktsegment:7(string!null) o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null) o_shippriority:16(int!null) l_orderkey:18(int!null) l_extendedprice:23(float!null) l_discount:24(float!null) l_shipdate:28(date!null)
 │         │    │    ├── key columns: [9] = [18]
 │         │    │    ├── fd: ()-->(7), (9)-->(10,13,16), (9)==(18), (18)==(9), (1)==(10), (10)==(1)
 │         │    │    ├── inner-join (lookup orders)
 │         │    │    │    ├── columns: c_custkey:1(int!null) c_mktsegment:7(string!null) o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null) o_shippriority:16(int!null)
 │         │    │    │    ├── key columns: [9] = [9]
 │         │    │    │    ├── key: (9)
 │         │    │    │    ├── fd: ()-->(7), (9)-->(10,13,16), (1)==(10), (10)==(1)
 │         │    │    │    ├── inner-join (lookup orders@o_ck)
 │         │    │    │    │    ├── columns: c_custkey:1(int!null) c_mktsegment:7(string!null) o_orderkey:9(int!null) o_custkey:10(int!null)
 │         │    │    │    │    ├── key columns: [1] = [10]
 │         │    │    │    │    ├── key: (9)
 │         │    │    │    │    ├── fd: ()-->(7), (9)-->(10), (1)==(10), (10)==(1)
 │         │    │    │    │    ├── select
 │         │    │    │    │    │    ├── columns: c_custkey:1(int!null) c_mktsegment:7(string!null)
 │         │    │    │    │    │    ├── key: (1)
 │         │    │    │    │    │    ├── fd: ()-->(7)
 │         │    │    │    │    │    ├── scan customer
 │         │    │    │    │    │    │    ├── columns: c_custkey:1(int!null) c_mktsegment:7(string!null)
 │         │    │    │    │    │    │    ├── key: (1)
 │         │    │    │    │    │    │    └── fd: (1)-->(7)
 │         │    │    │    │    │    └── filters
 │         │    │    │    │    │         └── c_mktsegment = 'BUILDING' [type=bool, outer=(7), constraints=(/7: [/'BUILDING' - /'BUILDING']; tight), fd=()-->(7)]
 │         │    │    │    │    └── filters (true)
 │         │    │    │    └── filters
 │         │    │    │         └── o_orderdate < '1995-03-15' [type=bool, outer=(13), constraints=(/13: (/NULL - /'1995-03-14']; tight)]
 │         │    │    └── filters
 │         │    │         └── l_shipdate > '1995-03-15' [type=bool, outer=(28), constraints=(/28: [/'1995-03-16' - ]; tight)]
 │         │    └── projections
 │         │         └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(23,24)]
 │         └── aggregations
 │              ├── sum [type=float, outer=(34)]
 │              │    └── variable: column34 [type=float]
 │              ├── const-agg [type=date, outer=(13)]
 │              │    └── variable: o_orderdate [type=date]
 │              └── const-agg [type=int, outer=(16)]
 │                   └── variable: o_shippriority [type=int]
 └── const: 10 [type=int]

# --------------------------------------------------
# Q4
# Order Priority Checking
# Determines how well the order priority system is working and gives an
# assessment of customer satisfaction.
#
# Counts the number of orders ordered in a given quarter of a given year in
# which at least one lineitem was received by the customer later than its
# committed date. The query lists the count of such orders for each order
# priority sorted in ascending priority order.
# --------------------------------------------------
opt
SELECT
    o_orderpriority,
    count(*) AS order_count
FROM
    orders
WHERE
    o_orderdate >= DATE '1993-07-01'
    AND o_orderdate < DATE '1993-07-01' + INTERVAL '3' MONTH
    AND EXISTS (
        SELECT
            *
        FROM
            lineitem
        WHERE
            l_orderkey = o_orderkey
            AND l_commitDATE < l_receiptdate
    )
GROUP BY
    o_orderpriority
ORDER BY
    o_orderpriority;
----
sort
 ├── columns: o_orderpriority:6(string!null) order_count:26(int)
 ├── key: (6)
 ├── fd: (6)-->(26)
 ├── ordering: +6
 └── group-by
      ├── columns: o_orderpriority:6(string!null) count_rows:26(int)
      ├── grouping columns: o_orderpriority:6(string!null)
      ├── key: (6)
      ├── fd: (6)-->(26)
      ├── semi-join
      │    ├── columns: o_orderkey:1(int!null) o_orderdate:5(date!null) o_orderpriority:6(string!null)
      │    ├── key: (1)
      │    ├── fd: (1)-->(5,6)
      │    ├── index-join orders
      │    │    ├── columns: o_orderkey:1(int!null) o_orderdate:5(date!null) o_orderpriority:6(string!null)
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(5,6)
      │    │    └── scan orders@o_od
      │    │         ├── columns: o_orderkey:1(int!null) o_orderdate:5(date!null)
      │    │         ├── constraint: /5/1: [/'1993-07-01' - /'1993-09-30']
      │    │         ├── key: (1)
      │    │         └── fd: (1)-->(5)
      │    ├── select
      │    │    ├── columns: l_orderkey:10(int!null) l_commitdate:21(date!null) l_receiptdate:22(date!null)
      │    │    ├── scan lineitem
      │    │    │    └── columns: l_orderkey:10(int!null) l_commitdate:21(date!null) l_receiptdate:22(date!null)
      │    │    └── filters
      │    │         └── l_commitdate < l_receiptdate [type=bool, outer=(21,22), constraints=(/21: (/NULL - ]; /22: (/NULL - ])]
      │    └── filters
      │         └── l_orderkey = o_orderkey [type=bool, outer=(1,10), constraints=(/1: (/NULL - ]; /10: (/NULL - ]), fd=(1)==(10), (10)==(1)]
      └── aggregations
           └── count-rows [type=int]

# --------------------------------------------------
# Q5
# Local Supplier Volume
# Lists the revenue volume done through local suppliers.
#
# Lists for each nation in a region the revenue volume that resulted from
# lineitem transactions in which the customer ordering parts and the supplier
# filling them were both within that nation. The query is run in order to
# determine whether to institute local distribution centers in a given region.
# The query considers only parts ordered in a given year. The query displays the
# nations and revenue volume in descending order by revenue. Revenue volume for
# all qualifying lineitems in a particular nation is defined as
# sum(l_extendedprice * (1 - l_discount)).
#
# TODO:
#   1. Join ordering
# --------------------------------------------------
opt
SELECT
    n_name,
    sum(l_extendedprice * (1 - l_discount)) AS revenue
FROM
    customer,
    orders,
    lineitem,
    supplier,
    nation,
    region
WHERE
    c_custkey = o_custkey
    AND l_orderkey = o_orderkey
    AND l_suppkey = s_suppkey
    AND c_nationkey = s_nationkey
    AND s_nationkey = n_nationkey
    AND n_regionkey = r_regionkey
    AND r_name = 'ASIA'
    AND o_orderDATE >= DATE '1994-01-01'
    AND o_orderDATE < DATE '1994-01-01' + INTERVAL '1' YEAR
GROUP BY
    n_name
ORDER BY
    revenue DESC;
----
sort
 ├── columns: n_name:42(string!null) revenue:49(float)
 ├── key: (42)
 ├── fd: (42)-->(49)
 ├── ordering: -49
 └── group-by
      ├── columns: n_name:42(string!null) sum:49(float)
      ├── grouping columns: n_name:42(string!null)
      ├── key: (42)
      ├── fd: (42)-->(49)
      ├── project
      │    ├── columns: column48:48(float) n_name:42(string!null)
      │    ├── inner-join
      │    │    ├── columns: c_custkey:1(int!null) c_nationkey:4(int!null) o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null) l_orderkey:18(int!null) l_suppkey:20(int!null) l_extendedprice:23(float!null) l_discount:24(float!null) s_suppkey:34(int!null) s_nationkey:37(int!null) n_nationkey:41(int!null) n_name:42(string!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │    │    ├── fd: ()-->(46), (1)-->(4), (9)-->(10,13), (34)-->(37), (41)-->(42,43), (43)==(45), (45)==(43), (37)==(4,41), (41)==(4,37), (20)==(34), (34)==(20), (9)==(18), (18)==(9), (1)==(10), (10)==(1), (4)==(37,41)
      │    │    ├── inner-join
      │    │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null) l_orderkey:18(int!null) l_suppkey:20(int!null) l_extendedprice:23(float!null) l_discount:24(float!null) s_suppkey:34(int!null) s_nationkey:37(int!null) n_nationkey:41(int!null) n_name:42(string!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │    │    │    ├── fd: ()-->(46), (9)-->(10,13), (34)-->(37), (41)-->(42,43), (43)==(45), (45)==(43), (37)==(41), (41)==(37), (20)==(34), (34)==(20), (9)==(18), (18)==(9)
      │    │    │    ├── inner-join
      │    │    │    │    ├── columns: l_orderkey:18(int!null) l_suppkey:20(int!null) l_extendedprice:23(float!null) l_discount:24(float!null) s_suppkey:34(int!null) s_nationkey:37(int!null) n_nationkey:41(int!null) n_name:42(string!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │    │    │    │    ├── fd: ()-->(46), (34)-->(37), (41)-->(42,43), (43)==(45), (45)==(43), (37)==(41), (41)==(37), (20)==(34), (34)==(20)
      │    │    │    │    ├── scan lineitem
      │    │    │    │    │    └── columns: l_orderkey:18(int!null) l_suppkey:20(int!null) l_extendedprice:23(float!null) l_discount:24(float!null)
      │    │    │    │    ├── inner-join
      │    │    │    │    │    ├── columns: s_suppkey:34(int!null) s_nationkey:37(int!null) n_nationkey:41(int!null) n_name:42(string!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │    │    │    │    │    ├── key: (34)
      │    │    │    │    │    ├── fd: ()-->(46), (34)-->(37), (41)-->(42,43), (43)==(45), (45)==(43), (37)==(41), (41)==(37)
      │    │    │    │    │    ├── scan supplier@s_nk
      │    │    │    │    │    │    ├── columns: s_suppkey:34(int!null) s_nationkey:37(int!null)
      │    │    │    │    │    │    ├── key: (34)
      │    │    │    │    │    │    └── fd: (34)-->(37)
      │    │    │    │    │    ├── inner-join (lookup nation)
      │    │    │    │    │    │    ├── columns: n_nationkey:41(int!null) n_name:42(string!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │    │    │    │    │    │    ├── key columns: [41] = [41]
      │    │    │    │    │    │    ├── key: (41)
      │    │    │    │    │    │    ├── fd: ()-->(46), (41)-->(42,43), (43)==(45), (45)==(43)
      │    │    │    │    │    │    ├── inner-join (lookup nation@n_rk)
      │    │    │    │    │    │    │    ├── columns: n_nationkey:41(int!null) n_regionkey:43(int!null) r_regionkey:45(int!null) r_name:46(string!null)
      │    │    │    │    │    │    │    ├── key columns: [45] = [43]
      │    │    │    │    │    │    │    ├── key: (41)
      │    │    │    │    │    │    │    ├── fd: ()-->(46), (41)-->(43), (43)==(45), (45)==(43)
      │    │    │    │    │    │    │    ├── select
      │    │    │    │    │    │    │    │    ├── columns: r_regionkey:45(int!null) r_name:46(string!null)
      │    │    │    │    │    │    │    │    ├── key: (45)
      │    │    │    │    │    │    │    │    ├── fd: ()-->(46)
      │    │    │    │    │    │    │    │    ├── scan region
      │    │    │    │    │    │    │    │    │    ├── columns: r_regionkey:45(int!null) r_name:46(string!null)
      │    │    │    │    │    │    │    │    │    ├── key: (45)
      │    │    │    │    │    │    │    │    │    └── fd: (45)-->(46)
      │    │    │    │    │    │    │    │    └── filters
      │    │    │    │    │    │    │    │         └── r_name = 'ASIA' [type=bool, outer=(46), constraints=(/46: [/'ASIA' - /'ASIA']; tight), fd=()-->(46)]
      │    │    │    │    │    │    │    └── filters (true)
      │    │    │    │    │    │    └── filters (true)
      │    │    │    │    │    └── filters
      │    │    │    │    │         └── s_nationkey = n_nationkey [type=bool, outer=(37,41), constraints=(/37: (/NULL - ]; /41: (/NULL - ]), fd=(37)==(41), (41)==(37)]
      │    │    │    │    └── filters
      │    │    │    │         └── l_suppkey = s_suppkey [type=bool, outer=(20,34), constraints=(/20: (/NULL - ]; /34: (/NULL - ]), fd=(20)==(34), (34)==(20)]
      │    │    │    ├── index-join orders
      │    │    │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null)
      │    │    │    │    ├── key: (9)
      │    │    │    │    ├── fd: (9)-->(10,13)
      │    │    │    │    └── scan orders@o_od
      │    │    │    │         ├── columns: o_orderkey:9(int!null) o_orderdate:13(date!null)
      │    │    │    │         ├── constraint: /13/9: [/'1994-01-01' - /'1994-12-31']
      │    │    │    │         ├── key: (9)
      │    │    │    │         └── fd: (9)-->(13)
      │    │    │    └── filters
      │    │    │         └── l_orderkey = o_orderkey [type=bool, outer=(9,18), constraints=(/9: (/NULL - ]; /18: (/NULL - ]), fd=(9)==(18), (18)==(9)]
      │    │    ├── scan customer@c_nk
      │    │    │    ├── columns: c_custkey:1(int!null) c_nationkey:4(int!null)
      │    │    │    ├── key: (1)
      │    │    │    └── fd: (1)-->(4)
      │    │    └── filters
      │    │         ├── c_custkey = o_custkey [type=bool, outer=(1,10), constraints=(/1: (/NULL - ]; /10: (/NULL - ]), fd=(1)==(10), (10)==(1)]
      │    │         └── c_nationkey = s_nationkey [type=bool, outer=(4,37), constraints=(/4: (/NULL - ]; /37: (/NULL - ]), fd=(4)==(37), (37)==(4)]
      │    └── projections
      │         └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(23,24)]
      └── aggregations
           └── sum [type=float, outer=(48)]
                └── variable: column48 [type=float]

# --------------------------------------------------
# Q6
# Forecasting Revenue Change
# Quantifies the amount of revenue increase that would have resulted from
# eliminating certain companywide discounts in a given percentage range in a
# given year. Asking this type of "what if" query can be used to look for ways
# to increase revenues.
#
# Considers all the lineitems shipped in a given year with discounts between
# DISCOUNT-0.01 and DISCOUNT+0.01. The query lists the amount by which the total
# revenue would have increased if these discounts had been eliminated for
# lineitems with l_quantity less than quantity. Note that the potential revenue
# increase is equal to the sum of [l_extendedprice * l_discount] for all
# lineitems with discounts and quantities in the qualifying range.
# --------------------------------------------------
opt
SELECT
    sum(l_extendedprice * l_discount) AS revenue
FROM
    lineitem
WHERE
    l_shipdate >= DATE '1994-01-01'
    AND l_shipdate < DATE '1994-01-01' + INTERVAL '1' YEAR
    AND l_discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01
    AND l_quantity < 24;
----
scalar-group-by
 ├── columns: revenue:18(float)
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(18)
 ├── project
 │    ├── columns: column17:17(float)
 │    ├── select
 │    │    ├── columns: l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipdate:11(date!null)
 │    │    ├── index-join lineitem
 │    │    │    ├── columns: l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipdate:11(date!null)
 │    │    │    └── scan lineitem@l_sd
 │    │    │         ├── columns: l_orderkey:1(int!null) l_linenumber:4(int!null) l_shipdate:11(date!null)
 │    │    │         ├── constraint: /11/1/4: [/'1994-01-01' - /'1994-12-31']
 │    │    │         ├── key: (1,4)
 │    │    │         └── fd: (1,4)-->(11)
 │    │    └── filters
 │    │         ├── (l_discount >= 0.05) AND (l_discount <= 0.07) [type=bool, outer=(7), constraints=(/7: [/0.05 - /0.07]; tight)]
 │    │         └── l_quantity < 24.0 [type=bool, outer=(5), constraints=(/5: (/NULL - /23.999999999999996]; tight)]
 │    └── projections
 │         └── l_extendedprice * l_discount [type=float, outer=(6,7)]
 └── aggregations
      └── sum [type=float, outer=(17)]
           └── variable: column17 [type=float]

# --------------------------------------------------
# Q7
# Volume Shipping
# Determines the value of goods shipped between certain nations to help in the
# re-negotiation of shipping contracts.
#
# Finds, for two given nations, the gross discounted revenues derived from
# lineitems in which parts were shipped from a supplier in either nation to a
# customer in the other nation during 1995 and 1996. The query lists the
# supplier nation, the customer nation, the year, and the revenue from shipments
# that took place in that year. The query orders the answer by Supplier nation,
# Customer nation, and year (all ascending).
#
# TODO:
#   1. Join ordering
# --------------------------------------------------
opt
SELECT
    supp_nation,
    cust_nation,
    l_year, sum(volume) AS revenue
FROM (
    SELECT
        n1.n_name AS supp_nation,
        n2.n_name AS cust_nation,
        extract(year FROM l_shipdate) AS l_year,
        l_extendedprice * (1 - l_discount) AS volume
    FROM
        supplier,
        lineitem,
        orders,
        customer,
        nation n1,
        nation n2
    WHERE
        s_suppkey = l_suppkey
        AND o_orderkey = l_orderkey
        AND c_custkey = o_custkey
        AND s_nationkey = n1.n_nationkey
        AND c_nationkey = n2.n_nationkey
        AND (
            (n1.n_name = 'FRANCE' AND n2.n_name = 'GERMANY')
            or (n1.n_name = 'GERMANY' AND n2.n_name = 'FRANCE')
        )
        AND l_shipdate BETWEEN DATE '1995-01-01' AND DATE '1996-12-31'
    ) AS shipping
GROUP BY
    supp_nation,
    cust_nation,
    l_year
ORDER BY
    supp_nation,
    cust_nation,
    l_year;
----
group-by
 ├── columns: supp_nation:42(string!null) cust_nation:46(string!null) l_year:49(int) revenue:51(float)
 ├── grouping columns: n1.n_name:42(string!null) n2.n_name:46(string!null) l_year:49(int)
 ├── key: (42,46,49)
 ├── fd: (42,46,49)-->(51)
 ├── ordering: +42,+46,+49
 ├── sort
 │    ├── columns: n1.n_name:42(string!null) n2.n_name:46(string!null) l_year:49(int) volume:50(float)
 │    ├── ordering: +42,+46,+49
 │    └── project
 │         ├── columns: l_year:49(int) volume:50(float) n1.n_name:42(string!null) n2.n_name:46(string!null)
 │         ├── inner-join
 │         │    ├── columns: s_suppkey:1(int!null) s_nationkey:4(int!null) l_orderkey:8(int!null) l_suppkey:10(int!null) l_extendedprice:13(float!null) l_discount:14(float!null) l_shipdate:18(date!null) o_orderkey:24(int!null) o_custkey:25(int!null) c_custkey:33(int!null) c_nationkey:36(int!null) n1.n_nationkey:41(int!null) n1.n_name:42(string!null) n2.n_nationkey:45(int!null) n2.n_name:46(string!null)
 │         │    ├── fd: (1)-->(4), (24)-->(25), (33)-->(36), (41)-->(42), (45)-->(46), (36)==(45), (45)==(36), (25)==(33), (33)==(25), (8)==(24), (24)==(8), (1)==(10), (10)==(1), (4)==(41), (41)==(4)
 │         │    ├── inner-join
 │         │    │    ├── columns: l_orderkey:8(int!null) l_suppkey:10(int!null) l_extendedprice:13(float!null) l_discount:14(float!null) l_shipdate:18(date!null) o_orderkey:24(int!null) o_custkey:25(int!null) c_custkey:33(int!null) c_nationkey:36(int!null) n1.n_nationkey:41(int!null) n1.n_name:42(string!null) n2.n_nationkey:45(int!null) n2.n_name:46(string!null)
 │         │    │    ├── fd: (24)-->(25), (33)-->(36), (41)-->(42), (45)-->(46), (36)==(45), (45)==(36), (25)==(33), (33)==(25), (8)==(24), (24)==(8)
 │         │    │    ├── inner-join
 │         │    │    │    ├── columns: o_orderkey:24(int!null) o_custkey:25(int!null) c_custkey:33(int!null) c_nationkey:36(int!null) n1.n_nationkey:41(int!null) n1.n_name:42(string!null) n2.n_nationkey:45(int!null) n2.n_name:46(string!null)
 │         │    │    │    ├── key: (24,41)
 │         │    │    │    ├── fd: (24)-->(25), (33)-->(36), (41)-->(42), (45)-->(46), (36)==(45), (45)==(36), (25)==(33), (33)==(25)
 │         │    │    │    ├── inner-join
 │         │    │    │    │    ├── columns: o_orderkey:24(int!null) o_custkey:25(int!null) c_custkey:33(int!null) c_nationkey:36(int!null) n2.n_nationkey:45(int!null) n2.n_name:46(string!null)
 │         │    │    │    │    ├── key: (24)
 │         │    │    │    │    ├── fd: (45)-->(46), (33)-->(36), (36)==(45), (45)==(36), (24)-->(25), (25)==(33), (33)==(25)
 │         │    │    │    │    ├── inner-join (merge)
 │         │    │    │    │    │    ├── columns: c_custkey:33(int!null) c_nationkey:36(int!null) n2.n_nationkey:45(int!null) n2.n_name:46(string!null)
 │         │    │    │    │    │    ├── left ordering: +45
 │         │    │    │    │    │    ├── right ordering: +36
 │         │    │    │    │    │    ├── key: (33)
 │         │    │    │    │    │    ├── fd: (45)-->(46), (33)-->(36), (36)==(45), (45)==(36)
 │         │    │    │    │    │    ├── scan n2
 │         │    │    │    │    │    │    ├── columns: n2.n_nationkey:45(int!null) n2.n_name:46(string!null)
 │         │    │    │    │    │    │    ├── key: (45)
 │         │    │    │    │    │    │    ├── fd: (45)-->(46)
 │         │    │    │    │    │    │    └── ordering: +45
 │         │    │    │    │    │    ├── scan customer@c_nk
 │         │    │    │    │    │    │    ├── columns: c_custkey:33(int!null) c_nationkey:36(int!null)
 │         │    │    │    │    │    │    ├── key: (33)
 │         │    │    │    │    │    │    ├── fd: (33)-->(36)
 │         │    │    │    │    │    │    └── ordering: +36
 │         │    │    │    │    │    └── filters (true)
 │         │    │    │    │    ├── scan orders@o_ck
 │         │    │    │    │    │    ├── columns: o_orderkey:24(int!null) o_custkey:25(int!null)
 │         │    │    │    │    │    ├── key: (24)
 │         │    │    │    │    │    └── fd: (24)-->(25)
 │         │    │    │    │    └── filters
 │         │    │    │    │         └── c_custkey = o_custkey [type=bool, outer=(25,33), constraints=(/25: (/NULL - ]; /33: (/NULL - ]), fd=(25)==(33), (33)==(25)]
 │         │    │    │    ├── scan n1
 │         │    │    │    │    ├── columns: n1.n_nationkey:41(int!null) n1.n_name:42(string!null)
 │         │    │    │    │    ├── key: (41)
 │         │    │    │    │    └── fd: (41)-->(42)
 │         │    │    │    └── filters
 │         │    │    │         └── ((n1.n_name = 'FRANCE') AND (n2.n_name = 'GERMANY')) OR ((n1.n_name = 'GERMANY') AND (n2.n_name = 'FRANCE')) [type=bool, outer=(42,46)]
 │         │    │    ├── index-join lineitem
 │         │    │    │    ├── columns: l_orderkey:8(int!null) l_suppkey:10(int!null) l_extendedprice:13(float!null) l_discount:14(float!null) l_shipdate:18(date!null)
 │         │    │    │    └── scan lineitem@l_sd
 │         │    │    │         ├── columns: l_orderkey:8(int!null) l_linenumber:11(int!null) l_shipdate:18(date!null)
 │         │    │    │         ├── constraint: /18/8/11: [/'1995-01-01' - /'1996-12-31']
 │         │    │    │         ├── key: (8,11)
 │         │    │    │         └── fd: (8,11)-->(18)
 │         │    │    └── filters
 │         │    │         └── o_orderkey = l_orderkey [type=bool, outer=(8,24), constraints=(/8: (/NULL - ]; /24: (/NULL - ]), fd=(8)==(24), (24)==(8)]
 │         │    ├── scan supplier@s_nk
 │         │    │    ├── columns: s_suppkey:1(int!null) s_nationkey:4(int!null)
 │         │    │    ├── key: (1)
 │         │    │    └── fd: (1)-->(4)
 │         │    └── filters
 │         │         ├── s_suppkey = l_suppkey [type=bool, outer=(1,10), constraints=(/1: (/NULL - ]; /10: (/NULL - ]), fd=(1)==(10), (10)==(1)]
 │         │         └── s_nationkey = n1.n_nationkey [type=bool, outer=(4,41), constraints=(/4: (/NULL - ]; /41: (/NULL - ]), fd=(4)==(41), (41)==(4)]
 │         └── projections
 │              ├── extract('year', l_shipdate) [type=int, outer=(18)]
 │              └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(13,14)]
 └── aggregations
      └── sum [type=float, outer=(50)]
           └── variable: volume [type=float]

# --------------------------------------------------
# Q8
# National Market Share
# Determines how the market share of a given nation within a given region has
# changed over two years for a given part type.
#
# The market share for a given nation within a given region is defined as the
# fraction of the revenue, the sum of [l_extendedprice * (1-l_discount)], from
# the products of a specified type in that region that was supplied by suppliers
# from the given nation. The query determines this for the years 1995 and 1996
# presented in this order.
#
# TODO:
#   1. Join ordering
#   2. Push down equivalent column comparisons
# --------------------------------------------------
opt
SELECT
    o_year,
    sum(CASE
        WHEN nation = 'BRAZIL'
            THEN volume
        ELSE 0
    END) / sum(volume) AS mkt_share
FROM (
    SELECT
        extract(year FROM o_orderdate) AS o_year,
        l_extendedprice * (1 - l_discount) AS volume,
        n2.n_name AS nation
    FROM
        part,
        supplier,
        lineitem,
        orders,
        customer,
        nation n1,
        nation n2,
        region
    WHERE
        p_partkey = l_partkey
        AND s_suppkey = l_suppkey
        AND l_orderkey = o_orderkey
        AND o_custkey = c_custkey
        AND c_nationkey = n1.n_nationkey
        AND n1.n_regionkey = r_regionkey
        AND r_name = 'AMERICA'
        AND s_nationkey = n2.n_nationkey
        AND o_orderdate BETWEEN DATE '1995-01-01' AND DATE '1996-12-31'
        AND p_type = 'ECONOMY ANODIZED STEEL'
    ) AS all_nations
GROUP BY
    o_year
ORDER BY
    o_year;
----
sort
 ├── columns: o_year:61(int) mkt_share:66(float)
 ├── side-effects
 ├── key: (61)
 ├── fd: (61)-->(66)
 ├── ordering: +61
 └── project
      ├── columns: mkt_share:66(float) o_year:61(int)
      ├── side-effects
      ├── key: (61)
      ├── fd: (61)-->(66)
      ├── group-by
      │    ├── columns: o_year:61(int) sum:64(float) sum:65(float)
      │    ├── grouping columns: o_year:61(int)
      │    ├── key: (61)
      │    ├── fd: (61)-->(64,65)
      │    ├── project
      │    │    ├── columns: column63:63(float) o_year:61(int) volume:62(float)
      │    │    ├── project
      │    │    │    ├── columns: o_year:61(int) volume:62(float) n2.n_name:55(string!null)
      │    │    │    ├── inner-join (lookup part)
      │    │    │    │    ├── columns: p_partkey:1(int!null) p_type:5(string!null) s_suppkey:10(int!null) s_nationkey:13(int!null) l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_extendedprice:22(float!null) l_discount:23(float!null) o_orderkey:33(int!null) o_custkey:34(int!null) o_orderdate:37(date!null) c_custkey:42(int!null) c_nationkey:45(int!null) n1.n_nationkey:50(int!null) n1.n_regionkey:52(int!null) n2.n_nationkey:54(int!null) n2.n_name:55(string!null) r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    ├── key columns: [18] = [1]
      │    │    │    │    ├── fd: ()-->(5,59), (10)-->(13), (33)-->(34,37), (42)-->(45), (50)-->(52), (54)-->(55), (52)==(58), (58)==(52), (45)==(50), (50)==(45), (34)==(42), (42)==(34), (17)==(33), (33)==(17), (10)==(19), (19)==(10), (13)==(54), (54)==(13), (1)==(18), (18)==(1)
      │    │    │    │    ├── inner-join
      │    │    │    │    │    ├── columns: s_suppkey:10(int!null) s_nationkey:13(int!null) l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_extendedprice:22(float!null) l_discount:23(float!null) o_orderkey:33(int!null) o_custkey:34(int!null) o_orderdate:37(date!null) c_custkey:42(int!null) c_nationkey:45(int!null) n1.n_nationkey:50(int!null) n1.n_regionkey:52(int!null) n2.n_nationkey:54(int!null) n2.n_name:55(string!null) r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    │    ├── fd: ()-->(59), (10)-->(13), (33)-->(34,37), (42)-->(45), (50)-->(52), (54)-->(55), (52)==(58), (58)==(52), (45)==(50), (50)==(45), (34)==(42), (42)==(34), (17)==(33), (33)==(17), (10)==(19), (19)==(10), (13)==(54), (54)==(13)
      │    │    │    │    │    ├── inner-join
      │    │    │    │    │    │    ├── columns: l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_extendedprice:22(float!null) l_discount:23(float!null) o_orderkey:33(int!null) o_custkey:34(int!null) o_orderdate:37(date!null) c_custkey:42(int!null) c_nationkey:45(int!null) n1.n_nationkey:50(int!null) n1.n_regionkey:52(int!null) n2.n_nationkey:54(int!null) n2.n_name:55(string!null) r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    │    │    ├── fd: ()-->(59), (33)-->(34,37), (42)-->(45), (50)-->(52), (54)-->(55), (52)==(58), (58)==(52), (45)==(50), (50)==(45), (34)==(42), (42)==(34), (17)==(33), (33)==(17)
      │    │    │    │    │    │    ├── inner-join
      │    │    │    │    │    │    │    ├── columns: o_orderkey:33(int!null) o_custkey:34(int!null) o_orderdate:37(date!null) c_custkey:42(int!null) c_nationkey:45(int!null) n1.n_nationkey:50(int!null) n1.n_regionkey:52(int!null) n2.n_nationkey:54(int!null) n2.n_name:55(string!null) r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    │    │    │    ├── key: (33,54)
      │    │    │    │    │    │    │    ├── fd: ()-->(59), (33)-->(34,37), (42)-->(45), (50)-->(52), (54)-->(55), (52)==(58), (58)==(52), (45)==(50), (50)==(45), (34)==(42), (42)==(34)
      │    │    │    │    │    │    │    ├── inner-join
      │    │    │    │    │    │    │    │    ├── columns: c_custkey:42(int!null) c_nationkey:45(int!null) n1.n_nationkey:50(int!null) n1.n_regionkey:52(int!null) n2.n_nationkey:54(int!null) n2.n_name:55(string!null) r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    │    │    │    │    ├── key: (42,54)
      │    │    │    │    │    │    │    │    ├── fd: ()-->(59), (42)-->(45), (50)-->(52), (54)-->(55), (52)==(58), (58)==(52), (45)==(50), (50)==(45)
      │    │    │    │    │    │    │    │    ├── scan n2
      │    │    │    │    │    │    │    │    │    ├── columns: n2.n_nationkey:54(int!null) n2.n_name:55(string!null)
      │    │    │    │    │    │    │    │    │    ├── key: (54)
      │    │    │    │    │    │    │    │    │    └── fd: (54)-->(55)
      │    │    │    │    │    │    │    │    ├── inner-join
      │    │    │    │    │    │    │    │    │    ├── columns: c_custkey:42(int!null) c_nationkey:45(int!null) n1.n_nationkey:50(int!null) n1.n_regionkey:52(int!null) r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    │    │    │    │    │    ├── key: (42)
      │    │    │    │    │    │    │    │    │    ├── fd: ()-->(59), (50)-->(52), (52)==(58), (58)==(52), (42)-->(45), (45)==(50), (50)==(45)
      │    │    │    │    │    │    │    │    │    ├── scan customer@c_nk
      │    │    │    │    │    │    │    │    │    │    ├── columns: c_custkey:42(int!null) c_nationkey:45(int!null)
      │    │    │    │    │    │    │    │    │    │    ├── key: (42)
      │    │    │    │    │    │    │    │    │    │    └── fd: (42)-->(45)
      │    │    │    │    │    │    │    │    │    ├── inner-join (lookup nation@n_rk)
      │    │    │    │    │    │    │    │    │    │    ├── columns: n1.n_nationkey:50(int!null) n1.n_regionkey:52(int!null) r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    │    │    │    │    │    │    ├── key columns: [58] = [52]
      │    │    │    │    │    │    │    │    │    │    ├── key: (50)
      │    │    │    │    │    │    │    │    │    │    ├── fd: ()-->(59), (50)-->(52), (52)==(58), (58)==(52)
      │    │    │    │    │    │    │    │    │    │    ├── select
      │    │    │    │    │    │    │    │    │    │    │    ├── columns: r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    │    │    │    │    │    │    │    ├── key: (58)
      │    │    │    │    │    │    │    │    │    │    │    ├── fd: ()-->(59)
      │    │    │    │    │    │    │    │    │    │    │    ├── scan region
      │    │    │    │    │    │    │    │    │    │    │    │    ├── columns: r_regionkey:58(int!null) r_name:59(string!null)
      │    │    │    │    │    │    │    │    │    │    │    │    ├── key: (58)
      │    │    │    │    │    │    │    │    │    │    │    │    └── fd: (58)-->(59)
      │    │    │    │    │    │    │    │    │    │    │    └── filters
      │    │    │    │    │    │    │    │    │    │    │         └── r_name = 'AMERICA' [type=bool, outer=(59), constraints=(/59: [/'AMERICA' - /'AMERICA']; tight), fd=()-->(59)]
      │    │    │    │    │    │    │    │    │    │    └── filters (true)
      │    │    │    │    │    │    │    │    │    └── filters
      │    │    │    │    │    │    │    │    │         └── c_nationkey = n1.n_nationkey [type=bool, outer=(45,50), constraints=(/45: (/NULL - ]; /50: (/NULL - ]), fd=(45)==(50), (50)==(45)]
      │    │    │    │    │    │    │    │    └── filters (true)
      │    │    │    │    │    │    │    ├── index-join orders
      │    │    │    │    │    │    │    │    ├── columns: o_orderkey:33(int!null) o_custkey:34(int!null) o_orderdate:37(date!null)
      │    │    │    │    │    │    │    │    ├── key: (33)
      │    │    │    │    │    │    │    │    ├── fd: (33)-->(34,37)
      │    │    │    │    │    │    │    │    └── scan orders@o_od
      │    │    │    │    │    │    │    │         ├── columns: o_orderkey:33(int!null) o_orderdate:37(date!null)
      │    │    │    │    │    │    │    │         ├── constraint: /37/33: [/'1995-01-01' - /'1996-12-31']
      │    │    │    │    │    │    │    │         ├── key: (33)
      │    │    │    │    │    │    │    │         └── fd: (33)-->(37)
      │    │    │    │    │    │    │    └── filters
      │    │    │    │    │    │    │         └── o_custkey = c_custkey [type=bool, outer=(34,42), constraints=(/34: (/NULL - ]; /42: (/NULL - ]), fd=(34)==(42), (42)==(34)]
      │    │    │    │    │    │    ├── scan lineitem
      │    │    │    │    │    │    │    └── columns: l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_extendedprice:22(float!null) l_discount:23(float!null)
      │    │    │    │    │    │    └── filters
      │    │    │    │    │    │         └── l_orderkey = o_orderkey [type=bool, outer=(17,33), constraints=(/17: (/NULL - ]; /33: (/NULL - ]), fd=(17)==(33), (33)==(17)]
      │    │    │    │    │    ├── scan supplier@s_nk
      │    │    │    │    │    │    ├── columns: s_suppkey:10(int!null) s_nationkey:13(int!null)
      │    │    │    │    │    │    ├── key: (10)
      │    │    │    │    │    │    └── fd: (10)-->(13)
      │    │    │    │    │    └── filters
      │    │    │    │    │         ├── s_suppkey = l_suppkey [type=bool, outer=(10,19), constraints=(/10: (/NULL - ]; /19: (/NULL - ]), fd=(10)==(19), (19)==(10)]
      │    │    │    │    │         └── s_nationkey = n2.n_nationkey [type=bool, outer=(13,54), constraints=(/13: (/NULL - ]; /54: (/NULL - ]), fd=(13)==(54), (54)==(13)]
      │    │    │    │    └── filters
      │    │    │    │         └── p_type = 'ECONOMY ANODIZED STEEL' [type=bool, outer=(5), constraints=(/5: [/'ECONOMY ANODIZED STEEL' - /'ECONOMY ANODIZED STEEL']; tight), fd=()-->(5)]
      │    │    │    └── projections
      │    │    │         ├── extract('year', o_orderdate) [type=int, outer=(37)]
      │    │    │         └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(22,23)]
      │    │    └── projections
      │    │         └── CASE WHEN n2.n_name = 'BRAZIL' THEN volume ELSE 0.0 END [type=float, outer=(55,62)]
      │    └── aggregations
      │         ├── sum [type=float, outer=(63)]
      │         │    └── variable: column63 [type=float]
      │         └── sum [type=float, outer=(62)]
      │              └── variable: volume [type=float]
      └── projections
           └── sum / sum [type=float, outer=(64,65), side-effects]

# --------------------------------------------------
# Q9
# Product Type Profit Measure
# Determines how much profit is made on a given line of parts, broken out by
# supplier nation and year.
#
# Finds, for each nation and each year, the profit for all parts ordered in that
# year that contain a specified substring in their names and that were filled by
# a supplier in that nation. The profit is defined as the sum of:
#
#   [(l_extendedprice*(1-l_discount)) - (ps_supplycost * l_quantity)]
#
# for all lineitems describing parts in the specified line. The query lists the
#  nations in ascending alphabetical order and, for each nation, the year and
#  profit in descending order by year (most recent first).
#
# TODO:
#   1. Join ordering
#   2. Push down equivalent column comparisons
# --------------------------------------------------
opt
SELECT
    nation,
    o_year,
    sum(amount) AS sum_profit
FROM (
    SELECT
        n_name AS nation,
        extract(year FROM o_orderdate) AS o_year,
        l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity AS amount
    FROM
        part,
        supplier,
        lineitem,
        partsupp,
        orders,
        nation
    WHERE
        s_suppkey = l_suppkey
        AND ps_suppkey = l_suppkey
        AND ps_partkey = l_partkey
        AND p_partkey = l_partkey
        AND o_orderkey = l_orderkey
        AND s_nationkey = n_nationkey
        AND p_name LIKE '%green%'
    ) AS profit
GROUP BY
    nation,
    o_year
ORDER BY
    nation,
    o_year DESC;
----
sort
 ├── columns: nation:48(string!null) o_year:51(int) sum_profit:53(float)
 ├── key: (48,51)
 ├── fd: (48,51)-->(53)
 ├── ordering: +48,-51
 └── group-by
      ├── columns: n_name:48(string!null) o_year:51(int) sum:53(float)
      ├── grouping columns: n_name:48(string!null) o_year:51(int)
      ├── key: (48,51)
      ├── fd: (48,51)-->(53)
      ├── project
      │    ├── columns: o_year:51(int) amount:52(float) n_name:48(string!null)
      │    ├── inner-join (lookup part)
      │    │    ├── columns: p_partkey:1(int!null) p_name:2(string!null) s_suppkey:10(int!null) s_nationkey:13(int!null) l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_quantity:21(float!null) l_extendedprice:22(float!null) l_discount:23(float!null) ps_partkey:33(int!null) ps_suppkey:34(int!null) ps_supplycost:36(float!null) o_orderkey:38(int!null) o_orderdate:42(date!null) n_nationkey:47(int!null) n_name:48(string!null)
      │    │    ├── key columns: [18] = [1]
      │    │    ├── fd: (1)-->(2), (10)-->(13), (33,34)-->(36), (38)-->(42), (47)-->(48), (19)==(10,34), (34)==(10,19), (18)==(1,33), (33)==(1,18), (17)==(38), (38)==(17), (10)==(19,34), (13)==(47), (47)==(13), (1)==(18,33)
      │    │    ├── inner-join
      │    │    │    ├── columns: s_suppkey:10(int!null) s_nationkey:13(int!null) l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_quantity:21(float!null) l_extendedprice:22(float!null) l_discount:23(float!null) ps_partkey:33(int!null) ps_suppkey:34(int!null) ps_supplycost:36(float!null) o_orderkey:38(int!null) o_orderdate:42(date!null) n_nationkey:47(int!null) n_name:48(string!null)
      │    │    │    ├── fd: (10)-->(13), (33,34)-->(36), (38)-->(42), (47)-->(48), (19)==(10,34), (34)==(10,19), (18)==(33), (33)==(18), (17)==(38), (38)==(17), (10)==(19,34), (13)==(47), (47)==(13)
      │    │    │    ├── inner-join
      │    │    │    │    ├── columns: l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_quantity:21(float!null) l_extendedprice:22(float!null) l_discount:23(float!null) ps_partkey:33(int!null) ps_suppkey:34(int!null) ps_supplycost:36(float!null) o_orderkey:38(int!null) o_orderdate:42(date!null) n_nationkey:47(int!null) n_name:48(string!null)
      │    │    │    │    ├── fd: (33,34)-->(36), (38)-->(42), (47)-->(48), (19)==(34), (34)==(19), (18)==(33), (33)==(18), (17)==(38), (38)==(17)
      │    │    │    │    ├── scan nation
      │    │    │    │    │    ├── columns: n_nationkey:47(int!null) n_name:48(string!null)
      │    │    │    │    │    ├── key: (47)
      │    │    │    │    │    └── fd: (47)-->(48)
      │    │    │    │    ├── inner-join (lookup orders)
      │    │    │    │    │    ├── columns: l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_quantity:21(float!null) l_extendedprice:22(float!null) l_discount:23(float!null) ps_partkey:33(int!null) ps_suppkey:34(int!null) ps_supplycost:36(float!null) o_orderkey:38(int!null) o_orderdate:42(date!null)
      │    │    │    │    │    ├── key columns: [17] = [38]
      │    │    │    │    │    ├── fd: (38)-->(42), (33,34)-->(36), (19)==(34), (34)==(19), (18)==(33), (33)==(18), (17)==(38), (38)==(17)
      │    │    │    │    │    ├── inner-join
      │    │    │    │    │    │    ├── columns: l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_quantity:21(float!null) l_extendedprice:22(float!null) l_discount:23(float!null) ps_partkey:33(int!null) ps_suppkey:34(int!null) ps_supplycost:36(float!null)
      │    │    │    │    │    │    ├── fd: (33,34)-->(36), (19)==(34), (34)==(19), (18)==(33), (33)==(18)
      │    │    │    │    │    │    ├── scan partsupp
      │    │    │    │    │    │    │    ├── columns: ps_partkey:33(int!null) ps_suppkey:34(int!null) ps_supplycost:36(float!null)
      │    │    │    │    │    │    │    ├── key: (33,34)
      │    │    │    │    │    │    │    └── fd: (33,34)-->(36)
      │    │    │    │    │    │    ├── scan lineitem
      │    │    │    │    │    │    │    └── columns: l_orderkey:17(int!null) l_partkey:18(int!null) l_suppkey:19(int!null) l_quantity:21(float!null) l_extendedprice:22(float!null) l_discount:23(float!null)
      │    │    │    │    │    │    └── filters
      │    │    │    │    │    │         ├── ps_suppkey = l_suppkey [type=bool, outer=(19,34), constraints=(/19: (/NULL - ]; /34: (/NULL - ]), fd=(19)==(34), (34)==(19)]
      │    │    │    │    │    │         └── ps_partkey = l_partkey [type=bool, outer=(18,33), constraints=(/18: (/NULL - ]; /33: (/NULL - ]), fd=(18)==(33), (33)==(18)]
      │    │    │    │    │    └── filters (true)
      │    │    │    │    └── filters (true)
      │    │    │    ├── scan supplier@s_nk
      │    │    │    │    ├── columns: s_suppkey:10(int!null) s_nationkey:13(int!null)
      │    │    │    │    ├── key: (10)
      │    │    │    │    └── fd: (10)-->(13)
      │    │    │    └── filters
      │    │    │         ├── s_suppkey = l_suppkey [type=bool, outer=(10,19), constraints=(/10: (/NULL - ]; /19: (/NULL - ]), fd=(10)==(19), (19)==(10)]
      │    │    │         └── s_nationkey = n_nationkey [type=bool, outer=(13,47), constraints=(/13: (/NULL - ]; /47: (/NULL - ]), fd=(13)==(47), (47)==(13)]
      │    │    └── filters
      │    │         └── p_name LIKE '%green%' [type=bool, outer=(2), constraints=(/2: (/NULL - ])]
      │    └── projections
      │         ├── extract('year', o_orderdate) [type=int, outer=(42)]
      │         └── (l_extendedprice * (1.0 - l_discount)) - (ps_supplycost * l_quantity) [type=float, outer=(21-23,36)]
      └── aggregations
           └── sum [type=float, outer=(52)]
                └── variable: amount [type=float]

# --------------------------------------------------
# Q10
# Returned Item Reporting
# Identifies customers who might be having problems with the parts that are
# shipped to them.
#
# Finds the top 20 customers, in terms of their effect on lost revenue for a
# given quarter, who have returned parts. The query considers only parts that
# were ordered in the specified quarter. The query lists the customer's name,
# address, nation, phone number, account balance, comment information and
# revenue lost. The customers are listed in descending order of lost revenue.
# Revenue lost is defined as sum(l_extendedprice*(1-l_discount)) for all
# qualifying lineitems.
# --------------------------------------------------
opt
SELECT
    c_custkey,
    c_name,
    sum(l_extendedprice * (1 - l_discount)) AS revenue,
    c_acctbal,
    n_name,
    c_address,
    c_phone,
    c_comment
FROM
    customer,
    orders,
    lineitem,
    nation
WHERE
    c_custkey = o_custkey
    AND l_orderkey = o_orderkey
    AND o_orderDATE >= DATE '1993-10-01'
    AND o_orderDATE < DATE '1993-10-01' + INTERVAL '3' MONTH
    AND l_returnflag = 'R'
    AND c_nationkey = n_nationkey
GROUP BY
    c_custkey,
    c_name,
    c_acctbal,
    c_phone,
    n_name,
    c_address,
    c_comment
ORDER BY
    revenue DESC
LIMIT 20;
----
limit
 ├── columns: c_custkey:1(int!null) c_name:2(string) revenue:39(float) c_acctbal:6(float) n_name:35(string) c_address:3(string) c_phone:5(string) c_comment:8(string)
 ├── internal-ordering: -39
 ├── cardinality: [0 - 20]
 ├── key: (1)
 ├── fd: (1)-->(2,3,5,6,8,35,39)
 ├── ordering: -39
 ├── sort
 │    ├── columns: c_custkey:1(int!null) c_name:2(string) c_address:3(string) c_phone:5(string) c_acctbal:6(float) c_comment:8(string) n_name:35(string) sum:39(float)
 │    ├── key: (1)
 │    ├── fd: (1)-->(2,3,5,6,8,35,39)
 │    ├── ordering: -39
 │    └── group-by
 │         ├── columns: c_custkey:1(int!null) c_name:2(string) c_address:3(string) c_phone:5(string) c_acctbal:6(float) c_comment:8(string) n_name:35(string) sum:39(float)
 │         ├── grouping columns: c_custkey:1(int!null)
 │         ├── key: (1)
 │         ├── fd: (1)-->(2,3,5,6,8,35,39)
 │         ├── project
 │         │    ├── columns: column38:38(float) c_custkey:1(int!null) c_name:2(string!null) c_address:3(string!null) c_phone:5(string!null) c_acctbal:6(float!null) c_comment:8(string!null) n_name:35(string!null)
 │         │    ├── fd: (1)-->(2,3,5,6,8,35)
 │         │    ├── inner-join (lookup nation)
 │         │    │    ├── columns: c_custkey:1(int!null) c_name:2(string!null) c_address:3(string!null) c_nationkey:4(int!null) c_phone:5(string!null) c_acctbal:6(float!null) c_comment:8(string!null) o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null) l_orderkey:18(int!null) l_extendedprice:23(float!null) l_discount:24(float!null) l_returnflag:26(string!null) n_nationkey:34(int!null) n_name:35(string!null)
 │         │    │    ├── key columns: [4] = [34]
 │         │    │    ├── fd: ()-->(26), (1)-->(2-6,8), (9)-->(10,13), (34)-->(35), (9)==(18), (18)==(9), (1)==(10), (10)==(1), (4)==(34), (34)==(4)
 │         │    │    ├── inner-join (lookup customer)
 │         │    │    │    ├── columns: c_custkey:1(int!null) c_name:2(string!null) c_address:3(string!null) c_nationkey:4(int!null) c_phone:5(string!null) c_acctbal:6(float!null) c_comment:8(string!null) o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null) l_orderkey:18(int!null) l_extendedprice:23(float!null) l_discount:24(float!null) l_returnflag:26(string!null)
 │         │    │    │    ├── key columns: [10] = [1]
 │         │    │    │    ├── fd: ()-->(26), (9)-->(10,13), (9)==(18), (18)==(9), (1)-->(2-6,8), (1)==(10), (10)==(1)
 │         │    │    │    ├── inner-join (lookup lineitem)
 │         │    │    │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null) l_orderkey:18(int!null) l_extendedprice:23(float!null) l_discount:24(float!null) l_returnflag:26(string!null)
 │         │    │    │    │    ├── key columns: [9] = [18]
 │         │    │    │    │    ├── fd: ()-->(26), (9)-->(10,13), (9)==(18), (18)==(9)
 │         │    │    │    │    ├── index-join orders
 │         │    │    │    │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_orderdate:13(date!null)
 │         │    │    │    │    │    ├── key: (9)
 │         │    │    │    │    │    ├── fd: (9)-->(10,13)
 │         │    │    │    │    │    └── scan orders@o_od
 │         │    │    │    │    │         ├── columns: o_orderkey:9(int!null) o_orderdate:13(date!null)
 │         │    │    │    │    │         ├── constraint: /13/9: [/'1993-10-01' - /'1993-12-31']
 │         │    │    │    │    │         ├── key: (9)
 │         │    │    │    │    │         └── fd: (9)-->(13)
 │         │    │    │    │    └── filters
 │         │    │    │    │         └── l_returnflag = 'R' [type=bool, outer=(26), constraints=(/26: [/'R' - /'R']; tight), fd=()-->(26)]
 │         │    │    │    └── filters (true)
 │         │    │    └── filters (true)
 │         │    └── projections
 │         │         └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(23,24)]
 │         └── aggregations
 │              ├── sum [type=float, outer=(38)]
 │              │    └── variable: column38 [type=float]
 │              ├── const-agg [type=string, outer=(2)]
 │              │    └── variable: c_name [type=string]
 │              ├── const-agg [type=string, outer=(3)]
 │              │    └── variable: c_address [type=string]
 │              ├── const-agg [type=string, outer=(5)]
 │              │    └── variable: c_phone [type=string]
 │              ├── const-agg [type=float, outer=(6)]
 │              │    └── variable: c_acctbal [type=float]
 │              ├── const-agg [type=string, outer=(8)]
 │              │    └── variable: c_comment [type=string]
 │              └── const-agg [type=string, outer=(35)]
 │                   └── variable: n_name [type=string]
 └── const: 20 [type=int]

# --------------------------------------------------
# Q11
# Important Stock Identification
# Finds the most important subset of suppliers' stock in a given nation.
#
# Finds, from scanning the available stock of suppliers in a given nation, all
# the parts that represent a significant percentage of the total value of all
# available parts. The query displays the part number and the value of those
# parts in descending order of value.
# --------------------------------------------------
opt
SELECT
    ps_partkey,
    sum(ps_supplycost * ps_availqty::float) AS value
FROM
    partsupp,
    supplier,
    nation
WHERE
    ps_suppkey = s_suppkey
    AND s_nationkey = n_nationkey
    AND n_name = 'GERMANY'
GROUP BY
    ps_partkey HAVING
        sum(ps_supplycost * ps_availqty::float) > (
            SELECT
                sum(ps_supplycost * ps_availqty::float) * 0.0001
            FROM
                partsupp,
                supplier,
                nation
            WHERE
                ps_suppkey = s_suppkey
                AND s_nationkey = n_nationkey
                AND n_name = 'GERMANY'
        )
ORDER BY
    value DESC;
----
sort
 ├── columns: ps_partkey:1(int!null) value:18(float!null)
 ├── key: (1)
 ├── fd: (1)-->(18)
 ├── ordering: -18
 └── select
      ├── columns: ps_partkey:1(int!null) sum:18(float!null)
      ├── key: (1)
      ├── fd: (1)-->(18)
      ├── group-by
      │    ├── columns: ps_partkey:1(int!null) sum:18(float)
      │    ├── grouping columns: ps_partkey:1(int!null)
      │    ├── key: (1)
      │    ├── fd: (1)-->(18)
      │    ├── project
      │    │    ├── columns: column17:17(float) ps_partkey:1(int!null)
      │    │    ├── inner-join
      │    │    │    ├── columns: ps_partkey:1(int!null) ps_suppkey:2(int!null) ps_availqty:3(int!null) ps_supplycost:4(float!null) s_suppkey:6(int!null) s_nationkey:9(int!null) n_nationkey:13(int!null) n_name:14(string!null)
      │    │    │    ├── key: (1,6)
      │    │    │    ├── fd: ()-->(14), (1,2)-->(3,4), (6)-->(9), (9)==(13), (13)==(9), (2)==(6), (6)==(2)
      │    │    │    ├── scan partsupp
      │    │    │    │    ├── columns: ps_partkey:1(int!null) ps_suppkey:2(int!null) ps_availqty:3(int!null) ps_supplycost:4(float!null)
      │    │    │    │    ├── key: (1,2)
      │    │    │    │    └── fd: (1,2)-->(3,4)
      │    │    │    ├── inner-join (lookup supplier@s_nk)
      │    │    │    │    ├── columns: s_suppkey:6(int!null) s_nationkey:9(int!null) n_nationkey:13(int!null) n_name:14(string!null)
      │    │    │    │    ├── key columns: [13] = [9]
      │    │    │    │    ├── key: (6)
      │    │    │    │    ├── fd: ()-->(14), (6)-->(9), (9)==(13), (13)==(9)
      │    │    │    │    ├── select
      │    │    │    │    │    ├── columns: n_nationkey:13(int!null) n_name:14(string!null)
      │    │    │    │    │    ├── key: (13)
      │    │    │    │    │    ├── fd: ()-->(14)
      │    │    │    │    │    ├── scan nation
      │    │    │    │    │    │    ├── columns: n_nationkey:13(int!null) n_name:14(string!null)
      │    │    │    │    │    │    ├── key: (13)
      │    │    │    │    │    │    └── fd: (13)-->(14)
      │    │    │    │    │    └── filters
      │    │    │    │    │         └── n_name = 'GERMANY' [type=bool, outer=(14), constraints=(/14: [/'GERMANY' - /'GERMANY']; tight), fd=()-->(14)]
      │    │    │    │    └── filters (true)
      │    │    │    └── filters
      │    │    │         └── ps_suppkey = s_suppkey [type=bool, outer=(2,6), constraints=(/2: (/NULL - ]; /6: (/NULL - ]), fd=(2)==(6), (6)==(2)]
      │    │    └── projections
      │    │         └── ps_supplycost * ps_availqty::FLOAT8 [type=float, outer=(3,4)]
      │    └── aggregations
      │         └── sum [type=float, outer=(17)]
      │              └── variable: column17 [type=float]
      └── filters
           └── gt [type=bool, outer=(18), subquery, constraints=(/18: (/NULL - ])]
                ├── variable: sum [type=float]
                └── subquery [type=float]
                     └── project
                          ├── columns: "?column?":37(float)
                          ├── cardinality: [1 - 1]
                          ├── key: ()
                          ├── fd: ()-->(37)
                          ├── scalar-group-by
                          │    ├── columns: sum:36(float)
                          │    ├── cardinality: [1 - 1]
                          │    ├── key: ()
                          │    ├── fd: ()-->(36)
                          │    ├── project
                          │    │    ├── columns: column35:35(float)
                          │    │    ├── inner-join
                          │    │    │    ├── columns: ps_suppkey:20(int!null) ps_availqty:21(int!null) ps_supplycost:22(float!null) s_suppkey:24(int!null) s_nationkey:27(int!null) n_nationkey:31(int!null) n_name:32(string!null)
                          │    │    │    ├── fd: ()-->(32), (24)-->(27), (27)==(31), (31)==(27), (20)==(24), (24)==(20)
                          │    │    │    ├── scan partsupp
                          │    │    │    │    └── columns: ps_suppkey:20(int!null) ps_availqty:21(int!null) ps_supplycost:22(float!null)
                          │    │    │    ├── inner-join (lookup supplier@s_nk)
                          │    │    │    │    ├── columns: s_suppkey:24(int!null) s_nationkey:27(int!null) n_nationkey:31(int!null) n_name:32(string!null)
                          │    │    │    │    ├── key columns: [31] = [27]
                          │    │    │    │    ├── key: (24)
                          │    │    │    │    ├── fd: ()-->(32), (24)-->(27), (27)==(31), (31)==(27)
                          │    │    │    │    ├── select
                          │    │    │    │    │    ├── columns: n_nationkey:31(int!null) n_name:32(string!null)
                          │    │    │    │    │    ├── key: (31)
                          │    │    │    │    │    ├── fd: ()-->(32)
                          │    │    │    │    │    ├── scan nation
                          │    │    │    │    │    │    ├── columns: n_nationkey:31(int!null) n_name:32(string!null)
                          │    │    │    │    │    │    ├── key: (31)
                          │    │    │    │    │    │    └── fd: (31)-->(32)
                          │    │    │    │    │    └── filters
                          │    │    │    │    │         └── n_name = 'GERMANY' [type=bool, outer=(32), constraints=(/32: [/'GERMANY' - /'GERMANY']; tight), fd=()-->(32)]
                          │    │    │    │    └── filters (true)
                          │    │    │    └── filters
                          │    │    │         └── ps_suppkey = s_suppkey [type=bool, outer=(20,24), constraints=(/20: (/NULL - ]; /24: (/NULL - ]), fd=(20)==(24), (24)==(20)]
                          │    │    └── projections
                          │    │         └── ps_supplycost * ps_availqty::FLOAT8 [type=float, outer=(21,22)]
                          │    └── aggregations
                          │         └── sum [type=float, outer=(35)]
                          │              └── variable: column35 [type=float]
                          └── projections
                               └── sum * 0.0001 [type=float, outer=(36)]

# --------------------------------------------------
# Q12
# Shipping Modes and Order Priority
# Determines whether selecting less expensive modes of shipping is negatively
# affecting the critical-priority orders by causing more parts to be received by
# customers after the committed date.
#
# Counts, by ship mode, for lineitems actually received by customers in a given
# year, the number of lineitems belonging to orders for which the l_receiptdate
# exceeds the l_commitdate for two different specified ship modes. Only
# lineitems that were actually shipped before the l_commitdate are considered.
# The late lineitems are partitioned into two groups, those with priority URGENT
# or HIGH, and those with a priority other than URGENT or HIGH.
# --------------------------------------------------
opt
SELECT
    l_shipmode,
    sum(CASE
        WHEN o_orderpriority = '1-URGENT'
            OR o_orderpriority = '2-HIGH'
            THEN 1
        ELSE 0
    END) AS high_line_count,
    sum(CASE
        WHEN o_orderpriority <> '1-URGENT'
            AND o_orderpriority <> '2-HIGH'
            THEN 1
        ELSE 0
    END) AS low_line_count
FROM
    orders,
    lineitem
WHERE
    o_orderkey = l_orderkey
    AND l_shipmode IN ('MAIL', 'SHIP')
    AND l_commitdate < l_receiptdate
    AND l_shipdate < l_commitdate
    AND l_receiptdate >= DATE '1994-01-01'
    AND l_receiptdate < DATE '1994-01-01' + INTERVAL '1' YEAR
GROUP BY
    l_shipmode
ORDER BY
    l_shipmode;
----
group-by
 ├── columns: l_shipmode:24(string!null) high_line_count:27(decimal) low_line_count:29(decimal)
 ├── grouping columns: l_shipmode:24(string!null)
 ├── key: (24)
 ├── fd: (24)-->(27,29)
 ├── ordering: +24
 ├── project
 │    ├── columns: column26:26(int) column28:28(int) l_shipmode:24(string!null)
 │    ├── ordering: +24
 │    ├── inner-join (lookup orders)
 │    │    ├── columns: o_orderkey:1(int!null) o_orderpriority:6(string!null) l_orderkey:10(int!null) l_shipdate:20(date!null) l_commitdate:21(date!null) l_receiptdate:22(date!null) l_shipmode:24(string!null)
 │    │    ├── key columns: [10] = [1]
 │    │    ├── fd: (1)-->(6), (1)==(10), (10)==(1)
 │    │    ├── ordering: +24
 │    │    ├── sort
 │    │    │    ├── columns: l_orderkey:10(int!null) l_shipdate:20(date!null) l_commitdate:21(date!null) l_receiptdate:22(date!null) l_shipmode:24(string!null)
 │    │    │    ├── ordering: +24
 │    │    │    └── select
 │    │    │         ├── columns: l_orderkey:10(int!null) l_shipdate:20(date!null) l_commitdate:21(date!null) l_receiptdate:22(date!null) l_shipmode:24(string!null)
 │    │    │         ├── index-join lineitem
 │    │    │         │    ├── columns: l_orderkey:10(int!null) l_shipdate:20(date!null) l_commitdate:21(date!null) l_receiptdate:22(date!null) l_shipmode:24(string!null)
 │    │    │         │    └── scan lineitem@l_rd
 │    │    │         │         ├── columns: l_orderkey:10(int!null) l_linenumber:13(int!null) l_receiptdate:22(date!null)
 │    │    │         │         ├── constraint: /22/10/13: [/'1994-01-01' - /'1994-12-31']
 │    │    │         │         ├── key: (10,13)
 │    │    │         │         └── fd: (10,13)-->(22)
 │    │    │         └── filters
 │    │    │              ├── l_shipmode IN ('MAIL', 'SHIP') [type=bool, outer=(24), constraints=(/24: [/'MAIL' - /'MAIL'] [/'SHIP' - /'SHIP']; tight)]
 │    │    │              ├── l_commitdate < l_receiptdate [type=bool, outer=(21,22), constraints=(/21: (/NULL - ]; /22: (/NULL - ])]
 │    │    │              └── l_shipdate < l_commitdate [type=bool, outer=(20,21), constraints=(/20: (/NULL - ]; /21: (/NULL - ])]
 │    │    └── filters (true)
 │    └── projections
 │         ├── CASE WHEN (o_orderpriority = '1-URGENT') OR (o_orderpriority = '2-HIGH') THEN 1 ELSE 0 END [type=int, outer=(6)]
 │         └── CASE WHEN (o_orderpriority != '1-URGENT') AND (o_orderpriority != '2-HIGH') THEN 1 ELSE 0 END [type=int, outer=(6)]
 └── aggregations
      ├── sum [type=decimal, outer=(26)]
      │    └── variable: column26 [type=int]
      └── sum [type=decimal, outer=(28)]
           └── variable: column28 [type=int]

# --------------------------------------------------
# Q13
# Customer Distribution
# Seeks relationships between customers and the size of their orders.
#
# Determines the distribution of customers by the number of orders they have
# made, including customers who have no record of orders, past or present. It
# counts and reports how many customers have no orders, how many have 1, 2, 3,
# etc. A check is made to ensure that the orders counted do not fall into one of
# several special categories of orders. Special categories are identified in the
# order comment column by looking for a particular pattern.
# --------------------------------------------------
opt
SELECT
    c_count, count(*) AS custdist
FROM (
    SELECT
        c_custkey,
        count(o_orderkey)
    FROM
        customer LEFT OUTER JOIN orders ON
            c_custkey = o_custkey
            AND o_comment NOT LIKE '%special%requests%'
    GROUP BY
        c_custkey
    ) AS c_orders (c_custkey, c_count)
GROUP BY
    c_count
ORDER BY
    custdist DESC,
    c_count DESC;
----
sort
 ├── columns: c_count:18(int) custdist:19(int)
 ├── key: (18)
 ├── fd: (18)-->(19)
 ├── ordering: -19,-18
 └── group-by
      ├── columns: count:18(int) count_rows:19(int)
      ├── grouping columns: count:18(int)
      ├── key: (18)
      ├── fd: (18)-->(19)
      ├── group-by
      │    ├── columns: c_custkey:1(int!null) count:18(int)
      │    ├── grouping columns: c_custkey:1(int!null)
      │    ├── key: (1)
      │    ├── fd: (1)-->(18)
      │    ├── left-join
      │    │    ├── columns: c_custkey:1(int!null) o_orderkey:9(int) o_custkey:10(int) o_comment:17(string)
      │    │    ├── key: (1,9)
      │    │    ├── fd: (9)-->(10,17)
      │    │    ├── scan customer@c_nk
      │    │    │    ├── columns: c_custkey:1(int!null)
      │    │    │    └── key: (1)
      │    │    ├── select
      │    │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_comment:17(string!null)
      │    │    │    ├── key: (9)
      │    │    │    ├── fd: (9)-->(10,17)
      │    │    │    ├── scan orders
      │    │    │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_comment:17(string!null)
      │    │    │    │    ├── key: (9)
      │    │    │    │    └── fd: (9)-->(10,17)
      │    │    │    └── filters
      │    │    │         └── o_comment NOT LIKE '%special%requests%' [type=bool, outer=(17), constraints=(/17: (/NULL - ])]
      │    │    └── filters
      │    │         └── c_custkey = o_custkey [type=bool, outer=(1,10), constraints=(/1: (/NULL - ]; /10: (/NULL - ]), fd=(1)==(10), (10)==(1)]
      │    └── aggregations
      │         └── count [type=int, outer=(9)]
      │              └── variable: o_orderkey [type=int]
      └── aggregations
           └── count-rows [type=int]

# --------------------------------------------------
# Q14
# Promotion Effect
# Monitors the market response to a promotion such as TV advertisements or a
# special campaign.
#
# Determines what percentage of the revenue in a given year and month was
# derived from promotional parts. The query considers only parts actually
# shipped in that month and gives the percentage. Revenue is defined as
# (l_extendedprice * (1-l_discount)).
# --------------------------------------------------
opt
SELECT
    100.00 * sum(CASE
        WHEN p_type LIKE 'PROMO%'
            THEN l_extendedprice * (1 - l_discount)
        ELSE 0
    END) / sum(l_extendedprice * (1 - l_discount)) AS promo_revenue
FROM
    lineitem,
    part
WHERE
    l_partkey = p_partkey
    AND l_shipdate >= DATE '1995-09-01'
    AND l_shipdate < DATE '1995-09-01' + INTERVAL '1' MONTH;
----
project
 ├── columns: promo_revenue:30(float)
 ├── cardinality: [1 - 1]
 ├── side-effects
 ├── key: ()
 ├── fd: ()-->(30)
 ├── scalar-group-by
 │    ├── columns: sum:27(float) sum:29(float)
 │    ├── cardinality: [1 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(27,29)
 │    ├── project
 │    │    ├── columns: column26:26(float) column28:28(float)
 │    │    ├── inner-join (lookup part)
 │    │    │    ├── columns: l_partkey:2(int!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipdate:11(date!null) p_partkey:17(int!null) p_type:21(string!null)
 │    │    │    ├── key columns: [2] = [17]
 │    │    │    ├── fd: (17)-->(21), (2)==(17), (17)==(2)
 │    │    │    ├── index-join lineitem
 │    │    │    │    ├── columns: l_partkey:2(int!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipdate:11(date!null)
 │    │    │    │    └── scan lineitem@l_sd
 │    │    │    │         ├── columns: l_orderkey:1(int!null) l_linenumber:4(int!null) l_shipdate:11(date!null)
 │    │    │    │         ├── constraint: /11/1/4: [/'1995-09-01' - /'1995-09-30']
 │    │    │    │         ├── key: (1,4)
 │    │    │    │         └── fd: (1,4)-->(11)
 │    │    │    └── filters (true)
 │    │    └── projections
 │    │         ├── CASE WHEN p_type LIKE 'PROMO%' THEN l_extendedprice * (1.0 - l_discount) ELSE 0.0 END [type=float, outer=(6,7,21)]
 │    │         └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(6,7)]
 │    └── aggregations
 │         ├── sum [type=float, outer=(26)]
 │         │    └── variable: column26 [type=float]
 │         └── sum [type=float, outer=(28)]
 │              └── variable: column28 [type=float]
 └── projections
      └── (sum * 100.0) / sum [type=float, outer=(27,29), side-effects]

# --------------------------------------------------
# Q15
# Top Supplier
# Determines the top supplier so it can be rewarded, given more business, or
# identified for special recognition.
#
# Finds the supplier who contributed the most to the overall revenue for parts
# shipped during a given quarter of a given year. In case of a tie, the query
# lists all suppliers whose contribution was equal to the maximum, presented in
# supplier number order.
# --------------------------------------------------
exec-ddl
CREATE VIEW revenue0 (supplier_no, total_revenue) AS
    SELECT
        l_suppkey,
        sum(l_extendedprice * (1 - l_discount))
    FROM
        lineitem
    WHERE
        l_shipdate >= DATE '1996-01-01'
        AND l_shipdate < DATE '1996-01-01' + INTERVAL '3' MONTH
    GROUP BY
        l_suppkey;
----
VIEW revenue0 (supplier_no, total_revenue)
 └── SELECT l_suppkey, sum(l_extendedprice * (1 - l_discount)) FROM lineitem WHERE (l_shipdate >= DATE '1996-01-01') AND (l_shipdate < (DATE '1996-01-01' + '3 mons':::INTERVAL)) GROUP BY l_suppkey

opt
SELECT
    s_suppkey,
    s_name,
    s_address,
    s_phone,
    total_revenue
FROM
    supplier,
    revenue0
WHERE
    s_suppkey = supplier_no
    AND total_revenue = (
        SELECT
            max(total_revenue)
        FROM
            revenue0
    )
ORDER BY
    s_suppkey;
----
sort
 ├── columns: s_suppkey:1(int!null) s_name:2(string!null) s_address:3(string!null) s_phone:5(string!null) total_revenue:25(float!null)
 ├── key: (1)
 ├── fd: (1)-->(2,3,5,25)
 ├── ordering: +1
 └── project
      ├── columns: s_suppkey:1(int!null) s_name:2(string!null) s_address:3(string!null) s_phone:5(string!null) sum:25(float!null)
      ├── key: (1)
      ├── fd: (1)-->(2,3,5,25)
      └── inner-join (lookup supplier)
           ├── columns: s_suppkey:1(int!null) s_name:2(string!null) s_address:3(string!null) s_phone:5(string!null) l_suppkey:10(int!null) sum:25(float!null)
           ├── key columns: [10] = [1]
           ├── key: (10)
           ├── fd: (1)-->(2,3,5), (10)-->(25), (1)==(10), (10)==(1)
           ├── select
           │    ├── columns: l_suppkey:10(int!null) sum:25(float!null)
           │    ├── key: (10)
           │    ├── fd: (10)-->(25)
           │    ├── group-by
           │    │    ├── columns: l_suppkey:10(int!null) sum:25(float)
           │    │    ├── grouping columns: l_suppkey:10(int!null)
           │    │    ├── key: (10)
           │    │    ├── fd: (10)-->(25)
           │    │    ├── project
           │    │    │    ├── columns: column24:24(float) l_suppkey:10(int!null)
           │    │    │    ├── index-join lineitem
           │    │    │    │    ├── columns: l_suppkey:10(int!null) l_extendedprice:13(float!null) l_discount:14(float!null) l_shipdate:18(date!null)
           │    │    │    │    └── scan lineitem@l_sd
           │    │    │    │         ├── columns: l_orderkey:8(int!null) l_linenumber:11(int!null) l_shipdate:18(date!null)
           │    │    │    │         ├── constraint: /18/8/11: [/'1996-01-01' - /'1996-03-31']
           │    │    │    │         ├── key: (8,11)
           │    │    │    │         └── fd: (8,11)-->(18)
           │    │    │    └── projections
           │    │    │         └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(13,14)]
           │    │    └── aggregations
           │    │         └── sum [type=float, outer=(24)]
           │    │              └── variable: column24 [type=float]
           │    └── filters
           │         └── eq [type=bool, outer=(25), subquery, constraints=(/25: (/NULL - ])]
           │              ├── variable: sum [type=float]
           │              └── subquery [type=float]
           │                   └── scalar-group-by
           │                        ├── columns: max:44(float)
           │                        ├── cardinality: [1 - 1]
           │                        ├── key: ()
           │                        ├── fd: ()-->(44)
           │                        ├── group-by
           │                        │    ├── columns: l_suppkey:28(int!null) sum:43(float)
           │                        │    ├── grouping columns: l_suppkey:28(int!null)
           │                        │    ├── key: (28)
           │                        │    ├── fd: (28)-->(43)
           │                        │    ├── project
           │                        │    │    ├── columns: column42:42(float) l_suppkey:28(int!null)
           │                        │    │    ├── index-join lineitem
           │                        │    │    │    ├── columns: l_suppkey:28(int!null) l_extendedprice:31(float!null) l_discount:32(float!null) l_shipdate:36(date!null)
           │                        │    │    │    └── scan lineitem@l_sd
           │                        │    │    │         ├── columns: l_orderkey:26(int!null) l_linenumber:29(int!null) l_shipdate:36(date!null)
           │                        │    │    │         ├── constraint: /36/26/29: [/'1996-01-01' - /'1996-03-31']
           │                        │    │    │         ├── key: (26,29)
           │                        │    │    │         └── fd: (26,29)-->(36)
           │                        │    │    └── projections
           │                        │    │         └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(31,32)]
           │                        │    └── aggregations
           │                        │         └── sum [type=float, outer=(42)]
           │                        │              └── variable: column42 [type=float]
           │                        └── aggregations
           │                             └── max [type=float, outer=(43)]
           │                                  └── variable: sum [type=float]
           └── filters (true)

# --------------------------------------------------
# Q16
# Parts/Supplier Relationship
# Finds out how many suppliers can supply parts with given attributes. It might
# be used, for example, to determine whether there is a sufficient number of
# suppliers for heavily ordered parts.
#
# Counts the number of suppliers who can supply parts that satisfy a particular
# customer's requirements. The customer is interested in parts of eight
# different sizes as long as they are not of a given type, not of a given brand,
# and not from a supplier who has had complaints registered at the Better
# Business Bureau. Results must be presented in descending count and ascending
# brand, type, and size.
# --------------------------------------------------
opt
SELECT
    p_brand,
    p_type,
    p_size,
    count(DISTINCT ps_suppkey) AS supplier_cnt
FROM
    partsupp,
    part
WHERE
    p_partkey = ps_partkey
    AND p_brand <> 'Brand#45'
    AND p_type NOT LIKE 'MEDIUM POLISHED %'
    AND p_size IN (49, 14, 23, 45, 19, 3, 36, 9)
    AND ps_suppkey NOT IN (
        SELECT
            s_suppkey
        FROM
            supplier
        WHERE
            s_comment LIKE '%Customer%Complaints%'
    )
GROUP BY
    p_brand,
    p_type,
    p_size
ORDER BY
    supplier_cnt DESC,
    p_brand,
    p_type,
    p_size;
----
sort
 ├── columns: p_brand:9(string!null) p_type:10(string!null) p_size:11(int!null) supplier_cnt:22(int)
 ├── key: (9-11)
 ├── fd: (9-11)-->(22)
 ├── ordering: -22,+9,+10,+11
 └── group-by
      ├── columns: p_brand:9(string!null) p_type:10(string!null) p_size:11(int!null) count:22(int)
      ├── grouping columns: p_brand:9(string!null) p_type:10(string!null) p_size:11(int!null)
      ├── key: (9-11)
      ├── fd: (9-11)-->(22)
      ├── inner-join
      │    ├── columns: ps_partkey:1(int!null) ps_suppkey:2(int!null) p_partkey:6(int!null) p_brand:9(string!null) p_type:10(string!null) p_size:11(int!null)
      │    ├── key: (2,6)
      │    ├── fd: (6)-->(9-11), (1)==(6), (6)==(1)
      │    ├── anti-join (merge)
      │    │    ├── columns: ps_partkey:1(int!null) ps_suppkey:2(int!null)
      │    │    ├── left ordering: +2
      │    │    ├── right ordering: +15
      │    │    ├── key: (1,2)
      │    │    ├── scan partsupp@ps_sk
      │    │    │    ├── columns: ps_partkey:1(int!null) ps_suppkey:2(int!null)
      │    │    │    ├── key: (1,2)
      │    │    │    └── ordering: +2
      │    │    ├── select
      │    │    │    ├── columns: s_suppkey:15(int!null) s_comment:21(string!null)
      │    │    │    ├── key: (15)
      │    │    │    ├── fd: (15)-->(21)
      │    │    │    ├── ordering: +15
      │    │    │    ├── scan supplier
      │    │    │    │    ├── columns: s_suppkey:15(int!null) s_comment:21(string!null)
      │    │    │    │    ├── key: (15)
      │    │    │    │    ├── fd: (15)-->(21)
      │    │    │    │    └── ordering: +15
      │    │    │    └── filters
      │    │    │         └── s_comment LIKE '%Customer%Complaints%' [type=bool, outer=(21), constraints=(/21: (/NULL - ])]
      │    │    └── filters (true)
      │    ├── select
      │    │    ├── columns: p_partkey:6(int!null) p_brand:9(string!null) p_type:10(string!null) p_size:11(int!null)
      │    │    ├── key: (6)
      │    │    ├── fd: (6)-->(9-11)
      │    │    ├── scan part
      │    │    │    ├── columns: p_partkey:6(int!null) p_brand:9(string!null) p_type:10(string!null) p_size:11(int!null)
      │    │    │    ├── key: (6)
      │    │    │    └── fd: (6)-->(9-11)
      │    │    └── filters
      │    │         ├── p_brand != 'Brand#45' [type=bool, outer=(9), constraints=(/9: (/NULL - /'Brand#45') [/e'Brand#45\x00' - ]; tight)]
      │    │         ├── p_type NOT LIKE 'MEDIUM POLISHED %' [type=bool, outer=(10), constraints=(/10: (/NULL - ])]
      │    │         └── p_size IN (3, 9, 14, 19, 23, 36, 45, 49) [type=bool, outer=(11), constraints=(/11: [/3 - /3] [/9 - /9] [/14 - /14] [/19 - /19] [/23 - /23] [/36 - /36] [/45 - /45] [/49 - /49]; tight)]
      │    └── filters
      │         └── p_partkey = ps_partkey [type=bool, outer=(1,6), constraints=(/1: (/NULL - ]; /6: (/NULL - ]), fd=(1)==(6), (6)==(1)]
      └── aggregations
           └── count [type=int, outer=(2)]
                └── agg-distinct [type=int]
                     └── variable: ps_suppkey [type=int]

# --------------------------------------------------
# Q17
# Small-Quantity-Order Revenue
# Determines how much average yearly revenue would be lost if orders were no
# longer filled for small quantities of certain parts. This may reduce overhead
# expenses by concentrating sales on larger shipments.
#
# Considers parts of a given brand and with a given container type and
# determines the average lineitem quantity of such parts ordered for all orders
# (past and pending) in the 7-year database. What would be the average yearly
# gross (undiscounted) loss in revenue if orders for these parts with a quantity
# of less than 20% of this average were no longer taken?
#
# TODO:
#   1. Allow Select to be pushed below RowNumber used to add key column
# --------------------------------------------------
opt
SELECT
    sum(l_extendedprice) / 7.0 AS avg_yearly
FROM
    lineitem,
    part
WHERE
    p_partkey = l_partkey
    AND p_brand = 'Brand#23'
    AND p_container = 'MED BOX'
    AND l_quantity < (
        SELECT
            0.2 * avg(l_quantity)
        FROM
            lineitem
        WHERE
            l_partkey = p_partkey
    );
----
project
 ├── columns: avg_yearly:45(float)
 ├── cardinality: [1 - 1]
 ├── side-effects
 ├── key: ()
 ├── fd: ()-->(45)
 ├── scalar-group-by
 │    ├── columns: sum:44(float)
 │    ├── cardinality: [1 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(44)
 │    ├── inner-join (lookup lineitem)
 │    │    ├── columns: l_partkey:2(int!null) l_quantity:5(float!null) l_extendedprice:6(float!null) p_partkey:17(int!null) "?column?":43(float!null)
 │    │    ├── key columns: [1 4] = [1 4]
 │    │    ├── fd: (17)-->(43), (2)==(17), (17)==(2)
 │    │    ├── inner-join (lookup lineitem@l_pk)
 │    │    │    ├── columns: l_orderkey:1(int!null) l_partkey:2(int!null) l_linenumber:4(int!null) p_partkey:17(int!null) "?column?":43(float)
 │    │    │    ├── key columns: [17] = [2]
 │    │    │    ├── key: (1,4)
 │    │    │    ├── fd: (17)-->(43), (1,4)-->(2), (2)==(17), (17)==(2)
 │    │    │    ├── project
 │    │    │    │    ├── columns: "?column?":43(float) p_partkey:17(int!null)
 │    │    │    │    ├── key: (17)
 │    │    │    │    ├── fd: (17)-->(43)
 │    │    │    │    ├── group-by
 │    │    │    │    │    ├── columns: p_partkey:17(int!null) avg:42(float)
 │    │    │    │    │    ├── grouping columns: p_partkey:17(int!null)
 │    │    │    │    │    ├── internal-ordering: +17 opt(20,23)
 │    │    │    │    │    ├── key: (17)
 │    │    │    │    │    ├── fd: (17)-->(42)
 │    │    │    │    │    ├── left-join (lookup lineitem)
 │    │    │    │    │    │    ├── columns: p_partkey:17(int!null) p_brand:20(string!null) p_container:23(string!null) l_partkey:27(int) l_quantity:30(float)
 │    │    │    │    │    │    ├── key columns: [26 29] = [26 29]
 │    │    │    │    │    │    ├── fd: ()-->(20,23)
 │    │    │    │    │    │    ├── ordering: +17 opt(20,23) [actual: +17]
 │    │    │    │    │    │    ├── left-join (lookup lineitem@l_pk)
 │    │    │    │    │    │    │    ├── columns: p_partkey:17(int!null) p_brand:20(string!null) p_container:23(string!null) l_orderkey:26(int) l_partkey:27(int) l_linenumber:29(int)
 │    │    │    │    │    │    │    ├── key columns: [17] = [27]
 │    │    │    │    │    │    │    ├── key: (17,26,29)
 │    │    │    │    │    │    │    ├── fd: ()-->(20,23), (26,29)-->(27)
 │    │    │    │    │    │    │    ├── ordering: +17 opt(20,23) [actual: +17]
 │    │    │    │    │    │    │    ├── select
 │    │    │    │    │    │    │    │    ├── columns: p_partkey:17(int!null) p_brand:20(string!null) p_container:23(string!null)
 │    │    │    │    │    │    │    │    ├── key: (17)
 │    │    │    │    │    │    │    │    ├── fd: ()-->(20,23)
 │    │    │    │    │    │    │    │    ├── ordering: +17 opt(20,23) [actual: +17]
 │    │    │    │    │    │    │    │    ├── scan part
 │    │    │    │    │    │    │    │    │    ├── columns: p_partkey:17(int!null) p_brand:20(string!null) p_container:23(string!null)
 │    │    │    │    │    │    │    │    │    ├── key: (17)
 │    │    │    │    │    │    │    │    │    ├── fd: (17)-->(20,23)
 │    │    │    │    │    │    │    │    │    └── ordering: +17 opt(20,23) [actual: +17]
 │    │    │    │    │    │    │    │    └── filters
 │    │    │    │    │    │    │    │         ├── p_brand = 'Brand#23' [type=bool, outer=(20), constraints=(/20: [/'Brand#23' - /'Brand#23']; tight), fd=()-->(20)]
 │    │    │    │    │    │    │    │         └── p_container = 'MED BOX' [type=bool, outer=(23), constraints=(/23: [/'MED BOX' - /'MED BOX']; tight), fd=()-->(23)]
 │    │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    └── aggregations
 │    │    │    │    │         └── avg [type=float, outer=(30)]
 │    │    │    │    │              └── variable: l_quantity [type=float]
 │    │    │    │    └── projections
 │    │    │    │         └── avg * 0.2 [type=float, outer=(42)]
 │    │    │    └── filters (true)
 │    │    └── filters
 │    │         └── l_quantity < ?column? [type=bool, outer=(5,43), constraints=(/5: (/NULL - ]; /43: (/NULL - ])]
 │    └── aggregations
 │         └── sum [type=float, outer=(6)]
 │              └── variable: l_extendedprice [type=float]
 └── projections
      └── sum / 7.0 [type=float, outer=(44), side-effects]

# --------------------------------------------------
# Q18
# Large Volume Customer
# Ranks customers based on their having placed a large quantity order. Large
# quantity orders are defined as those orders whose total quantity is above a
# certain level.
#
# Finds a list of the top 100 customers who have ever placed large quantity
# orders. The query lists the customer name, customer key, the order key, date
# and total price and the quantity for the order.
# --------------------------------------------------
opt
SELECT
    c_name,
    c_custkey,
    o_orderkey,
    o_orderdate,
    o_totalprice,
    sum(l_quantity)
FROM
    customer,
    orders,
    lineitem
WHERE
    o_orderkey IN (
        SELECT
            l_orderkey
        FROM
            lineitem
        GROUP BY
            l_orderkey HAVING
                sum(l_quantity) > 300
    )
    AND c_custkey = o_custkey
    AND o_orderkey = l_orderkey
GROUP BY
    c_name,
    c_custkey,
    o_orderkey,
    o_orderdate,
    o_totalprice
ORDER BY
    o_totalprice DESC,
    o_orderdate
LIMIT 100;
----
limit
 ├── columns: c_name:2(string) c_custkey:1(int) o_orderkey:9(int!null) o_orderdate:13(date) o_totalprice:12(float) sum:51(float)
 ├── internal-ordering: -12,+13
 ├── cardinality: [0 - 100]
 ├── key: (9)
 ├── fd: (1)-->(2), (9)-->(1,2,12,13,51)
 ├── ordering: -12,+13
 ├── sort
 │    ├── columns: c_custkey:1(int) c_name:2(string) o_orderkey:9(int!null) o_totalprice:12(float) o_orderdate:13(date) sum:51(float)
 │    ├── key: (9)
 │    ├── fd: (1)-->(2), (9)-->(1,2,12,13,51)
 │    ├── ordering: -12,+13
 │    └── group-by
 │         ├── columns: c_custkey:1(int) c_name:2(string) o_orderkey:9(int!null) o_totalprice:12(float) o_orderdate:13(date) sum:51(float)
 │         ├── grouping columns: o_orderkey:9(int!null)
 │         ├── key: (9)
 │         ├── fd: (1)-->(2), (9)-->(1,2,12,13,51)
 │         ├── inner-join
 │         │    ├── columns: c_custkey:1(int!null) c_name:2(string!null) o_orderkey:9(int!null) o_custkey:10(int!null) o_totalprice:12(float!null) o_orderdate:13(date!null) l_orderkey:18(int!null) l_quantity:22(float!null)
 │         │    ├── fd: (1)-->(2), (9)-->(10,12,13), (9)==(18), (18)==(9), (1)==(10), (10)==(1)
 │         │    ├── scan customer
 │         │    │    ├── columns: c_custkey:1(int!null) c_name:2(string!null)
 │         │    │    ├── key: (1)
 │         │    │    └── fd: (1)-->(2)
 │         │    ├── inner-join (merge)
 │         │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_totalprice:12(float!null) o_orderdate:13(date!null) l_orderkey:18(int!null) l_quantity:22(float!null)
 │         │    │    ├── left ordering: +9
 │         │    │    ├── right ordering: +18
 │         │    │    ├── fd: (9)-->(10,12,13), (9)==(18), (18)==(9)
 │         │    │    ├── semi-join (merge)
 │         │    │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_totalprice:12(float!null) o_orderdate:13(date!null)
 │         │    │    │    ├── left ordering: +9
 │         │    │    │    ├── right ordering: +34
 │         │    │    │    ├── key: (9)
 │         │    │    │    ├── fd: (9)-->(10,12,13)
 │         │    │    │    ├── ordering: +9
 │         │    │    │    ├── scan orders
 │         │    │    │    │    ├── columns: o_orderkey:9(int!null) o_custkey:10(int!null) o_totalprice:12(float!null) o_orderdate:13(date!null)
 │         │    │    │    │    ├── key: (9)
 │         │    │    │    │    ├── fd: (9)-->(10,12,13)
 │         │    │    │    │    └── ordering: +9
 │         │    │    │    ├── select
 │         │    │    │    │    ├── columns: l_orderkey:34(int!null) sum:50(float!null)
 │         │    │    │    │    ├── key: (34)
 │         │    │    │    │    ├── fd: (34)-->(50)
 │         │    │    │    │    ├── ordering: +34
 │         │    │    │    │    ├── group-by
 │         │    │    │    │    │    ├── columns: l_orderkey:34(int!null) sum:50(float)
 │         │    │    │    │    │    ├── grouping columns: l_orderkey:34(int!null)
 │         │    │    │    │    │    ├── key: (34)
 │         │    │    │    │    │    ├── fd: (34)-->(50)
 │         │    │    │    │    │    ├── ordering: +34
 │         │    │    │    │    │    ├── scan lineitem
 │         │    │    │    │    │    │    ├── columns: l_orderkey:34(int!null) l_quantity:38(float!null)
 │         │    │    │    │    │    │    └── ordering: +34
 │         │    │    │    │    │    └── aggregations
 │         │    │    │    │    │         └── sum [type=float, outer=(38)]
 │         │    │    │    │    │              └── variable: l_quantity [type=float]
 │         │    │    │    │    └── filters
 │         │    │    │    │         └── sum > 300.0 [type=bool, outer=(50), constraints=(/50: [/300.00000000000006 - ]; tight)]
 │         │    │    │    └── filters (true)
 │         │    │    ├── scan lineitem
 │         │    │    │    ├── columns: l_orderkey:18(int!null) l_quantity:22(float!null)
 │         │    │    │    └── ordering: +18
 │         │    │    └── filters (true)
 │         │    └── filters
 │         │         └── c_custkey = o_custkey [type=bool, outer=(1,10), constraints=(/1: (/NULL - ]; /10: (/NULL - ]), fd=(1)==(10), (10)==(1)]
 │         └── aggregations
 │              ├── sum [type=float, outer=(22)]
 │              │    └── variable: l_quantity [type=float]
 │              ├── const-agg [type=int, outer=(1)]
 │              │    └── variable: c_custkey [type=int]
 │              ├── const-agg [type=string, outer=(2)]
 │              │    └── variable: c_name [type=string]
 │              ├── const-agg [type=float, outer=(12)]
 │              │    └── variable: o_totalprice [type=float]
 │              └── const-agg [type=date, outer=(13)]
 │                   └── variable: o_orderdate [type=date]
 └── const: 100 [type=int]

# --------------------------------------------------
# Q19
# Discounted Revenue
# Reports the gross discounted revenue attributed to the sale of selected parts
# handled in a particular manner. This query is an example of code such as might
# be produced programmatically by a data mining tool.
#
# The Discounted Revenue query finds the gross discounted revenue for all orders
# for three different types of parts that were shipped by air and delivered in
# person. Parts are selected based on the combination of specific brands, a list
# of containers, and a range of sizes.
# --------------------------------------------------
opt
SELECT
    sum(l_extendedprice* (1 - l_discount)) AS revenue
FROM
    lineitem,
    part
WHERE
    (
        p_partkey = l_partkey
        AND p_brand = 'Brand#12'
        AND p_container IN ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
        AND l_quantity >= 1 AND l_quantity <= 1 + 10
        AND p_size BETWEEN 1 AND 5
        AND l_shipmode IN ('AIR', 'AIR REG')
        AND l_shipinstruct = 'DELIVER IN PERSON'
    )
    OR
    (
        p_partkey = l_partkey
        AND p_brand = 'Brand#23'
        AND p_container IN ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
        AND l_quantity >= 10 AND l_quantity <= 10 + 10
        AND p_size BETWEEN 1 AND 10
        AND l_shipmode IN ('AIR', 'AIR REG')
        AND l_shipinstruct = 'DELIVER IN PERSON'
    )
    OR
    (
        p_partkey = l_partkey
        AND p_brand = 'Brand#34'
        AND p_container IN ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
        AND l_quantity >= 20 AND l_quantity <= 20 + 10
        AND p_size BETWEEN 1 AND 15
        AND l_shipmode IN ('AIR', 'AIR REG')
        AND l_shipinstruct = 'DELIVER IN PERSON'
    );
----
scalar-group-by
 ├── columns: revenue:27(float)
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(27)
 ├── project
 │    ├── columns: column26:26(float)
 │    ├── inner-join (lookup part)
 │    │    ├── columns: l_partkey:2(int!null) l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipinstruct:14(string!null) l_shipmode:15(string!null) p_partkey:17(int!null) p_brand:20(string!null) p_size:22(int!null) p_container:23(string!null)
 │    │    ├── key columns: [2] = [17]
 │    │    ├── fd: ()-->(14), (17)-->(20,22,23), (2)==(17), (17)==(2)
 │    │    ├── select
 │    │    │    ├── columns: l_partkey:2(int!null) l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipinstruct:14(string!null) l_shipmode:15(string!null)
 │    │    │    ├── fd: ()-->(14)
 │    │    │    ├── scan lineitem
 │    │    │    │    └── columns: l_partkey:2(int!null) l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipinstruct:14(string!null) l_shipmode:15(string!null)
 │    │    │    └── filters
 │    │    │         ├── l_shipmode IN ('AIR', 'AIR REG') [type=bool, outer=(15), constraints=(/15: [/'AIR' - /'AIR'] [/'AIR REG' - /'AIR REG']; tight)]
 │    │    │         └── l_shipinstruct = 'DELIVER IN PERSON' [type=bool, outer=(14), constraints=(/14: [/'DELIVER IN PERSON' - /'DELIVER IN PERSON']; tight), fd=()-->(14)]
 │    │    └── filters
 │    │         ├── ((((((p_brand = 'Brand#12') AND (p_container IN ('SM BOX', 'SM CASE', 'SM PACK', 'SM PKG'))) AND (l_quantity >= 1.0)) AND (l_quantity <= 11.0)) AND (p_size <= 5)) OR (((((p_brand = 'Brand#23') AND (p_container IN ('MED BAG', 'MED BOX', 'MED PACK', 'MED PKG'))) AND (l_quantity >= 10.0)) AND (l_quantity <= 20.0)) AND (p_size <= 10))) OR (((((p_brand = 'Brand#34') AND (p_container IN ('LG BOX', 'LG CASE', 'LG PACK', 'LG PKG'))) AND (l_quantity >= 20.0)) AND (l_quantity <= 30.0)) AND (p_size <= 15)) [type=bool, outer=(5,20,22,23)]
 │    │         └── p_size >= 1 [type=bool, outer=(22), constraints=(/22: [/1 - ]; tight)]
 │    └── projections
 │         └── l_extendedprice * (1.0 - l_discount) [type=float, outer=(6,7)]
 └── aggregations
      └── sum [type=float, outer=(26)]
           └── variable: column26 [type=float]

# --------------------------------------------------
# Q20
# Potential Part Promotion
# Identifies suppliers in a particular nation having selected parts that may be
# candidates for a promotional offer.
#
# Identifies suppliers who have an excess of a given part available; an excess
# defined to be more than 50% of the parts like the given part that the supplier
# shipped in a given year for a given nation. Only parts whose names share a
# certain naming convention are considered.
#
# TODO:
#   1. Push 'forest%' prefix filter down into Scan
# --------------------------------------------------
opt
SELECT
    s_name,
    s_address
FROM
    supplier,
    nation
WHERE
    s_suppkey IN (
        SELECT
            ps_suppkey
        FROM
            partsupp
        WHERE
            ps_partkey IN (
                SELECT
                    p_partkey
                FROM
                    part
                WHERE
                    p_name LIKE 'forest%'
            )
            AND ps_availqty > (
                SELECT
                    0.5 * sum(l_quantity)
                FROM
                    lineitem
                WHERE
                    l_partkey = ps_partkey
                    AND l_suppkey = ps_suppkey
                    AND l_shipdate >= DATE '1994-01-01'
                    AND l_shipdate < DATE '1994-01-01' + INTERVAL '1' YEAR
            )
    )
    AND s_nationkey = n_nationkey
    AND n_name = 'CANADA'
ORDER BY
    s_name;
----
sort
 ├── columns: s_name:2(string!null) s_address:3(string!null)
 ├── ordering: +2
 └── project
      ├── columns: s_name:2(string!null) s_address:3(string!null)
      └── inner-join
           ├── columns: s_suppkey:1(int!null) s_name:2(string!null) s_address:3(string!null) s_nationkey:4(int!null) n_nationkey:8(int!null) n_name:9(string!null)
           ├── key: (1)
           ├── fd: ()-->(9), (1)-->(2-4), (4)==(8), (8)==(4)
           ├── semi-join
           │    ├── columns: s_suppkey:1(int!null) s_name:2(string!null) s_address:3(string!null) s_nationkey:4(int!null)
           │    ├── key: (1)
           │    ├── fd: (1)-->(2-4)
           │    ├── scan supplier
           │    │    ├── columns: s_suppkey:1(int!null) s_name:2(string!null) s_address:3(string!null) s_nationkey:4(int!null)
           │    │    ├── key: (1)
           │    │    └── fd: (1)-->(2-4)
           │    ├── semi-join
           │    │    ├── columns: ps_partkey:12(int!null) ps_suppkey:13(int!null)
           │    │    ├── key: (12,13)
           │    │    ├── project
           │    │    │    ├── columns: ps_partkey:12(int!null) ps_suppkey:13(int!null)
           │    │    │    ├── key: (12,13)
           │    │    │    └── select
           │    │    │         ├── columns: ps_partkey:12(int!null) ps_suppkey:13(int!null) ps_availqty:14(int!null) sum:42(float)
           │    │    │         ├── key: (12,13)
           │    │    │         ├── fd: (12,13)-->(14,42)
           │    │    │         ├── group-by
           │    │    │         │    ├── columns: ps_partkey:12(int!null) ps_suppkey:13(int!null) ps_availqty:14(int) sum:42(float)
           │    │    │         │    ├── grouping columns: ps_partkey:12(int!null) ps_suppkey:13(int!null)
           │    │    │         │    ├── key: (12,13)
           │    │    │         │    ├── fd: (12,13)-->(14,42)
           │    │    │         │    ├── left-join
           │    │    │         │    │    ├── columns: ps_partkey:12(int!null) ps_suppkey:13(int!null) ps_availqty:14(int!null) l_partkey:27(int) l_suppkey:28(int) l_quantity:30(float) l_shipdate:36(date)
           │    │    │         │    │    ├── fd: (12,13)-->(14)
           │    │    │         │    │    ├── scan partsupp
           │    │    │         │    │    │    ├── columns: ps_partkey:12(int!null) ps_suppkey:13(int!null) ps_availqty:14(int!null)
           │    │    │         │    │    │    ├── key: (12,13)
           │    │    │         │    │    │    └── fd: (12,13)-->(14)
           │    │    │         │    │    ├── index-join lineitem
           │    │    │         │    │    │    ├── columns: l_partkey:27(int!null) l_suppkey:28(int!null) l_quantity:30(float!null) l_shipdate:36(date!null)
           │    │    │         │    │    │    └── scan lineitem@l_sd
           │    │    │         │    │    │         ├── columns: l_orderkey:26(int!null) l_linenumber:29(int!null) l_shipdate:36(date!null)
           │    │    │         │    │    │         ├── constraint: /36/26/29: [/'1994-01-01' - /'1994-12-31']
           │    │    │         │    │    │         ├── key: (26,29)
           │    │    │         │    │    │         └── fd: (26,29)-->(36)
           │    │    │         │    │    └── filters
           │    │    │         │    │         ├── l_partkey = ps_partkey [type=bool, outer=(12,27), constraints=(/12: (/NULL - ]; /27: (/NULL - ]), fd=(12)==(27), (27)==(12)]
           │    │    │         │    │         └── l_suppkey = ps_suppkey [type=bool, outer=(13,28), constraints=(/13: (/NULL - ]; /28: (/NULL - ]), fd=(13)==(28), (28)==(13)]
           │    │    │         │    └── aggregations
           │    │    │         │         ├── sum [type=float, outer=(30)]
           │    │    │         │         │    └── variable: l_quantity [type=float]
           │    │    │         │         └── const-agg [type=int, outer=(14)]
           │    │    │         │              └── variable: ps_availqty [type=int]
           │    │    │         └── filters
           │    │    │              └── ps_availqty > (sum * 0.5) [type=bool, outer=(14,42), constraints=(/14: (/NULL - ])]
           │    │    ├── select
           │    │    │    ├── columns: p_partkey:17(int!null) p_name:18(string!null)
           │    │    │    ├── key: (17)
           │    │    │    ├── fd: (17)-->(18)
           │    │    │    ├── scan part
           │    │    │    │    ├── columns: p_partkey:17(int!null) p_name:18(string!null)
           │    │    │    │    ├── key: (17)
           │    │    │    │    └── fd: (17)-->(18)
           │    │    │    └── filters
           │    │    │         └── p_name LIKE 'forest%' [type=bool, outer=(18), constraints=(/18: [/'forest' - /'foresu'); tight)]
           │    │    └── filters
           │    │         └── ps_partkey = p_partkey [type=bool, outer=(12,17), constraints=(/12: (/NULL - ]; /17: (/NULL - ]), fd=(12)==(17), (17)==(12)]
           │    └── filters
           │         └── s_suppkey = ps_suppkey [type=bool, outer=(1,13), constraints=(/1: (/NULL - ]; /13: (/NULL - ]), fd=(1)==(13), (13)==(1)]
           ├── select
           │    ├── columns: n_nationkey:8(int!null) n_name:9(string!null)
           │    ├── key: (8)
           │    ├── fd: ()-->(9)
           │    ├── scan nation
           │    │    ├── columns: n_nationkey:8(int!null) n_name:9(string!null)
           │    │    ├── key: (8)
           │    │    └── fd: (8)-->(9)
           │    └── filters
           │         └── n_name = 'CANADA' [type=bool, outer=(9), constraints=(/9: [/'CANADA' - /'CANADA']; tight), fd=()-->(9)]
           └── filters
                └── s_nationkey = n_nationkey [type=bool, outer=(4,8), constraints=(/4: (/NULL - ]; /8: (/NULL - ]), fd=(4)==(8), (8)==(4)]

# --------------------------------------------------
# Q21
# Suppliers Who Kept Orders Waiting Query
# Identifies certain suppliers who were not able to ship required parts in a
#  timely manner.
#
# Identifies suppliers, for a given nation, whose product was part of a multi-
# supplier order (with current status of 'F') where they were the only supplier
# who failed to meet the committed delivery date.
# --------------------------------------------------
opt
SELECT
    s_name,
    count(*) AS numwait
FROM
    supplier,
    lineitem l1,
    orders,
    nation
WHERE
    s_suppkey = l1.l_suppkey
    AND o_orderkey = l1.l_orderkey
    AND o_orderstatus = 'F'
    AND l1.l_receiptDATE > l1.l_commitdate
    AND EXISTS (
        SELECT
            *
        FROM
            lineitem l2
        WHERE
            l2.l_orderkey = l1.l_orderkey
            AND l2.l_suppkey <> l1.l_suppkey
    )
    AND NOT EXISTS (
        SELECT
            *
        FROM
            lineitem l3
        WHERE
            l3.l_orderkey = l1.l_orderkey
            AND l3.l_suppkey <> l1.l_suppkey
            AND l3.l_receiptDATE > l3.l_commitdate
    )
    AND s_nationkey = n_nationkey
    AND n_name = 'SAUDI ARABIA'
GROUP BY
    s_name
ORDER BY
    numwait DESC,
    s_name
LIMIT 100;
----
limit
 ├── columns: s_name:2(string!null) numwait:69(int)
 ├── internal-ordering: -69,+2
 ├── cardinality: [0 - 100]
 ├── key: (2)
 ├── fd: (2)-->(69)
 ├── ordering: -69,+2
 ├── sort
 │    ├── columns: s_name:2(string!null) count_rows:69(int)
 │    ├── key: (2)
 │    ├── fd: (2)-->(69)
 │    ├── ordering: -69,+2
 │    └── group-by
 │         ├── columns: s_name:2(string!null) count_rows:69(int)
 │         ├── grouping columns: s_name:2(string!null)
 │         ├── key: (2)
 │         ├── fd: (2)-->(69)
 │         ├── inner-join (lookup nation)
 │         │    ├── columns: s_suppkey:1(int!null) s_name:2(string!null) s_nationkey:4(int!null) l1.l_orderkey:8(int!null) l1.l_suppkey:10(int!null) l1.l_commitdate:19(date!null) l1.l_receiptdate:20(date!null) o_orderkey:24(int!null) o_orderstatus:26(string!null) n_nationkey:33(int!null) n_name:34(string!null)
 │         │    ├── key columns: [4] = [33]
 │         │    ├── fd: ()-->(26,34), (1)-->(2,4), (8)==(24), (24)==(8), (1)==(10), (10)==(1), (4)==(33), (33)==(4)
 │         │    ├── inner-join (lookup supplier)
 │         │    │    ├── columns: s_suppkey:1(int!null) s_name:2(string!null) s_nationkey:4(int!null) l1.l_orderkey:8(int!null) l1.l_suppkey:10(int!null) l1.l_commitdate:19(date!null) l1.l_receiptdate:20(date!null) o_orderkey:24(int!null) o_orderstatus:26(string!null)
 │         │    │    ├── key columns: [10] = [1]
 │         │    │    ├── fd: ()-->(26), (8)==(24), (24)==(8), (1)-->(2,4), (1)==(10), (10)==(1)
 │         │    │    ├── inner-join (merge)
 │         │    │    │    ├── columns: l1.l_orderkey:8(int!null) l1.l_suppkey:10(int!null) l1.l_commitdate:19(date!null) l1.l_receiptdate:20(date!null) o_orderkey:24(int!null) o_orderstatus:26(string!null)
 │         │    │    │    ├── left ordering: +24
 │         │    │    │    ├── right ordering: +8
 │         │    │    │    ├── fd: ()-->(26), (8)==(24), (24)==(8)
 │         │    │    │    ├── select
 │         │    │    │    │    ├── columns: o_orderkey:24(int!null) o_orderstatus:26(string!null)
 │         │    │    │    │    ├── key: (24)
 │         │    │    │    │    ├── fd: ()-->(26)
 │         │    │    │    │    ├── ordering: +24 opt(26) [actual: +24]
 │         │    │    │    │    ├── scan orders
 │         │    │    │    │    │    ├── columns: o_orderkey:24(int!null) o_orderstatus:26(string!null)
 │         │    │    │    │    │    ├── key: (24)
 │         │    │    │    │    │    ├── fd: (24)-->(26)
 │         │    │    │    │    │    └── ordering: +24 opt(26) [actual: +24]
 │         │    │    │    │    └── filters
 │         │    │    │    │         └── o_orderstatus = 'F' [type=bool, outer=(26), constraints=(/26: [/'F' - /'F']; tight), fd=()-->(26)]
 │         │    │    │    ├── semi-join (merge)
 │         │    │    │    │    ├── columns: l1.l_orderkey:8(int!null) l1.l_suppkey:10(int!null) l1.l_commitdate:19(date!null) l1.l_receiptdate:20(date!null)
 │         │    │    │    │    ├── left ordering: +8
 │         │    │    │    │    ├── right ordering: +37
 │         │    │    │    │    ├── ordering: +8
 │         │    │    │    │    ├── anti-join (merge)
 │         │    │    │    │    │    ├── columns: l1.l_orderkey:8(int!null) l1.l_suppkey:10(int!null) l1.l_commitdate:19(date!null) l1.l_receiptdate:20(date!null)
 │         │    │    │    │    │    ├── left ordering: +8
 │         │    │    │    │    │    ├── right ordering: +53
 │         │    │    │    │    │    ├── ordering: +8
 │         │    │    │    │    │    ├── select
 │         │    │    │    │    │    │    ├── columns: l1.l_orderkey:8(int!null) l1.l_suppkey:10(int!null) l1.l_commitdate:19(date!null) l1.l_receiptdate:20(date!null)
 │         │    │    │    │    │    │    ├── ordering: +8
 │         │    │    │    │    │    │    ├── scan l1
 │         │    │    │    │    │    │    │    ├── columns: l1.l_orderkey:8(int!null) l1.l_suppkey:10(int!null) l1.l_commitdate:19(date!null) l1.l_receiptdate:20(date!null)
 │         │    │    │    │    │    │    │    └── ordering: +8
 │         │    │    │    │    │    │    └── filters
 │         │    │    │    │    │    │         └── l1.l_receiptdate > l1.l_commitdate [type=bool, outer=(19,20), constraints=(/19: (/NULL - ]; /20: (/NULL - ])]
 │         │    │    │    │    │    ├── select
 │         │    │    │    │    │    │    ├── columns: l3.l_orderkey:53(int!null) l3.l_partkey:54(int!null) l3.l_suppkey:55(int!null) l3.l_linenumber:56(int!null) l3.l_quantity:57(float!null) l3.l_extendedprice:58(float!null) l3.l_discount:59(float!null) l3.l_tax:60(float!null) l3.l_returnflag:61(string!null) l3.l_linestatus:62(string!null) l3.l_shipdate:63(date!null) l3.l_commitdate:64(date!null) l3.l_receiptdate:65(date!null) l3.l_shipinstruct:66(string!null) l3.l_shipmode:67(string!null) l3.l_comment:68(string!null)
 │         │    │    │    │    │    │    ├── key: (53,56)
 │         │    │    │    │    │    │    ├── fd: (53,56)-->(54,55,57-68)
 │         │    │    │    │    │    │    ├── ordering: +53
 │         │    │    │    │    │    │    ├── scan l3
 │         │    │    │    │    │    │    │    ├── columns: l3.l_orderkey:53(int!null) l3.l_partkey:54(int!null) l3.l_suppkey:55(int!null) l3.l_linenumber:56(int!null) l3.l_quantity:57(float!null) l3.l_extendedprice:58(float!null) l3.l_discount:59(float!null) l3.l_tax:60(float!null) l3.l_returnflag:61(string!null) l3.l_linestatus:62(string!null) l3.l_shipdate:63(date!null) l3.l_commitdate:64(date!null) l3.l_receiptdate:65(date!null) l3.l_shipinstruct:66(string!null) l3.l_shipmode:67(string!null) l3.l_comment:68(string!null)
 │         │    │    │    │    │    │    │    ├── key: (53,56)
 │         │    │    │    │    │    │    │    ├── fd: (53,56)-->(54,55,57-68)
 │         │    │    │    │    │    │    │    └── ordering: +53
 │         │    │    │    │    │    │    └── filters
 │         │    │    │    │    │    │         └── l3.l_receiptdate > l3.l_commitdate [type=bool, outer=(64,65), constraints=(/64: (/NULL - ]; /65: (/NULL - ])]
 │         │    │    │    │    │    └── filters
 │         │    │    │    │    │         └── l3.l_suppkey != l1.l_suppkey [type=bool, outer=(10,55), constraints=(/10: (/NULL - ]; /55: (/NULL - ])]
 │         │    │    │    │    ├── scan l2
 │         │    │    │    │    │    ├── columns: l2.l_orderkey:37(int!null) l2.l_partkey:38(int!null) l2.l_suppkey:39(int!null) l2.l_linenumber:40(int!null) l2.l_quantity:41(float!null) l2.l_extendedprice:42(float!null) l2.l_discount:43(float!null) l2.l_tax:44(float!null) l2.l_returnflag:45(string!null) l2.l_linestatus:46(string!null) l2.l_shipdate:47(date!null) l2.l_commitdate:48(date!null) l2.l_receiptdate:49(date!null) l2.l_shipinstruct:50(string!null) l2.l_shipmode:51(string!null) l2.l_comment:52(string!null)
 │         │    │    │    │    │    ├── key: (37,40)
 │         │    │    │    │    │    ├── fd: (37,40)-->(38,39,41-52)
 │         │    │    │    │    │    └── ordering: +37
 │         │    │    │    │    └── filters
 │         │    │    │    │         └── l2.l_suppkey != l1.l_suppkey [type=bool, outer=(10,39), constraints=(/10: (/NULL - ]; /39: (/NULL - ])]
 │         │    │    │    └── filters (true)
 │         │    │    └── filters (true)
 │         │    └── filters
 │         │         └── n_name = 'SAUDI ARABIA' [type=bool, outer=(34), constraints=(/34: [/'SAUDI ARABIA' - /'SAUDI ARABIA']; tight), fd=()-->(34)]
 │         └── aggregations
 │              └── count-rows [type=int]
 └── const: 100 [type=int]

# --------------------------------------------------
# Q22
# Global Sales Opportunity
# Identifies geographies where there are customers who may be likely to make a
# purchase.
#
# This query counts how many customers within a specific range of country codes
# have not placed orders for 7 years but who have a greater than average
# “positive” account balance. It also reflects the magnitude of that balance.
# Country code is defined as the first two characters of c_phone.
# --------------------------------------------------
opt
SELECT
    cntrycode,
    count(*) AS numcust,
    sum(c_acctbal) AS totacctbal
FROM (
    SELECT
        substring(c_phone FROM 1 FOR 2) AS cntrycode,
        c_acctbal
    FROM
        customer
    WHERE
        substring(c_phone FROM 1 FOR 2) in
            ('13', '31', '23', '29', '30', '18', '17')
        AND c_acctbal > (
            SELECT
                avg(c_acctbal)
            FROM
                customer
            WHERE
                c_acctbal > 0.00
                AND substring(c_phone FROM 1 FOR 2) in
                    ('13', '31', '23', '29', '30', '18', '17')
        )
        AND NOT EXISTS (
            SELECT
                *
            FROM
                orders
            WHERE
                o_custkey = c_custkey
        )
    ) AS custsale
GROUP BY
    cntrycode
ORDER BY
    cntrycode;
----
sort
 ├── columns: cntrycode:27(string) numcust:28(int) totacctbal:29(float)
 ├── key: (27)
 ├── fd: (27)-->(28,29)
 ├── ordering: +27
 └── group-by
      ├── columns: cntrycode:27(string) count_rows:28(int) sum:29(float)
      ├── grouping columns: cntrycode:27(string)
      ├── key: (27)
      ├── fd: (27)-->(28,29)
      ├── project
      │    ├── columns: cntrycode:27(string) c_acctbal:6(float!null)
      │    ├── anti-join (merge)
      │    │    ├── columns: c_custkey:1(int!null) c_phone:5(string!null) c_acctbal:6(float!null)
      │    │    ├── left ordering: +1
      │    │    ├── right ordering: +19
      │    │    ├── key: (1)
      │    │    ├── fd: (1)-->(5,6)
      │    │    ├── select
      │    │    │    ├── columns: c_custkey:1(int!null) c_phone:5(string!null) c_acctbal:6(float!null)
      │    │    │    ├── key: (1)
      │    │    │    ├── fd: (1)-->(5,6)
      │    │    │    ├── ordering: +1
      │    │    │    ├── scan customer
      │    │    │    │    ├── columns: c_custkey:1(int!null) c_phone:5(string!null) c_acctbal:6(float!null)
      │    │    │    │    ├── key: (1)
      │    │    │    │    ├── fd: (1)-->(5,6)
      │    │    │    │    └── ordering: +1
      │    │    │    └── filters
      │    │    │         ├── substring(c_phone, 1, 2) IN ('13', '17', '18', '23', '29', '30', '31') [type=bool, outer=(5)]
      │    │    │         └── gt [type=bool, outer=(6), subquery, constraints=(/6: (/NULL - ])]
      │    │    │              ├── variable: c_acctbal [type=float]
      │    │    │              └── subquery [type=float]
      │    │    │                   └── scalar-group-by
      │    │    │                        ├── columns: avg:17(float)
      │    │    │                        ├── cardinality: [1 - 1]
      │    │    │                        ├── key: ()
      │    │    │                        ├── fd: ()-->(17)
      │    │    │                        ├── select
      │    │    │                        │    ├── columns: c_phone:13(string!null) c_acctbal:14(float!null)
      │    │    │                        │    ├── scan customer
      │    │    │                        │    │    └── columns: c_phone:13(string!null) c_acctbal:14(float!null)
      │    │    │                        │    └── filters
      │    │    │                        │         ├── c_acctbal > 0.0 [type=bool, outer=(14), constraints=(/14: [/5e-324 - ]; tight)]
      │    │    │                        │         └── substring(c_phone, 1, 2) IN ('13', '17', '18', '23', '29', '30', '31') [type=bool, outer=(13)]
      │    │    │                        └── aggregations
      │    │    │                             └── avg [type=float, outer=(14)]
      │    │    │                                  └── variable: c_acctbal [type=float]
      │    │    ├── scan orders@o_ck
      │    │    │    ├── columns: o_custkey:19(int!null)
      │    │    │    └── ordering: +19
      │    │    └── filters (true)
      │    └── projections
      │         └── substring(c_phone, 1, 2) [type=string, outer=(5)]
      └── aggregations
           ├── count-rows [type=int]
           └── sum [type=float, outer=(6)]
                └── variable: c_acctbal [type=float]
