import file=tpce_schema
----

import file=tpce_stats_c5000
----

# TPC-E consists of 12 transactions with varying numbers of queries.

# --------------------------------------------------
# T1
# Broker-Volume
# Emulates a brokerage house’s “up-to-the-minute" internal business processing.
# An example of a Broker-Volume Transaction would be a manager generating a
# report on the current performance potential of various brokers.
# --------------------------------------------------

# Q1
opt
SELECT b_name, sum(tr_qty * tr_bid_price)::FLOAT8
FROM sector, industry, company, security, trade_request, broker
WHERE tr_b_id = b_id
  AND s_symb = tr_s_symb
  AND co_id = s_co_id
  AND in_id = co_in_id
  AND sc_id = in_sc_id
  AND b_name = ANY ARRAY[
                     'Broker1', 'Broker2', 'Broker3', 'Broker4', 'Broker5',
                     'Broker6', 'Broker7', 'Broker8', 'Broker9', 'Broker10',
                     'Broker11', 'Broker12', 'Broker13', 'Broker14', 'Broker15',
                     'Broker16', 'Broker17', 'Broker18', 'Broker19', 'Broker20',
                     'Broker21', 'Broker22', 'Broker23', 'Broker24', 'Broker25',
                     'Broker26', 'Broker27', 'Broker28', 'Broker29', 'Broker30'
                   ]
  AND sc_name = 'Energy'
GROUP BY b_name
ORDER BY 2 DESC;
----
sort
 ├── columns: b_name:49!null sum:56!null
 ├── immutable
 ├── key: (49)
 ├── fd: (49)-->(56)
 ├── ordering: -56
 └── project
      ├── columns: sum:56!null b_name:49!null
      ├── immutable
      ├── key: (49)
      ├── fd: (49)-->(56)
      ├── group-by (hash)
      │    ├── columns: b_name:49!null sum:55!null
      │    ├── grouping columns: b_name:49!null
      │    ├── immutable
      │    ├── key: (49)
      │    ├── fd: (49)-->(55)
      │    ├── project
      │    │    ├── columns: column54:54!null b_name:49!null
      │    │    ├── immutable
      │    │    ├── inner-join (hash)
      │    │    │    ├── columns: sc_id:1!null sc_name:2!null in_id:5!null in_sc_id:7!null co_id:10!null co_in_id:13!null s_symb:21!null s_co_id:26!null tr_s_symb:41!null tr_qty:42!null tr_bid_price:43!null tr_b_id:44!null b_id:47!null b_name:49!null
      │    │    │    ├── multiplicity: left-rows(zero-or-more), right-rows(zero-or-one)
      │    │    │    ├── fd: ()-->(1,2,7), (10)-->(13), (21)-->(26), (47)-->(49), (44)==(47), (47)==(44), (21)==(41), (41)==(21), (10)==(26), (26)==(10), (5)==(13), (13)==(5), (1)==(7), (7)==(1)
      │    │    │    ├── scan sector@sector_sc_name_key
      │    │    │    │    ├── columns: sc_id:1!null sc_name:2!null
      │    │    │    │    ├── constraint: /2: [/'Energy' - /'Energy']
      │    │    │    │    ├── cardinality: [0 - 1]
      │    │    │    │    ├── key: ()
      │    │    │    │    └── fd: ()-->(1,2)
      │    │    │    ├── inner-join (lookup industry)
      │    │    │    │    ├── columns: in_id:5!null in_sc_id:7!null co_id:10!null co_in_id:13!null s_symb:21!null s_co_id:26!null tr_s_symb:41!null tr_qty:42!null tr_bid_price:43!null tr_b_id:44!null b_id:47!null b_name:49!null
      │    │    │    │    ├── key columns: [13] = [5]
      │    │    │    │    ├── lookup columns are key
      │    │    │    │    ├── fd: (5)-->(7), (10)-->(13), (21)-->(26), (47)-->(49), (44)==(47), (47)==(44), (21)==(41), (41)==(21), (10)==(26), (26)==(10), (5)==(13), (13)==(5)
      │    │    │    │    ├── inner-join (lookup company)
      │    │    │    │    │    ├── columns: co_id:10!null co_in_id:13!null s_symb:21!null s_co_id:26!null tr_s_symb:41!null tr_qty:42!null tr_bid_price:43!null tr_b_id:44!null b_id:47!null b_name:49!null
      │    │    │    │    │    ├── key columns: [26] = [10]
      │    │    │    │    │    ├── lookup columns are key
      │    │    │    │    │    ├── fd: (10)-->(13), (21)-->(26), (47)-->(49), (44)==(47), (47)==(44), (21)==(41), (41)==(21), (10)==(26), (26)==(10)
      │    │    │    │    │    ├── inner-join (lookup security)
      │    │    │    │    │    │    ├── columns: s_symb:21!null s_co_id:26!null tr_s_symb:41!null tr_qty:42!null tr_bid_price:43!null tr_b_id:44!null b_id:47!null b_name:49!null
      │    │    │    │    │    │    ├── key columns: [41] = [21]
      │    │    │    │    │    │    ├── lookup columns are key
      │    │    │    │    │    │    ├── fd: (21)-->(26), (47)-->(49), (44)==(47), (47)==(44), (21)==(41), (41)==(21)
      │    │    │    │    │    │    ├── inner-join (lookup broker)
      │    │    │    │    │    │    │    ├── columns: tr_s_symb:41!null tr_qty:42!null tr_bid_price:43!null tr_b_id:44!null b_id:47!null b_name:49!null
      │    │    │    │    │    │    │    ├── key columns: [44] = [47]
      │    │    │    │    │    │    │    ├── lookup columns are key
      │    │    │    │    │    │    │    ├── fd: (47)-->(49), (44)==(47), (47)==(44)
      │    │    │    │    │    │    │    ├── scan trade_request@trade_request_tr_b_id_tr_s_symb_idx
      │    │    │    │    │    │    │    │    └── columns: tr_s_symb:41!null tr_qty:42!null tr_bid_price:43!null tr_b_id:44!null
      │    │    │    │    │    │    │    └── filters
      │    │    │    │    │    │    │         └── b_name:49 IN ('Broker1', 'Broker10', 'Broker11', 'Broker12', 'Broker13', 'Broker14', 'Broker15', 'Broker16', 'Broker17', 'Broker18', 'Broker19', 'Broker2', 'Broker20', 'Broker21', 'Broker22', 'Broker23', 'Broker24', 'Broker25', 'Broker26', 'Broker27', 'Broker28', 'Broker29', 'Broker3', 'Broker30', 'Broker4', 'Broker5', 'Broker6', 'Broker7', 'Broker8', 'Broker9') [outer=(49), constraints=(/49: [/'Broker1' - /'Broker1'] [/'Broker10' - /'Broker10'] [/'Broker11' - /'Broker11'] [/'Broker12' - /'Broker12'] [/'Broker13' - /'Broker13'] [/'Broker14' - /'Broker14'] [/'Broker15' - /'Broker15'] [/'Broker16' - /'Broker16'] [/'Broker17' - /'Broker17'] [/'Broker18' - /'Broker18'] [/'Broker19' - /'Broker19'] [/'Broker2' - /'Broker2'] [/'Broker20' - /'Broker20'] [/'Broker21' - /'Broker21'] [/'Broker22' - /'Broker22'] [/'Broker23' - /'Broker23'] [/'Broker24' - /'Broker24'] [/'Broker25' - /'Broker25'] [/'Broker26' - /'Broker26'] [/'Broker27' - /'Broker27'] [/'Broker28' - /'Broker28'] [/'Broker29' - /'Broker29'] [/'Broker3' - /'Broker3'] [/'Broker30' - /'Broker30'] [/'Broker4' - /'Broker4'] [/'Broker5' - /'Broker5'] [/'Broker6' - /'Broker6'] [/'Broker7' - /'Broker7'] [/'Broker8' - /'Broker8'] [/'Broker9' - /'Broker9']; tight)]
      │    │    │    │    │    │    └── filters (true)
      │    │    │    │    │    └── filters (true)
      │    │    │    │    └── filters (true)
      │    │    │    └── filters
      │    │    │         └── sc_id:1 = in_sc_id:7 [outer=(1,7), constraints=(/1: (/NULL - ]; /7: (/NULL - ]), fd=(1)==(7), (7)==(1)]
      │    │    └── projections
      │    │         └── tr_qty:42 * tr_bid_price:43 [as=column54:54, outer=(42,43), immutable]
      │    └── aggregations
      │         └── sum [as=sum:55, outer=(54)]
      │              └── column54:54
      └── projections
           └── sum:55::FLOAT8 [as=sum:56, outer=(55), immutable]

# Q1 (With Hints)
opt
           SELECT b_name, sum(tr_qty * tr_bid_price)::FLOAT8
             FROM broker
INNER LOOKUP JOIN trade_request ON tr_b_id = b_id
INNER LOOKUP JOIN security      ON s_symb = tr_s_symb
INNER LOOKUP JOIN company       ON co_id = s_co_id
  INNER HASH JOIN (
             SELECT in_id
               FROM sector
  INNER LOOKUP JOIN industry ON in_sc_id = sc_id
              WHERE sc_name = 'Energy'
                  ) ON in_id = co_in_id
            WHERE b_name = ANY ARRAY[
                                 'Broker1', 'Broker2', 'Broker3', 'Broker4', 'Broker5',
                                 'Broker6', 'Broker7', 'Broker8', 'Broker9', 'Broker10',
                                 'Broker11', 'Broker12', 'Broker13', 'Broker14', 'Broker15',
                                 'Broker16', 'Broker17', 'Broker18', 'Broker19', 'Broker20',
                                 'Broker21', 'Broker22', 'Broker23', 'Broker24', 'Broker25',
                                 'Broker26', 'Broker27', 'Broker28', 'Broker29', 'Broker30'
                               ]
         GROUP BY b_name
         ORDER BY 2 DESC;
----
sort
 ├── columns: b_name:3!null sum:56!null
 ├── immutable
 ├── key: (3)
 ├── fd: (3)-->(56)
 ├── ordering: -56
 └── project
      ├── columns: sum:56!null b_name:3!null
      ├── immutable
      ├── key: (3)
      ├── fd: (3)-->(56)
      ├── group-by (hash)
      │    ├── columns: b_name:3!null sum:55!null
      │    ├── grouping columns: b_name:3!null
      │    ├── immutable
      │    ├── key: (3)
      │    ├── fd: (3)-->(55)
      │    ├── project
      │    │    ├── columns: column54:54!null b_name:3!null
      │    │    ├── immutable
      │    │    ├── inner-join (hash)
      │    │    │    ├── columns: b_id:1!null b_name:3!null tr_s_symb:10!null tr_qty:11!null tr_bid_price:12!null tr_b_id:13!null s_symb:16!null s_co_id:21!null co_id:34!null co_in_id:37!null sc_id:45!null sc_name:46!null in_id:49!null in_sc_id:51!null
      │    │    │    ├── flags: force hash join (store right side)
      │    │    │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
      │    │    │    ├── fd: ()-->(45,46,51), (1)-->(3), (16)-->(21), (34)-->(37), (1)==(13), (13)==(1), (10)==(16), (16)==(10), (21)==(34), (34)==(21), (45)==(51), (51)==(45), (37)==(49), (49)==(37)
      │    │    │    ├── inner-join (lookup company)
      │    │    │    │    ├── columns: b_id:1!null b_name:3!null tr_s_symb:10!null tr_qty:11!null tr_bid_price:12!null tr_b_id:13!null s_symb:16!null s_co_id:21!null co_id:34!null co_in_id:37!null
      │    │    │    │    ├── flags: force lookup join (into right side)
      │    │    │    │    ├── key columns: [21] = [34]
      │    │    │    │    ├── lookup columns are key
      │    │    │    │    ├── fd: (1)-->(3), (16)-->(21), (34)-->(37), (1)==(13), (13)==(1), (10)==(16), (16)==(10), (21)==(34), (34)==(21)
      │    │    │    │    ├── inner-join (lookup security)
      │    │    │    │    │    ├── columns: b_id:1!null b_name:3!null tr_s_symb:10!null tr_qty:11!null tr_bid_price:12!null tr_b_id:13!null s_symb:16!null s_co_id:21!null
      │    │    │    │    │    ├── flags: force lookup join (into right side)
      │    │    │    │    │    ├── key columns: [10] = [16]
      │    │    │    │    │    ├── lookup columns are key
      │    │    │    │    │    ├── fd: (1)-->(3), (16)-->(21), (1)==(13), (13)==(1), (10)==(16), (16)==(10)
      │    │    │    │    │    ├── inner-join (lookup trade_request@trade_request_tr_b_id_tr_s_symb_idx)
      │    │    │    │    │    │    ├── columns: b_id:1!null b_name:3!null tr_s_symb:10!null tr_qty:11!null tr_bid_price:12!null tr_b_id:13!null
      │    │    │    │    │    │    ├── flags: force lookup join (into right side)
      │    │    │    │    │    │    ├── key columns: [1] = [13]
      │    │    │    │    │    │    ├── fd: (1)-->(3), (1)==(13), (13)==(1)
      │    │    │    │    │    │    ├── select
      │    │    │    │    │    │    │    ├── columns: b_id:1!null b_name:3!null
      │    │    │    │    │    │    │    ├── key: (1)
      │    │    │    │    │    │    │    ├── fd: (1)-->(3)
      │    │    │    │    │    │    │    ├── scan broker@broker_b_name_idx
      │    │    │    │    │    │    │    │    ├── columns: b_id:1!null b_name:3!null
      │    │    │    │    │    │    │    │    ├── key: (1)
      │    │    │    │    │    │    │    │    └── fd: (1)-->(3)
      │    │    │    │    │    │    │    └── filters
      │    │    │    │    │    │    │         └── b_name:3 IN ('Broker1', 'Broker10', 'Broker11', 'Broker12', 'Broker13', 'Broker14', 'Broker15', 'Broker16', 'Broker17', 'Broker18', 'Broker19', 'Broker2', 'Broker20', 'Broker21', 'Broker22', 'Broker23', 'Broker24', 'Broker25', 'Broker26', 'Broker27', 'Broker28', 'Broker29', 'Broker3', 'Broker30', 'Broker4', 'Broker5', 'Broker6', 'Broker7', 'Broker8', 'Broker9') [outer=(3), constraints=(/3: [/'Broker1' - /'Broker1'] [/'Broker10' - /'Broker10'] [/'Broker11' - /'Broker11'] [/'Broker12' - /'Broker12'] [/'Broker13' - /'Broker13'] [/'Broker14' - /'Broker14'] [/'Broker15' - /'Broker15'] [/'Broker16' - /'Broker16'] [/'Broker17' - /'Broker17'] [/'Broker18' - /'Broker18'] [/'Broker19' - /'Broker19'] [/'Broker2' - /'Broker2'] [/'Broker20' - /'Broker20'] [/'Broker21' - /'Broker21'] [/'Broker22' - /'Broker22'] [/'Broker23' - /'Broker23'] [/'Broker24' - /'Broker24'] [/'Broker25' - /'Broker25'] [/'Broker26' - /'Broker26'] [/'Broker27' - /'Broker27'] [/'Broker28' - /'Broker28'] [/'Broker29' - /'Broker29'] [/'Broker3' - /'Broker3'] [/'Broker30' - /'Broker30'] [/'Broker4' - /'Broker4'] [/'Broker5' - /'Broker5'] [/'Broker6' - /'Broker6'] [/'Broker7' - /'Broker7'] [/'Broker8' - /'Broker8'] [/'Broker9' - /'Broker9']; tight)]
      │    │    │    │    │    │    └── filters (true)
      │    │    │    │    │    └── filters (true)
      │    │    │    │    └── filters (true)
      │    │    │    ├── inner-join (lookup industry@industry_in_sc_id_idx)
      │    │    │    │    ├── columns: sc_id:45!null sc_name:46!null in_id:49!null in_sc_id:51!null
      │    │    │    │    ├── flags: force lookup join (into right side)
      │    │    │    │    ├── key columns: [45] = [51]
      │    │    │    │    ├── key: (49)
      │    │    │    │    ├── fd: ()-->(45,46,51), (45)==(51), (51)==(45)
      │    │    │    │    ├── scan sector@sector_sc_name_key
      │    │    │    │    │    ├── columns: sc_id:45!null sc_name:46!null
      │    │    │    │    │    ├── constraint: /46: [/'Energy' - /'Energy']
      │    │    │    │    │    ├── cardinality: [0 - 1]
      │    │    │    │    │    ├── key: ()
      │    │    │    │    │    └── fd: ()-->(45,46)
      │    │    │    │    └── filters (true)
      │    │    │    └── filters
      │    │    │         └── in_id:49 = co_in_id:37 [outer=(37,49), constraints=(/37: (/NULL - ]; /49: (/NULL - ]), fd=(37)==(49), (49)==(37)]
      │    │    └── projections
      │    │         └── tr_qty:11 * tr_bid_price:12 [as=column54:54, outer=(11,12), immutable]
      │    └── aggregations
      │         └── sum [as=sum:55, outer=(54)]
      │              └── column54:54
      └── projections
           └── sum:55::FLOAT8 [as=sum:56, outer=(55), immutable]

# --------------------------------------------------
# T2
# Customer-Position
# Emulates the process of retrieving the customer’s profile and summarizing
# their overall standing based on current market values for all assets. This is
# representative of the work performed when a customer asks the question “What
# am I worth today?”
# --------------------------------------------------

# Q1
opt
SELECT c_id,
       c_st_id,
       c_l_name,
       c_f_name,
       c_m_name,
       c_gndr,
       c_tier,
       c_dob,
       c_ad_id,
       c_ctry_1,
       c_area_1,
       c_local_1,
       c_ext_1,
       c_ctry_2,
       c_area_2,
       c_local_2,
       c_ext_2,
       c_ctry_3,
       c_area_3,
       c_local_3,
       c_ext_3,
       c_email_1,
       c_email_2
  FROM customer
 WHERE c_id = 0;
----
scan customer
 ├── columns: c_id:1!null c_st_id:3!null c_l_name:4!null c_f_name:5!null c_m_name:6 c_gndr:7 c_tier:8!null c_dob:9!null c_ad_id:10!null c_ctry_1:11 c_area_1:12 c_local_1:13 c_ext_1:14 c_ctry_2:15 c_area_2:16 c_local_2:17 c_ext_2:18 c_ctry_3:19 c_area_3:20 c_local_3:21 c_ext_3:22 c_email_1:23 c_email_2:24
 ├── constraint: /1: [/0 - /0]
 ├── cardinality: [0 - 1]
 ├── key: ()
 └── fd: ()-->(1,3-24)

# Q2
opt
SELECT c_id,
       c_st_id,
       c_l_name,
       c_f_name,
       c_m_name,
       c_gndr,
       c_tier,
       c_dob,
       c_ad_id,
       c_ctry_1,
       c_area_1,
       c_local_1,
       c_ext_1,
       c_ctry_2,
       c_area_2,
       c_local_2,
       c_ext_2,
       c_ctry_3,
       c_area_3,
       c_local_3,
       c_ext_3,
       c_email_1,
       c_email_2
  FROM customer
 WHERE c_tax_id = 'US1';
----
project
 ├── columns: c_id:1!null c_st_id:3!null c_l_name:4!null c_f_name:5!null c_m_name:6 c_gndr:7 c_tier:8!null c_dob:9!null c_ad_id:10!null c_ctry_1:11 c_area_1:12 c_local_1:13 c_ext_1:14 c_ctry_2:15 c_area_2:16 c_local_2:17 c_ext_2:18 c_ctry_3:19 c_area_3:20 c_local_3:21 c_ext_3:22 c_email_1:23 c_email_2:24
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(1,3-24)
 └── index-join customer
      ├── columns: c_id:1!null c_tax_id:2!null c_st_id:3!null c_l_name:4!null c_f_name:5!null c_m_name:6 c_gndr:7 c_tier:8!null c_dob:9!null c_ad_id:10!null c_ctry_1:11 c_area_1:12 c_local_1:13 c_ext_1:14 c_ctry_2:15 c_area_2:16 c_local_2:17 c_ext_2:18 c_ctry_3:19 c_area_3:20 c_local_3:21 c_ext_3:22 c_email_1:23 c_email_2:24
      ├── cardinality: [0 - 1]
      ├── key: ()
      ├── fd: ()-->(1-24)
      └── scan customer@customer_c_tax_id_key
           ├── columns: c_id:1!null c_tax_id:2!null
           ├── constraint: /2: [/'US1' - /'US1']
           ├── cardinality: [0 - 1]
           ├── key: ()
           └── fd: ()-->(1,2)

# Q3
opt
   SELECT ca_id,
          ca_bal::FLOAT8,
          IFNULL((sum(hs_qty * lt_price)), 0)::FLOAT8
     FROM customer_account
LEFT JOIN holding_summary ON hs_ca_id  = ca_id
LEFT JOIN last_trade      ON lt_s_symb = hs_s_symb
    WHERE ca_c_id = 0
 GROUP BY ca_id, ca_bal
 ORDER BY 3 ASC
    LIMIT 10;
----
top-k
 ├── columns: ca_id:1!null ca_bal:23!null coalesce:24
 ├── internal-ordering: +24
 ├── k: 10
 ├── cardinality: [0 - 10]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(23,24)
 ├── ordering: +24
 └── project
      ├── columns: ca_bal:23!null coalesce:24 ca_id:1!null
      ├── immutable
      ├── key: (1)
      ├── fd: (1)-->(23,24)
      ├── group-by (streaming)
      │    ├── columns: ca_id:1!null customer_account.ca_bal:6!null sum:22
      │    ├── grouping columns: ca_id:1!null
      │    ├── internal-ordering: +1
      │    ├── immutable
      │    ├── key: (1)
      │    ├── fd: (1)-->(6,22)
      │    ├── project
      │    │    ├── columns: column21:21 ca_id:1!null customer_account.ca_bal:6!null
      │    │    ├── immutable
      │    │    ├── fd: (1)-->(6)
      │    │    ├── ordering: +1
      │    │    ├── left-join (lookup last_trade)
      │    │    │    ├── columns: ca_id:1!null ca_c_id:3!null customer_account.ca_bal:6!null hs_ca_id:9 hs_s_symb:10 hs_qty:11 lt_s_symb:14 lt_price:16
      │    │    │    ├── key columns: [10] = [14]
      │    │    │    ├── lookup columns are key
      │    │    │    ├── key: (1,9,10)
      │    │    │    ├── fd: ()-->(3), (1)-->(6), (9,10)-->(11), (14)-->(16), (1,9,10)-->(14,16)
      │    │    │    ├── ordering: +1 opt(3) [actual: +1]
      │    │    │    ├── left-join (lookup holding_summary)
      │    │    │    │    ├── columns: ca_id:1!null ca_c_id:3!null customer_account.ca_bal:6!null hs_ca_id:9 hs_s_symb:10 hs_qty:11
      │    │    │    │    ├── key columns: [1] = [9]
      │    │    │    │    ├── key: (1,9,10)
      │    │    │    │    ├── fd: ()-->(3), (1)-->(6), (9,10)-->(11)
      │    │    │    │    ├── ordering: +1 opt(3) [actual: +1]
      │    │    │    │    ├── index-join customer_account
      │    │    │    │    │    ├── columns: ca_id:1!null ca_c_id:3!null customer_account.ca_bal:6!null
      │    │    │    │    │    ├── key: (1)
      │    │    │    │    │    ├── fd: ()-->(3), (1)-->(6)
      │    │    │    │    │    ├── ordering: +1 opt(3) [actual: +1]
      │    │    │    │    │    └── scan customer_account@customer_account_ca_c_id_idx
      │    │    │    │    │         ├── columns: ca_id:1!null ca_c_id:3!null
      │    │    │    │    │         ├── constraint: /3/1: [/0 - /0]
      │    │    │    │    │         ├── key: (1)
      │    │    │    │    │         ├── fd: ()-->(3)
      │    │    │    │    │         └── ordering: +1 opt(3) [actual: +1]
      │    │    │    │    └── filters (true)
      │    │    │    └── filters (true)
      │    │    └── projections
      │    │         └── hs_qty:11 * lt_price:16 [as=column21:21, outer=(11,16), immutable]
      │    └── aggregations
      │         ├── sum [as=sum:22, outer=(21)]
      │         │    └── column21:21
      │         └── const-agg [as=customer_account.ca_bal:6, outer=(6)]
      │              └── customer_account.ca_bal:6
      └── projections
           ├── customer_account.ca_bal:6::FLOAT8 [as=ca_bal:23, outer=(6), immutable]
           └── COALESCE(sum:22, 0)::FLOAT8 [as=coalesce:24, outer=(22), immutable]

# Q3 (With Hints)
opt
          SELECT ca_id,
                 ca_bal::FLOAT8,
                 IFNULL((sum(hs_qty * lt_price)), 0)::FLOAT8
            FROM customer_account
LEFT LOOKUP JOIN holding_summary ON hs_ca_id  = ca_id
LEFT LOOKUP JOIN last_trade      ON lt_s_symb = hs_s_symb
           WHERE ca_c_id = 0
        GROUP BY ca_id, ca_bal
        ORDER BY 3 ASC
           LIMIT 10;
----
top-k
 ├── columns: ca_id:1!null ca_bal:23!null coalesce:24
 ├── internal-ordering: +24
 ├── k: 10
 ├── cardinality: [0 - 10]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(23,24)
 ├── ordering: +24
 └── project
      ├── columns: ca_bal:23!null coalesce:24 ca_id:1!null
      ├── immutable
      ├── key: (1)
      ├── fd: (1)-->(23,24)
      ├── group-by (streaming)
      │    ├── columns: ca_id:1!null customer_account.ca_bal:6!null sum:22
      │    ├── grouping columns: ca_id:1!null
      │    ├── internal-ordering: +1
      │    ├── immutable
      │    ├── key: (1)
      │    ├── fd: (1)-->(6,22)
      │    ├── project
      │    │    ├── columns: column21:21 ca_id:1!null customer_account.ca_bal:6!null
      │    │    ├── immutable
      │    │    ├── fd: (1)-->(6)
      │    │    ├── ordering: +1
      │    │    ├── left-join (lookup last_trade)
      │    │    │    ├── columns: ca_id:1!null ca_c_id:3!null customer_account.ca_bal:6!null hs_ca_id:9 hs_s_symb:10 hs_qty:11 lt_s_symb:14 lt_price:16
      │    │    │    ├── flags: force lookup join (into right side)
      │    │    │    ├── key columns: [10] = [14]
      │    │    │    ├── lookup columns are key
      │    │    │    ├── key: (1,9,10)
      │    │    │    ├── fd: ()-->(3), (1)-->(6), (9,10)-->(11), (14)-->(16), (1,9,10)-->(14,16)
      │    │    │    ├── ordering: +1 opt(3) [actual: +1]
      │    │    │    ├── left-join (lookup holding_summary)
      │    │    │    │    ├── columns: ca_id:1!null ca_c_id:3!null customer_account.ca_bal:6!null hs_ca_id:9 hs_s_symb:10 hs_qty:11
      │    │    │    │    ├── flags: force lookup join (into right side)
      │    │    │    │    ├── key columns: [1] = [9]
      │    │    │    │    ├── key: (1,9,10)
      │    │    │    │    ├── fd: ()-->(3), (1)-->(6), (9,10)-->(11)
      │    │    │    │    ├── ordering: +1 opt(3) [actual: +1]
      │    │    │    │    ├── index-join customer_account
      │    │    │    │    │    ├── columns: ca_id:1!null ca_c_id:3!null customer_account.ca_bal:6!null
      │    │    │    │    │    ├── key: (1)
      │    │    │    │    │    ├── fd: ()-->(3), (1)-->(6)
      │    │    │    │    │    ├── ordering: +1 opt(3) [actual: +1]
      │    │    │    │    │    └── scan customer_account@customer_account_ca_c_id_idx
      │    │    │    │    │         ├── columns: ca_id:1!null ca_c_id:3!null
      │    │    │    │    │         ├── constraint: /3/1: [/0 - /0]
      │    │    │    │    │         ├── key: (1)
      │    │    │    │    │         ├── fd: ()-->(3)
      │    │    │    │    │         └── ordering: +1 opt(3) [actual: +1]
      │    │    │    │    └── filters (true)
      │    │    │    └── filters (true)
      │    │    └── projections
      │    │         └── hs_qty:11 * lt_price:16 [as=column21:21, outer=(11,16), immutable]
      │    └── aggregations
      │         ├── sum [as=sum:22, outer=(21)]
      │         │    └── column21:21
      │         └── const-agg [as=customer_account.ca_bal:6, outer=(6)]
      │              └── customer_account.ca_bal:6
      └── projections
           ├── customer_account.ca_bal:6::FLOAT8 [as=ca_bal:23, outer=(6), immutable]
           └── COALESCE(sum:22, 0)::FLOAT8 [as=coalesce:24, outer=(22), immutable]

# Q4
#
# TODO: The limit could be pushed down if limit push-down rules could see
# different join orders.
#
opt
SELECT t_id, t_s_symb, t_qty, st_name, th_dts
FROM (
          SELECT t_id, t_s_symb, t_qty
            FROM trade
           WHERE t_ca_id = 0
        ORDER BY t_dts DESC
           LIMIT 10
     ) AS t,
     trade_history,
     status_type
WHERE th_t_id = t_id AND st_id = th_st_id
ORDER BY th_dts DESC
LIMIT 30;
----
project
 ├── columns: t_id:1!null t_s_symb:6!null t_qty:7!null st_name:24!null th_dts:19!null
 ├── cardinality: [0 - 30]
 ├── fd: (1)-->(6,7)
 ├── ordering: -19
 └── top-k
      ├── columns: t_id:1!null t_dts:2!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null th_t_id:18!null th_dts:19!null th_st_id:20!null st_id:23!null st_name:24!null
      ├── internal-ordering: -19 opt(9)
      ├── k: 30
      ├── cardinality: [0 - 30]
      ├── key: (18,23)
      ├── fd: ()-->(9), (1)-->(2,6,7), (18,20)-->(19), (23)-->(24), (20)==(23), (23)==(20), (1)==(18), (18)==(1)
      ├── ordering: -19 opt(9) [actual: -19]
      └── inner-join (hash)
           ├── columns: t_id:1!null t_dts:2!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null th_t_id:18!null th_dts:19!null th_st_id:20!null st_id:23!null st_name:24!null
           ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
           ├── key: (18,23)
           ├── fd: ()-->(9), (1)-->(2,6,7), (18,20)-->(19), (23)-->(24), (20)==(23), (23)==(20), (1)==(18), (18)==(1)
           ├── inner-join (lookup trade_history)
           │    ├── columns: t_id:1!null t_dts:2!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null th_t_id:18!null th_dts:19!null th_st_id:20!null
           │    ├── key columns: [1] = [18]
           │    ├── key: (18,20)
           │    ├── fd: ()-->(9), (1)-->(2,6,7), (18,20)-->(19), (1)==(18), (18)==(1)
           │    ├── scan trade@trade_t_ca_id_t_dts_idx
           │    │    ├── columns: t_id:1!null t_dts:2!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null
           │    │    ├── constraint: /9/-2/1: [/0 - /0]
           │    │    ├── limit: 10
           │    │    ├── key: (1)
           │    │    └── fd: ()-->(9), (1)-->(2,6,7)
           │    └── filters (true)
           ├── scan status_type
           │    ├── columns: st_id:23!null st_name:24!null
           │    ├── key: (23)
           │    └── fd: (23)-->(24)
           └── filters
                └── st_id:23 = th_st_id:20 [outer=(20,23), constraints=(/20: (/NULL - ]; /23: (/NULL - ]), fd=(20)==(23), (23)==(20)]

# --------------------------------------------------
# T3
# Market-Feed
# Emulates the process of tracking the current market activity. This is
# representative of the brokerage house processing the “ticker-tape” from the
# market exchange.
# --------------------------------------------------

# Q1
opt
WITH
update_last_trade AS (
    UPDATE last_trade
       SET lt_vol = lt_vol + 10,
           lt_price = 100.00:::FLOAT8::DECIMAL,
           lt_dts = '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP
     WHERE lt_s_symb = 'ROACH'
    RETURNING NULL
),
request_list AS (
    SELECT tr_t_id, tr_bid_price::FLOAT8, tr_tt_id, tr_qty
      FROM trade_request
     WHERE tr_s_symb = 'ROACH'
       AND (
             (tr_tt_id = 'TMB'::VARCHAR(3) AND tr_bid_price >= 100.00:::FLOAT8::DECIMAL) OR
             (tr_tt_id = 'TMS'::VARCHAR(3) AND tr_bid_price <= 100.00:::FLOAT8::DECIMAL) OR
             (tr_tt_id = 'TLS'::VARCHAR(3) AND tr_bid_price >= 100.00:::FLOAT8::DECIMAL)
           )
),
delete_trade_request AS (
    DELETE FROM trade_request
    WHERE tr_t_id IN (SELECT tr_t_id FROM request_list)
    RETURNING NULL
),
insert_trade_history AS (
    INSERT INTO trade_history (th_t_id, th_st_id, th_dts)
    (SELECT tr_t_id, 'SBMT', '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP FROM request_list)
    RETURNING NULL
),
update_trade_submitted AS (
       UPDATE trade
          SET t_st_id = 'SBMT', t_dts = '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP
        WHERE t_id IN (SELECT tr_t_id FROM request_list)
    RETURNING NULL
)
SELECT * FROM request_list;
----
with &1 (update_last_trade)
 ├── columns: tr_t_id:141!null tr_bid_price:142!null tr_tt_id:143!null tr_qty:144!null
 ├── volatile, mutations
 ├── key: (141)
 ├── fd: (141)-->(142-144)
 ├── project
 │    ├── columns: "?column?":19
 │    ├── cardinality: [0 - 1]
 │    ├── volatile, mutations
 │    ├── key: ()
 │    ├── fd: ()-->(19)
 │    ├── update last_trade
 │    │    ├── columns: lt_s_symb:1!null
 │    │    ├── fetch columns: lt_s_symb:8 lt_dts:9 lt_price:10 lt_open_price:11 lt_vol:12
 │    │    ├── update-mapping:
 │    │    │    ├── lt_dts_new:17 => lt_dts:2
 │    │    │    ├── lt_price_cast:18 => lt_price:3
 │    │    │    └── lt_vol_new:15 => lt_vol:5
 │    │    ├── return-mapping:
 │    │    │    └── lt_s_symb:8 => lt_s_symb:1
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── volatile, mutations
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1)
 │    │    └── project
 │    │         ├── columns: lt_price_cast:18!null lt_vol_new:15!null lt_dts_new:17!null lt_s_symb:8!null lt_dts:9!null lt_price:10!null lt_open_price:11!null lt_vol:12!null
 │    │         ├── cardinality: [0 - 1]
 │    │         ├── immutable
 │    │         ├── key: ()
 │    │         ├── fd: ()-->(8-12,15,17,18)
 │    │         ├── scan last_trade
 │    │         │    ├── columns: lt_s_symb:8!null lt_dts:9!null lt_price:10!null lt_open_price:11!null lt_vol:12!null
 │    │         │    ├── constraint: /8: [/'ROACH' - /'ROACH']
 │    │         │    ├── flags: avoid-full-scan
 │    │         │    ├── cardinality: [0 - 1]
 │    │         │    ├── key: ()
 │    │         │    └── fd: ()-->(8-12)
 │    │         └── projections
 │    │              ├── 100.00 [as=lt_price_cast:18]
 │    │              ├── lt_vol:12 + 10 [as=lt_vol_new:15, outer=(12), immutable]
 │    │              └── '2020-06-15 22:27:42.148484' [as=lt_dts_new:17]
 │    └── projections
 │         └── NULL [as="?column?":19]
 └── with &2 (request_list)
      ├── columns: tr_t_id:141!null tr_bid_price:142!null tr_tt_id:143!null tr_qty:144!null
      ├── volatile, mutations
      ├── key: (141)
      ├── fd: (141)-->(142-144)
      ├── project
      │    ├── columns: tr_bid_price:28!null trade_request.tr_t_id:20!null trade_request.tr_tt_id:21!null trade_request.tr_qty:23!null
      │    ├── immutable
      │    ├── key: (20)
      │    ├── fd: (20)-->(21,23,28)
      │    ├── select
      │    │    ├── columns: trade_request.tr_t_id:20!null trade_request.tr_tt_id:21!null tr_s_symb:22!null trade_request.tr_qty:23!null trade_request.tr_bid_price:24!null
      │    │    ├── immutable
      │    │    ├── key: (20)
      │    │    ├── fd: ()-->(22), (20)-->(21,23,24)
      │    │    ├── scan trade_request
      │    │    │    ├── columns: trade_request.tr_t_id:20!null trade_request.tr_tt_id:21!null tr_s_symb:22!null trade_request.tr_qty:23!null trade_request.tr_bid_price:24!null
      │    │    │    ├── check constraint expressions
      │    │    │    │    ├── trade_request.tr_qty:23 > 0 [outer=(23), constraints=(/23: [/1 - ]; tight)]
      │    │    │    │    └── trade_request.tr_bid_price:24 > 0 [outer=(24), immutable, constraints=(/24: (/0 - ]; tight)]
      │    │    │    ├── partial index predicates
      │    │    │    │    └── trade_request_tr_s_symb_idx: filters
      │    │    │    │         └── trade_request.tr_tt_id:21 IN ('TLB', 'TLS', 'TSL') [outer=(21), constraints=(/21: [/'TLB' - /'TLB'] [/'TLS' - /'TLS'] [/'TSL' - /'TSL']; tight)]
      │    │    │    ├── key: (20)
      │    │    │    └── fd: (20)-->(21-24)
      │    │    └── filters
      │    │         ├── tr_s_symb:22 = 'ROACH' [outer=(22), constraints=(/22: [/'ROACH' - /'ROACH']; tight), fd=()-->(22)]
      │    │         └── (((trade_request.tr_tt_id:21 = 'TMB') AND (trade_request.tr_bid_price:24 >= 1E+2)) OR ((trade_request.tr_tt_id:21 = 'TMS') AND (trade_request.tr_bid_price:24 <= 1E+2))) OR ((trade_request.tr_tt_id:21 = 'TLS') AND (trade_request.tr_bid_price:24 >= 1E+2)) [outer=(21,24), immutable, constraints=(/21: [/'TLS' - /'TLS'] [/'TMB' - /'TMB'] [/'TMS' - /'TMS']; /24: (/NULL - ])]
      │    └── projections
      │         └── trade_request.tr_bid_price:24::FLOAT8 [as=tr_bid_price:28, outer=(24), immutable]
      └── with &3 (delete_trade_request)
           ├── columns: tr_t_id:141!null tr_bid_price:142!null tr_tt_id:143!null tr_qty:144!null
           ├── volatile, mutations
           ├── key: (141)
           ├── fd: (141)-->(142-144)
           ├── project
           │    ├── columns: "?column?":51
           │    ├── volatile, mutations
           │    ├── fd: ()-->(51)
           │    ├── delete trade_request
           │    │    ├── columns: trade_request.tr_t_id:29!null
           │    │    ├── fetch columns: trade_request.tr_t_id:37 tr_s_symb:39 tr_b_id:42
           │    │    ├── return-mapping:
           │    │    │    └── trade_request.tr_t_id:37 => trade_request.tr_t_id:29
           │    │    ├── partial index del columns: partial_index_del1:50
           │    │    ├── volatile, mutations
           │    │    ├── key: (29)
           │    │    └── project
           │    │         ├── columns: partial_index_del1:50!null trade_request.tr_t_id:37!null tr_s_symb:39!null tr_b_id:42!null
           │    │         ├── key: (37)
           │    │         ├── fd: (37)-->(39,42,50)
           │    │         ├── project
           │    │         │    ├── columns: trade_request.tr_t_id:37!null trade_request.tr_tt_id:38!null tr_s_symb:39!null tr_b_id:42!null
           │    │         │    ├── key: (37)
           │    │         │    ├── fd: (37)-->(38,39,42)
           │    │         │    └── inner-join (lookup trade_request)
           │    │         │         ├── columns: trade_request.tr_t_id:37!null trade_request.tr_tt_id:38!null tr_s_symb:39!null tr_b_id:42!null tr_t_id:45!null
           │    │         │         ├── key columns: [45] = [37]
           │    │         │         ├── lookup columns are key
           │    │         │         ├── key: (45)
           │    │         │         ├── fd: (37)-->(38,39,42), (37)==(45), (45)==(37)
           │    │         │         ├── with-scan &2 (request_list)
           │    │         │         │    ├── columns: tr_t_id:45!null
           │    │         │         │    ├── mapping:
           │    │         │         │    │    └──  trade_request.tr_t_id:20 => tr_t_id:45
           │    │         │         │    └── key: (45)
           │    │         │         └── filters (true)
           │    │         └── projections
           │    │              └── trade_request.tr_tt_id:38 IN ('TLB', 'TLS', 'TSL') [as=partial_index_del1:50, outer=(38)]
           │    └── projections
           │         └── NULL [as="?column?":51]
           └── with &5 (insert_trade_history)
                ├── columns: tr_t_id:141!null tr_bid_price:142!null tr_tt_id:143!null tr_qty:144!null
                ├── volatile, mutations
                ├── key: (141)
                ├── fd: (141)-->(142-144)
                ├── project
                │    ├── columns: "?column?":87
                │    ├── volatile, mutations
                │    ├── fd: ()-->(87)
                │    ├── insert trade_history
                │    │    ├── columns: trade_history.th_t_id:52!null trade_history.th_st_id:54!null
                │    │    ├── insert-mapping:
                │    │    │    ├── tr_t_id:57 => trade_history.th_t_id:52
                │    │    │    ├── timestamp:62 => th_dts:53
                │    │    │    └── th_st_id_cast:63 => trade_history.th_st_id:54
                │    │    ├── return-mapping:
                │    │    │    ├── tr_t_id:57 => trade_history.th_t_id:52
                │    │    │    └── th_st_id_cast:63 => trade_history.th_st_id:54
                │    │    ├── input binding: &4
                │    │    ├── volatile, mutations
                │    │    ├── key: (52)
                │    │    ├── fd: ()-->(54)
                │    │    ├── project
                │    │    │    ├── columns: th_st_id_cast:63!null timestamp:62!null tr_t_id:57!null
                │    │    │    ├── key: (57)
                │    │    │    ├── fd: ()-->(62,63)
                │    │    │    ├── with-scan &2 (request_list)
                │    │    │    │    ├── columns: tr_t_id:57!null
                │    │    │    │    ├── mapping:
                │    │    │    │    │    └──  trade_request.tr_t_id:20 => tr_t_id:57
                │    │    │    │    └── key: (57)
                │    │    │    └── projections
                │    │    │         ├── 'SBMT' [as=th_st_id_cast:63]
                │    │    │         └── '2020-06-15 22:27:42.148484' [as=timestamp:62]
                │    │    └── f-k-checks
                │    │         ├── f-k-checks-item: trade_history(th_t_id) -> trade(t_id)
                │    │         │    └── anti-join (lookup trade)
                │    │         │         ├── columns: th_t_id:64!null
                │    │         │         ├── key columns: [64] = [65]
                │    │         │         ├── lookup columns are key
                │    │         │         ├── key: (64)
                │    │         │         ├── with-scan &4
                │    │         │         │    ├── columns: th_t_id:64!null
                │    │         │         │    ├── mapping:
                │    │         │         │    │    └──  tr_t_id:57 => th_t_id:64
                │    │         │         │    └── key: (64)
                │    │         │         └── filters (true)
                │    │         └── f-k-checks-item: trade_history(th_st_id) -> status_type(st_id)
                │    │              └── anti-join (lookup status_type)
                │    │                   ├── columns: th_st_id:82!null
                │    │                   ├── key columns: [82] = [83]
                │    │                   ├── lookup columns are key
                │    │                   ├── fd: ()-->(82)
                │    │                   ├── with-scan &4
                │    │                   │    ├── columns: th_st_id:82!null
                │    │                   │    ├── mapping:
                │    │                   │    │    └──  th_st_id_cast:63 => th_st_id:82
                │    │                   │    └── fd: ()-->(82)
                │    │                   └── filters (true)
                │    └── projections
                │         └── NULL [as="?column?":87]
                └── with &7 (update_trade_submitted)
                     ├── columns: tr_t_id:141!null tr_bid_price:142!null tr_tt_id:143!null tr_qty:144!null
                     ├── volatile, mutations
                     ├── key: (141)
                     ├── fd: (141)-->(142-144)
                     ├── project
                     │    ├── columns: "?column?":140
                     │    ├── volatile, mutations
                     │    ├── fd: ()-->(140)
                     │    ├── update trade
                     │    │    ├── columns: t_id:88!null
                     │    │    ├── fetch columns: t_id:105 t_dts:106 trade.t_st_id:107 t_tt_id:108 t_is_cash:109 t_s_symb:110 t_qty:111 t_bid_price:112 t_ca_id:113 t_exec_name:114 t_trade_price:115 t_chrg:116 t_comm:117 t_lifo:119
                     │    │    ├── update-mapping:
                     │    │    │    ├── t_dts_new:128 => t_dts:89
                     │    │    │    └── t_st_id_cast:129 => trade.t_st_id:90
                     │    │    ├── return-mapping:
                     │    │    │    └── t_id:105 => t_id:88
                     │    │    ├── input binding: &6
                     │    │    ├── volatile, mutations
                     │    │    ├── key: (88)
                     │    │    ├── project
                     │    │    │    ├── columns: t_st_id_cast:129!null t_dts_new:128!null t_id:105!null t_dts:106!null trade.t_st_id:107!null t_tt_id:108!null t_is_cash:109!null t_s_symb:110!null t_qty:111!null t_bid_price:112!null t_ca_id:113!null t_exec_name:114!null t_trade_price:115 t_chrg:116!null t_comm:117!null t_lifo:119!null
                     │    │    │    ├── key: (105)
                     │    │    │    ├── fd: ()-->(128,129), (105)-->(106-117,119)
                     │    │    │    ├── project
                     │    │    │    │    ├── columns: t_id:105!null t_dts:106!null trade.t_st_id:107!null t_tt_id:108!null t_is_cash:109!null t_s_symb:110!null t_qty:111!null t_bid_price:112!null t_ca_id:113!null t_exec_name:114!null t_trade_price:115 t_chrg:116!null t_comm:117!null t_lifo:119!null
                     │    │    │    │    ├── key: (105)
                     │    │    │    │    ├── fd: (105)-->(106-117,119)
                     │    │    │    │    └── inner-join (lookup trade)
                     │    │    │    │         ├── columns: t_id:105!null t_dts:106!null trade.t_st_id:107!null t_tt_id:108!null t_is_cash:109!null t_s_symb:110!null t_qty:111!null t_bid_price:112!null t_ca_id:113!null t_exec_name:114!null t_trade_price:115 t_chrg:116!null t_comm:117!null t_lifo:119!null tr_t_id:122!null
                     │    │    │    │         ├── key columns: [122] = [105]
                     │    │    │    │         ├── lookup columns are key
                     │    │    │    │         ├── key: (122)
                     │    │    │    │         ├── fd: (105)-->(106-117,119), (105)==(122), (122)==(105)
                     │    │    │    │         ├── with-scan &2 (request_list)
                     │    │    │    │         │    ├── columns: tr_t_id:122!null
                     │    │    │    │         │    ├── mapping:
                     │    │    │    │         │    │    └──  trade_request.tr_t_id:20 => tr_t_id:122
                     │    │    │    │         │    └── key: (122)
                     │    │    │    │         └── filters (true)
                     │    │    │    └── projections
                     │    │    │         ├── 'SBMT' [as=t_st_id_cast:129]
                     │    │    │         └── '2020-06-15 22:27:42.148484' [as=t_dts_new:128]
                     │    │    └── f-k-checks
                     │    │         └── f-k-checks-item: trade(t_st_id) -> status_type(st_id)
                     │    │              └── anti-join (lookup status_type)
                     │    │                   ├── columns: t_st_id:135!null
                     │    │                   ├── key columns: [135] = [136]
                     │    │                   ├── lookup columns are key
                     │    │                   ├── fd: ()-->(135)
                     │    │                   ├── with-scan &6
                     │    │                   │    ├── columns: t_st_id:135!null
                     │    │                   │    ├── mapping:
                     │    │                   │    │    └──  t_st_id_cast:129 => t_st_id:135
                     │    │                   │    └── fd: ()-->(135)
                     │    │                   └── filters (true)
                     │    └── projections
                     │         └── NULL [as="?column?":140]
                     └── with-scan &2 (request_list)
                          ├── columns: tr_t_id:141!null tr_bid_price:142!null tr_tt_id:143!null tr_qty:144!null
                          ├── mapping:
                          │    ├──  trade_request.tr_t_id:20 => tr_t_id:141
                          │    ├──  tr_bid_price:28 => tr_bid_price:142
                          │    ├──  trade_request.tr_tt_id:21 => tr_tt_id:143
                          │    └──  trade_request.tr_qty:23 => tr_qty:144
                          ├── key: (141)
                          └── fd: (141)-->(142-144)

# --------------------------------------------------
# T4
# Market-Watch
# Emulates the process of monitoring the overall performance of the market by
# allowing a customer to track the current daily trend (up or down) of a
# collection of securities. The collection of securities being monitored may be
# based upon a customer’s current holdings, a customer’s watch list of
# prospective securities, or a particular industry.
# --------------------------------------------------

# Q1
opt
WITH watch_stock_list AS
(
  SELECT wi_s_symb AS symb
    FROM watch_item, watch_list
   WHERE wi_wl_id = 0 AND wl_c_id = 0
)
SELECT sum(s_num_out * dm_close)::FLOAT8 AS old_mkt_cap,
       sum(s_num_out * lt_price)::FLOAT8 AS new_mkt_cap
FROM watch_stock_list, daily_market, last_trade, security
WHERE dm_s_symb = symb
  AND lt_s_symb = symb
  AND s_symb = symb
  AND dm_date = '2020-06-17'::DATE;
----
project
 ├── columns: old_mkt_cap:47 new_mkt_cap:48
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(47,48)
 ├── scalar-group-by
 │    ├── columns: sum:44 sum:46
 │    ├── cardinality: [1 - 1]
 │    ├── immutable
 │    ├── key: ()
 │    ├── fd: ()-->(44,46)
 │    ├── project
 │    │    ├── columns: column43:43!null column45:45!null
 │    │    ├── immutable
 │    │    ├── inner-join (lookup last_trade)
 │    │    │    ├── columns: symb:9!null dm_date:10!null dm_s_symb:11!null dm_close:12!null lt_s_symb:18!null lt_price:20!null s_symb:25!null s_num_out:31!null
 │    │    │    ├── key columns: [25] = [18]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── fd: ()-->(10), (11)-->(12), (18)-->(20), (25)-->(31), (9)==(11,18,25), (11)==(9,18,25), (18)==(9,11,25), (25)==(9,11,18)
 │    │    │    ├── inner-join (lookup security)
 │    │    │    │    ├── columns: symb:9!null dm_date:10!null dm_s_symb:11!null dm_close:12!null s_symb:25!null s_num_out:31!null
 │    │    │    │    ├── key columns: [9] = [25]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── fd: ()-->(10), (11)-->(12), (25)-->(31), (9)==(11,25), (11)==(9,25), (25)==(9,11)
 │    │    │    │    ├── inner-join (lookup daily_market)
 │    │    │    │    │    ├── columns: symb:9!null dm_date:10!null dm_s_symb:11!null dm_close:12!null
 │    │    │    │    │    ├── key columns: [55 9] = [10 11]
 │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    ├── fd: ()-->(10), (11)-->(12), (9)==(11), (11)==(9)
 │    │    │    │    │    ├── project
 │    │    │    │    │    │    ├── columns: "lookup_join_const_col_@10":55!null symb:9!null
 │    │    │    │    │    │    ├── fd: ()-->(55)
 │    │    │    │    │    │    ├── inner-join (cross)
 │    │    │    │    │    │    │    ├── columns: wi_wl_id:1!null wi_s_symb:2!null wl_c_id:6!null
 │    │    │    │    │    │    │    ├── fd: ()-->(1,6)
 │    │    │    │    │    │    │    ├── scan watch_item
 │    │    │    │    │    │    │    │    ├── columns: wi_wl_id:1!null wi_s_symb:2!null
 │    │    │    │    │    │    │    │    ├── constraint: /1/2: [/0 - /0]
 │    │    │    │    │    │    │    │    ├── key: (2)
 │    │    │    │    │    │    │    │    └── fd: ()-->(1)
 │    │    │    │    │    │    │    ├── scan watch_list@watch_list_wl_c_id_idx
 │    │    │    │    │    │    │    │    ├── columns: wl_c_id:6!null
 │    │    │    │    │    │    │    │    ├── constraint: /6/5: [/0 - /0]
 │    │    │    │    │    │    │    │    └── fd: ()-->(6)
 │    │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    │    └── projections
 │    │    │    │    │    │         ├── '2020-06-17' [as="lookup_join_const_col_@10":55]
 │    │    │    │    │    │         └── wi_s_symb:2 [as=symb:9, outer=(2)]
 │    │    │    │    │    └── filters (true)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── projections
 │    │         ├── s_num_out:31 * dm_close:12 [as=column43:43, outer=(12,31), immutable]
 │    │         └── s_num_out:31 * lt_price:20 [as=column45:45, outer=(20,31), immutable]
 │    └── aggregations
 │         ├── sum [as=sum:44, outer=(43)]
 │         │    └── column43:43
 │         └── sum [as=sum:46, outer=(45)]
 │              └── column45:45
 └── projections
      ├── sum:44::FLOAT8 [as=old_mkt_cap:47, outer=(44), immutable]
      └── sum:46::FLOAT8 [as=new_mkt_cap:48, outer=(46), immutable]

# Q1 (With Hints)
opt
WITH watch_stock_list AS
(
  SELECT wi_s_symb AS symb
    FROM watch_item, watch_list
   WHERE wi_wl_id = 0 AND wl_c_id = 0
)
SELECT sum(s_num_out * dm_close)::FLOAT8 AS old_mkt_cap,
       sum(s_num_out * lt_price)::FLOAT8 AS new_mkt_cap
  FROM watch_stock_list
  INNER LOOKUP JOIN (SELECT dm_s_symb, dm_close FROM daily_market WHERE dm_date = '2020-06-17'::DATE) ON dm_s_symb = symb
  INNER LOOKUP JOIN (SELECT lt_s_symb, lt_price FROM last_trade)                                      ON lt_s_symb = symb
  INNER LOOKUP JOIN (SELECT s_symb, s_num_out   FROM security)                                        ON s_symb    = symb;
----
project
 ├── columns: old_mkt_cap:47 new_mkt_cap:48
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(47,48)
 ├── scalar-group-by
 │    ├── columns: sum:44 sum:46
 │    ├── cardinality: [1 - 1]
 │    ├── immutable
 │    ├── key: ()
 │    ├── fd: ()-->(44,46)
 │    ├── project
 │    │    ├── columns: column43:43!null column45:45!null
 │    │    ├── immutable
 │    │    ├── inner-join (lookup security)
 │    │    │    ├── columns: symb:9!null dm_date:10!null dm_s_symb:11!null dm_close:12!null lt_s_symb:18!null lt_price:20!null s_symb:25!null s_num_out:31!null
 │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    ├── key columns: [9] = [25]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── fd: ()-->(10), (11)-->(12), (18)-->(20), (25)-->(31), (9)==(11,18,25), (11)==(9,18,25), (18)==(9,11,25), (25)==(9,11,18)
 │    │    │    ├── inner-join (lookup last_trade)
 │    │    │    │    ├── columns: symb:9!null dm_date:10!null dm_s_symb:11!null dm_close:12!null lt_s_symb:18!null lt_price:20!null
 │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    ├── key columns: [9] = [18]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── fd: ()-->(10), (11)-->(12), (18)-->(20), (9)==(11,18), (11)==(9,18), (18)==(9,11)
 │    │    │    │    ├── inner-join (lookup daily_market)
 │    │    │    │    │    ├── columns: symb:9!null dm_date:10!null dm_s_symb:11!null dm_close:12!null
 │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    ├── key columns: [49 9] = [10 11]
 │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    ├── fd: ()-->(10), (11)-->(12), (9)==(11), (11)==(9)
 │    │    │    │    │    ├── project
 │    │    │    │    │    │    ├── columns: "lookup_join_const_col_@10":49!null symb:9!null
 │    │    │    │    │    │    ├── fd: ()-->(49)
 │    │    │    │    │    │    ├── inner-join (cross)
 │    │    │    │    │    │    │    ├── columns: wi_wl_id:1!null wi_s_symb:2!null wl_c_id:6!null
 │    │    │    │    │    │    │    ├── fd: ()-->(1,6)
 │    │    │    │    │    │    │    ├── scan watch_item
 │    │    │    │    │    │    │    │    ├── columns: wi_wl_id:1!null wi_s_symb:2!null
 │    │    │    │    │    │    │    │    ├── constraint: /1/2: [/0 - /0]
 │    │    │    │    │    │    │    │    ├── key: (2)
 │    │    │    │    │    │    │    │    └── fd: ()-->(1)
 │    │    │    │    │    │    │    ├── scan watch_list@watch_list_wl_c_id_idx
 │    │    │    │    │    │    │    │    ├── columns: wl_c_id:6!null
 │    │    │    │    │    │    │    │    ├── constraint: /6/5: [/0 - /0]
 │    │    │    │    │    │    │    │    └── fd: ()-->(6)
 │    │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    │    └── projections
 │    │    │    │    │    │         ├── '2020-06-17' [as="lookup_join_const_col_@10":49]
 │    │    │    │    │    │         └── wi_s_symb:2 [as=symb:9, outer=(2)]
 │    │    │    │    │    └── filters (true)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── projections
 │    │         ├── s_num_out:31 * dm_close:12 [as=column43:43, outer=(12,31), immutable]
 │    │         └── s_num_out:31 * lt_price:20 [as=column45:45, outer=(20,31), immutable]
 │    └── aggregations
 │         ├── sum [as=sum:44, outer=(43)]
 │         │    └── column43:43
 │         └── sum [as=sum:46, outer=(45)]
 │              └── column45:45
 └── projections
      ├── sum:44::FLOAT8 [as=old_mkt_cap:47, outer=(44), immutable]
      └── sum:46::FLOAT8 [as=new_mkt_cap:48, outer=(46), immutable]

# Q2
opt
WITH watch_stock_list AS
(
  SELECT
    s_symb AS symb
  FROM
    industry,
    company,
    security
  WHERE
    in_name = 'Software' AND
    co_in_id = in_id AND
    co_id >= 1 AND
    co_id <= 5000 AND
    s_co_id = co_id
)
SELECT sum(s_num_out * dm_close)::FLOAT8 AS old_mkt_cap,
       sum(s_num_out * lt_price)::FLOAT8 AS new_mkt_cap
FROM watch_stock_list, daily_market, last_trade, security
WHERE dm_s_symb = symb
  AND lt_s_symb = symb
  AND s_symb = symb
  AND dm_date = '2020-06-17'::DATE
----
project
 ├── columns: old_mkt_cap:73 new_mkt_cap:74
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(73,74)
 ├── scalar-group-by
 │    ├── columns: sum:70 sum:72
 │    ├── cardinality: [1 - 1]
 │    ├── immutable
 │    ├── key: ()
 │    ├── fd: ()-->(70,72)
 │    ├── project
 │    │    ├── columns: column69:69!null column71:71!null
 │    │    ├── immutable
 │    │    ├── inner-join (lookup last_trade)
 │    │    │    ├── columns: symb:35!null dm_date:36!null dm_s_symb:37!null dm_close:38!null lt_s_symb:44!null lt_price:46!null s_symb:51!null s_num_out:57!null
 │    │    │    ├── key columns: [51] = [44]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── key: (51)
 │    │    │    ├── fd: ()-->(36), (37)-->(38), (44)-->(46), (51)-->(57), (35)==(37,44,51), (37)==(35,44,51), (44)==(35,37,51), (51)==(35,37,44)
 │    │    │    ├── inner-join (lookup security)
 │    │    │    │    ├── columns: symb:35!null dm_date:36!null dm_s_symb:37!null dm_close:38!null s_symb:51!null s_num_out:57!null
 │    │    │    │    ├── key columns: [35] = [51]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── key: (51)
 │    │    │    │    ├── fd: ()-->(36), (37)-->(38), (51)-->(57), (35)==(37,51), (37)==(35,51), (51)==(35,37)
 │    │    │    │    ├── inner-join (lookup daily_market)
 │    │    │    │    │    ├── columns: symb:35!null dm_date:36!null dm_s_symb:37!null dm_close:38!null
 │    │    │    │    │    ├── key columns: [83 35] = [36 37]
 │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    ├── key: (37)
 │    │    │    │    │    ├── fd: ()-->(36), (37)-->(38), (35)==(37), (37)==(35)
 │    │    │    │    │    ├── project
 │    │    │    │    │    │    ├── columns: "lookup_join_const_col_@36":83!null symb:35!null
 │    │    │    │    │    │    ├── key: (35)
 │    │    │    │    │    │    ├── fd: ()-->(83)
 │    │    │    │    │    │    ├── inner-join (lookup security@security_s_co_id_s_issue_key)
 │    │    │    │    │    │    │    ├── columns: in_id:1!null in_name:2!null co_id:6!null co_in_id:9!null s_symb:17!null s_co_id:22!null
 │    │    │    │    │    │    │    ├── key columns: [6] = [22]
 │    │    │    │    │    │    │    ├── key: (17)
 │    │    │    │    │    │    │    ├── fd: ()-->(1,2,9), (17)-->(22), (6)==(22), (22)==(6), (1)==(9), (9)==(1)
 │    │    │    │    │    │    │    ├── inner-join (lookup company@company_co_in_id_idx)
 │    │    │    │    │    │    │    │    ├── columns: in_id:1!null in_name:2!null co_id:6!null co_in_id:9!null
 │    │    │    │    │    │    │    │    ├── lookup expression
 │    │    │    │    │    │    │    │    │    └── filters
 │    │    │    │    │    │    │    │    │         ├── (co_id:6 >= 1) AND (co_id:6 <= 5000) [outer=(6), constraints=(/6: [/1 - /5000]; tight)]
 │    │    │    │    │    │    │    │    │         └── in_id:1 = co_in_id:9 [outer=(1,9), constraints=(/1: (/NULL - ]; /9: (/NULL - ]), fd=(1)==(9), (9)==(1)]
 │    │    │    │    │    │    │    │    ├── cardinality: [0 - 5000]
 │    │    │    │    │    │    │    │    ├── key: (6)
 │    │    │    │    │    │    │    │    ├── fd: ()-->(1,2,9), (1)==(9), (9)==(1)
 │    │    │    │    │    │    │    │    ├── scan industry@industry_in_name_key
 │    │    │    │    │    │    │    │    │    ├── columns: in_id:1!null in_name:2!null
 │    │    │    │    │    │    │    │    │    ├── constraint: /2: [/'Software' - /'Software']
 │    │    │    │    │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    │    │    │    │    ├── key: ()
 │    │    │    │    │    │    │    │    │    └── fd: ()-->(1,2)
 │    │    │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    │    │    └── filters
 │    │    │    │    │    │    │         └── (s_co_id:22 >= 1) AND (s_co_id:22 <= 5000) [outer=(22), constraints=(/22: [/1 - /5000]; tight)]
 │    │    │    │    │    │    └── projections
 │    │    │    │    │    │         ├── '2020-06-17' [as="lookup_join_const_col_@36":83]
 │    │    │    │    │    │         └── s_symb:17 [as=symb:35, outer=(17)]
 │    │    │    │    │    └── filters (true)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── projections
 │    │         ├── s_num_out:57 * dm_close:38 [as=column69:69, outer=(38,57), immutable]
 │    │         └── s_num_out:57 * lt_price:46 [as=column71:71, outer=(46,57), immutable]
 │    └── aggregations
 │         ├── sum [as=sum:70, outer=(69)]
 │         │    └── column69:69
 │         └── sum [as=sum:72, outer=(71)]
 │              └── column71:71
 └── projections
      ├── sum:70::FLOAT8 [as=old_mkt_cap:73, outer=(70), immutable]
      └── sum:72::FLOAT8 [as=new_mkt_cap:74, outer=(72), immutable]

# Q2 (With Hints)
opt
WITH watch_stock_list AS
(
             SELECT s_symb AS symb
               FROM industry
  INNER LOOKUP JOIN company  ON co_in_id = in_id
  INNER LOOKUP JOIN security ON s_co_id  = co_id
              WHERE in_name = 'Software'
              AND co_id BETWEEN 1 AND 5000
)
SELECT sum(s_num_out * dm_close)::FLOAT8 AS old_mkt_cap,
       sum(s_num_out * lt_price)::FLOAT8 AS new_mkt_cap
  FROM watch_stock_list
  INNER LOOKUP JOIN (SELECT dm_s_symb, dm_close FROM daily_market WHERE dm_date = '2020-06-17'::DATE) ON dm_s_symb = symb
  INNER LOOKUP JOIN (SELECT lt_s_symb, lt_price FROM last_trade)                                      ON lt_s_symb = symb
  INNER LOOKUP JOIN (SELECT s_symb, s_num_out   FROM security)                                        ON s_symb    = symb;
----
project
 ├── columns: old_mkt_cap:73 new_mkt_cap:74
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(73,74)
 ├── scalar-group-by
 │    ├── columns: sum:70 sum:72
 │    ├── cardinality: [1 - 1]
 │    ├── immutable
 │    ├── key: ()
 │    ├── fd: ()-->(70,72)
 │    ├── project
 │    │    ├── columns: column69:69!null column71:71!null
 │    │    ├── immutable
 │    │    ├── inner-join (lookup security)
 │    │    │    ├── columns: symb:35!null dm_date:36!null dm_s_symb:37!null dm_close:38!null lt_s_symb:44!null lt_price:46!null s_symb:51!null s_num_out:57!null
 │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    ├── key columns: [35] = [51]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── key: (51)
 │    │    │    ├── fd: ()-->(36), (37)-->(38), (44)-->(46), (51)-->(57), (35)==(37,44,51), (37)==(35,44,51), (44)==(35,37,51), (51)==(35,37,44)
 │    │    │    ├── inner-join (lookup last_trade)
 │    │    │    │    ├── columns: symb:35!null dm_date:36!null dm_s_symb:37!null dm_close:38!null lt_s_symb:44!null lt_price:46!null
 │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    ├── key columns: [35] = [44]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── key: (44)
 │    │    │    │    ├── fd: ()-->(36), (37)-->(38), (44)-->(46), (35)==(37,44), (37)==(35,44), (44)==(35,37)
 │    │    │    │    ├── inner-join (lookup daily_market)
 │    │    │    │    │    ├── columns: symb:35!null dm_date:36!null dm_s_symb:37!null dm_close:38!null
 │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    ├── key columns: [75 35] = [36 37]
 │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    ├── key: (37)
 │    │    │    │    │    ├── fd: ()-->(36), (37)-->(38), (35)==(37), (37)==(35)
 │    │    │    │    │    ├── project
 │    │    │    │    │    │    ├── columns: "lookup_join_const_col_@36":75!null symb:35!null
 │    │    │    │    │    │    ├── key: (35)
 │    │    │    │    │    │    ├── fd: ()-->(75)
 │    │    │    │    │    │    ├── inner-join (lookup security@security_s_co_id_s_issue_key)
 │    │    │    │    │    │    │    ├── columns: in_id:1!null in_name:2!null co_id:6!null co_in_id:9!null s_symb:17!null s_co_id:22!null
 │    │    │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    │    │    ├── key columns: [6] = [22]
 │    │    │    │    │    │    │    ├── key: (17)
 │    │    │    │    │    │    │    ├── fd: ()-->(1,2,9), (17)-->(22), (1)==(9), (9)==(1), (6)==(22), (22)==(6)
 │    │    │    │    │    │    │    ├── inner-join (lookup company@company_co_in_id_idx)
 │    │    │    │    │    │    │    │    ├── columns: in_id:1!null in_name:2!null co_id:6!null co_in_id:9!null
 │    │    │    │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    │    │    │    ├── lookup expression
 │    │    │    │    │    │    │    │    │    └── filters
 │    │    │    │    │    │    │    │    │         ├── (co_id:6 >= 1) AND (co_id:6 <= 5000) [outer=(6), constraints=(/6: [/1 - /5000]; tight)]
 │    │    │    │    │    │    │    │    │         └── in_id:1 = co_in_id:9 [outer=(1,9), constraints=(/1: (/NULL - ]; /9: (/NULL - ]), fd=(1)==(9), (9)==(1)]
 │    │    │    │    │    │    │    │    ├── cardinality: [0 - 5000]
 │    │    │    │    │    │    │    │    ├── key: (6)
 │    │    │    │    │    │    │    │    ├── fd: ()-->(1,2,9), (1)==(9), (9)==(1)
 │    │    │    │    │    │    │    │    ├── scan industry@industry_in_name_key
 │    │    │    │    │    │    │    │    │    ├── columns: in_id:1!null in_name:2!null
 │    │    │    │    │    │    │    │    │    ├── constraint: /2: [/'Software' - /'Software']
 │    │    │    │    │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    │    │    │    │    ├── key: ()
 │    │    │    │    │    │    │    │    │    └── fd: ()-->(1,2)
 │    │    │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    │    │    └── filters
 │    │    │    │    │    │    │         └── (s_co_id:22 >= 1) AND (s_co_id:22 <= 5000) [outer=(22), constraints=(/22: [/1 - /5000]; tight)]
 │    │    │    │    │    │    └── projections
 │    │    │    │    │    │         ├── '2020-06-17' [as="lookup_join_const_col_@36":75]
 │    │    │    │    │    │         └── s_symb:17 [as=symb:35, outer=(17)]
 │    │    │    │    │    └── filters (true)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── projections
 │    │         ├── s_num_out:57 * dm_close:38 [as=column69:69, outer=(38,57), immutable]
 │    │         └── s_num_out:57 * lt_price:46 [as=column71:71, outer=(46,57), immutable]
 │    └── aggregations
 │         ├── sum [as=sum:70, outer=(69)]
 │         │    └── column69:69
 │         └── sum [as=sum:72, outer=(71)]
 │              └── column71:71
 └── projections
      ├── sum:70::FLOAT8 [as=old_mkt_cap:73, outer=(70), immutable]
      └── sum:72::FLOAT8 [as=new_mkt_cap:74, outer=(72), immutable]

# Q3
opt
WITH watch_stock_list AS
(
  SELECT hs_s_symb AS symb
  FROM holding_summary
  WHERE hs_ca_id = 0
)
SELECT sum(s_num_out * dm_close)::FLOAT8 AS old_mkt_cap,
       sum(s_num_out * lt_price)::FLOAT8 AS new_mkt_cap
FROM watch_stock_list, daily_market, last_trade, security
WHERE dm_s_symb = symb
  AND lt_s_symb = symb
  AND s_symb = symb
  AND dm_date = '2020-06-17'::DATE
----
project
 ├── columns: old_mkt_cap:44 new_mkt_cap:45
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(44,45)
 ├── scalar-group-by
 │    ├── columns: sum:41 sum:43
 │    ├── cardinality: [1 - 1]
 │    ├── immutable
 │    ├── key: ()
 │    ├── fd: ()-->(41,43)
 │    ├── project
 │    │    ├── columns: column40:40!null column42:42!null
 │    │    ├── immutable
 │    │    ├── inner-join (lookup last_trade)
 │    │    │    ├── columns: symb:6!null dm_date:7!null dm_s_symb:8!null dm_close:9!null lt_s_symb:15!null lt_price:17!null s_symb:22!null s_num_out:28!null
 │    │    │    ├── key columns: [22] = [15]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── key: (22)
 │    │    │    ├── fd: ()-->(7), (8)-->(9), (15)-->(17), (22)-->(28), (6)==(8,15,22), (8)==(6,15,22), (15)==(6,8,22), (22)==(6,8,15)
 │    │    │    ├── inner-join (lookup security)
 │    │    │    │    ├── columns: symb:6!null dm_date:7!null dm_s_symb:8!null dm_close:9!null s_symb:22!null s_num_out:28!null
 │    │    │    │    ├── key columns: [6] = [22]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── key: (22)
 │    │    │    │    ├── fd: ()-->(7), (8)-->(9), (22)-->(28), (6)==(8,22), (8)==(6,22), (22)==(6,8)
 │    │    │    │    ├── inner-join (lookup daily_market)
 │    │    │    │    │    ├── columns: symb:6!null dm_date:7!null dm_s_symb:8!null dm_close:9!null
 │    │    │    │    │    ├── key columns: [52 6] = [7 8]
 │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    ├── key: (8)
 │    │    │    │    │    ├── fd: ()-->(7), (8)-->(9), (6)==(8), (8)==(6)
 │    │    │    │    │    ├── project
 │    │    │    │    │    │    ├── columns: "lookup_join_const_col_@7":52!null symb:6!null
 │    │    │    │    │    │    ├── key: (6)
 │    │    │    │    │    │    ├── fd: ()-->(52)
 │    │    │    │    │    │    ├── scan holding_summary
 │    │    │    │    │    │    │    ├── columns: hs_ca_id:1!null hs_s_symb:2!null
 │    │    │    │    │    │    │    ├── constraint: /1/2: [/0 - /0]
 │    │    │    │    │    │    │    ├── key: (2)
 │    │    │    │    │    │    │    └── fd: ()-->(1)
 │    │    │    │    │    │    └── projections
 │    │    │    │    │    │         ├── '2020-06-17' [as="lookup_join_const_col_@7":52]
 │    │    │    │    │    │         └── hs_s_symb:2 [as=symb:6, outer=(2)]
 │    │    │    │    │    └── filters (true)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── projections
 │    │         ├── s_num_out:28 * dm_close:9 [as=column40:40, outer=(9,28), immutable]
 │    │         └── s_num_out:28 * lt_price:17 [as=column42:42, outer=(17,28), immutable]
 │    └── aggregations
 │         ├── sum [as=sum:41, outer=(40)]
 │         │    └── column40:40
 │         └── sum [as=sum:43, outer=(42)]
 │              └── column42:42
 └── projections
      ├── sum:41::FLOAT8 [as=old_mkt_cap:44, outer=(41), immutable]
      └── sum:43::FLOAT8 [as=new_mkt_cap:45, outer=(43), immutable]

# Q3 (With Hints)
opt
WITH watch_stock_list AS
(
  SELECT hs_s_symb AS symb
  FROM holding_summary
  WHERE hs_ca_id = 0
)
SELECT sum(s_num_out * dm_close)::FLOAT8 AS old_mkt_cap,
       sum(s_num_out * lt_price)::FLOAT8 AS new_mkt_cap
  FROM watch_stock_list
  INNER LOOKUP JOIN (SELECT dm_s_symb, dm_close FROM daily_market WHERE dm_date = '2020-06-17'::DATE) ON dm_s_symb = symb
  INNER LOOKUP JOIN (SELECT lt_s_symb, lt_price FROM last_trade)                                      ON lt_s_symb = symb
  INNER LOOKUP JOIN (SELECT s_symb, s_num_out   FROM security)                                        ON s_symb    = symb;
----
project
 ├── columns: old_mkt_cap:44 new_mkt_cap:45
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(44,45)
 ├── scalar-group-by
 │    ├── columns: sum:41 sum:43
 │    ├── cardinality: [1 - 1]
 │    ├── immutable
 │    ├── key: ()
 │    ├── fd: ()-->(41,43)
 │    ├── project
 │    │    ├── columns: column40:40!null column42:42!null
 │    │    ├── immutable
 │    │    ├── inner-join (lookup security)
 │    │    │    ├── columns: symb:6!null dm_date:7!null dm_s_symb:8!null dm_close:9!null lt_s_symb:15!null lt_price:17!null s_symb:22!null s_num_out:28!null
 │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    ├── key columns: [6] = [22]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── key: (22)
 │    │    │    ├── fd: ()-->(7), (8)-->(9), (15)-->(17), (22)-->(28), (6)==(8,15,22), (8)==(6,15,22), (15)==(6,8,22), (22)==(6,8,15)
 │    │    │    ├── inner-join (lookup last_trade)
 │    │    │    │    ├── columns: symb:6!null dm_date:7!null dm_s_symb:8!null dm_close:9!null lt_s_symb:15!null lt_price:17!null
 │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    ├── key columns: [6] = [15]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── key: (15)
 │    │    │    │    ├── fd: ()-->(7), (8)-->(9), (15)-->(17), (6)==(8,15), (8)==(6,15), (15)==(6,8)
 │    │    │    │    ├── inner-join (lookup daily_market)
 │    │    │    │    │    ├── columns: symb:6!null dm_date:7!null dm_s_symb:8!null dm_close:9!null
 │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    ├── key columns: [46 6] = [7 8]
 │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    ├── key: (8)
 │    │    │    │    │    ├── fd: ()-->(7), (8)-->(9), (6)==(8), (8)==(6)
 │    │    │    │    │    ├── project
 │    │    │    │    │    │    ├── columns: "lookup_join_const_col_@7":46!null symb:6!null
 │    │    │    │    │    │    ├── key: (6)
 │    │    │    │    │    │    ├── fd: ()-->(46)
 │    │    │    │    │    │    ├── scan holding_summary
 │    │    │    │    │    │    │    ├── columns: hs_ca_id:1!null hs_s_symb:2!null
 │    │    │    │    │    │    │    ├── constraint: /1/2: [/0 - /0]
 │    │    │    │    │    │    │    ├── key: (2)
 │    │    │    │    │    │    │    └── fd: ()-->(1)
 │    │    │    │    │    │    └── projections
 │    │    │    │    │    │         ├── '2020-06-17' [as="lookup_join_const_col_@7":46]
 │    │    │    │    │    │         └── hs_s_symb:2 [as=symb:6, outer=(2)]
 │    │    │    │    │    └── filters (true)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── projections
 │    │         ├── s_num_out:28 * dm_close:9 [as=column40:40, outer=(9,28), immutable]
 │    │         └── s_num_out:28 * lt_price:17 [as=column42:42, outer=(17,28), immutable]
 │    └── aggregations
 │         ├── sum [as=sum:41, outer=(40)]
 │         │    └── column40:40
 │         └── sum [as=sum:43, outer=(42)]
 │              └── column42:42
 └── projections
      ├── sum:41::FLOAT8 [as=old_mkt_cap:44, outer=(41), immutable]
      └── sum:43::FLOAT8 [as=new_mkt_cap:45, outer=(43), immutable]

# --------------------------------------------------
# T5
# Security-Detail
# Emulates the process of accessing detailed information on a particular
# security. This is representative of a customer doing research on a security
# prior to making a decision about whether or not to execute a trade.
# --------------------------------------------------

# Q1
opt
SELECT s_name,
       co_id,
       co_name,
       co_sp_rate,
       co_ceo,
       co_desc,
       co_open_date,
       co_st_id,
       ca.ad_line1,
       ca.ad_line2,
       zca.zc_town,
       zca.zc_div,
       ca.ad_zc_code,
       ca.ad_ctry,
       s_num_out,
       s_start_date,
       s_exch_date,
       s_pe::FLOAT8,
       s_52wk_high::FLOAT8,
       s_52wk_high_date,
       s_52wk_low::FLOAT8,
       s_52wk_low_date,
       s_dividend::FLOAT8,
       s_yield::FLOAT8,
       zea.zc_div,
       ea.ad_ctry,
       ea.ad_line1,
       ea.ad_line2,
       zea.zc_town,
       ea.ad_zc_code,
       ex_close,
       ex_desc,
       ex_name,
       ex_num_symb,
       ex_open,
       lt_price::FLOAT8,
       lt_open_price::FLOAT8,
       lt_vol
  FROM security,
       company,
       address AS ca,
       zip_code AS zca,
       exchange,
       address AS ea,
       zip_code AS zea,
       last_trade
 WHERE s_symb = 'SYMB' AND
       co_id = s_co_id AND
       ca.ad_id = co_ad_id AND
       ea.ad_id = ex_ad_id AND
       ex_id = s_ex_id AND
       ca.ad_zc_code = zca.zc_code AND
       ea.ad_zc_code = zea.zc_code AND
       lt_s_symb = s_symb
----
project
 ├── columns: s_name:4!null co_id:19!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_desc:26!null co_open_date:27!null co_st_id:20!null ad_line1:31 ad_line2:32 zc_town:38!null zc_div:39!null ad_zc_code:33!null ad_ctry:34 s_num_out:7!null s_start_date:8!null s_exch_date:9!null s_pe:70!null s_52wk_high:71!null s_52wk_high_date:12!null s_52wk_low:72!null s_52wk_low_date:14!null s_dividend:73!null s_yield:74!null zc_div:60!null ad_ctry:55 ad_line1:52 ad_line2:53 zc_town:59!null ad_zc_code:54!null ex_close:46!null ex_desc:47 ex_name:43!null ex_num_symb:44!null ex_open:45!null lt_price:75!null lt_open_price:76!null lt_vol:67!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(4,7-9,12,14,19-21,23,24,26,27,31-34,38,39,43-47,52-55,59,60,67,70-76)
 ├── inner-join (cross)
 │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34 zca.zc_code:37!null zca.zc_town:38!null zca.zc_div:39!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55 zea.zc_code:58!null zea.zc_town:59!null zea.zc_div:60!null lt_s_symb:63!null last_trade.lt_price:65!null last_trade.lt_open_price:66!null lt_vol:67!null
 │    ├── cardinality: [0 - 1]
 │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-one)
 │    ├── key: ()
 │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34,37-39,42-48,51-55,58-60,63,65-67), (33)==(37), (37)==(33), (25)==(30), (30)==(25), (6)==(19), (19)==(6), (54)==(58), (58)==(54), (48)==(51), (51)==(48), (5)==(42), (42)==(5)
 │    ├── inner-join (lookup zip_code [as=zca])
 │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34 zca.zc_code:37!null zca.zc_town:38!null zca.zc_div:39!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55 zea.zc_code:58!null zea.zc_town:59!null zea.zc_div:60!null
 │    │    ├── key columns: [33] = [37]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34,37-39,42-48,51-55,58-60), (33)==(37), (37)==(33), (25)==(30), (30)==(25), (6)==(19), (19)==(6), (54)==(58), (58)==(54), (48)==(51), (51)==(48), (5)==(42), (42)==(5)
 │    │    ├── inner-join (lookup address [as=ca])
 │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34 ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55 zea.zc_code:58!null zea.zc_town:59!null zea.zc_div:60!null
 │    │    │    ├── key columns: [25] = [30]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34,42-48,51-55,58-60), (54)==(58), (58)==(54), (48)==(51), (51)==(48), (5)==(42), (42)==(5), (6)==(19), (19)==(6), (25)==(30), (30)==(25)
 │    │    │    ├── inner-join (lookup company)
 │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55 zea.zc_code:58!null zea.zc_town:59!null zea.zc_div:60!null
 │    │    │    │    ├── key columns: [6] = [19]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    ├── fd: ()-->(1,4-16,19-21,23-27,42-48,51-55,58-60), (54)==(58), (58)==(54), (48)==(51), (51)==(48), (5)==(42), (42)==(5), (6)==(19), (19)==(6)
 │    │    │    │    ├── inner-join (lookup zip_code [as=zea])
 │    │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55 zea.zc_code:58!null zea.zc_town:59!null zea.zc_div:60!null
 │    │    │    │    │    ├── key columns: [54] = [58]
 │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    ├── key: ()
 │    │    │    │    │    ├── fd: ()-->(1,4-16,42-48,51-55,58-60), (54)==(58), (58)==(54), (48)==(51), (51)==(48), (5)==(42), (42)==(5)
 │    │    │    │    │    ├── inner-join (lookup address [as=ea])
 │    │    │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55
 │    │    │    │    │    │    ├── key columns: [48] = [51]
 │    │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    │    ├── key: ()
 │    │    │    │    │    │    ├── fd: ()-->(1,4-16,42-48,51-55), (48)==(51), (51)==(48), (5)==(42), (42)==(5)
 │    │    │    │    │    │    ├── inner-join (lookup exchange)
 │    │    │    │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null
 │    │    │    │    │    │    │    ├── key columns: [5] = [42]
 │    │    │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    │    │    ├── key: ()
 │    │    │    │    │    │    │    ├── fd: ()-->(1,4-16,42-48), (5)==(42), (42)==(5)
 │    │    │    │    │    │    │    ├── scan security
 │    │    │    │    │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null
 │    │    │    │    │    │    │    │    ├── constraint: /1: [/'SYMB' - /'SYMB']
 │    │    │    │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    │    │    │    ├── key: ()
 │    │    │    │    │    │    │    │    └── fd: ()-->(1,4-16)
 │    │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    └── filters (true)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── filters (true)
 │    ├── scan last_trade
 │    │    ├── columns: lt_s_symb:63!null last_trade.lt_price:65!null last_trade.lt_open_price:66!null lt_vol:67!null
 │    │    ├── constraint: /63: [/'SYMB' - /'SYMB']
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    └── fd: ()-->(63,65-67)
 │    └── filters (true)
 └── projections
      ├── security.s_pe:10::FLOAT8 [as=s_pe:70, outer=(10), immutable]
      ├── security.s_52wk_high:11::FLOAT8 [as=s_52wk_high:71, outer=(11), immutable]
      ├── security.s_52wk_low:13::FLOAT8 [as=s_52wk_low:72, outer=(13), immutable]
      ├── security.s_dividend:15::FLOAT8 [as=s_dividend:73, outer=(15), immutable]
      ├── security.s_yield:16::FLOAT8 [as=s_yield:74, outer=(16), immutable]
      ├── last_trade.lt_price:65::FLOAT8 [as=lt_price:75, outer=(65), immutable]
      └── last_trade.lt_open_price:66::FLOAT8 [as=lt_open_price:76, outer=(66), immutable]

# Q1 (With Hints)
opt
           SELECT s_name,
                  co_id,
                  co_name,
                  co_sp_rate,
                  co_ceo,
                  co_desc,
                  co_open_date,
                  co_st_id,
                  ca.ad_line1,
                  ca.ad_line2,
                  zca.zc_town,
                  zca.zc_div,
                  ca.ad_zc_code,
                  ca.ad_ctry,
                  s_num_out,
                  s_start_date,
                  s_exch_date,
                  s_pe::FLOAT8,
                  s_52wk_high::FLOAT8,
                  s_52wk_high_date,
                  s_52wk_low::FLOAT8,
                  s_52wk_low_date,
                  s_dividend::FLOAT8,
                  s_yield::FLOAT8,
                  zea.zc_div,
                  ea.ad_ctry,
                  ea.ad_line1,
                  ea.ad_line2,
                  zea.zc_town,
                  ea.ad_zc_code,
                  ex_close,
                  ex_desc,
                  ex_name,
                  ex_num_symb,
                  ex_open,
                  lt_price::FLOAT8,
                  lt_open_price::FLOAT8,
                  lt_vol
             FROM security
INNER LOOKUP JOIN company         ON co_id = s_co_id
INNER LOOKUP JOIN address  AS ca  ON ca.ad_id = co_ad_id AND ca.ad_id = co_ad_id
INNER LOOKUP JOIN zip_code AS zca ON ca.ad_zc_code = zca.zc_code
INNER LOOKUP JOIN exchange        ON ex_id = s_ex_id
INNER LOOKUP JOIN address  AS ea  ON ea.ad_id = ex_ad_id
INNER LOOKUP JOIN zip_code AS zea ON ea.ad_zc_code = zea.zc_code
INNER LOOKUP JOIN last_trade      ON lt_s_symb = s_symb
            WHERE s_symb = 'SYMB';
----
project
 ├── columns: s_name:4!null co_id:19!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_desc:26!null co_open_date:27!null co_st_id:20!null ad_line1:31 ad_line2:32 zc_town:38!null zc_div:39!null ad_zc_code:33!null ad_ctry:34 s_num_out:7!null s_start_date:8!null s_exch_date:9!null s_pe:70!null s_52wk_high:71!null s_52wk_high_date:12!null s_52wk_low:72!null s_52wk_low_date:14!null s_dividend:73!null s_yield:74!null zc_div:60!null ad_ctry:55 ad_line1:52 ad_line2:53 zc_town:59!null ad_zc_code:54!null ex_close:46!null ex_desc:47 ex_name:43!null ex_num_symb:44!null ex_open:45!null lt_price:75!null lt_open_price:76!null lt_vol:67!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(4,7-9,12,14,19-21,23,24,26,27,31-34,38,39,43-47,52-55,59,60,67,70-76)
 ├── inner-join (lookup last_trade)
 │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34 zca.zc_code:37!null zca.zc_town:38!null zca.zc_div:39!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55 zea.zc_code:58!null zea.zc_town:59!null zea.zc_div:60!null lt_s_symb:63!null last_trade.lt_price:65!null last_trade.lt_open_price:66!null lt_vol:67!null
 │    ├── flags: force lookup join (into right side)
 │    ├── key columns: [1] = [63]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34,37-39,42-48,51-55,58-60,63,65-67), (6)==(19), (19)==(6), (25)==(30), (30)==(25), (33)==(37), (37)==(33), (5)==(42), (42)==(5), (48)==(51), (51)==(48), (54)==(58), (58)==(54), (1)==(63), (63)==(1)
 │    ├── inner-join (lookup zip_code [as=zea])
 │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34 zca.zc_code:37!null zca.zc_town:38!null zca.zc_div:39!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55 zea.zc_code:58!null zea.zc_town:59!null zea.zc_div:60!null
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [54] = [58]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34,37-39,42-48,51-55,58-60), (6)==(19), (19)==(6), (25)==(30), (30)==(25), (33)==(37), (37)==(33), (5)==(42), (42)==(5), (48)==(51), (51)==(48), (54)==(58), (58)==(54)
 │    │    ├── inner-join (lookup address [as=ea])
 │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34 zca.zc_code:37!null zca.zc_town:38!null zca.zc_div:39!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null ea.ad_id:51!null ea.ad_line1:52 ea.ad_line2:53 ea.ad_zc_code:54!null ea.ad_ctry:55
 │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    ├── key columns: [48] = [51]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34,37-39,42-48,51-55), (6)==(19), (19)==(6), (25)==(30), (30)==(25), (33)==(37), (37)==(33), (5)==(42), (42)==(5), (48)==(51), (51)==(48)
 │    │    │    ├── inner-join (lookup exchange)
 │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34 zca.zc_code:37!null zca.zc_town:38!null zca.zc_div:39!null ex_id:42!null ex_name:43!null ex_num_symb:44!null ex_open:45!null ex_close:46!null ex_desc:47 ex_ad_id:48!null
 │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    ├── key columns: [5] = [42]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34,37-39,42-48), (6)==(19), (19)==(6), (25)==(30), (30)==(25), (33)==(37), (37)==(33), (5)==(42), (42)==(5)
 │    │    │    │    ├── inner-join (lookup zip_code [as=zca])
 │    │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34 zca.zc_code:37!null zca.zc_town:38!null zca.zc_div:39!null
 │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    ├── key columns: [33] = [37]
 │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    ├── key: ()
 │    │    │    │    │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34,37-39), (6)==(19), (19)==(6), (25)==(30), (30)==(25), (33)==(37), (37)==(33)
 │    │    │    │    │    ├── inner-join (lookup address [as=ca])
 │    │    │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null ca.ad_id:30!null ca.ad_line1:31 ca.ad_line2:32 ca.ad_zc_code:33!null ca.ad_ctry:34
 │    │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    │    ├── key columns: [25] = [30]
 │    │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    │    ├── key: ()
 │    │    │    │    │    │    ├── fd: ()-->(1,4-16,19-21,23-27,30-34), (6)==(19), (19)==(6), (25)==(30), (30)==(25)
 │    │    │    │    │    │    ├── inner-join (lookup company)
 │    │    │    │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null co_id:19!null co_st_id:20!null co_name:21!null co_sp_rate:23!null co_ceo:24!null co_ad_id:25!null co_desc:26!null co_open_date:27!null
 │    │    │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    │    │    ├── key columns: [6] = [19]
 │    │    │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    │    │    ├── key: ()
 │    │    │    │    │    │    │    ├── fd: ()-->(1,4-16,19-21,23-27), (6)==(19), (19)==(6)
 │    │    │    │    │    │    │    ├── scan security
 │    │    │    │    │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null s_num_out:7!null s_start_date:8!null s_exch_date:9!null security.s_pe:10!null security.s_52wk_high:11!null s_52wk_high_date:12!null security.s_52wk_low:13!null s_52wk_low_date:14!null security.s_dividend:15!null security.s_yield:16!null
 │    │    │    │    │    │    │    │    ├── constraint: /1: [/'SYMB' - /'SYMB']
 │    │    │    │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    │    │    │    ├── key: ()
 │    │    │    │    │    │    │    │    └── fd: ()-->(1,4-16)
 │    │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    └── filters (true)
 │    │    │    │    └── filters (true)
 │    │    │    └── filters (true)
 │    │    └── filters (true)
 │    └── filters
 │         └── lt_s_symb:63 = 'SYMB' [outer=(63), constraints=(/63: [/'SYMB' - /'SYMB']; tight), fd=()-->(63)]
 └── projections
      ├── security.s_pe:10::FLOAT8 [as=s_pe:70, outer=(10), immutable]
      ├── security.s_52wk_high:11::FLOAT8 [as=s_52wk_high:71, outer=(11), immutable]
      ├── security.s_52wk_low:13::FLOAT8 [as=s_52wk_low:72, outer=(13), immutable]
      ├── security.s_dividend:15::FLOAT8 [as=s_dividend:73, outer=(15), immutable]
      ├── security.s_yield:16::FLOAT8 [as=s_yield:74, outer=(16), immutable]
      ├── last_trade.lt_price:65::FLOAT8 [as=lt_price:75, outer=(65), immutable]
      └── last_trade.lt_open_price:66::FLOAT8 [as=lt_open_price:76, outer=(66), immutable]

# Q2
opt
   SELECT co_name, in_name
     FROM company_competitor
LEFT JOIN company ON co_id = cp_comp_co_id
LEFT JOIN industry ON in_id = cp_in_id
    WHERE cp_co_id = 0
    LIMIT 3;
----
project
 ├── columns: co_name:8!null in_name:18!null
 ├── cardinality: [0 - 3]
 ├── key: (8,18)
 └── inner-join (lookup industry)
      ├── columns: cp_co_id:1!null cp_comp_co_id:2!null cp_in_id:3!null co_id:6!null co_name:8!null in_id:17!null in_name:18!null
      ├── key columns: [3] = [17]
      ├── lookup columns are key
      ├── cardinality: [0 - 3]
      ├── key: (6,17)
      ├── fd: ()-->(1), (6)-->(8), (8)-->(6), (17)-->(18), (18)-->(17), (2)==(6), (6)==(2), (3)==(17), (17)==(3)
      ├── inner-join (lookup company)
      │    ├── columns: cp_co_id:1!null cp_comp_co_id:2!null cp_in_id:3!null co_id:6!null co_name:8!null
      │    ├── key columns: [2] = [6]
      │    ├── lookup columns are key
      │    ├── cardinality: [0 - 3]
      │    ├── key: (3,6)
      │    ├── fd: ()-->(1), (6)-->(8), (8)-->(6), (2)==(6), (6)==(2)
      │    ├── scan company_competitor
      │    │    ├── columns: cp_co_id:1!null cp_comp_co_id:2!null cp_in_id:3!null
      │    │    ├── constraint: /1/2/3: [/0 - /0]
      │    │    ├── limit: 3
      │    │    ├── key: (2,3)
      │    │    └── fd: ()-->(1)
      │    └── filters (true)
      └── filters (true)

# Q2 (With Hints)
opt
          SELECT co_name, in_name
            FROM company_competitor
LEFT LOOKUP JOIN company ON co_id = cp_comp_co_id
LEFT LOOKUP JOIN industry ON in_id = cp_in_id
           WHERE cp_co_id = 0
           LIMIT 3;
----
project
 ├── columns: co_name:8!null in_name:18!null
 ├── cardinality: [0 - 3]
 ├── key: (8,18)
 └── inner-join (lookup industry)
      ├── columns: cp_co_id:1!null cp_comp_co_id:2!null cp_in_id:3!null co_id:6!null co_name:8!null in_id:17!null in_name:18!null
      ├── flags: force lookup join (into right side)
      ├── key columns: [3] = [17]
      ├── lookup columns are key
      ├── cardinality: [0 - 3]
      ├── key: (6,17)
      ├── fd: ()-->(1), (6)-->(8), (8)-->(6), (17)-->(18), (18)-->(17), (2)==(6), (6)==(2), (3)==(17), (17)==(3)
      ├── inner-join (lookup company)
      │    ├── columns: cp_co_id:1!null cp_comp_co_id:2!null cp_in_id:3!null co_id:6!null co_name:8!null
      │    ├── flags: force lookup join (into right side)
      │    ├── key columns: [2] = [6]
      │    ├── lookup columns are key
      │    ├── cardinality: [0 - 3]
      │    ├── key: (3,6)
      │    ├── fd: ()-->(1), (6)-->(8), (8)-->(6), (2)==(6), (6)==(2)
      │    ├── scan company_competitor
      │    │    ├── columns: cp_co_id:1!null cp_comp_co_id:2!null cp_in_id:3!null
      │    │    ├── constraint: /1/2/3: [/0 - /0]
      │    │    ├── limit: 3
      │    │    ├── key: (2,3)
      │    │    └── fd: ()-->(1)
      │    └── filters (true)
      └── filters (true)

# Q3
opt
  SELECT fi_year,
         fi_qtr,
         fi_qtr_start_date,
         fi_revenue::FLOAT8,
         fi_net_earn::FLOAT8,
         fi_basic_eps::FLOAT8,
         fi_dilut_eps::FLOAT8,
         fi_margin::FLOAT8,
         fi_inventory::FLOAT8,
         fi_assets::FLOAT8,
         fi_liability::FLOAT8,
         fi_out_basic,
         fi_out_dilut
    FROM financial
   WHERE fi_co_id = 0
ORDER BY fi_year ASC, fi_qtr ASC
   LIMIT 20;
----
project
 ├── columns: fi_year:2!null fi_qtr:3!null fi_qtr_start_date:4!null fi_revenue:17!null fi_net_earn:18!null fi_basic_eps:19!null fi_dilut_eps:20!null fi_margin:21!null fi_inventory:22!null fi_assets:23!null fi_liability:24!null fi_out_basic:13!null fi_out_dilut:14!null
 ├── cardinality: [0 - 20]
 ├── immutable
 ├── key: (2,3)
 ├── fd: (2,3)-->(4,13,14,17-24)
 ├── ordering: +2,+3
 ├── scan financial
 │    ├── columns: fi_co_id:1!null fi_year:2!null fi_qtr:3!null fi_qtr_start_date:4!null financial.fi_revenue:5!null financial.fi_net_earn:6!null financial.fi_basic_eps:7!null financial.fi_dilut_eps:8!null financial.fi_margin:9!null financial.fi_inventory:10!null financial.fi_assets:11!null financial.fi_liability:12!null fi_out_basic:13!null fi_out_dilut:14!null
 │    ├── constraint: /1/2/3: [/0 - /0]
 │    ├── limit: 20
 │    ├── key: (2,3)
 │    ├── fd: ()-->(1), (2,3)-->(4-14)
 │    └── ordering: +2,+3 opt(1) [actual: +2,+3]
 └── projections
      ├── financial.fi_revenue:5::FLOAT8 [as=fi_revenue:17, outer=(5), immutable]
      ├── financial.fi_net_earn:6::FLOAT8 [as=fi_net_earn:18, outer=(6), immutable]
      ├── financial.fi_basic_eps:7::FLOAT8 [as=fi_basic_eps:19, outer=(7), immutable]
      ├── financial.fi_dilut_eps:8::FLOAT8 [as=fi_dilut_eps:20, outer=(8), immutable]
      ├── financial.fi_margin:9::FLOAT8 [as=fi_margin:21, outer=(9), immutable]
      ├── financial.fi_inventory:10::FLOAT8 [as=fi_inventory:22, outer=(10), immutable]
      ├── financial.fi_assets:11::FLOAT8 [as=fi_assets:23, outer=(11), immutable]
      └── financial.fi_liability:12::FLOAT8 [as=fi_liability:24, outer=(12), immutable]

# Q4
opt
  SELECT dm_date, dm_close::FLOAT8, dm_high::FLOAT8, dm_low::FLOAT8, dm_vol
    FROM daily_market
   WHERE dm_s_symb = 'SYMB' AND dm_date >= '2020-06-17'::DATE
ORDER BY dm_date ASC
   LIMIT 10
----
project
 ├── columns: dm_date:1!null dm_close:9!null dm_high:10!null dm_low:11!null dm_vol:6!null
 ├── cardinality: [0 - 10]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(6,9-11)
 ├── ordering: +1
 ├── scan daily_market@daily_market_dm_s_symb_dm_date_key
 │    ├── columns: dm_date:1!null dm_s_symb:2!null daily_market.dm_close:3!null daily_market.dm_high:4!null daily_market.dm_low:5!null dm_vol:6!null
 │    ├── constraint: /2/1: [/'SYMB'/'2020-06-17' - /'SYMB']
 │    ├── limit: 10
 │    ├── key: (1)
 │    ├── fd: ()-->(2), (1)-->(3-6)
 │    └── ordering: +1 opt(2) [actual: +1]
 └── projections
      ├── daily_market.dm_close:3::FLOAT8 [as=dm_close:9, outer=(3), immutable]
      ├── daily_market.dm_high:4::FLOAT8 [as=dm_high:10, outer=(4), immutable]
      └── daily_market.dm_low:5::FLOAT8 [as=dm_low:11, outer=(5), immutable]

# Q5
opt
SELECT ni_dts, ni_source, ni_author, ni_item
  FROM news_xref, news_item
 WHERE ni_id = nx_ni_id AND nx_co_id = 0
 LIMIT 2
----
project
 ├── columns: ni_dts:9!null ni_source:10!null ni_author:11 ni_item:8!null
 ├── cardinality: [0 - 2]
 └── inner-join (lookup news_item)
      ├── columns: nx_ni_id:1!null nx_co_id:2!null ni_id:5!null ni_item:8!null ni_dts:9!null ni_source:10!null ni_author:11
      ├── key columns: [1] = [5]
      ├── lookup columns are key
      ├── cardinality: [0 - 2]
      ├── key: (5)
      ├── fd: ()-->(2), (5)-->(8-11), (1)==(5), (5)==(1)
      ├── scan news_xref@news_xref_nx_co_id_idx
      │    ├── columns: nx_ni_id:1!null nx_co_id:2!null
      │    ├── constraint: /2/1: [/0 - /0]
      │    ├── limit: 2
      │    ├── key: (1)
      │    └── fd: ()-->(2)
      └── filters (true)

# Q6
opt
SELECT ni_dts, ni_source, ni_author, ni_headline, ni_summary
  FROM news_xref, news_item
 WHERE ni_id = nx_ni_id AND nx_co_id = 0
 LIMIT 2
----
project
 ├── columns: ni_dts:9!null ni_source:10!null ni_author:11 ni_headline:6!null ni_summary:7!null
 ├── cardinality: [0 - 2]
 └── inner-join (lookup news_item)
      ├── columns: nx_ni_id:1!null nx_co_id:2!null ni_id:5!null ni_headline:6!null ni_summary:7!null ni_dts:9!null ni_source:10!null ni_author:11
      ├── key columns: [1] = [5]
      ├── lookup columns are key
      ├── cardinality: [0 - 2]
      ├── key: (5)
      ├── fd: ()-->(2), (5)-->(6,7,9-11), (1)==(5), (5)==(1)
      ├── scan news_xref@news_xref_nx_co_id_idx
      │    ├── columns: nx_ni_id:1!null nx_co_id:2!null
      │    ├── constraint: /2/1: [/0 - /0]
      │    ├── limit: 2
      │    ├── key: (1)
      │    └── fd: ()-->(2)
      └── filters (true)

# --------------------------------------------------
# T6
# Trade-Lookup
# Emulates information retrieval by either a customer or a broker to satisfy
# their questions regarding a set of trades. The various sets of trades are
# chosen such that the work is representative of:
#   * performing general market analysis
#   * reviewing trades for a period of time prior to the most recent account
#     statement
#   * analyzing past performance of a particular security
#   * analyzing the history of a particular customer holding
# --------------------------------------------------

# Q1
opt
   SELECT t_id,
          t_bid_price::FLOAT8,
          t_exec_name,
          t_is_cash,
          t_trade_price::FLOAT8,
          tt_is_mrkt,
          se_amt::FLOAT8,
          se_cash_due_date,
          se_cash_type,
          ct_amt::FLOAT8,
          ct_dts,
          ct_name
     FROM trade
LEFT JOIN trade_type       ON tt_id   = t_tt_id
LEFT JOIN settlement       ON se_t_id = t_id
LEFT JOIN cash_transaction ON ct_t_id = t_id
    WHERE t_id = ANY ARRAY [1, 2, 3, 4, 5];
----
project
 ├── columns: t_id:1!null t_bid_price:36!null t_exec_name:10!null t_is_cash:5!null t_trade_price:37 tt_is_mrkt:21!null se_amt:38 se_cash_due_date:26 se_cash_type:25 ct_amt:39 ct_dts:31 ct_name:33
 ├── cardinality: [0 - 5]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(5,10,21,25,26,31,33,36-39)
 ├── inner-join (hash)
 │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null se_t_id:24 se_cash_type:25 se_cash_due_date:26 settlement.se_amt:27 ct_t_id:30 ct_dts:31 cash_transaction.ct_amt:32 ct_name:33
 │    ├── cardinality: [0 - 5]
 │    ├── multiplicity: left-rows(zero-or-more), right-rows(exactly-one)
 │    ├── key: (1)
 │    ├── fd: (1)-->(4,5,8,10,11,24-27,30-33), (18)-->(21), (24)-->(25-27), (30)-->(31-33), (4)==(18), (18)==(4)
 │    ├── scan trade_type
 │    │    ├── columns: tt_id:18!null tt_is_mrkt:21!null
 │    │    ├── key: (18)
 │    │    └── fd: (18)-->(21)
 │    ├── left-join (merge)
 │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:24 se_cash_type:25 se_cash_due_date:26 settlement.se_amt:27 ct_t_id:30 ct_dts:31 cash_transaction.ct_amt:32 ct_name:33
 │    │    ├── left ordering: +1
 │    │    ├── right ordering: +24
 │    │    ├── cardinality: [0 - 5]
 │    │    ├── key: (1)
 │    │    ├── fd: (1)-->(4,5,8,10,11,24-27,30-33), (30)-->(31-33), (24)-->(25-27)
 │    │    ├── left-join (merge)
 │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 ct_t_id:30 ct_dts:31 cash_transaction.ct_amt:32 ct_name:33
 │    │    │    ├── left ordering: +1
 │    │    │    ├── right ordering: +30
 │    │    │    ├── cardinality: [0 - 5]
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: (1)-->(4,5,8,10,11,30-33), (30)-->(31-33)
 │    │    │    ├── ordering: +1
 │    │    │    ├── scan trade
 │    │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    │    ├── constraint: /1: [/1 - /5]
 │    │    │    │    ├── cardinality: [0 - 5]
 │    │    │    │    ├── key: (1)
 │    │    │    │    ├── fd: (1)-->(4,5,8,10,11)
 │    │    │    │    └── ordering: +1
 │    │    │    ├── scan cash_transaction
 │    │    │    │    ├── columns: ct_t_id:30!null ct_dts:31!null cash_transaction.ct_amt:32!null ct_name:33
 │    │    │    │    ├── constraint: /30: [/1 - /5]
 │    │    │    │    ├── cardinality: [0 - 5]
 │    │    │    │    ├── key: (30)
 │    │    │    │    ├── fd: (30)-->(31-33)
 │    │    │    │    └── ordering: +30
 │    │    │    └── filters (true)
 │    │    ├── scan settlement
 │    │    │    ├── columns: se_t_id:24!null se_cash_type:25!null se_cash_due_date:26!null settlement.se_amt:27!null
 │    │    │    ├── constraint: /24: [/1 - /5]
 │    │    │    ├── cardinality: [0 - 5]
 │    │    │    ├── key: (24)
 │    │    │    ├── fd: (24)-->(25-27)
 │    │    │    └── ordering: +24
 │    │    └── filters (true)
 │    └── filters
 │         └── tt_id:18 = t_tt_id:4 [outer=(4,18), constraints=(/4: (/NULL - ]; /18: (/NULL - ]), fd=(4)==(18), (18)==(4)]
 └── projections
      ├── trade.t_bid_price:8::FLOAT8 [as=t_bid_price:36, outer=(8), immutable]
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:37, outer=(11), immutable]
      ├── settlement.se_amt:27::FLOAT8 [as=se_amt:38, outer=(27), immutable]
      └── cash_transaction.ct_amt:32::FLOAT8 [as=ct_amt:39, outer=(32), immutable]

# Q1 (With Hints)
opt
          SELECT t_id,
                 t_bid_price::FLOAT8,
                 t_exec_name,
                 t_is_cash,
                 t_trade_price::FLOAT8,
                 tt_is_mrkt,
                 se_amt::FLOAT8,
                 se_cash_due_date,
                 se_cash_type,
                 ct_amt::FLOAT8,
                 ct_dts,
                 ct_name
            FROM trade
  LEFT HASH JOIN trade_type       ON tt_id   = t_tt_id
LEFT LOOKUP JOIN settlement       ON se_t_id = t_id
LEFT LOOKUP JOIN cash_transaction ON ct_t_id = t_id
           WHERE t_id = ANY ARRAY [1, 2, 3, 4, 5];
----
project
 ├── columns: t_id:1!null t_bid_price:36!null t_exec_name:10!null t_is_cash:5!null t_trade_price:37 tt_is_mrkt:21!null se_amt:38 se_cash_due_date:26 se_cash_type:25 ct_amt:39 ct_dts:31 ct_name:33
 ├── cardinality: [0 - 5]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(5,10,21,25,26,31,33,36-39)
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null se_t_id:24 se_cash_type:25 se_cash_due_date:26 settlement.se_amt:27 ct_t_id:30 ct_dts:31 cash_transaction.ct_amt:32 ct_name:33
 │    ├── flags: force lookup join (into right side)
 │    ├── key columns: [1] = [30]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 5]
 │    ├── key: (1)
 │    ├── fd: (1)-->(4,5,8,10,11,24-27,30-33), (18)-->(21), (24)-->(25-27), (30)-->(31-33), (4)==(18), (18)==(4)
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null se_t_id:24 se_cash_type:25 se_cash_due_date:26 settlement.se_amt:27
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [1] = [24]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 5]
 │    │    ├── key: (1)
 │    │    ├── fd: (1)-->(4,5,8,10,11,24-27), (18)-->(21), (24)-->(25-27), (4)==(18), (18)==(4)
 │    │    ├── inner-join (hash)
 │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null
 │    │    │    ├── flags: force hash join (store right side)
 │    │    │    ├── cardinality: [0 - 5]
 │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: (1)-->(4,5,8,10,11), (18)-->(21), (4)==(18), (18)==(4)
 │    │    │    ├── scan trade
 │    │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    │    ├── constraint: /1: [/1 - /5]
 │    │    │    │    ├── cardinality: [0 - 5]
 │    │    │    │    ├── key: (1)
 │    │    │    │    └── fd: (1)-->(4,5,8,10,11)
 │    │    │    ├── scan trade_type
 │    │    │    │    ├── columns: tt_id:18!null tt_is_mrkt:21!null
 │    │    │    │    ├── key: (18)
 │    │    │    │    └── fd: (18)-->(21)
 │    │    │    └── filters
 │    │    │         └── tt_id:18 = t_tt_id:4 [outer=(4,18), constraints=(/4: (/NULL - ]; /18: (/NULL - ]), fd=(4)==(18), (18)==(4)]
 │    │    └── filters
 │    │         └── se_t_id:24 IN (1, 2, 3, 4, 5) [outer=(24), constraints=(/24: [/1 - /1] [/2 - /2] [/3 - /3] [/4 - /4] [/5 - /5]; tight)]
 │    └── filters
 │         └── ct_t_id:30 IN (1, 2, 3, 4, 5) [outer=(30), constraints=(/30: [/1 - /1] [/2 - /2] [/3 - /3] [/4 - /4] [/5 - /5]; tight)]
 └── projections
      ├── trade.t_bid_price:8::FLOAT8 [as=t_bid_price:36, outer=(8), immutable]
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:37, outer=(11), immutable]
      ├── settlement.se_amt:27::FLOAT8 [as=se_amt:38, outer=(27), immutable]
      └── cash_transaction.ct_amt:32::FLOAT8 [as=ct_amt:39, outer=(32), immutable]

# Q2
opt
  SELECT th_t_id, th_dts, th_st_id
    FROM trade_history
   WHERE th_t_id = ANY ARRAY[1, 2, 3, 4, 5]
ORDER BY th_dts;
----
sort
 ├── columns: th_t_id:1!null th_dts:2!null th_st_id:3!null
 ├── key: (1,3)
 ├── fd: (1,3)-->(2)
 ├── ordering: +2
 └── scan trade_history
      ├── columns: th_t_id:1!null th_dts:2!null th_st_id:3!null
      ├── constraint: /1/3: [/1 - /5]
      ├── key: (1,3)
      └── fd: (1,3)-->(2)

# Q3
opt
   SELECT t_id,
          t_bid_price::FLOAT8,
          t_exec_name,
          t_is_cash,
          t_trade_price::FLOAT8,
          se_amt::FLOAT8,
          se_cash_due_date,
          se_cash_type,
          ct_amt::FLOAT8,
          ct_dts,
          ct_name
     FROM trade
LEFT JOIN settlement       ON se_t_id = t_id
LEFT JOIN cash_transaction ON ct_t_id = t_id
    WHERE t_ca_id = 0 AND
          t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP AND
          t_dts <= '2020-06-25 22:27:42.148484+00:00'::TIMESTAMP
 ORDER BY t_dts ASC
    LIMIT 20;
----
project
 ├── columns: t_id:1!null t_bid_price:30!null t_exec_name:10!null t_is_cash:5!null t_trade_price:31 se_amt:32 se_cash_due_date:20 se_cash_type:19 ct_amt:33 ct_dts:25 ct_name:27  [hidden: t_dts:2!null]
 ├── cardinality: [0 - 20]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(2,5,10,19,20,25,27,30-33)
 ├── ordering: +2
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21 ct_t_id:24 ct_dts:25 cash_transaction.ct_amt:26 ct_name:27
 │    ├── key columns: [1] = [24]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 20]
 │    ├── key: (1)
 │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11,18-21,24-27), (18)-->(19-21), (24)-->(25-27)
 │    ├── ordering: +2 opt(9) [actual: +2]
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21
 │    │    ├── key columns: [1] = [18]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 20]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11,18-21), (18)-->(19-21)
 │    │    ├── ordering: +2 opt(9) [actual: +2]
 │    │    ├── scan trade@trade_t_ca_id_t_dts_idx,rev
 │    │    │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    ├── constraint: /9/-2/1: [/0/'2020-06-25 22:27:42.148484' - /0/'2020-06-15 22:27:42.148484']
 │    │    │    ├── limit: 20(rev)
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11)
 │    │    │    └── ordering: +2 opt(9) [actual: +2]
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_bid_price:8::FLOAT8 [as=t_bid_price:30, outer=(8), immutable]
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:31, outer=(11), immutable]
      ├── settlement.se_amt:21::FLOAT8 [as=se_amt:32, outer=(21), immutable]
      └── cash_transaction.ct_amt:26::FLOAT8 [as=ct_amt:33, outer=(26), immutable]

# Q3 (With Hints)
opt
          SELECT t_id,
                 t_bid_price::FLOAT8,
                 t_exec_name,
                 t_is_cash,
                 t_trade_price::FLOAT8,
                 se_amt::FLOAT8,
                 se_cash_due_date,
                 se_cash_type,
                 ct_amt::FLOAT8,
                 ct_dts,
                 ct_name
            FROM trade
LEFT LOOKUP JOIN settlement       ON se_t_id = t_id
LEFT LOOKUP JOIN cash_transaction ON ct_t_id = t_id
           WHERE t_ca_id = 0 AND
                 t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP AND
                 t_dts <= '2020-06-25 22:27:42.148484+00:00'::TIMESTAMP
        ORDER BY t_dts ASC
           LIMIT 20;
----
project
 ├── columns: t_id:1!null t_bid_price:30!null t_exec_name:10!null t_is_cash:5!null t_trade_price:31 se_amt:32 se_cash_due_date:20 se_cash_type:19 ct_amt:33 ct_dts:25 ct_name:27  [hidden: t_dts:2!null]
 ├── cardinality: [0 - 20]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(2,5,10,19,20,25,27,30-33)
 ├── ordering: +2
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21 ct_t_id:24 ct_dts:25 cash_transaction.ct_amt:26 ct_name:27
 │    ├── flags: force lookup join (into right side)
 │    ├── key columns: [1] = [24]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 20]
 │    ├── key: (1)
 │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11,18-21,24-27), (18)-->(19-21), (24)-->(25-27)
 │    ├── ordering: +2 opt(9) [actual: +2]
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [1] = [18]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 20]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11,18-21), (18)-->(19-21)
 │    │    ├── ordering: +2 opt(9) [actual: +2]
 │    │    ├── scan trade@trade_t_ca_id_t_dts_idx,rev
 │    │    │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    ├── constraint: /9/-2/1: [/0/'2020-06-25 22:27:42.148484' - /0/'2020-06-15 22:27:42.148484']
 │    │    │    ├── limit: 20(rev)
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11)
 │    │    │    └── ordering: +2 opt(9) [actual: +2]
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_bid_price:8::FLOAT8 [as=t_bid_price:30, outer=(8), immutable]
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:31, outer=(11), immutable]
      ├── settlement.se_amt:21::FLOAT8 [as=se_amt:32, outer=(21), immutable]
      └── cash_transaction.ct_amt:26::FLOAT8 [as=ct_amt:33, outer=(26), immutable]

# Q4
opt
   SELECT t_id,
          t_ca_id,
          t_exec_name,
          t_is_cash,
          t_trade_price::FLOAT8,
          t_qty,
          t_dts,
          t_tt_id,
          se_amt::FLOAT8,
          se_cash_due_date,
          se_cash_type,
          ct_amt::FLOAT8,
          ct_dts,
          ct_name
     FROM trade
LEFT JOIN settlement       ON se_t_id = t_id
LEFT JOIN cash_transaction ON ct_t_id = t_id
    WHERE t_s_symb = 'Limit-Sell' AND
          t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP AND
          t_dts <= '2020-06-25 22:27:42.148484+00:00'::TIMESTAMP
 ORDER BY t_dts ASC
    LIMIT 20;
----
project
 ├── columns: t_id:1!null t_ca_id:9!null t_exec_name:10!null t_is_cash:5!null t_trade_price:30 t_qty:7!null t_dts:2!null t_tt_id:4!null se_amt:31 se_cash_due_date:20 se_cash_type:19 ct_amt:32 ct_dts:25 ct_name:27
 ├── cardinality: [0 - 20]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(2,4,5,7,9,10,19,20,25,27,30-32)
 ├── ordering: +2
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21 ct_t_id:24 ct_dts:25 cash_transaction.ct_amt:26 ct_name:27
 │    ├── key columns: [1] = [24]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 20]
 │    ├── key: (1)
 │    ├── fd: ()-->(6), (1)-->(2,4,5,7,9-11,18-21,24-27), (18)-->(19-21), (24)-->(25-27)
 │    ├── ordering: +2 opt(6) [actual: +2]
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21
 │    │    ├── key columns: [1] = [18]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 20]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(6), (1)-->(2,4,5,7,9-11,18-21), (18)-->(19-21)
 │    │    ├── ordering: +2 opt(6) [actual: +2]
 │    │    ├── scan trade@trade_t_s_symb_t_dts_idx
 │    │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    ├── constraint: /6/2/1: [/'Limit-Sell'/'2020-06-15 22:27:42.148484' - /'Limit-Sell'/'2020-06-25 22:27:42.148484']
 │    │    │    ├── limit: 20
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(6), (1)-->(2,4,5,7,9-11)
 │    │    │    └── ordering: +2 opt(6) [actual: +2]
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:30, outer=(11), immutable]
      ├── settlement.se_amt:21::FLOAT8 [as=se_amt:31, outer=(21), immutable]
      └── cash_transaction.ct_amt:26::FLOAT8 [as=ct_amt:32, outer=(26), immutable]

# Q4 (With Hints)
opt
          SELECT t_id,
                 t_ca_id,
                 t_exec_name,
                 t_is_cash,
                 t_trade_price::FLOAT8,
                 t_qty,
                 t_dts,
                 t_tt_id,
                 se_amt::FLOAT8,
                 se_cash_due_date,
                 se_cash_type,
                 ct_amt::FLOAT8,
                 ct_dts,
                 ct_name
            FROM trade
LEFT LOOKUP JOIN settlement       ON se_t_id = t_id
LEFT LOOKUP JOIN cash_transaction ON ct_t_id = t_id
           WHERE t_s_symb = 'Limit-Sell' AND
                 t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP AND
                 t_dts <= '2020-06-25 22:27:42.148484+00:00'::TIMESTAMP
        ORDER BY t_dts ASC
           LIMIT 20;
----
project
 ├── columns: t_id:1!null t_ca_id:9!null t_exec_name:10!null t_is_cash:5!null t_trade_price:30 t_qty:7!null t_dts:2!null t_tt_id:4!null se_amt:31 se_cash_due_date:20 se_cash_type:19 ct_amt:32 ct_dts:25 ct_name:27
 ├── cardinality: [0 - 20]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(2,4,5,7,9,10,19,20,25,27,30-32)
 ├── ordering: +2
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21 ct_t_id:24 ct_dts:25 cash_transaction.ct_amt:26 ct_name:27
 │    ├── flags: force lookup join (into right side)
 │    ├── key columns: [1] = [24]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 20]
 │    ├── key: (1)
 │    ├── fd: ()-->(6), (1)-->(2,4,5,7,9-11,18-21,24-27), (18)-->(19-21), (24)-->(25-27)
 │    ├── ordering: +2 opt(6) [actual: +2]
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [1] = [18]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 20]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(6), (1)-->(2,4,5,7,9-11,18-21), (18)-->(19-21)
 │    │    ├── ordering: +2 opt(6) [actual: +2]
 │    │    ├── scan trade@trade_t_s_symb_t_dts_idx
 │    │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    ├── constraint: /6/2/1: [/'Limit-Sell'/'2020-06-15 22:27:42.148484' - /'Limit-Sell'/'2020-06-25 22:27:42.148484']
 │    │    │    ├── limit: 20
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(6), (1)-->(2,4,5,7,9-11)
 │    │    │    └── ordering: +2 opt(6) [actual: +2]
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:30, outer=(11), immutable]
      ├── settlement.se_amt:21::FLOAT8 [as=se_amt:31, outer=(21), immutable]
      └── cash_transaction.ct_amt:26::FLOAT8 [as=ct_amt:32, outer=(26), immutable]

# Q5
opt
  SELECT t_id
    FROM trade
   WHERE t_ca_id = 0 AND t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP
ORDER BY t_dts ASC
   LIMIT 1;
----
project
 ├── columns: t_id:1!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(1)
 └── scan trade@trade_t_ca_id_t_dts_idx,rev
      ├── columns: t_id:1!null t_dts:2!null t_ca_id:9!null
      ├── constraint: /9/-2/1: [/0 - /0/'2020-06-15 22:27:42.148484']
      ├── limit: 1(rev)
      ├── key: ()
      └── fd: ()-->(1,2,9)

# Q6
#
# TODO: The limit should be pushed down, but the projects (added during
# exploration) are in the way.
#
opt
SELECT hh_h_t_id, hh_t_id, hh_before_qty, hh_after_qty
  FROM holding_history
 WHERE hh_h_t_id IN (SELECT hh_h_t_id FROM holding_history WHERE hh_t_id = 0)
 LIMIT 20;
----
limit
 ├── columns: hh_h_t_id:1!null hh_t_id:2!null hh_before_qty:3!null hh_after_qty:4!null
 ├── cardinality: [0 - 20]
 ├── key: (1,2)
 ├── fd: (1,2)-->(3,4)
 ├── project
 │    ├── columns: hh_h_t_id:1!null hh_t_id:2!null hh_before_qty:3!null hh_after_qty:4!null
 │    ├── key: (1,2)
 │    ├── fd: (1,2)-->(3,4)
 │    ├── limit hint: 20.00
 │    └── project
 │         ├── columns: hh_h_t_id:1!null hh_t_id:2!null hh_before_qty:3!null hh_after_qty:4!null hh_h_t_id:7!null
 │         ├── key: (2,7)
 │         ├── fd: (1,2)-->(3,4), (1)==(7), (7)==(1)
 │         ├── limit hint: 20.00
 │         └── inner-join (lookup holding_history)
 │              ├── columns: hh_h_t_id:1!null hh_t_id:2!null hh_before_qty:3!null hh_after_qty:4!null hh_h_t_id:7!null hh_t_id:8!null
 │              ├── key columns: [7] = [1]
 │              ├── key: (2,7)
 │              ├── fd: ()-->(8), (1,2)-->(3,4), (1)==(7), (7)==(1)
 │              ├── limit hint: 20.00
 │              ├── scan holding_history@holding_history_hh_t_id_hh_h_t_id_key
 │              │    ├── columns: hh_h_t_id:7!null hh_t_id:8!null
 │              │    ├── constraint: /8/7: [/0 - /0]
 │              │    ├── key: (7)
 │              │    ├── fd: ()-->(8)
 │              │    └── limit hint: 10.00
 │              └── filters (true)
 └── 20

# --------------------------------------------------
# T7
# Trade-Order
# Emulates the process of buying or selling a security by a Customer, Broker, or
# authorized third-party. If the person executing the trade order is not the
# account owner, the Transaction will verify that the person has the appropriate
# authorization to perform the trade order. The Transaction allows the person
# trading to execute buys at the current market price, sells at the current
# market price, or limit buys and sells at a requested price. The Transaction
# also provides an estimate of the financial impact of the proposed trade by
# providing profit/loss data, tax implications, and anticipated commission fees.
# This allows the trader to evaluate the desirability of the proposed security
# trade before either submitting or canceling the trade.
# --------------------------------------------------

# Q1
opt
    SELECT ca_name,
           ca_b_id,
           ca_c_id,
           ca_tax_st,
           c_f_name,
           c_l_name,
           c_tier,
           c_tax_id,
           b_name
      FROM customer_account
INNER JOIN customer ON c_id = ca_c_id
INNER JOIN broker   ON b_id = ca_b_id
     WHERE ca_id = 0;
----
project
 ├── columns: ca_name:4 ca_b_id:2!null ca_c_id:3!null ca_tax_st:5!null c_f_name:13!null c_l_name:12!null c_tier:16!null c_tax_id:10!null b_name:37!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(2-5,10,12,13,16,37)
 └── inner-join (lookup broker)
      ├── columns: ca_id:1!null ca_b_id:2!null ca_c_id:3!null ca_name:4 ca_tax_st:5!null c_id:9!null c_tax_id:10!null c_l_name:12!null c_f_name:13!null c_tier:16!null b_id:35!null b_name:37!null
      ├── key columns: [2] = [35]
      ├── lookup columns are key
      ├── cardinality: [0 - 1]
      ├── key: ()
      ├── fd: ()-->(1-5,9,10,12,13,16,35,37), (3)==(9), (9)==(3), (2)==(35), (35)==(2)
      ├── inner-join (lookup customer)
      │    ├── columns: ca_id:1!null ca_b_id:2!null ca_c_id:3!null ca_name:4 ca_tax_st:5!null c_id:9!null c_tax_id:10!null c_l_name:12!null c_f_name:13!null c_tier:16!null
      │    ├── key columns: [3] = [9]
      │    ├── lookup columns are key
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    ├── fd: ()-->(1-5,9,10,12,13,16), (3)==(9), (9)==(3)
      │    ├── scan customer_account
      │    │    ├── columns: ca_id:1!null ca_b_id:2!null ca_c_id:3!null ca_name:4 ca_tax_st:5!null
      │    │    ├── constraint: /1: [/0 - /0]
      │    │    ├── cardinality: [0 - 1]
      │    │    ├── key: ()
      │    │    └── fd: ()-->(1-5)
      │    └── filters (true)
      └── filters (true)

# Q1 (With Hints)
opt
           SELECT ca_name,
                  ca_b_id,
                  ca_c_id,
                  ca_tax_st,
                  c_f_name,
                  c_l_name,
                  c_tier,
                  c_tax_id,
                  b_name
             FROM customer_account
INNER LOOKUP JOIN customer ON c_id = ca_c_id
INNER LOOKUP JOIN broker   ON b_id = ca_b_id
            WHERE ca_id = 0;
----
project
 ├── columns: ca_name:4 ca_b_id:2!null ca_c_id:3!null ca_tax_st:5!null c_f_name:13!null c_l_name:12!null c_tier:16!null c_tax_id:10!null b_name:37!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(2-5,10,12,13,16,37)
 └── inner-join (lookup broker)
      ├── columns: ca_id:1!null ca_b_id:2!null ca_c_id:3!null ca_name:4 ca_tax_st:5!null c_id:9!null c_tax_id:10!null c_l_name:12!null c_f_name:13!null c_tier:16!null b_id:35!null b_name:37!null
      ├── flags: force lookup join (into right side)
      ├── key columns: [2] = [35]
      ├── lookup columns are key
      ├── cardinality: [0 - 1]
      ├── key: ()
      ├── fd: ()-->(1-5,9,10,12,13,16,35,37), (3)==(9), (9)==(3), (2)==(35), (35)==(2)
      ├── inner-join (lookup customer)
      │    ├── columns: ca_id:1!null ca_b_id:2!null ca_c_id:3!null ca_name:4 ca_tax_st:5!null c_id:9!null c_tax_id:10!null c_l_name:12!null c_f_name:13!null c_tier:16!null
      │    ├── flags: force lookup join (into right side)
      │    ├── key columns: [3] = [9]
      │    ├── lookup columns are key
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    ├── fd: ()-->(1-5,9,10,12,13,16), (3)==(9), (9)==(3)
      │    ├── scan customer_account
      │    │    ├── columns: ca_id:1!null ca_b_id:2!null ca_c_id:3!null ca_name:4 ca_tax_st:5!null
      │    │    ├── constraint: /1: [/0 - /0]
      │    │    ├── cardinality: [0 - 1]
      │    │    ├── key: ()
      │    │    └── fd: ()-->(1-5)
      │    └── filters (true)
      └── filters (true)

# Q2
opt
SELECT ap_acl
  FROM account_permission
 WHERE ap_ca_id = 0
   AND ap_f_name = 'customer_first'
   AND ap_l_name = 'customer_last'
   AND ap_tax_id = 'US1';
----
project
 ├── columns: ap_acl:2!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(2)
 └── select
      ├── columns: ap_ca_id:1!null ap_acl:2!null ap_tax_id:3!null ap_l_name:4!null ap_f_name:5!null
      ├── cardinality: [0 - 1]
      ├── key: ()
      ├── fd: ()-->(1-5)
      ├── scan account_permission
      │    ├── columns: ap_ca_id:1!null ap_acl:2!null ap_tax_id:3!null ap_l_name:4!null ap_f_name:5!null
      │    ├── constraint: /1/3: [/0/'US1' - /0/'US1']
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(1-5)
      └── filters
           ├── ap_f_name:5 = 'customer_first' [outer=(5), constraints=(/5: [/'customer_first' - /'customer_first']; tight), fd=()-->(5)]
           └── ap_l_name:4 = 'customer_last' [outer=(4), constraints=(/4: [/'customer_last' - /'customer_last']; tight), fd=()-->(4)]

# Q3
opt
SELECT s_ex_id, s_name, s_symb, lt_price::FLOAT8, tt_is_mrkt, tt_is_sell
FROM company, security, last_trade, trade_type
WHERE s_co_id = co_id AND
      lt_s_symb = s_symb AND
      tt_id = 'TLS' AND
      co_name = 'cockroach labs' AND
      s_issue = 'COMMON';
----
project
 ├── columns: s_ex_id:16!null s_name:15!null s_symb:12!null lt_price:43!null tt_is_mrkt:40!null tt_is_sell:39!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(12,15,16,39,40,43)
 ├── inner-join (cross)
 │    ├── columns: co_id:1!null co_name:3!null s_symb:12!null s_issue:13!null s_name:15!null s_ex_id:16!null s_co_id:17!null lt_s_symb:30!null last_trade.lt_price:32!null tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    ├── cardinality: [0 - 1]
 │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-one)
 │    ├── key: ()
 │    ├── fd: ()-->(1,3,12,13,15-17,30,32,37,39,40), (12)==(30), (30)==(12), (1)==(17), (17)==(1)
 │    ├── inner-join (lookup last_trade)
 │    │    ├── columns: co_id:1!null co_name:3!null s_symb:12!null s_issue:13!null s_name:15!null s_ex_id:16!null s_co_id:17!null lt_s_symb:30!null last_trade.lt_price:32!null
 │    │    ├── key columns: [12] = [30]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1,3,12,13,15-17,30,32), (12)==(30), (30)==(12), (1)==(17), (17)==(1)
 │    │    ├── inner-join (lookup security@security_s_co_id_s_issue_key)
 │    │    │    ├── columns: co_id:1!null co_name:3!null s_symb:12!null s_issue:13!null s_name:15!null s_ex_id:16!null s_co_id:17!null
 │    │    │    ├── key columns: [1 44] = [17 13]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(1,3,12,13,15-17), (1)==(17), (17)==(1)
 │    │    │    ├── project
 │    │    │    │    ├── columns: "lookup_join_const_col_@13":44!null co_id:1!null co_name:3!null
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    ├── fd: ()-->(1,3,44)
 │    │    │    │    ├── scan company@company_co_name_key
 │    │    │    │    │    ├── columns: co_id:1!null co_name:3!null
 │    │    │    │    │    ├── constraint: /3: [/'cockroach labs' - /'cockroach labs']
 │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    ├── key: ()
 │    │    │    │    │    └── fd: ()-->(1,3)
 │    │    │    │    └── projections
 │    │    │    │         └── 'COMMON' [as="lookup_join_const_col_@13":44]
 │    │    │    └── filters (true)
 │    │    └── filters (true)
 │    ├── scan trade_type
 │    │    ├── columns: tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    │    ├── constraint: /37: [/'TLS' - /'TLS']
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    └── fd: ()-->(37,39,40)
 │    └── filters (true)
 └── projections
      └── last_trade.lt_price:32::FLOAT8 [as=lt_price:43, outer=(32), immutable]

# Q3 (With Hints)
opt
           SELECT s_ex_id, s_name, s_symb, lt_price::FLOAT8, tt_is_mrkt, tt_is_sell
             FROM company
INNER LOOKUP JOIN security   ON s_co_id   = co_id
INNER LOOKUP JOIN last_trade ON lt_s_symb = s_symb
             JOIN trade_type ON tt_id     = 'TLS'
            WHERE co_name = 'cockroach labs' AND s_issue = 'COMMON';
----
project
 ├── columns: s_ex_id:16!null s_name:15!null s_symb:12!null lt_price:43!null tt_is_mrkt:40!null tt_is_sell:39!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(12,15,16,39,40,43)
 ├── inner-join (cross)
 │    ├── columns: co_id:1!null co_name:3!null s_symb:12!null s_issue:13!null s_name:15!null s_ex_id:16!null s_co_id:17!null lt_s_symb:30!null last_trade.lt_price:32!null tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    ├── cardinality: [0 - 1]
 │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-one)
 │    ├── key: ()
 │    ├── fd: ()-->(1,3,12,13,15-17,30,32,37,39,40), (1)==(17), (17)==(1), (12)==(30), (30)==(12)
 │    ├── inner-join (lookup last_trade)
 │    │    ├── columns: co_id:1!null co_name:3!null s_symb:12!null s_issue:13!null s_name:15!null s_ex_id:16!null s_co_id:17!null lt_s_symb:30!null last_trade.lt_price:32!null
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [12] = [30]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1,3,12,13,15-17,30,32), (1)==(17), (17)==(1), (12)==(30), (30)==(12)
 │    │    ├── inner-join (lookup security@security_s_co_id_s_issue_key)
 │    │    │    ├── columns: co_id:1!null co_name:3!null s_symb:12!null s_issue:13!null s_name:15!null s_ex_id:16!null s_co_id:17!null
 │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    ├── key columns: [1 44] = [17 13]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(1,3,12,13,15-17), (1)==(17), (17)==(1)
 │    │    │    ├── project
 │    │    │    │    ├── columns: "lookup_join_const_col_@13":44!null co_id:1!null co_name:3!null
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    ├── fd: ()-->(1,3,44)
 │    │    │    │    ├── scan company@company_co_name_key
 │    │    │    │    │    ├── columns: co_id:1!null co_name:3!null
 │    │    │    │    │    ├── constraint: /3: [/'cockroach labs' - /'cockroach labs']
 │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    ├── key: ()
 │    │    │    │    │    └── fd: ()-->(1,3)
 │    │    │    │    └── projections
 │    │    │    │         └── 'COMMON' [as="lookup_join_const_col_@13":44]
 │    │    │    └── filters (true)
 │    │    └── filters (true)
 │    ├── scan trade_type
 │    │    ├── columns: tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    │    ├── constraint: /37: [/'TLS' - /'TLS']
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    └── fd: ()-->(37,39,40)
 │    └── filters (true)
 └── projections
      └── last_trade.lt_price:32::FLOAT8 [as=lt_price:43, outer=(32), immutable]

# Q4
opt
SELECT s_ex_id, s_name, co_name, lt_price::FLOAT8, tt_is_mrkt, tt_is_sell
FROM security, company, last_trade, trade_type
WHERE tt_id = 'TLS' AND
      s_symb = 'ROACH' AND
      co_id     = s_co_id AND
      lt_s_symb = s_symb;
----
project
 ├── columns: s_ex_id:5!null s_name:4!null co_name:21!null lt_price:43!null tt_is_mrkt:40!null tt_is_sell:39!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(4,5,21,39,40,43)
 ├── inner-join (cross)
 │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null co_id:19!null co_name:21!null lt_s_symb:30!null last_trade.lt_price:32!null tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    ├── cardinality: [0 - 1]
 │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-one)
 │    ├── key: ()
 │    ├── fd: ()-->(1,4-6,19,21,30,32,37,39,40), (6)==(19), (19)==(6)
 │    ├── inner-join (lookup company)
 │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null co_id:19!null co_name:21!null
 │    │    ├── key columns: [6] = [19]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1,4-6,19,21), (6)==(19), (19)==(6)
 │    │    ├── scan security
 │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null
 │    │    │    ├── constraint: /1: [/'ROACH' - /'ROACH']
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    └── fd: ()-->(1,4-6)
 │    │    └── filters (true)
 │    ├── inner-join (cross)
 │    │    ├── columns: lt_s_symb:30!null last_trade.lt_price:32!null tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-one)
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(30,32,37,39,40)
 │    │    ├── scan last_trade
 │    │    │    ├── columns: lt_s_symb:30!null last_trade.lt_price:32!null
 │    │    │    ├── constraint: /30: [/'ROACH' - /'ROACH']
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    └── fd: ()-->(30,32)
 │    │    ├── scan trade_type
 │    │    │    ├── columns: tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    │    │    ├── constraint: /37: [/'TLS' - /'TLS']
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    └── fd: ()-->(37,39,40)
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      └── last_trade.lt_price:32::FLOAT8 [as=lt_price:43, outer=(32), immutable]

# Q4 (With Hints)
opt
           SELECT s_ex_id, s_name, co_name, lt_price::FLOAT8, tt_is_mrkt, tt_is_sell
             FROM security
INNER LOOKUP JOIN company    ON co_id     = s_co_id
INNER LOOKUP JOIN last_trade ON lt_s_symb = s_symb
             JOIN trade_type ON tt_id     = 'TLS' -- coalesced but not morally part of query
            WHERE s_symb = 'ROACH';
----
project
 ├── columns: s_ex_id:5!null s_name:4!null co_name:21!null lt_price:43!null tt_is_mrkt:40!null tt_is_sell:39!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(4,5,21,39,40,43)
 ├── inner-join (cross)
 │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null co_id:19!null co_name:21!null lt_s_symb:30!null last_trade.lt_price:32!null tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    ├── cardinality: [0 - 1]
 │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-one)
 │    ├── key: ()
 │    ├── fd: ()-->(1,4-6,19,21,30,32,37,39,40), (6)==(19), (19)==(6), (1)==(30), (30)==(1)
 │    ├── inner-join (lookup last_trade)
 │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null co_id:19!null co_name:21!null lt_s_symb:30!null last_trade.lt_price:32!null
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [1] = [30]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1,4-6,19,21,30,32), (6)==(19), (19)==(6), (1)==(30), (30)==(1)
 │    │    ├── inner-join (lookup company)
 │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null co_id:19!null co_name:21!null
 │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    ├── key columns: [6] = [19]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(1,4-6,19,21), (6)==(19), (19)==(6)
 │    │    │    ├── scan security
 │    │    │    │    ├── columns: s_symb:1!null s_name:4!null s_ex_id:5!null s_co_id:6!null
 │    │    │    │    ├── constraint: /1: [/'ROACH' - /'ROACH']
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    └── fd: ()-->(1,4-6)
 │    │    │    └── filters (true)
 │    │    └── filters
 │    │         └── lt_s_symb:30 = 'ROACH' [outer=(30), constraints=(/30: [/'ROACH' - /'ROACH']; tight), fd=()-->(30)]
 │    ├── scan trade_type
 │    │    ├── columns: tt_id:37!null tt_is_sell:39!null tt_is_mrkt:40!null
 │    │    ├── constraint: /37: [/'TLS' - /'TLS']
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    └── fd: ()-->(37,39,40)
 │    └── filters (true)
 └── projections
      └── last_trade.lt_price:32::FLOAT8 [as=lt_price:43, outer=(32), immutable]

# Q5
opt
SELECT hs_qty
  FROM holding_summary
 WHERE hs_ca_id = 0 AND hs_s_symb = 'ROACH';
----
project
 ├── columns: hs_qty:3!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(3)
 └── scan holding_summary
      ├── columns: hs_ca_id:1!null hs_s_symb:2!null hs_qty:3!null
      ├── constraint: /1/2: [/0/'ROACH' - /0/'ROACH']
      ├── cardinality: [0 - 1]
      ├── key: ()
      └── fd: ()-->(1-3)

# Q6
opt
  SELECT h_qty, h_price::FLOAT8
    FROM holding
   WHERE h_ca_id = 0 AND h_s_symb = 'ROACH'
ORDER BY h_dts DESC;
----
project
 ├── columns: h_qty:6!null h_price:9!null  [hidden: h_dts:4!null]
 ├── immutable
 ├── ordering: -4
 ├── scan holding@holding_h_ca_id_h_s_symb_h_dts_idx,rev
 │    ├── columns: h_ca_id:2!null h_s_symb:3!null h_dts:4!null holding.h_price:5!null h_qty:6!null
 │    ├── constraint: /2/3/4/1: [/0/'ROACH' - /0/'ROACH']
 │    ├── fd: ()-->(2,3)
 │    └── ordering: -4 opt(2,3) [actual: -4]
 └── projections
      └── holding.h_price:5::FLOAT8 [as=h_price:9, outer=(5), immutable]

# Q7
opt
  SELECT h_qty, h_price::FLOAT8
    FROM holding
   WHERE h_ca_id = 0 AND h_s_symb = 'ROACH'
ORDER BY h_dts ASC;
----
project
 ├── columns: h_qty:6!null h_price:9!null  [hidden: h_dts:4!null]
 ├── immutable
 ├── ordering: +4
 ├── scan holding@holding_h_ca_id_h_s_symb_h_dts_idx
 │    ├── columns: h_ca_id:2!null h_s_symb:3!null h_dts:4!null holding.h_price:5!null h_qty:6!null
 │    ├── constraint: /2/3/4/1: [/0/'ROACH' - /0/'ROACH']
 │    ├── fd: ()-->(2,3)
 │    └── ordering: +4 opt(2,3) [actual: +4]
 └── projections
      └── holding.h_price:5::FLOAT8 [as=h_price:9, outer=(5), immutable]

# Q8
opt
SELECT sum(tx_rate)::FLOAT8
  FROM taxrate
 WHERE tx_id IN (SELECT cx_tx_id FROM customer_taxrate WHERE cx_c_id = 0);
----
project
 ├── columns: sum:12
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(12)
 ├── scalar-group-by
 │    ├── columns: sum:11
 │    ├── cardinality: [1 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(11)
 │    ├── inner-join (lookup taxrate)
 │    │    ├── columns: tx_id:1!null tx_rate:3!null cx_tx_id:6!null cx_c_id:7!null
 │    │    ├── key columns: [6] = [1]
 │    │    ├── lookup columns are key
 │    │    ├── key: (6)
 │    │    ├── fd: ()-->(7), (1)-->(3), (1)==(6), (6)==(1)
 │    │    ├── scan customer_taxrate@customer_taxrate_cx_c_id_idx
 │    │    │    ├── columns: cx_tx_id:6!null cx_c_id:7!null
 │    │    │    ├── constraint: /7/6: [/0 - /0]
 │    │    │    ├── key: (6)
 │    │    │    └── fd: ()-->(7)
 │    │    └── filters (true)
 │    └── aggregations
 │         └── sum [as=sum:11, outer=(3)]
 │              └── tx_rate:3
 └── projections
      └── sum:11::FLOAT8 [as=sum:12, outer=(11), immutable]

# Q9
opt
SELECT cr_rate::FLOAT8, ch_chrg::FLOAT8
  FROM (
        SELECT cr_rate
          FROM commission_rate
         WHERE cr_c_tier = 3
           AND cr_tt_id = 'TLB'
           AND cr_ex_id = 'NYSE'
           AND cr_from_qty <= 5
           AND cr_to_qty >= 100
         LIMIT 1
       ),
       (
        SELECT ch_chrg
          FROM charge
         WHERE ch_c_tier = 3 AND ch_tt_id = 'TLB'
       );
----
project
 ├── columns: cr_rate:14!null ch_chrg:15!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(14,15)
 ├── inner-join (cross)
 │    ├── columns: cr_c_tier:1!null cr_tt_id:2!null cr_ex_id:3!null cr_from_qty:4!null cr_to_qty:5!null commission_rate.cr_rate:6!null ch_tt_id:9!null ch_c_tier:10!null charge.ch_chrg:11!null
 │    ├── cardinality: [0 - 1]
 │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-one)
 │    ├── key: ()
 │    ├── fd: ()-->(1-6,9-11)
 │    ├── limit
 │    │    ├── columns: cr_c_tier:1!null cr_tt_id:2!null cr_ex_id:3!null cr_from_qty:4!null cr_to_qty:5!null commission_rate.cr_rate:6!null
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1-6)
 │    │    ├── select
 │    │    │    ├── columns: cr_c_tier:1!null cr_tt_id:2!null cr_ex_id:3!null cr_from_qty:4!null cr_to_qty:5!null commission_rate.cr_rate:6!null
 │    │    │    ├── key: (4)
 │    │    │    ├── fd: ()-->(1-3), (4)-->(5,6)
 │    │    │    ├── limit hint: 1.00
 │    │    │    ├── scan commission_rate
 │    │    │    │    ├── columns: cr_c_tier:1!null cr_tt_id:2!null cr_ex_id:3!null cr_from_qty:4!null cr_to_qty:5!null commission_rate.cr_rate:6!null
 │    │    │    │    ├── constraint: /1/2/3/4: [/3/'TLB'/'NYSE'/0 - /3/'TLB'/'NYSE'/5]
 │    │    │    │    ├── cardinality: [0 - 6]
 │    │    │    │    ├── key: (4)
 │    │    │    │    ├── fd: ()-->(1-3), (4)-->(5,6)
 │    │    │    │    └── limit hint: 1.00
 │    │    │    └── filters
 │    │    │         └── cr_to_qty:5 >= 100 [outer=(5), constraints=(/5: [/100 - ]; tight)]
 │    │    └── 1
 │    ├── scan charge
 │    │    ├── columns: ch_tt_id:9!null ch_c_tier:10!null charge.ch_chrg:11!null
 │    │    ├── constraint: /9/10: [/'TLB'/3 - /'TLB'/3]
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    └── fd: ()-->(9-11)
 │    └── filters (true)
 └── projections
      ├── commission_rate.cr_rate:6::FLOAT8 [as=cr_rate:14, outer=(6), immutable]
      └── charge.ch_chrg:11::FLOAT8 [as=ch_chrg:15, outer=(11), immutable]

# Q10
opt
SELECT (ca_bal
        + (SELECT COALESCE(sum(hs_qty * lt_price), 0) AS ca_assets
             FROM holding_summary
 LEFT LOOKUP JOIN last_trade ON lt_s_symb = hs_s_symb
            WHERE hs_ca_id = 0)
       )::FLOAT8
  FROM customer_account
 WHERE ca_id = 0;
----
project
 ├── columns: float8:24
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(24)
 ├── scan customer_account
 │    ├── columns: ca_id:1!null ca_bal:6!null
 │    ├── constraint: /1: [/0 - /0]
 │    ├── cardinality: [0 - 1]
 │    ├── key: ()
 │    └── fd: ()-->(1,6)
 └── projections
      └── cast: FLOAT8 [as=float8:24, outer=(6), immutable, subquery]
           └── plus
                ├── ca_bal:6
                └── subquery
                     └── project
                          ├── columns: ca_assets:23
                          ├── cardinality: [1 - 1]
                          ├── immutable
                          ├── key: ()
                          ├── fd: ()-->(23)
                          ├── scalar-group-by
                          │    ├── columns: sum:22
                          │    ├── cardinality: [1 - 1]
                          │    ├── immutable
                          │    ├── key: ()
                          │    ├── fd: ()-->(22)
                          │    ├── project
                          │    │    ├── columns: column21:21
                          │    │    ├── immutable
                          │    │    ├── left-join (lookup last_trade)
                          │    │    │    ├── columns: hs_ca_id:9!null hs_s_symb:10!null hs_qty:11!null lt_s_symb:14 lt_price:16
                          │    │    │    ├── flags: force lookup join (into right side)
                          │    │    │    ├── key columns: [10] = [14]
                          │    │    │    ├── lookup columns are key
                          │    │    │    ├── key: (10)
                          │    │    │    ├── fd: ()-->(9), (10)-->(11,14,16), (14)-->(16)
                          │    │    │    ├── scan holding_summary
                          │    │    │    │    ├── columns: hs_ca_id:9!null hs_s_symb:10!null hs_qty:11!null
                          │    │    │    │    ├── constraint: /9/10: [/0 - /0]
                          │    │    │    │    ├── key: (10)
                          │    │    │    │    └── fd: ()-->(9), (10)-->(11)
                          │    │    │    └── filters (true)
                          │    │    └── projections
                          │    │         └── hs_qty:11 * lt_price:16 [as=column21:21, outer=(11,16), immutable]
                          │    └── aggregations
                          │         └── sum [as=sum:22, outer=(21)]
                          │              └── column21:21
                          └── projections
                               └── COALESCE(sum:22, 0) [as=ca_assets:23, outer=(22)]

# Q11
opt
WITH
insert_trade AS (
  INSERT INTO trade
  (
    t_id,
    t_dts,
    t_st_id,
    t_tt_id,
    t_is_cash,
    t_s_symb,
    t_qty,
    t_bid_price,
    t_ca_id,
    t_exec_name,
    t_trade_price,
    t_chrg,
    t_comm,
    t_tax,
    t_lifo
  )
  VALUES
  (
    0,
    '2020-06-17 22:27:42.148484+00:00'::TIMESTAMP,
    'SBMT',
    'TMB',
    True,
    'SYMB',
    10,
    '100.00':::FLOAT8::DECIMAL,
    0,
    'Name',
    NULL,
    '1.00':::FLOAT8::DECIMAL,
    '0.00':::FLOAT8::DECIMAL,
    0,
    True
  )
  RETURNING 1
),
insert_trade_history AS (
  INSERT INTO trade_history (th_t_id, th_dts, th_st_id)
  VALUES (0, '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP, 'SBMT')
  RETURNING 1
)
SELECT 1;
----
with &2 (insert_trade)
 ├── columns: "?column?":121!null
 ├── cardinality: [1 - 1]
 ├── volatile, mutations
 ├── key: ()
 ├── fd: ()-->(121)
 ├── project
 │    ├── columns: "?column?":87!null
 │    ├── cardinality: [1 - 1]
 │    ├── volatile, mutations
 │    ├── key: ()
 │    ├── fd: ()-->(87)
 │    ├── insert trade
 │    │    ├── columns: t_id:1!null
 │    │    ├── insert-mapping:
 │    │    │    ├── column1:18 => t_id:1
 │    │    │    ├── column2:19 => t_dts:2
 │    │    │    ├── t_st_id_cast:33 => trade.t_st_id:3
 │    │    │    ├── t_tt_id_cast:34 => trade.t_tt_id:4
 │    │    │    ├── column5:22 => t_is_cash:5
 │    │    │    ├── t_s_symb_cast:35 => trade.t_s_symb:6
 │    │    │    ├── t_qty_cast:36 => t_qty:7
 │    │    │    ├── t_bid_price_cast:37 => t_bid_price:8
 │    │    │    ├── column9:26 => trade.t_ca_id:9
 │    │    │    ├── t_exec_name_cast:38 => t_exec_name:10
 │    │    │    ├── column11:28 => t_trade_price:11
 │    │    │    ├── t_chrg_cast:39 => t_chrg:12
 │    │    │    ├── t_comm_cast:40 => t_comm:13
 │    │    │    ├── t_tax_cast:41 => t_tax:14
 │    │    │    └── column15:32 => t_lifo:15
 │    │    ├── return-mapping:
 │    │    │    └── column1:18 => t_id:1
 │    │    ├── check columns: check1:42 check2:43 check3:44 check4:45 check5:46
 │    │    ├── input binding: &1
 │    │    ├── cardinality: [1 - 1]
 │    │    ├── volatile, mutations
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1)
 │    │    ├── values
 │    │    │    ├── columns: column1:18!null column2:19!null column5:22!null column9:26!null column11:28 column15:32!null t_st_id_cast:33!null t_tt_id_cast:34!null t_s_symb_cast:35!null t_qty_cast:36!null t_bid_price_cast:37!null t_exec_name_cast:38!null t_chrg_cast:39!null t_comm_cast:40!null t_tax_cast:41!null check1:42!null check2:43!null check3:44!null check4:45!null check5:46!null
 │    │    │    ├── cardinality: [1 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(18,19,22,26,28,32-46)
 │    │    │    └── (0, '2020-06-17 22:27:42.148484', true, 0, NULL, true, 'SBMT', 'TMB', 'SYMB', 10, 100.00, 'Name', 1.00, 0.00, 0.00, true, true, true, true, true)
 │    │    └── f-k-checks
 │    │         ├── f-k-checks-item: trade(t_st_id) -> status_type(st_id)
 │    │         │    └── anti-join (lookup status_type)
 │    │         │         ├── columns: t_st_id:47!null
 │    │         │         ├── key columns: [47] = [48]
 │    │         │         ├── lookup columns are key
 │    │         │         ├── cardinality: [0 - 1]
 │    │         │         ├── key: ()
 │    │         │         ├── fd: ()-->(47)
 │    │         │         ├── with-scan &1
 │    │         │         │    ├── columns: t_st_id:47!null
 │    │         │         │    ├── mapping:
 │    │         │         │    │    └──  t_st_id_cast:33 => t_st_id:47
 │    │         │         │    ├── cardinality: [1 - 1]
 │    │         │         │    ├── key: ()
 │    │         │         │    └── fd: ()-->(47)
 │    │         │         └── filters (true)
 │    │         ├── f-k-checks-item: trade(t_tt_id) -> trade_type(tt_id)
 │    │         │    └── anti-join (lookup trade_type)
 │    │         │         ├── columns: t_tt_id:52!null
 │    │         │         ├── key columns: [52] = [53]
 │    │         │         ├── lookup columns are key
 │    │         │         ├── cardinality: [0 - 1]
 │    │         │         ├── key: ()
 │    │         │         ├── fd: ()-->(52)
 │    │         │         ├── with-scan &1
 │    │         │         │    ├── columns: t_tt_id:52!null
 │    │         │         │    ├── mapping:
 │    │         │         │    │    └──  t_tt_id_cast:34 => t_tt_id:52
 │    │         │         │    ├── cardinality: [1 - 1]
 │    │         │         │    ├── key: ()
 │    │         │         │    └── fd: ()-->(52)
 │    │         │         └── filters (true)
 │    │         ├── f-k-checks-item: trade(t_s_symb) -> security(s_symb)
 │    │         │    └── anti-join (lookup security)
 │    │         │         ├── columns: t_s_symb:59!null
 │    │         │         ├── key columns: [59] = [60]
 │    │         │         ├── lookup columns are key
 │    │         │         ├── cardinality: [0 - 1]
 │    │         │         ├── key: ()
 │    │         │         ├── fd: ()-->(59)
 │    │         │         ├── with-scan &1
 │    │         │         │    ├── columns: t_s_symb:59!null
 │    │         │         │    ├── mapping:
 │    │         │         │    │    └──  t_s_symb_cast:35 => t_s_symb:59
 │    │         │         │    ├── cardinality: [1 - 1]
 │    │         │         │    ├── key: ()
 │    │         │         │    └── fd: ()-->(59)
 │    │         │         └── filters (true)
 │    │         └── f-k-checks-item: trade(t_ca_id) -> customer_account(ca_id)
 │    │              └── anti-join (lookup customer_account)
 │    │                   ├── columns: t_ca_id:78!null
 │    │                   ├── key columns: [78] = [79]
 │    │                   ├── lookup columns are key
 │    │                   ├── cardinality: [0 - 1]
 │    │                   ├── key: ()
 │    │                   ├── fd: ()-->(78)
 │    │                   ├── with-scan &1
 │    │                   │    ├── columns: t_ca_id:78!null
 │    │                   │    ├── mapping:
 │    │                   │    │    └──  column9:26 => t_ca_id:78
 │    │                   │    ├── cardinality: [1 - 1]
 │    │                   │    ├── key: ()
 │    │                   │    └── fd: ()-->(78)
 │    │                   └── filters (true)
 │    └── projections
 │         └── 1 [as="?column?":87]
 └── with &4 (insert_trade_history)
      ├── columns: "?column?":121!null
      ├── cardinality: [1 - 1]
      ├── volatile, mutations
      ├── key: ()
      ├── fd: ()-->(121)
      ├── project
      │    ├── columns: "?column?":120!null
      │    ├── cardinality: [1 - 1]
      │    ├── volatile, mutations
      │    ├── key: ()
      │    ├── fd: ()-->(120)
      │    ├── insert trade_history
      │    │    ├── columns: trade_history.th_t_id:88!null trade_history.th_st_id:90!null
      │    │    ├── insert-mapping:
      │    │    │    ├── column1:93 => trade_history.th_t_id:88
      │    │    │    ├── column2:94 => th_dts:89
      │    │    │    └── th_st_id_cast:96 => trade_history.th_st_id:90
      │    │    ├── return-mapping:
      │    │    │    ├── column1:93 => trade_history.th_t_id:88
      │    │    │    └── th_st_id_cast:96 => trade_history.th_st_id:90
      │    │    ├── input binding: &3
      │    │    ├── cardinality: [1 - 1]
      │    │    ├── volatile, mutations
      │    │    ├── key: ()
      │    │    ├── fd: ()-->(88,90)
      │    │    ├── values
      │    │    │    ├── columns: column1:93!null column2:94!null th_st_id_cast:96!null
      │    │    │    ├── cardinality: [1 - 1]
      │    │    │    ├── key: ()
      │    │    │    ├── fd: ()-->(93,94,96)
      │    │    │    └── (0, '2020-06-15 22:27:42.148484', 'SBMT')
      │    │    └── f-k-checks
      │    │         ├── f-k-checks-item: trade_history(th_t_id) -> trade(t_id)
      │    │         │    └── anti-join (lookup trade)
      │    │         │         ├── columns: th_t_id:97!null
      │    │         │         ├── key columns: [97] = [98]
      │    │         │         ├── lookup columns are key
      │    │         │         ├── cardinality: [0 - 1]
      │    │         │         ├── key: ()
      │    │         │         ├── fd: ()-->(97)
      │    │         │         ├── with-scan &3
      │    │         │         │    ├── columns: th_t_id:97!null
      │    │         │         │    ├── mapping:
      │    │         │         │    │    └──  column1:93 => th_t_id:97
      │    │         │         │    ├── cardinality: [1 - 1]
      │    │         │         │    ├── key: ()
      │    │         │         │    └── fd: ()-->(97)
      │    │         │         └── filters (true)
      │    │         └── f-k-checks-item: trade_history(th_st_id) -> status_type(st_id)
      │    │              └── anti-join (lookup status_type)
      │    │                   ├── columns: th_st_id:115!null
      │    │                   ├── key columns: [115] = [116]
      │    │                   ├── lookup columns are key
      │    │                   ├── cardinality: [0 - 1]
      │    │                   ├── key: ()
      │    │                   ├── fd: ()-->(115)
      │    │                   ├── with-scan &3
      │    │                   │    ├── columns: th_st_id:115!null
      │    │                   │    ├── mapping:
      │    │                   │    │    └──  th_st_id_cast:96 => th_st_id:115
      │    │                   │    ├── cardinality: [1 - 1]
      │    │                   │    ├── key: ()
      │    │                   │    └── fd: ()-->(115)
      │    │                   └── filters (true)
      │    └── projections
      │         └── 1 [as="?column?":120]
      └── values
           ├── columns: "?column?":121!null
           ├── cardinality: [1 - 1]
           ├── key: ()
           ├── fd: ()-->(121)
           └── (1,)

# Q12
opt
WITH
insert_trade AS (
  INSERT INTO trade
  (
    t_id,
    t_dts,
    t_st_id,
    t_tt_id,
    t_is_cash,
    t_s_symb,
    t_qty,
    t_bid_price,
    t_ca_id,
    t_exec_name,
    t_trade_price,
    t_chrg,
    t_comm,
    t_tax,
    t_lifo
  )
  VALUES
  (
    0,
    '2020-06-17 22:27:42.148484+00:00'::TIMESTAMP,
    'SBMT',
    'TMB',
    True,
    'SYMB',
    10,
    '100.00':::FLOAT8::DECIMAL,
    0,
    'Name',
    NULL,
    '1.00':::FLOAT8::DECIMAL,
    '0.00':::FLOAT8::DECIMAL,
    0,
    True
  )
  RETURNING 1
),
insert_trade_history AS (
  INSERT INTO trade_history (th_t_id, th_dts, th_st_id)
  VALUES (0, '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP, 'SBMT')
  RETURNING 1
),
insert_trade_request AS (
  INSERT INTO trade_request
  (
    tr_t_id,
    tr_tt_id,
    tr_s_symb,
    tr_qty,
    tr_bid_price,
    tr_b_id
  )
  VALUES
  (
    0,
    'TMB',
    'SYMB',
    10,
    '100.00':::FLOAT8::DECIMAL,
    0
  )
  RETURNING 1
)
SELECT 1;
----
with &2 (insert_trade)
 ├── columns: "?column?":195!null
 ├── cardinality: [1 - 1]
 ├── volatile, mutations
 ├── key: ()
 ├── fd: ()-->(195)
 ├── project
 │    ├── columns: "?column?":87!null
 │    ├── cardinality: [1 - 1]
 │    ├── volatile, mutations
 │    ├── key: ()
 │    ├── fd: ()-->(87)
 │    ├── insert trade
 │    │    ├── columns: t_id:1!null
 │    │    ├── insert-mapping:
 │    │    │    ├── column1:18 => t_id:1
 │    │    │    ├── column2:19 => t_dts:2
 │    │    │    ├── t_st_id_cast:33 => trade.t_st_id:3
 │    │    │    ├── t_tt_id_cast:34 => trade.t_tt_id:4
 │    │    │    ├── column5:22 => t_is_cash:5
 │    │    │    ├── t_s_symb_cast:35 => trade.t_s_symb:6
 │    │    │    ├── t_qty_cast:36 => t_qty:7
 │    │    │    ├── t_bid_price_cast:37 => t_bid_price:8
 │    │    │    ├── column9:26 => trade.t_ca_id:9
 │    │    │    ├── t_exec_name_cast:38 => t_exec_name:10
 │    │    │    ├── column11:28 => t_trade_price:11
 │    │    │    ├── t_chrg_cast:39 => t_chrg:12
 │    │    │    ├── t_comm_cast:40 => t_comm:13
 │    │    │    ├── t_tax_cast:41 => t_tax:14
 │    │    │    └── column15:32 => t_lifo:15
 │    │    ├── return-mapping:
 │    │    │    └── column1:18 => t_id:1
 │    │    ├── check columns: check1:42 check2:43 check3:44 check4:45 check5:46
 │    │    ├── input binding: &1
 │    │    ├── cardinality: [1 - 1]
 │    │    ├── volatile, mutations
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1)
 │    │    ├── values
 │    │    │    ├── columns: column1:18!null column2:19!null column5:22!null column9:26!null column11:28 column15:32!null t_st_id_cast:33!null t_tt_id_cast:34!null t_s_symb_cast:35!null t_qty_cast:36!null t_bid_price_cast:37!null t_exec_name_cast:38!null t_chrg_cast:39!null t_comm_cast:40!null t_tax_cast:41!null check1:42!null check2:43!null check3:44!null check4:45!null check5:46!null
 │    │    │    ├── cardinality: [1 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(18,19,22,26,28,32-46)
 │    │    │    └── (0, '2020-06-17 22:27:42.148484', true, 0, NULL, true, 'SBMT', 'TMB', 'SYMB', 10, 100.00, 'Name', 1.00, 0.00, 0.00, true, true, true, true, true)
 │    │    └── f-k-checks
 │    │         ├── f-k-checks-item: trade(t_st_id) -> status_type(st_id)
 │    │         │    └── anti-join (lookup status_type)
 │    │         │         ├── columns: t_st_id:47!null
 │    │         │         ├── key columns: [47] = [48]
 │    │         │         ├── lookup columns are key
 │    │         │         ├── cardinality: [0 - 1]
 │    │         │         ├── key: ()
 │    │         │         ├── fd: ()-->(47)
 │    │         │         ├── with-scan &1
 │    │         │         │    ├── columns: t_st_id:47!null
 │    │         │         │    ├── mapping:
 │    │         │         │    │    └──  t_st_id_cast:33 => t_st_id:47
 │    │         │         │    ├── cardinality: [1 - 1]
 │    │         │         │    ├── key: ()
 │    │         │         │    └── fd: ()-->(47)
 │    │         │         └── filters (true)
 │    │         ├── f-k-checks-item: trade(t_tt_id) -> trade_type(tt_id)
 │    │         │    └── anti-join (lookup trade_type)
 │    │         │         ├── columns: t_tt_id:52!null
 │    │         │         ├── key columns: [52] = [53]
 │    │         │         ├── lookup columns are key
 │    │         │         ├── cardinality: [0 - 1]
 │    │         │         ├── key: ()
 │    │         │         ├── fd: ()-->(52)
 │    │         │         ├── with-scan &1
 │    │         │         │    ├── columns: t_tt_id:52!null
 │    │         │         │    ├── mapping:
 │    │         │         │    │    └──  t_tt_id_cast:34 => t_tt_id:52
 │    │         │         │    ├── cardinality: [1 - 1]
 │    │         │         │    ├── key: ()
 │    │         │         │    └── fd: ()-->(52)
 │    │         │         └── filters (true)
 │    │         ├── f-k-checks-item: trade(t_s_symb) -> security(s_symb)
 │    │         │    └── anti-join (lookup security)
 │    │         │         ├── columns: t_s_symb:59!null
 │    │         │         ├── key columns: [59] = [60]
 │    │         │         ├── lookup columns are key
 │    │         │         ├── cardinality: [0 - 1]
 │    │         │         ├── key: ()
 │    │         │         ├── fd: ()-->(59)
 │    │         │         ├── with-scan &1
 │    │         │         │    ├── columns: t_s_symb:59!null
 │    │         │         │    ├── mapping:
 │    │         │         │    │    └──  t_s_symb_cast:35 => t_s_symb:59
 │    │         │         │    ├── cardinality: [1 - 1]
 │    │         │         │    ├── key: ()
 │    │         │         │    └── fd: ()-->(59)
 │    │         │         └── filters (true)
 │    │         └── f-k-checks-item: trade(t_ca_id) -> customer_account(ca_id)
 │    │              └── anti-join (lookup customer_account)
 │    │                   ├── columns: t_ca_id:78!null
 │    │                   ├── key columns: [78] = [79]
 │    │                   ├── lookup columns are key
 │    │                   ├── cardinality: [0 - 1]
 │    │                   ├── key: ()
 │    │                   ├── fd: ()-->(78)
 │    │                   ├── with-scan &1
 │    │                   │    ├── columns: t_ca_id:78!null
 │    │                   │    ├── mapping:
 │    │                   │    │    └──  column9:26 => t_ca_id:78
 │    │                   │    ├── cardinality: [1 - 1]
 │    │                   │    ├── key: ()
 │    │                   │    └── fd: ()-->(78)
 │    │                   └── filters (true)
 │    └── projections
 │         └── 1 [as="?column?":87]
 └── with &4 (insert_trade_history)
      ├── columns: "?column?":195!null
      ├── cardinality: [1 - 1]
      ├── volatile, mutations
      ├── key: ()
      ├── fd: ()-->(195)
      ├── project
      │    ├── columns: "?column?":120!null
      │    ├── cardinality: [1 - 1]
      │    ├── volatile, mutations
      │    ├── key: ()
      │    ├── fd: ()-->(120)
      │    ├── insert trade_history
      │    │    ├── columns: trade_history.th_t_id:88!null trade_history.th_st_id:90!null
      │    │    ├── insert-mapping:
      │    │    │    ├── column1:93 => trade_history.th_t_id:88
      │    │    │    ├── column2:94 => th_dts:89
      │    │    │    └── th_st_id_cast:96 => trade_history.th_st_id:90
      │    │    ├── return-mapping:
      │    │    │    ├── column1:93 => trade_history.th_t_id:88
      │    │    │    └── th_st_id_cast:96 => trade_history.th_st_id:90
      │    │    ├── input binding: &3
      │    │    ├── cardinality: [1 - 1]
      │    │    ├── volatile, mutations
      │    │    ├── key: ()
      │    │    ├── fd: ()-->(88,90)
      │    │    ├── values
      │    │    │    ├── columns: column1:93!null column2:94!null th_st_id_cast:96!null
      │    │    │    ├── cardinality: [1 - 1]
      │    │    │    ├── key: ()
      │    │    │    ├── fd: ()-->(93,94,96)
      │    │    │    └── (0, '2020-06-15 22:27:42.148484', 'SBMT')
      │    │    └── f-k-checks
      │    │         ├── f-k-checks-item: trade_history(th_t_id) -> trade(t_id)
      │    │         │    └── anti-join (lookup trade)
      │    │         │         ├── columns: th_t_id:97!null
      │    │         │         ├── key columns: [97] = [98]
      │    │         │         ├── lookup columns are key
      │    │         │         ├── cardinality: [0 - 1]
      │    │         │         ├── key: ()
      │    │         │         ├── fd: ()-->(97)
      │    │         │         ├── with-scan &3
      │    │         │         │    ├── columns: th_t_id:97!null
      │    │         │         │    ├── mapping:
      │    │         │         │    │    └──  column1:93 => th_t_id:97
      │    │         │         │    ├── cardinality: [1 - 1]
      │    │         │         │    ├── key: ()
      │    │         │         │    └── fd: ()-->(97)
      │    │         │         └── filters (true)
      │    │         └── f-k-checks-item: trade_history(th_st_id) -> status_type(st_id)
      │    │              └── anti-join (lookup status_type)
      │    │                   ├── columns: th_st_id:115!null
      │    │                   ├── key columns: [115] = [116]
      │    │                   ├── lookup columns are key
      │    │                   ├── cardinality: [0 - 1]
      │    │                   ├── key: ()
      │    │                   ├── fd: ()-->(115)
      │    │                   ├── with-scan &3
      │    │                   │    ├── columns: th_st_id:115!null
      │    │                   │    ├── mapping:
      │    │                   │    │    └──  th_st_id_cast:96 => th_st_id:115
      │    │                   │    ├── cardinality: [1 - 1]
      │    │                   │    ├── key: ()
      │    │                   │    └── fd: ()-->(115)
      │    │                   └── filters (true)
      │    └── projections
      │         └── 1 [as="?column?":120]
      └── with &6 (insert_trade_request)
           ├── columns: "?column?":195!null
           ├── cardinality: [1 - 1]
           ├── volatile, mutations
           ├── key: ()
           ├── fd: ()-->(195)
           ├── project
           │    ├── columns: "?column?":194!null
           │    ├── cardinality: [1 - 1]
           │    ├── volatile, mutations
           │    ├── key: ()
           │    ├── fd: ()-->(194)
           │    ├── insert trade_request
           │    │    ├── columns: trade_request.tr_t_id:121!null
           │    │    ├── insert-mapping:
           │    │    │    ├── column1:129 => trade_request.tr_t_id:121
           │    │    │    ├── tr_tt_id_cast:135 => trade_request.tr_tt_id:122
           │    │    │    ├── tr_s_symb_cast:136 => trade_request.tr_s_symb:123
           │    │    │    ├── tr_qty_cast:137 => tr_qty:124
           │    │    │    ├── tr_bid_price_cast:138 => tr_bid_price:125
           │    │    │    └── column6:134 => trade_request.tr_b_id:126
           │    │    ├── return-mapping:
           │    │    │    └── column1:129 => trade_request.tr_t_id:121
           │    │    ├── check columns: check1:139 check2:140
           │    │    ├── partial index put columns: partial_index_put1:141
           │    │    ├── input binding: &5
           │    │    ├── cardinality: [1 - 1]
           │    │    ├── volatile, mutations
           │    │    ├── key: ()
           │    │    ├── fd: ()-->(121)
           │    │    ├── values
           │    │    │    ├── columns: column1:129!null column6:134!null tr_tt_id_cast:135!null tr_s_symb_cast:136!null tr_qty_cast:137!null tr_bid_price_cast:138!null check1:139!null check2:140!null partial_index_put1:141!null
           │    │    │    ├── cardinality: [1 - 1]
           │    │    │    ├── key: ()
           │    │    │    ├── fd: ()-->(129,134-141)
           │    │    │    └── (0, 0, 'TMB', 'SYMB', 10, 100.00, true, true, false)
           │    │    └── f-k-checks
           │    │         ├── f-k-checks-item: trade_request(tr_t_id) -> trade(t_id)
           │    │         │    └── anti-join (lookup trade)
           │    │         │         ├── columns: tr_t_id:142!null
           │    │         │         ├── key columns: [142] = [143]
           │    │         │         ├── lookup columns are key
           │    │         │         ├── cardinality: [0 - 1]
           │    │         │         ├── key: ()
           │    │         │         ├── fd: ()-->(142)
           │    │         │         ├── with-scan &5
           │    │         │         │    ├── columns: tr_t_id:142!null
           │    │         │         │    ├── mapping:
           │    │         │         │    │    └──  column1:129 => tr_t_id:142
           │    │         │         │    ├── cardinality: [1 - 1]
           │    │         │         │    ├── key: ()
           │    │         │         │    └── fd: ()-->(142)
           │    │         │         └── filters (true)
           │    │         ├── f-k-checks-item: trade_request(tr_tt_id) -> trade_type(tt_id)
           │    │         │    └── anti-join (lookup trade_type)
           │    │         │         ├── columns: tr_tt_id:160!null
           │    │         │         ├── key columns: [160] = [161]
           │    │         │         ├── lookup columns are key
           │    │         │         ├── cardinality: [0 - 1]
           │    │         │         ├── key: ()
           │    │         │         ├── fd: ()-->(160)
           │    │         │         ├── with-scan &5
           │    │         │         │    ├── columns: tr_tt_id:160!null
           │    │         │         │    ├── mapping:
           │    │         │         │    │    └──  tr_tt_id_cast:135 => tr_tt_id:160
           │    │         │         │    ├── cardinality: [1 - 1]
           │    │         │         │    ├── key: ()
           │    │         │         │    └── fd: ()-->(160)
           │    │         │         └── filters (true)
           │    │         ├── f-k-checks-item: trade_request(tr_s_symb) -> security(s_symb)
           │    │         │    └── anti-join (lookup security)
           │    │         │         ├── columns: tr_s_symb:167!null
           │    │         │         ├── key columns: [167] = [168]
           │    │         │         ├── lookup columns are key
           │    │         │         ├── cardinality: [0 - 1]
           │    │         │         ├── key: ()
           │    │         │         ├── fd: ()-->(167)
           │    │         │         ├── with-scan &5
           │    │         │         │    ├── columns: tr_s_symb:167!null
           │    │         │         │    ├── mapping:
           │    │         │         │    │    └──  tr_s_symb_cast:136 => tr_s_symb:167
           │    │         │         │    ├── cardinality: [1 - 1]
           │    │         │         │    ├── key: ()
           │    │         │         │    └── fd: ()-->(167)
           │    │         │         └── filters (true)
           │    │         └── f-k-checks-item: trade_request(tr_b_id) -> broker(b_id)
           │    │              └── anti-join (lookup broker)
           │    │                   ├── columns: tr_b_id:186!null
           │    │                   ├── key columns: [186] = [187]
           │    │                   ├── lookup columns are key
           │    │                   ├── cardinality: [0 - 1]
           │    │                   ├── key: ()
           │    │                   ├── fd: ()-->(186)
           │    │                   ├── with-scan &5
           │    │                   │    ├── columns: tr_b_id:186!null
           │    │                   │    ├── mapping:
           │    │                   │    │    └──  column6:134 => tr_b_id:186
           │    │                   │    ├── cardinality: [1 - 1]
           │    │                   │    ├── key: ()
           │    │                   │    └── fd: ()-->(186)
           │    │                   └── filters (true)
           │    └── projections
           │         └── 1 [as="?column?":194]
           └── values
                ├── columns: "?column?":195!null
                ├── cardinality: [1 - 1]
                ├── key: ()
                ├── fd: ()-->(195)
                └── (1,)

# --------------------------------------------------
# T8
# Trade-Result
# Emulates the process of completing a stock market trade. This is
# representative of a brokerage house receiving from the market exchange the
# final confirmation and price for the trade. The customer’s holdings are
# updated to reflect that the trade has completed. Estimates generated when the
# trade was ordered for the broker commission and other similar quantities are
# replaced with the actual numbers and historical information about the trade is
# recorded for later reference.
# --------------------------------------------------

# Q1
opt
    SELECT t_ca_id,
           t_tt_id,
           t_s_symb,
           t_qty,
           t_chrg::FLOAT8,
           t_lifo,
           t_is_cash,
           tt_name,
           tt_is_sell,
           tt_is_mrkt,
           COALESCE(hs_qty, 0)
      FROM trade
INNER JOIN trade_type      ON tt_id = t_tt_id
 LEFT JOIN holding_summary ON hs_ca_id = t_ca_id AND hs_s_symb = t_s_symb
     WHERE t_id = 0
FOR UPDATE OF trade;
----
project
 ├── columns: t_ca_id:9!null t_tt_id:4!null t_s_symb:6!null t_qty:7!null t_chrg:29!null t_lifo:15!null t_is_cash:5!null tt_name:19!null tt_is_sell:20!null tt_is_mrkt:21!null coalesce:30
 ├── cardinality: [0 - 1]
 ├── volatile
 ├── key: ()
 ├── fd: ()-->(4-7,9,15,19-21,29,30)
 ├── left-join (lookup holding_summary)
 │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null trade.t_chrg:12!null t_lifo:15!null tt_id:18!null tt_name:19!null tt_is_sell:20!null tt_is_mrkt:21!null hs_ca_id:24 hs_s_symb:25 hs_qty:26
 │    ├── key columns: [9 6] = [24 25]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 1]
 │    ├── volatile
 │    ├── key: ()
 │    ├── fd: ()-->(1,4-7,9,12,15,18-21,24-26), (4)==(18), (18)==(4)
 │    ├── inner-join (lookup trade_type)
 │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null trade.t_chrg:12!null t_lifo:15!null tt_id:18!null tt_name:19!null tt_is_sell:20!null tt_is_mrkt:21!null
 │    │    ├── key columns: [4] = [18]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── volatile
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1,4-7,9,12,15,18-21), (4)==(18), (18)==(4)
 │    │    ├── scan trade
 │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null trade.t_chrg:12!null t_lifo:15!null
 │    │    │    ├── constraint: /1: [/0 - /0]
 │    │    │    ├── locking: for-update
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── volatile
 │    │    │    ├── key: ()
 │    │    │    └── fd: ()-->(1,4-7,9,12,15)
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_chrg:12::FLOAT8 [as=t_chrg:29, outer=(12), immutable]
      └── COALESCE(hs_qty:26, 0) [as=coalesce:30, outer=(26)]

# Q1 (With Hints)
opt
           SELECT t_ca_id,
                  t_tt_id,
                  t_s_symb,
                  t_qty,
                  t_chrg::FLOAT8,
                  t_lifo,
                  t_is_cash,
                  tt_name,
                  tt_is_sell,
                  tt_is_mrkt,
                  COALESCE(hs_qty, 0)
             FROM trade
INNER LOOKUP JOIN trade_type      ON tt_id = t_tt_id
 LEFT LOOKUP JOIN holding_summary ON hs_ca_id = t_ca_id AND hs_s_symb = t_s_symb
            WHERE t_id = 0
       FOR UPDATE OF trade;
----
project
 ├── columns: t_ca_id:9!null t_tt_id:4!null t_s_symb:6!null t_qty:7!null t_chrg:29!null t_lifo:15!null t_is_cash:5!null tt_name:19!null tt_is_sell:20!null tt_is_mrkt:21!null coalesce:30
 ├── cardinality: [0 - 1]
 ├── volatile
 ├── key: ()
 ├── fd: ()-->(4-7,9,15,19-21,29,30)
 ├── left-join (lookup holding_summary)
 │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null trade.t_chrg:12!null t_lifo:15!null tt_id:18!null tt_name:19!null tt_is_sell:20!null tt_is_mrkt:21!null hs_ca_id:24 hs_s_symb:25 hs_qty:26
 │    ├── flags: force lookup join (into right side)
 │    ├── key columns: [9 6] = [24 25]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 1]
 │    ├── volatile
 │    ├── key: ()
 │    ├── fd: ()-->(1,4-7,9,12,15,18-21,24-26), (4)==(18), (18)==(4)
 │    ├── inner-join (lookup trade_type)
 │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null trade.t_chrg:12!null t_lifo:15!null tt_id:18!null tt_name:19!null tt_is_sell:20!null tt_is_mrkt:21!null
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [4] = [18]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── volatile
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1,4-7,9,12,15,18-21), (4)==(18), (18)==(4)
 │    │    ├── scan trade
 │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null trade.t_chrg:12!null t_lifo:15!null
 │    │    │    ├── constraint: /1: [/0 - /0]
 │    │    │    ├── locking: for-update
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── volatile
 │    │    │    ├── key: ()
 │    │    │    └── fd: ()-->(1,4-7,9,12,15)
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_chrg:12::FLOAT8 [as=t_chrg:29, outer=(12), immutable]
      └── COALESCE(hs_qty:26, 0) [as=coalesce:30, outer=(26)]

# Q2
opt
SELECT ca_b_id, ca_c_id, ca_tax_st FROM customer_account WHERE ca_id = 0;
----
project
 ├── columns: ca_b_id:2!null ca_c_id:3!null ca_tax_st:5!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(2,3,5)
 └── scan customer_account
      ├── columns: ca_id:1!null ca_b_id:2!null ca_c_id:3!null ca_tax_st:5!null
      ├── constraint: /1: [/0 - /0]
      ├── cardinality: [0 - 1]
      ├── key: ()
      └── fd: ()-->(1-3,5)

# Q3
opt
INSERT INTO holding_summary (hs_ca_id, hs_s_symb, hs_qty) VALUES (0, 'ROACH', 100);
----
insert holding_summary
 ├── columns: <none>
 ├── insert-mapping:
 │    ├── column1:6 => holding_summary.hs_ca_id:1
 │    ├── hs_s_symb_cast:9 => holding_summary.hs_s_symb:2
 │    └── hs_qty_cast:10 => hs_qty:3
 ├── input binding: &1
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── values
 │    ├── columns: column1:6!null hs_s_symb_cast:9!null hs_qty_cast:10!null
 │    ├── cardinality: [1 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(6,9,10)
 │    └── (0, 'ROACH', 100)
 └── f-k-checks
      ├── f-k-checks-item: holding_summary(hs_ca_id) -> customer_account(ca_id)
      │    └── anti-join (lookup customer_account)
      │         ├── columns: hs_ca_id:11!null
      │         ├── key columns: [11] = [12]
      │         ├── lookup columns are key
      │         ├── cardinality: [0 - 1]
      │         ├── key: ()
      │         ├── fd: ()-->(11)
      │         ├── with-scan &1
      │         │    ├── columns: hs_ca_id:11!null
      │         │    ├── mapping:
      │         │    │    └──  column1:6 => hs_ca_id:11
      │         │    ├── cardinality: [1 - 1]
      │         │    ├── key: ()
      │         │    └── fd: ()-->(11)
      │         └── filters (true)
      └── f-k-checks-item: holding_summary(hs_s_symb) -> security(s_symb)
           └── anti-join (lookup security)
                ├── columns: hs_s_symb:20!null
                ├── key columns: [20] = [21]
                ├── lookup columns are key
                ├── cardinality: [0 - 1]
                ├── key: ()
                ├── fd: ()-->(20)
                ├── with-scan &1
                │    ├── columns: hs_s_symb:20!null
                │    ├── mapping:
                │    │    └──  hs_s_symb_cast:9 => hs_s_symb:20
                │    ├── cardinality: [1 - 1]
                │    ├── key: ()
                │    └── fd: ()-->(20)
                └── filters (true)

# Q4
opt
UPDATE holding_summary
   SET hs_qty = hs_qty + 10
 WHERE hs_ca_id = 0 AND hs_s_symb = 'ROACH';
----
update holding_summary
 ├── columns: <none>
 ├── fetch columns: hs_ca_id:6 hs_s_symb:7 hs_qty:8
 ├── update-mapping:
 │    └── hs_qty_cast:12 => hs_qty:3
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: hs_qty_cast:12!null hs_ca_id:6!null hs_s_symb:7!null hs_qty:8!null
      ├── cardinality: [0 - 1]
      ├── immutable
      ├── key: ()
      ├── fd: ()-->(6-8,12)
      ├── scan holding_summary
      │    ├── columns: hs_ca_id:6!null hs_s_symb:7!null hs_qty:8!null
      │    ├── constraint: /6/7: [/0/'ROACH' - /0/'ROACH']
      │    ├── flags: avoid-full-scan
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(6-8)
      └── projections
           └── assignment-cast: INT4 [as=hs_qty_cast:12, outer=(8), immutable]
                └── hs_qty:8 + 10

# Q5
opt
  SELECT h_t_id, h_qty, h_price::FLOAT8
    FROM holding
   WHERE h_ca_id = 0 AND h_s_symb = 'ROACH'
ORDER BY h_dts DESC;
----
project
 ├── columns: h_t_id:1!null h_qty:6!null h_price:9!null  [hidden: h_dts:4!null]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(4,6,9)
 ├── ordering: -4
 ├── scan holding@holding_h_ca_id_h_s_symb_h_dts_idx,rev
 │    ├── columns: h_t_id:1!null h_ca_id:2!null h_s_symb:3!null h_dts:4!null holding.h_price:5!null h_qty:6!null
 │    ├── constraint: /2/3/4/1: [/0/'ROACH' - /0/'ROACH']
 │    ├── key: (1)
 │    ├── fd: ()-->(2,3), (1)-->(4-6)
 │    └── ordering: -4 opt(2,3) [actual: -4]
 └── projections
      └── holding.h_price:5::FLOAT8 [as=h_price:9, outer=(5), immutable]

# Q6
opt
  SELECT h_t_id, h_qty, h_price::FLOAT8
    FROM holding
   WHERE h_ca_id = 0 AND h_s_symb = 'ROACH'
ORDER BY h_dts ASC;
----
project
 ├── columns: h_t_id:1!null h_qty:6!null h_price:9!null  [hidden: h_dts:4!null]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(4,6,9)
 ├── ordering: +4
 ├── scan holding@holding_h_ca_id_h_s_symb_h_dts_idx
 │    ├── columns: h_t_id:1!null h_ca_id:2!null h_s_symb:3!null h_dts:4!null holding.h_price:5!null h_qty:6!null
 │    ├── constraint: /2/3/4/1: [/0/'ROACH' - /0/'ROACH']
 │    ├── key: (1)
 │    ├── fd: ()-->(2,3), (1)-->(4-6)
 │    └── ordering: +4 opt(2,3) [actual: +4]
 └── projections
      └── holding.h_price:5::FLOAT8 [as=h_price:9, outer=(5), immutable]

# Q7
opt
INSERT INTO holding_history (hh_h_t_id, hh_t_id, hh_before_qty, hh_after_qty)
(
  SELECT unnest(ARRAY[1, 2, 3]:::INT8[]),
  0,
  unnest(ARRAY[10, 20, 30]:::INT4[]),
  unnest(ARRAY[20, 30, 40]:::INT4[])
);
----
insert holding_history
 ├── columns: <none>
 ├── insert-mapping:
 │    ├── unnest:7 => holding_history.hh_h_t_id:1
 │    ├── "?column?":10 => holding_history.hh_t_id:2
 │    ├── unnest:8 => hh_before_qty:3
 │    └── unnest:9 => hh_after_qty:4
 ├── input binding: &1
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── project
 │    ├── columns: "?column?":10!null unnest:7!null unnest:8!null unnest:9!null
 │    ├── cardinality: [3 - 3]
 │    ├── fd: ()-->(10)
 │    ├── values
 │    │    ├── columns: unnest:7!null unnest:8!null unnest:9!null
 │    │    ├── cardinality: [3 - 3]
 │    │    ├── (1, 10, 20)
 │    │    ├── (2, 20, 30)
 │    │    └── (3, 30, 40)
 │    └── projections
 │         └── 0 [as="?column?":10]
 └── f-k-checks
      ├── f-k-checks-item: holding_history(hh_h_t_id) -> trade(t_id)
      │    └── anti-join (lookup trade)
      │         ├── columns: hh_h_t_id:11!null
      │         ├── key columns: [11] = [12]
      │         ├── lookup columns are key
      │         ├── cardinality: [0 - 3]
      │         ├── with-scan &1
      │         │    ├── columns: hh_h_t_id:11!null
      │         │    ├── mapping:
      │         │    │    └──  unnest:7 => hh_h_t_id:11
      │         │    └── cardinality: [3 - 3]
      │         └── filters (true)
      └── f-k-checks-item: holding_history(hh_t_id) -> trade(t_id)
           └── anti-join (lookup trade)
                ├── columns: hh_t_id:29!null
                ├── key columns: [29] = [30]
                ├── lookup columns are key
                ├── cardinality: [0 - 3]
                ├── fd: ()-->(29)
                ├── with-scan &1
                │    ├── columns: hh_t_id:29!null
                │    ├── mapping:
                │    │    └──  "?column?":10 => hh_t_id:29
                │    ├── cardinality: [3 - 3]
                │    └── fd: ()-->(29)
                └── filters (true)

# Q8
opt
UPDATE holding SET h_qty = h_qty + 10 WHERE h_t_id = 0
----
update holding
 ├── columns: <none>
 ├── fetch columns: h_t_id:9 h_ca_id:10 h_s_symb:11 h_dts:12 h_price:13 h_qty:14
 ├── update-mapping:
 │    └── h_qty_cast:18 => h_qty:6
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: h_qty_cast:18!null h_t_id:9!null h_ca_id:10!null h_s_symb:11!null h_dts:12!null h_price:13!null h_qty:14!null
      ├── cardinality: [0 - 1]
      ├── immutable
      ├── key: ()
      ├── fd: ()-->(9-14,18)
      ├── scan holding
      │    ├── columns: h_t_id:9!null h_ca_id:10!null h_s_symb:11!null h_dts:12!null h_price:13!null h_qty:14!null
      │    ├── constraint: /9: [/0 - /0]
      │    ├── flags: avoid-full-scan
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(9-14)
      └── projections
           └── assignment-cast: INT4 [as=h_qty_cast:18, outer=(14), immutable]
                └── h_qty:14 + 10

# Q9
opt
DELETE FROM holding WHERE h_t_id = ANY ARRAY [0, 1, 2, 3, 4, 5];
----
delete holding
 ├── columns: <none>
 ├── fetch columns: h_t_id:9 h_ca_id:10 h_s_symb:11 h_dts:12
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── scan holding
      ├── columns: h_t_id:9!null h_ca_id:10!null h_s_symb:11!null h_dts:12!null
      ├── constraint: /9: [/0 - /5]
      ├── flags: avoid-full-scan
      ├── cardinality: [0 - 6]
      ├── key: (9)
      └── fd: (9)-->(10-12)

# Q10
opt
INSERT
INTO holding (h_t_id, h_ca_id, h_s_symb, h_dts, h_price, h_qty)
VALUES
(
  0,
  0,
  'ROACH',
  '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP,
  100.00:::FLOAT8::DECIMAL,
  10
);
----
insert holding
 ├── columns: <none>
 ├── insert-mapping:
 │    ├── column1:9 => holding.h_t_id:1
 │    ├── column2:10 => holding.h_ca_id:2
 │    ├── h_s_symb_cast:15 => holding.h_s_symb:3
 │    ├── column4:12 => h_dts:4
 │    ├── h_price_cast:16 => h_price:5
 │    └── h_qty_cast:17 => h_qty:6
 ├── check columns: check1:18
 ├── input binding: &1
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── values
 │    ├── columns: column1:9!null column2:10!null column4:12!null h_s_symb_cast:15!null h_price_cast:16!null h_qty_cast:17!null check1:18!null
 │    ├── cardinality: [1 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(9,10,12,15-18)
 │    └── (0, 0, '2020-06-15 22:27:42.148484', 'ROACH', 100.00, 10, true)
 └── f-k-checks
      ├── f-k-checks-item: holding(h_t_id) -> trade(t_id)
      │    └── anti-join (lookup trade)
      │         ├── columns: h_t_id:19!null
      │         ├── key columns: [19] = [20]
      │         ├── lookup columns are key
      │         ├── cardinality: [0 - 1]
      │         ├── key: ()
      │         ├── fd: ()-->(19)
      │         ├── with-scan &1
      │         │    ├── columns: h_t_id:19!null
      │         │    ├── mapping:
      │         │    │    └──  column1:9 => h_t_id:19
      │         │    ├── cardinality: [1 - 1]
      │         │    ├── key: ()
      │         │    └── fd: ()-->(19)
      │         └── filters (true)
      └── f-k-checks-item: holding(h_ca_id,h_s_symb) -> holding_summary(hs_ca_id,hs_s_symb)
           └── anti-join (lookup holding_summary)
                ├── columns: h_ca_id:37!null h_s_symb:38!null
                ├── key columns: [37 38] = [39 40]
                ├── lookup columns are key
                ├── cardinality: [0 - 1]
                ├── key: ()
                ├── fd: ()-->(37,38)
                ├── with-scan &1
                │    ├── columns: h_ca_id:37!null h_s_symb:38!null
                │    ├── mapping:
                │    │    ├──  column2:10 => h_ca_id:37
                │    │    └──  h_s_symb_cast:15 => h_s_symb:38
                │    ├── cardinality: [1 - 1]
                │    ├── key: ()
                │    └── fd: ()-->(37,38)
                └── filters (true)

# Q11
opt
DELETE FROM holding_summary WHERE hs_ca_id = 0 AND hs_s_symb = 'ROACH';
----
delete holding_summary
 ├── columns: <none>
 ├── fetch columns: holding_summary.hs_ca_id:6 holding_summary.hs_s_symb:7
 ├── input binding: &1
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── scan holding_summary
 │    ├── columns: holding_summary.hs_ca_id:6!null holding_summary.hs_s_symb:7!null
 │    ├── constraint: /6/7: [/0/'ROACH' - /0/'ROACH']
 │    ├── flags: avoid-full-scan
 │    ├── cardinality: [0 - 1]
 │    ├── key: ()
 │    └── fd: ()-->(6,7)
 └── f-k-checks
      └── f-k-checks-item: holding(h_ca_id,h_s_symb) -> holding_summary(hs_ca_id,hs_s_symb)
           └── semi-join (lookup holding@holding_h_ca_id_h_s_symb_h_dts_idx)
                ├── columns: hs_ca_id:11!null hs_s_symb:12!null
                ├── key columns: [11 12] = [14 15]
                ├── cardinality: [0 - 1]
                ├── key: ()
                ├── fd: ()-->(11,12)
                ├── with-scan &1
                │    ├── columns: hs_ca_id:11!null hs_s_symb:12!null
                │    ├── mapping:
                │    │    ├──  holding_summary.hs_ca_id:6 => hs_ca_id:11
                │    │    └──  holding_summary.hs_s_symb:7 => hs_s_symb:12
                │    ├── cardinality: [0 - 1]
                │    ├── key: ()
                │    └── fd: ()-->(11,12)
                └── filters (true)

# Q12
opt
   UPDATE trade
      SET t_tax = 20.00:::FLOAT8::DECIMAL
                * (
                        SELECT sum(tx_rate)
                          FROM taxrate
                         WHERE tx_id IN (SELECT cx_tx_id FROM customer_taxrate WHERE cx_c_id = 0)
                    )
    WHERE t_id = 0
RETURNING t_tax::FLOAT8;
----
project
 ├── columns: t_tax:53!null
 ├── cardinality: [0 - 1]
 ├── volatile, mutations
 ├── key: ()
 ├── fd: ()-->(53)
 ├── update trade
 │    ├── columns: t_id:1!null trade.t_tax:14!null
 │    ├── fetch columns: t_id:18 trade.t_tax:31
 │    ├── update-mapping:
 │    │    └── t_tax_cast:47 => trade.t_tax:14
 │    ├── return-mapping:
 │    │    ├── t_id:18 => t_id:1
 │    │    └── t_tax_cast:47 => trade.t_tax:14
 │    ├── check columns: check5:52
 │    ├── cardinality: [0 - 1]
 │    ├── volatile, mutations
 │    ├── key: ()
 │    ├── fd: ()-->(1,14)
 │    └── project
 │         ├── columns: check5:52 t_id:18!null trade.t_tax:31!null t_tax_cast:47
 │         ├── cardinality: [0 - 1]
 │         ├── immutable
 │         ├── key: ()
 │         ├── fd: ()-->(18,31,47,52)
 │         ├── project
 │         │    ├── columns: t_tax_cast:47 t_id:18!null trade.t_tax:31!null
 │         │    ├── cardinality: [0 - 1]
 │         │    ├── immutable
 │         │    ├── key: ()
 │         │    ├── fd: ()-->(18,31,47)
 │         │    ├── scan trade
 │         │    │    ├── columns: t_id:18!null trade.t_tax:31!null
 │         │    │    ├── constraint: /18: [/0 - /0]
 │         │    │    ├── flags: avoid-full-scan
 │         │    │    ├── cardinality: [0 - 1]
 │         │    │    ├── key: ()
 │         │    │    └── fd: ()-->(18,31)
 │         │    └── projections
 │         │         └── assignment-cast: DECIMAL(10,2) [as=t_tax_cast:47, immutable, subquery]
 │         │              └── mult
 │         │                   ├── subquery
 │         │                   │    └── scalar-group-by
 │         │                   │         ├── columns: sum:45
 │         │                   │         ├── cardinality: [1 - 1]
 │         │                   │         ├── key: ()
 │         │                   │         ├── fd: ()-->(45)
 │         │                   │         ├── inner-join (lookup taxrate)
 │         │                   │         │    ├── columns: tx_id:35!null tx_rate:37!null cx_tx_id:40!null cx_c_id:41!null
 │         │                   │         │    ├── key columns: [40] = [35]
 │         │                   │         │    ├── lookup columns are key
 │         │                   │         │    ├── key: (40)
 │         │                   │         │    ├── fd: ()-->(41), (35)-->(37), (35)==(40), (40)==(35)
 │         │                   │         │    ├── scan customer_taxrate@customer_taxrate_cx_c_id_idx
 │         │                   │         │    │    ├── columns: cx_tx_id:40!null cx_c_id:41!null
 │         │                   │         │    │    ├── constraint: /41/40: [/0 - /0]
 │         │                   │         │    │    ├── key: (40)
 │         │                   │         │    │    └── fd: ()-->(41)
 │         │                   │         │    └── filters (true)
 │         │                   │         └── aggregations
 │         │                   │              └── sum [as=sum:45, outer=(37)]
 │         │                   │                   └── tx_rate:37
 │         │                   └── 2E+1
 │         └── projections
 │              └── t_tax_cast:47 >= 0 [as=check5:52, outer=(47), immutable]
 └── projections
      └── trade.t_tax:14::FLOAT8 [as=t_tax:53, outer=(14), immutable]

# Q13
opt
SELECT s_name, cr_rate::FLOAT8
  FROM customer, security, commission_rate
 WHERE c_id = 0
   AND s_symb = 'ROACH'
   AND cr_tt_id = 'TLS'
   AND cr_from_qty <= 100
   AND cr_to_qty >= 200
   AND cr_ex_id = s_ex_id
   AND cr_c_tier = c_tier
 LIMIT 1;
----
project
 ├── columns: s_name:30!null cr_rate:53!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(30,53)
 ├── limit
 │    ├── columns: c_id:1!null c_tier:8!null s_symb:27!null s_name:30!null s_ex_id:31!null cr_c_tier:45!null cr_tt_id:46!null cr_ex_id:47!null cr_from_qty:48!null cr_to_qty:49!null commission_rate.cr_rate:50!null
 │    ├── cardinality: [0 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(1,8,27,30,31,45-50), (31)==(47), (47)==(31), (8)==(45), (45)==(8)
 │    ├── inner-join (merge)
 │    │    ├── columns: c_id:1!null c_tier:8!null s_symb:27!null s_name:30!null s_ex_id:31!null cr_c_tier:45!null cr_tt_id:46!null cr_ex_id:47!null cr_from_qty:48!null cr_to_qty:49!null commission_rate.cr_rate:50!null
 │    │    ├── left ordering: +31
 │    │    ├── right ordering: +47
 │    │    ├── key: (48)
 │    │    ├── fd: ()-->(1,8,27,30,31,45-47), (48)-->(49,50), (31)==(47), (47)==(31), (8)==(45), (45)==(8)
 │    │    ├── limit hint: 1.00
 │    │    ├── scan security
 │    │    │    ├── columns: s_symb:27!null s_name:30!null s_ex_id:31!null
 │    │    │    ├── constraint: /27: [/'ROACH' - /'ROACH']
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    └── fd: ()-->(27,30,31)
 │    │    ├── inner-join (lookup commission_rate)
 │    │    │    ├── columns: c_id:1!null c_tier:8!null cr_c_tier:45!null cr_tt_id:46!null cr_ex_id:47!null cr_from_qty:48!null cr_to_qty:49!null commission_rate.cr_rate:50!null
 │    │    │    ├── key columns: [8 56] = [45 46]
 │    │    │    ├── key: (47,48)
 │    │    │    ├── fd: ()-->(1,8,45,46), (47,48)-->(49,50), (8)==(45), (45)==(8)
 │    │    │    ├── ordering: +47 opt(1,8,45,46) [actual: +47]
 │    │    │    ├── project
 │    │    │    │    ├── columns: "lookup_join_const_col_@46":56!null c_id:1!null c_tier:8!null
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    ├── fd: ()-->(1,8,56)
 │    │    │    │    ├── scan customer
 │    │    │    │    │    ├── columns: c_id:1!null c_tier:8!null
 │    │    │    │    │    ├── constraint: /1: [/0 - /0]
 │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    ├── key: ()
 │    │    │    │    │    └── fd: ()-->(1,8)
 │    │    │    │    └── projections
 │    │    │    │         └── 'TLS' [as="lookup_join_const_col_@46":56]
 │    │    │    └── filters
 │    │    │         ├── cr_from_qty:48 <= 100 [outer=(48), constraints=(/48: (/NULL - /100]; tight)]
 │    │    │         └── cr_to_qty:49 >= 200 [outer=(49), constraints=(/49: [/200 - ]; tight)]
 │    │    └── filters (true)
 │    └── 1
 └── projections
      └── commission_rate.cr_rate:50::FLOAT8 [as=cr_rate:53, outer=(50), immutable]

# Q13 (With Hints)
opt
           SELECT s_name, cr_rate::FLOAT8
             FROM customer
       CROSS JOIN security
INNER LOOKUP JOIN commission_rate ON cr_c_tier = c_tier AND cr_ex_id = s_ex_id
            WHERE c_id = 0
              AND s_symb = 'ROACH'
              AND cr_tt_id = 'TLS'
              AND cr_from_qty <= 100
              AND cr_to_qty >= 200
            LIMIT 1;
----
project
 ├── columns: s_name:30!null cr_rate:53!null
 ├── cardinality: [0 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(30,53)
 ├── limit
 │    ├── columns: c_id:1!null c_tier:8!null s_symb:27!null s_name:30!null s_ex_id:31!null cr_c_tier:45!null cr_tt_id:46!null cr_ex_id:47!null cr_from_qty:48!null cr_to_qty:49!null commission_rate.cr_rate:50!null
 │    ├── cardinality: [0 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(1,8,27,30,31,45-50), (8)==(45), (45)==(8), (31)==(47), (47)==(31)
 │    ├── inner-join (lookup commission_rate)
 │    │    ├── columns: c_id:1!null c_tier:8!null s_symb:27!null s_name:30!null s_ex_id:31!null cr_c_tier:45!null cr_tt_id:46!null cr_ex_id:47!null cr_from_qty:48!null cr_to_qty:49!null commission_rate.cr_rate:50!null
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── lookup expression
 │    │    │    └── filters
 │    │    │         ├── cr_from_qty:48 <= 100 [outer=(48), constraints=(/48: (/NULL - /100]; tight)]
 │    │    │         ├── c_tier:8 = cr_c_tier:45 [outer=(8,45), constraints=(/8: (/NULL - ]; /45: (/NULL - ]), fd=(8)==(45), (45)==(8)]
 │    │    │         ├── "lookup_join_const_col_@46":54 = cr_tt_id:46 [outer=(46,54), constraints=(/46: (/NULL - ]; /54: (/NULL - ]), fd=(46)==(54), (54)==(46)]
 │    │    │         └── s_ex_id:31 = cr_ex_id:47 [outer=(31,47), constraints=(/31: (/NULL - ]; /47: (/NULL - ]), fd=(31)==(47), (47)==(31)]
 │    │    ├── key: (48)
 │    │    ├── fd: ()-->(1,8,27,30,31,45-47), (48)-->(49,50), (8)==(45), (45)==(8), (31)==(47), (47)==(31)
 │    │    ├── limit hint: 1.00
 │    │    ├── project
 │    │    │    ├── columns: "lookup_join_const_col_@46":54!null c_id:1!null c_tier:8!null s_symb:27!null s_name:30!null s_ex_id:31!null
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(1,8,27,30,31,54)
 │    │    │    ├── inner-join (cross)
 │    │    │    │    ├── columns: c_id:1!null c_tier:8!null s_symb:27!null s_name:30!null s_ex_id:31!null
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-one)
 │    │    │    │    ├── key: ()
 │    │    │    │    ├── fd: ()-->(1,8,27,30,31)
 │    │    │    │    ├── scan security
 │    │    │    │    │    ├── columns: s_symb:27!null s_name:30!null s_ex_id:31!null
 │    │    │    │    │    ├── constraint: /27: [/'ROACH' - /'ROACH']
 │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    ├── key: ()
 │    │    │    │    │    └── fd: ()-->(27,30,31)
 │    │    │    │    ├── scan customer
 │    │    │    │    │    ├── columns: c_id:1!null c_tier:8!null
 │    │    │    │    │    ├── constraint: /1: [/0 - /0]
 │    │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    │    ├── key: ()
 │    │    │    │    │    └── fd: ()-->(1,8)
 │    │    │    │    └── filters (true)
 │    │    │    └── projections
 │    │    │         └── 'TLS' [as="lookup_join_const_col_@46":54]
 │    │    └── filters
 │    │         └── cr_to_qty:49 >= 200 [outer=(49), constraints=(/49: [/200 - ]; tight)]
 │    └── 1
 └── projections
      └── commission_rate.cr_rate:50::FLOAT8 [as=cr_rate:53, outer=(50), immutable]

# Q14
opt
WITH
update_trade_commission AS (
       UPDATE trade
          SET t_comm = 50.00:::FLOAT8::DECIMAL,
              t_dts = '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP,
              t_st_id = 'ACTV',
              t_trade_price = 100.00:::FLOAT8::DECIMAL
        WHERE t_id = 0
    RETURNING 1
),
update_broker_commission AS (
       UPDATE broker
          SET b_comm_total = b_comm_total + 50.00:::FLOAT8::DECIMAL,
              b_num_trades = b_num_trades + 1
        WHERE b_id = 0
    RETURNING 1
),
insert_trade_history AS (
       INSERT INTO trade_history (th_t_id, th_dts, th_st_id)
       VALUES (0, '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP, 'ACTV')
    RETURNING 1
)
SELECT 1
----
with &2 (update_trade_commission)
 ├── columns: "?column?":104!null
 ├── cardinality: [1 - 1]
 ├── volatile, mutations
 ├── key: ()
 ├── fd: ()-->(104)
 ├── project
 │    ├── columns: "?column?":52!null
 │    ├── cardinality: [0 - 1]
 │    ├── volatile, mutations
 │    ├── key: ()
 │    ├── fd: ()-->(52)
 │    ├── update trade
 │    │    ├── columns: t_id:1!null
 │    │    ├── fetch columns: t_id:18 t_dts:19 trade.t_st_id:20 t_tt_id:21 t_is_cash:22 t_s_symb:23 t_qty:24 t_bid_price:25 t_ca_id:26 t_exec_name:27 t_trade_price:28 t_chrg:29 t_comm:30 t_lifo:32
 │    │    ├── update-mapping:
 │    │    │    ├── t_dts_new:36 => t_dts:2
 │    │    │    ├── t_st_id_cast:39 => trade.t_st_id:3
 │    │    │    ├── t_trade_price_cast:40 => t_trade_price:11
 │    │    │    └── t_comm_cast:41 => t_comm:13
 │    │    ├── return-mapping:
 │    │    │    └── t_id:18 => t_id:1
 │    │    ├── check columns: check4:45
 │    │    ├── input binding: &1
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── volatile, mutations
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1)
 │    │    ├── project
 │    │    │    ├── columns: check4:45!null t_comm_cast:41!null t_st_id_cast:39!null t_trade_price_cast:40!null t_dts_new:36!null t_id:18!null t_dts:19!null trade.t_st_id:20!null t_tt_id:21!null t_is_cash:22!null t_s_symb:23!null t_qty:24!null t_bid_price:25!null t_ca_id:26!null t_exec_name:27!null t_trade_price:28 t_chrg:29!null t_comm:30!null t_lifo:32!null
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(18-30,32,36,39-41,45)
 │    │    │    ├── scan trade
 │    │    │    │    ├── columns: t_id:18!null t_dts:19!null trade.t_st_id:20!null t_tt_id:21!null t_is_cash:22!null t_s_symb:23!null t_qty:24!null t_bid_price:25!null t_ca_id:26!null t_exec_name:27!null t_trade_price:28 t_chrg:29!null t_comm:30!null t_lifo:32!null
 │    │    │    │    ├── constraint: /18: [/0 - /0]
 │    │    │    │    ├── flags: avoid-full-scan
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    └── fd: ()-->(18-30,32)
 │    │    │    └── projections
 │    │    │         ├── true [as=check4:45]
 │    │    │         ├── 50.00 [as=t_comm_cast:41]
 │    │    │         ├── 'ACTV' [as=t_st_id_cast:39]
 │    │    │         ├── 100.00 [as=t_trade_price_cast:40]
 │    │    │         └── '2020-06-15 22:27:42.148484' [as=t_dts_new:36]
 │    │    └── f-k-checks
 │    │         └── f-k-checks-item: trade(t_st_id) -> status_type(st_id)
 │    │              └── anti-join (lookup status_type)
 │    │                   ├── columns: t_st_id:47!null
 │    │                   ├── key columns: [47] = [48]
 │    │                   ├── lookup columns are key
 │    │                   ├── cardinality: [0 - 1]
 │    │                   ├── key: ()
 │    │                   ├── fd: ()-->(47)
 │    │                   ├── with-scan &1
 │    │                   │    ├── columns: t_st_id:47!null
 │    │                   │    ├── mapping:
 │    │                   │    │    └──  t_st_id_cast:39 => t_st_id:47
 │    │                   │    ├── cardinality: [0 - 1]
 │    │                   │    ├── key: ()
 │    │                   │    └── fd: ()-->(47)
 │    │                   └── filters (true)
 │    └── projections
 │         └── 1 [as="?column?":52]
 └── with &3 (update_broker_commission)
      ├── columns: "?column?":104!null
      ├── cardinality: [1 - 1]
      ├── volatile, mutations
      ├── key: ()
      ├── fd: ()-->(104)
      ├── project
      │    ├── columns: "?column?":70!null
      │    ├── cardinality: [0 - 1]
      │    ├── volatile, mutations
      │    ├── key: ()
      │    ├── fd: ()-->(70)
      │    ├── update broker
      │    │    ├── columns: b_id:53!null
      │    │    ├── fetch columns: b_id:60 b_num_trades:63 b_comm_total:64
      │    │    ├── update-mapping:
      │    │    │    ├── b_num_trades_new:68 => b_num_trades:56
      │    │    │    └── b_comm_total_cast:69 => b_comm_total:57
      │    │    ├── return-mapping:
      │    │    │    └── b_id:60 => b_id:53
      │    │    ├── cardinality: [0 - 1]
      │    │    ├── volatile, mutations
      │    │    ├── key: ()
      │    │    ├── fd: ()-->(53)
      │    │    └── project
      │    │         ├── columns: b_comm_total_cast:69!null b_num_trades_new:68!null b_id:60!null b_num_trades:63!null b_comm_total:64!null
      │    │         ├── cardinality: [0 - 1]
      │    │         ├── immutable
      │    │         ├── key: ()
      │    │         ├── fd: ()-->(60,63,64,68,69)
      │    │         ├── scan broker
      │    │         │    ├── columns: b_id:60!null b_num_trades:63!null b_comm_total:64!null
      │    │         │    ├── constraint: /60: [/0 - /0]
      │    │         │    ├── flags: avoid-full-scan
      │    │         │    ├── cardinality: [0 - 1]
      │    │         │    ├── key: ()
      │    │         │    └── fd: ()-->(60,63,64)
      │    │         └── projections
      │    │              ├── assignment-cast: DECIMAL(12,2) [as=b_comm_total_cast:69, outer=(64), immutable]
      │    │              │    └── b_comm_total:64 + 5E+1
      │    │              └── b_num_trades:63 + 1 [as=b_num_trades_new:68, outer=(63), immutable]
      │    └── projections
      │         └── 1 [as="?column?":70]
      └── with &5 (insert_trade_history)
           ├── columns: "?column?":104!null
           ├── cardinality: [1 - 1]
           ├── volatile, mutations
           ├── key: ()
           ├── fd: ()-->(104)
           ├── project
           │    ├── columns: "?column?":103!null
           │    ├── cardinality: [1 - 1]
           │    ├── volatile, mutations
           │    ├── key: ()
           │    ├── fd: ()-->(103)
           │    ├── insert trade_history
           │    │    ├── columns: trade_history.th_t_id:71!null trade_history.th_st_id:73!null
           │    │    ├── insert-mapping:
           │    │    │    ├── column1:76 => trade_history.th_t_id:71
           │    │    │    ├── column2:77 => th_dts:72
           │    │    │    └── th_st_id_cast:79 => trade_history.th_st_id:73
           │    │    ├── return-mapping:
           │    │    │    ├── column1:76 => trade_history.th_t_id:71
           │    │    │    └── th_st_id_cast:79 => trade_history.th_st_id:73
           │    │    ├── input binding: &4
           │    │    ├── cardinality: [1 - 1]
           │    │    ├── volatile, mutations
           │    │    ├── key: ()
           │    │    ├── fd: ()-->(71,73)
           │    │    ├── values
           │    │    │    ├── columns: column1:76!null column2:77!null th_st_id_cast:79!null
           │    │    │    ├── cardinality: [1 - 1]
           │    │    │    ├── key: ()
           │    │    │    ├── fd: ()-->(76,77,79)
           │    │    │    └── (0, '2020-06-15 22:27:42.148484', 'ACTV')
           │    │    └── f-k-checks
           │    │         ├── f-k-checks-item: trade_history(th_t_id) -> trade(t_id)
           │    │         │    └── anti-join (lookup trade)
           │    │         │         ├── columns: th_t_id:80!null
           │    │         │         ├── key columns: [80] = [81]
           │    │         │         ├── lookup columns are key
           │    │         │         ├── cardinality: [0 - 1]
           │    │         │         ├── key: ()
           │    │         │         ├── fd: ()-->(80)
           │    │         │         ├── with-scan &4
           │    │         │         │    ├── columns: th_t_id:80!null
           │    │         │         │    ├── mapping:
           │    │         │         │    │    └──  column1:76 => th_t_id:80
           │    │         │         │    ├── cardinality: [1 - 1]
           │    │         │         │    ├── key: ()
           │    │         │         │    └── fd: ()-->(80)
           │    │         │         └── filters (true)
           │    │         └── f-k-checks-item: trade_history(th_st_id) -> status_type(st_id)
           │    │              └── anti-join (lookup status_type)
           │    │                   ├── columns: th_st_id:98!null
           │    │                   ├── key columns: [98] = [99]
           │    │                   ├── lookup columns are key
           │    │                   ├── cardinality: [0 - 1]
           │    │                   ├── key: ()
           │    │                   ├── fd: ()-->(98)
           │    │                   ├── with-scan &4
           │    │                   │    ├── columns: th_st_id:98!null
           │    │                   │    ├── mapping:
           │    │                   │    │    └──  th_st_id_cast:79 => th_st_id:98
           │    │                   │    ├── cardinality: [1 - 1]
           │    │                   │    ├── key: ()
           │    │                   │    └── fd: ()-->(98)
           │    │                   └── filters (true)
           │    └── projections
           │         └── 1 [as="?column?":103]
           └── values
                ├── columns: "?column?":104!null
                ├── cardinality: [1 - 1]
                ├── key: ()
                ├── fd: ()-->(104)
                └── (1,)


# Q15
opt
WITH
insert_settlement AS (
       INSERT
         INTO settlement (se_t_id, se_cash_type, se_cash_due_date, se_amt)
       VALUES (0, 'Margin', '2020-06-15'::DATE, 100.00:::FLOAT8::DECIMAL)
    RETURNING 1
),
insert_cash_transaction AS (
       INSERT
         INTO cash_transaction (ct_t_id, ct_dts, ct_amt, ct_name)
       VALUES (
               0,
               '2020-06-10 22:27:42.148484+00:00'::TIMESTAMP,
               100.00:::FLOAT8::DECIMAL,
               concat_ws(' ', 'Buy', 2:::INT4::STRING, 'shares of', 'ROACH')
              )
    RETURNING 1
)
UPDATE customer_account
SET ca_bal = ca_bal + 100.00:::FLOAT8::DECIMAL
WHERE ca_id = 0
RETURNING ca_bal::FLOAT8;
----
with &2 (insert_settlement)
 ├── columns: ca_bal:82!null
 ├── cardinality: [0 - 1]
 ├── volatile, mutations
 ├── key: ()
 ├── fd: ()-->(82)
 ├── project
 │    ├── columns: "?column?":31!null
 │    ├── cardinality: [1 - 1]
 │    ├── volatile, mutations
 │    ├── key: ()
 │    ├── fd: ()-->(31)
 │    ├── insert settlement
 │    │    ├── columns: settlement.se_t_id:1!null
 │    │    ├── insert-mapping:
 │    │    │    ├── column1:7 => settlement.se_t_id:1
 │    │    │    ├── se_cash_type_cast:11 => se_cash_type:2
 │    │    │    ├── column3:9 => se_cash_due_date:3
 │    │    │    └── se_amt_cast:12 => se_amt:4
 │    │    ├── return-mapping:
 │    │    │    └── column1:7 => settlement.se_t_id:1
 │    │    ├── input binding: &1
 │    │    ├── cardinality: [1 - 1]
 │    │    ├── volatile, mutations
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1)
 │    │    ├── values
 │    │    │    ├── columns: column1:7!null column3:9!null se_cash_type_cast:11!null se_amt_cast:12!null
 │    │    │    ├── cardinality: [1 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(7,9,11,12)
 │    │    │    └── (0, '2020-06-15', 'Margin', 100.00)
 │    │    └── f-k-checks
 │    │         └── f-k-checks-item: settlement(se_t_id) -> trade(t_id)
 │    │              └── anti-join (lookup trade)
 │    │                   ├── columns: se_t_id:13!null
 │    │                   ├── key columns: [13] = [14]
 │    │                   ├── lookup columns are key
 │    │                   ├── cardinality: [0 - 1]
 │    │                   ├── key: ()
 │    │                   ├── fd: ()-->(13)
 │    │                   ├── with-scan &1
 │    │                   │    ├── columns: se_t_id:13!null
 │    │                   │    ├── mapping:
 │    │                   │    │    └──  column1:7 => se_t_id:13
 │    │                   │    ├── cardinality: [1 - 1]
 │    │                   │    ├── key: ()
 │    │                   │    └── fd: ()-->(13)
 │    │                   └── filters (true)
 │    └── projections
 │         └── 1 [as="?column?":31]
 └── with &4 (insert_cash_transaction)
      ├── columns: ca_bal:82!null
      ├── cardinality: [0 - 1]
      ├── volatile, mutations
      ├── key: ()
      ├── fd: ()-->(82)
      ├── project
      │    ├── columns: "?column?":62!null
      │    ├── cardinality: [1 - 1]
      │    ├── volatile, mutations
      │    ├── key: ()
      │    ├── fd: ()-->(62)
      │    ├── insert cash_transaction
      │    │    ├── columns: cash_transaction.ct_t_id:32!null
      │    │    ├── insert-mapping:
      │    │    │    ├── column1:38 => cash_transaction.ct_t_id:32
      │    │    │    ├── column2:39 => ct_dts:33
      │    │    │    ├── ct_amt_cast:42 => ct_amt:34
      │    │    │    └── ct_name_cast:43 => ct_name:35
      │    │    ├── return-mapping:
      │    │    │    └── column1:38 => cash_transaction.ct_t_id:32
      │    │    ├── input binding: &3
      │    │    ├── cardinality: [1 - 1]
      │    │    ├── volatile, mutations
      │    │    ├── key: ()
      │    │    ├── fd: ()-->(32)
      │    │    ├── values
      │    │    │    ├── columns: column1:38!null column2:39!null ct_amt_cast:42!null ct_name_cast:43!null
      │    │    │    ├── cardinality: [1 - 1]
      │    │    │    ├── key: ()
      │    │    │    ├── fd: ()-->(38,39,42,43)
      │    │    │    └── (0, '2020-06-10 22:27:42.148484', 100.00, 'Buy 2 shares of ROACH')
      │    │    └── f-k-checks
      │    │         └── f-k-checks-item: cash_transaction(ct_t_id) -> trade(t_id)
      │    │              └── anti-join (lookup trade)
      │    │                   ├── columns: ct_t_id:44!null
      │    │                   ├── key columns: [44] = [45]
      │    │                   ├── lookup columns are key
      │    │                   ├── cardinality: [0 - 1]
      │    │                   ├── key: ()
      │    │                   ├── fd: ()-->(44)
      │    │                   ├── with-scan &3
      │    │                   │    ├── columns: ct_t_id:44!null
      │    │                   │    ├── mapping:
      │    │                   │    │    └──  column1:38 => ct_t_id:44
      │    │                   │    ├── cardinality: [1 - 1]
      │    │                   │    ├── key: ()
      │    │                   │    └── fd: ()-->(44)
      │    │                   └── filters (true)
      │    └── projections
      │         └── 1 [as="?column?":62]
      └── project
           ├── columns: ca_bal:82!null
           ├── cardinality: [0 - 1]
           ├── volatile, mutations
           ├── key: ()
           ├── fd: ()-->(82)
           ├── update customer_account
           │    ├── columns: ca_id:63!null customer_account.ca_bal:68!null
           │    ├── fetch columns: ca_id:71 ca_b_id:72 ca_c_id:73 ca_name:74 ca_tax_st:75 customer_account.ca_bal:76
           │    ├── update-mapping:
           │    │    └── ca_bal_cast:80 => customer_account.ca_bal:68
           │    ├── return-mapping:
           │    │    ├── ca_id:71 => ca_id:63
           │    │    └── ca_bal_cast:80 => customer_account.ca_bal:68
           │    ├── cardinality: [0 - 1]
           │    ├── volatile, mutations
           │    ├── key: ()
           │    ├── fd: ()-->(63,68)
           │    └── project
           │         ├── columns: ca_bal_cast:80!null ca_id:71!null ca_b_id:72!null ca_c_id:73!null ca_name:74 ca_tax_st:75!null customer_account.ca_bal:76!null
           │         ├── cardinality: [0 - 1]
           │         ├── immutable
           │         ├── key: ()
           │         ├── fd: ()-->(71-76,80)
           │         ├── scan customer_account
           │         │    ├── columns: ca_id:71!null ca_b_id:72!null ca_c_id:73!null ca_name:74 ca_tax_st:75!null customer_account.ca_bal:76!null
           │         │    ├── constraint: /71: [/0 - /0]
           │         │    ├── flags: avoid-full-scan
           │         │    ├── cardinality: [0 - 1]
           │         │    ├── key: ()
           │         │    └── fd: ()-->(71-76)
           │         └── projections
           │              └── assignment-cast: DECIMAL(12,2) [as=ca_bal_cast:80, outer=(76), immutable]
           │                   └── customer_account.ca_bal:76 + 1E+2
           └── projections
                └── customer_account.ca_bal:68::FLOAT8 [as=ca_bal:82, outer=(68), immutable]

# Q16
opt
WITH
insert_settlement AS (
       INSERT
         INTO settlement (se_t_id, se_cash_type, se_cash_due_date, se_amt)
       VALUES (0, 'Margin', '2020-06-15'::DATE, 100.00:::FLOAT8::DECIMAL)
    RETURNING 1
)
SELECT ca_bal::FLOAT8 FROM customer_account WHERE ca_id = 0;
----
with &2 (insert_settlement)
 ├── columns: ca_bal:40!null
 ├── cardinality: [0 - 1]
 ├── volatile, mutations
 ├── key: ()
 ├── fd: ()-->(40)
 ├── project
 │    ├── columns: "?column?":31!null
 │    ├── cardinality: [1 - 1]
 │    ├── volatile, mutations
 │    ├── key: ()
 │    ├── fd: ()-->(31)
 │    ├── insert settlement
 │    │    ├── columns: settlement.se_t_id:1!null
 │    │    ├── insert-mapping:
 │    │    │    ├── column1:7 => settlement.se_t_id:1
 │    │    │    ├── se_cash_type_cast:11 => se_cash_type:2
 │    │    │    ├── column3:9 => se_cash_due_date:3
 │    │    │    └── se_amt_cast:12 => se_amt:4
 │    │    ├── return-mapping:
 │    │    │    └── column1:7 => settlement.se_t_id:1
 │    │    ├── input binding: &1
 │    │    ├── cardinality: [1 - 1]
 │    │    ├── volatile, mutations
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1)
 │    │    ├── values
 │    │    │    ├── columns: column1:7!null column3:9!null se_cash_type_cast:11!null se_amt_cast:12!null
 │    │    │    ├── cardinality: [1 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(7,9,11,12)
 │    │    │    └── (0, '2020-06-15', 'Margin', 100.00)
 │    │    └── f-k-checks
 │    │         └── f-k-checks-item: settlement(se_t_id) -> trade(t_id)
 │    │              └── anti-join (lookup trade)
 │    │                   ├── columns: se_t_id:13!null
 │    │                   ├── key columns: [13] = [14]
 │    │                   ├── lookup columns are key
 │    │                   ├── cardinality: [0 - 1]
 │    │                   ├── key: ()
 │    │                   ├── fd: ()-->(13)
 │    │                   ├── with-scan &1
 │    │                   │    ├── columns: se_t_id:13!null
 │    │                   │    ├── mapping:
 │    │                   │    │    └──  column1:7 => se_t_id:13
 │    │                   │    ├── cardinality: [1 - 1]
 │    │                   │    ├── key: ()
 │    │                   │    └── fd: ()-->(13)
 │    │                   └── filters (true)
 │    └── projections
 │         └── 1 [as="?column?":31]
 └── project
      ├── columns: ca_bal:40!null
      ├── cardinality: [0 - 1]
      ├── immutable
      ├── key: ()
      ├── fd: ()-->(40)
      ├── scan customer_account
      │    ├── columns: ca_id:32!null customer_account.ca_bal:37!null
      │    ├── constraint: /32: [/0 - /0]
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(32,37)
      └── projections
           └── customer_account.ca_bal:37::FLOAT8 [as=ca_bal:40, outer=(37), immutable]

# --------------------------------------------------
# T9
# Trade-Status
# Emulates the process of providing an update on the status of a particular set
# of trades. It is representative of a customer reviewing a summary of the
# recent trading activity for one of their accounts.
# --------------------------------------------------

# Q1
opt
WITH
top_50_trades_for_account AS
(
   SELECT row_number() OVER (),
          t_id,
          t_dts,
          st_name,
          tt_name,
          t_s_symb,
          t_qty,
          t_exec_name,
          t_chrg::FLOAT8,
          s_name,
          ex_name
     FROM trade
LEFT JOIN security    ON s_symb = t_s_symb
LEFT JOIN exchange    ON ex_id  = s_ex_id
LEFT JOIN status_type ON st_id  = t_st_id
LEFT JOIN trade_type  ON tt_id  = t_tt_id
    WHERE t_ca_id = 0
 ORDER BY t_dts DESC
    LIMIT 50
),
info_for_account AS
(
  SELECT c_l_name, c_f_name, b_name
  FROM customer_account, customer, broker
  WHERE c_id = ca_c_id AND b_id = ca_b_id AND ca_id = 0
)
SELECT * FROM top_50_trades_for_account
LEFT JOIN info_for_account ON row_number = 1;
----
left-join (cross)
 ├── columns: row_number:98 t_id:99!null t_dts:100!null st_name:101!null tt_name:102!null t_s_symb:103!null t_qty:104!null t_exec_name:105!null t_chrg:106!null s_name:107!null ex_name:108!null c_l_name:109 c_f_name:110 b_name:111
 ├── cardinality: [0 - 50]
 ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 ├── immutable
 ├── key: (99,106)
 ├── fd: (99)-->(98,100-105,107,108), (103)-->(107,108), (99,106)-->(109-111)
 ├── project
 │    ├── columns: row_number:98 t_id:99!null t_dts:100!null st_name:101!null tt_name:102!null t_s_symb:103!null t_qty:104!null t_exec_name:105!null t_chrg:106!null s_name:107!null ex_name:108!null
 │    ├── cardinality: [0 - 50]
 │    ├── immutable
 │    ├── key: (99,106)
 │    ├── fd: (99)-->(98,100-105,107,108), (103)-->(107,108)
 │    ├── window partition=()
 │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null s_symb:18!null security.s_name:21!null s_ex_id:22!null ex_id:36!null exchange.ex_name:37!null st_id:45!null status_type.st_name:46!null tt_id:49!null trade_type.tt_name:50!null row_number:55
 │    │    ├── cardinality: [0 - 50]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(9), (1)-->(2-4,6,7,10,12,55), (18)-->(21,22), (36)-->(37), (45)-->(46), (49)-->(50), (6)==(18), (18)==(6), (22)==(36), (36)==(22), (3)==(45), (45)==(3), (4)==(49), (49)==(4)
 │    │    ├── inner-join (hash)
 │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null s_symb:18!null security.s_name:21!null s_ex_id:22!null ex_id:36!null exchange.ex_name:37!null st_id:45!null status_type.st_name:46!null tt_id:49!null trade_type.tt_name:50!null
 │    │    │    ├── cardinality: [0 - 50]
 │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(9), (1)-->(2-4,6,7,10,12), (18)-->(21,22), (36)-->(37), (45)-->(46), (49)-->(50), (6)==(18), (18)==(6), (22)==(36), (36)==(22), (3)==(45), (45)==(3), (4)==(49), (49)==(4)
 │    │    │    ├── inner-join (lookup security)
 │    │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null s_symb:18!null security.s_name:21!null s_ex_id:22!null st_id:45!null status_type.st_name:46!null tt_id:49!null trade_type.tt_name:50!null
 │    │    │    │    ├── key columns: [6] = [18]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── cardinality: [0 - 50]
 │    │    │    │    ├── key: (1)
 │    │    │    │    ├── fd: ()-->(9), (18)-->(21,22), (45)-->(46), (1)-->(2-4,6,7,10,12), (49)-->(50), (4)==(49), (49)==(4), (3)==(45), (45)==(3), (6)==(18), (18)==(6)
 │    │    │    │    ├── inner-join (hash)
 │    │    │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null st_id:45!null status_type.st_name:46!null tt_id:49!null trade_type.tt_name:50!null
 │    │    │    │    │    ├── cardinality: [0 - 50]
 │    │    │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    ├── fd: ()-->(9), (45)-->(46), (1)-->(2-4,6,7,10,12), (49)-->(50), (4)==(49), (49)==(4), (3)==(45), (45)==(3)
 │    │    │    │    │    ├── inner-join (hash)
 │    │    │    │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null tt_id:49!null trade_type.tt_name:50!null
 │    │    │    │    │    │    ├── cardinality: [0 - 50]
 │    │    │    │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    │    ├── fd: ()-->(9), (1)-->(2-4,6,7,10,12), (49)-->(50), (4)==(49), (49)==(4)
 │    │    │    │    │    │    ├── scan trade@trade_t_ca_id_t_dts_idx
 │    │    │    │    │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null
 │    │    │    │    │    │    │    ├── constraint: /9/-2/1: [/0 - /0]
 │    │    │    │    │    │    │    ├── limit: 50
 │    │    │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    │    │    └── fd: ()-->(9), (1)-->(2-4,6,7,10,12)
 │    │    │    │    │    │    ├── scan trade_type
 │    │    │    │    │    │    │    ├── columns: tt_id:49!null trade_type.tt_name:50!null
 │    │    │    │    │    │    │    ├── key: (49)
 │    │    │    │    │    │    │    └── fd: (49)-->(50)
 │    │    │    │    │    │    └── filters
 │    │    │    │    │    │         └── tt_id:49 = t_tt_id:4 [outer=(4,49), constraints=(/4: (/NULL - ]; /49: (/NULL - ]), fd=(4)==(49), (49)==(4)]
 │    │    │    │    │    ├── scan status_type
 │    │    │    │    │    │    ├── columns: st_id:45!null status_type.st_name:46!null
 │    │    │    │    │    │    ├── key: (45)
 │    │    │    │    │    │    └── fd: (45)-->(46)
 │    │    │    │    │    └── filters
 │    │    │    │    │         └── st_id:45 = t_st_id:3 [outer=(3,45), constraints=(/3: (/NULL - ]; /45: (/NULL - ]), fd=(3)==(45), (45)==(3)]
 │    │    │    │    └── filters (true)
 │    │    │    ├── scan exchange
 │    │    │    │    ├── columns: ex_id:36!null exchange.ex_name:37!null
 │    │    │    │    ├── key: (36)
 │    │    │    │    └── fd: (36)-->(37)
 │    │    │    └── filters
 │    │    │         └── ex_id:36 = s_ex_id:22 [outer=(22,36), constraints=(/22: (/NULL - ]; /36: (/NULL - ]), fd=(22)==(36), (36)==(22)]
 │    │    └── windows
 │    │         └── row-number [as=row_number:55]
 │    └── projections
 │         ├── row_number:55 [as=row_number:98, outer=(55)]
 │         ├── trade.t_id:1 [as=t_id:99, outer=(1)]
 │         ├── trade.t_dts:2 [as=t_dts:100, outer=(2)]
 │         ├── status_type.st_name:46 [as=st_name:101, outer=(46)]
 │         ├── trade_type.tt_name:50 [as=tt_name:102, outer=(50)]
 │         ├── trade.t_s_symb:6 [as=t_s_symb:103, outer=(6)]
 │         ├── trade.t_qty:7 [as=t_qty:104, outer=(7)]
 │         ├── trade.t_exec_name:10 [as=t_exec_name:105, outer=(10)]
 │         ├── trade.t_chrg:12::FLOAT8 [as=t_chrg:106, outer=(12), immutable]
 │         ├── security.s_name:21 [as=s_name:107, outer=(21)]
 │         └── exchange.ex_name:37 [as=ex_name:108, outer=(37)]
 ├── project
 │    ├── columns: c_l_name:109!null c_f_name:110!null b_name:111!null
 │    ├── cardinality: [0 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(109-111)
 │    ├── inner-join (lookup broker)
 │    │    ├── columns: ca_id:57!null ca_b_id:58!null ca_c_id:59!null c_id:65!null customer.c_l_name:68!null customer.c_f_name:69!null b_id:91!null broker.b_name:93!null
 │    │    ├── key columns: [58] = [91]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(57-59,65,68,69,91,93), (59)==(65), (65)==(59), (58)==(91), (91)==(58)
 │    │    ├── inner-join (lookup customer)
 │    │    │    ├── columns: ca_id:57!null ca_b_id:58!null ca_c_id:59!null c_id:65!null customer.c_l_name:68!null customer.c_f_name:69!null
 │    │    │    ├── key columns: [59] = [65]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(57-59,65,68,69), (59)==(65), (65)==(59)
 │    │    │    ├── scan customer_account
 │    │    │    │    ├── columns: ca_id:57!null ca_b_id:58!null ca_c_id:59!null
 │    │    │    │    ├── constraint: /57: [/0 - /0]
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    └── fd: ()-->(57-59)
 │    │    │    └── filters (true)
 │    │    └── filters (true)
 │    └── projections
 │         ├── customer.c_l_name:68 [as=c_l_name:109, outer=(68)]
 │         ├── customer.c_f_name:69 [as=c_f_name:110, outer=(69)]
 │         └── broker.b_name:93 [as=b_name:111, outer=(93)]
 └── filters
      └── row_number:98 = 1 [outer=(98), constraints=(/98: [/1 - /1]; tight), fd=()-->(98)]

# Q1 (With Hints)
opt
WITH
top_50_trades_for_account AS (
          SELECT row_number() OVER (),
                 t_id,
                 t_dts,
                 st_name,
                 tt_name,
                 t_s_symb,
                 t_qty,
                 t_exec_name,
                 t_chrg::FLOAT8,
                 s_name,
                 ex_name
            FROM trade
LEFT LOOKUP JOIN security    ON s_symb = t_s_symb
  LEFT HASH JOIN exchange    ON ex_id  = s_ex_id
  LEFT HASH JOIN status_type ON st_id  = t_st_id
  LEFT HASH JOIN trade_type  ON tt_id  = t_tt_id
           WHERE t_ca_id = 0
        ORDER BY t_dts DESC
           LIMIT 50
),
info_for_account AS (
           SELECT c_l_name, c_f_name, b_name
             FROM customer_account
INNER LOOKUP JOIN customer ON c_id = ca_c_id
INNER LOOKUP JOIN broker   ON b_id = ca_b_id
            WHERE ca_id = 0
)
   SELECT *
     FROM top_50_trades_for_account
LEFT JOIN info_for_account ON row_number = 1;
----
left-join (cross)
 ├── columns: row_number:98 t_id:99!null t_dts:100!null st_name:101!null tt_name:102!null t_s_symb:103!null t_qty:104!null t_exec_name:105!null t_chrg:106!null s_name:107!null ex_name:108!null c_l_name:109 c_f_name:110 b_name:111
 ├── cardinality: [0 - 50]
 ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 ├── immutable
 ├── key: (99,106)
 ├── fd: (99)-->(98,100-105,107,108), (103)-->(107,108), (99,106)-->(109-111)
 ├── project
 │    ├── columns: row_number:98 t_id:99!null t_dts:100!null st_name:101!null tt_name:102!null t_s_symb:103!null t_qty:104!null t_exec_name:105!null t_chrg:106!null s_name:107!null ex_name:108!null
 │    ├── cardinality: [0 - 50]
 │    ├── immutable
 │    ├── key: (99,106)
 │    ├── fd: (99)-->(98,100-105,107,108), (103)-->(107,108)
 │    ├── window partition=()
 │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null s_symb:18!null security.s_name:21!null s_ex_id:22!null ex_id:36!null exchange.ex_name:37!null st_id:45!null status_type.st_name:46!null tt_id:49!null trade_type.tt_name:50!null row_number:55
 │    │    ├── cardinality: [0 - 50]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(9), (1)-->(2-4,6,7,10,12,55), (18)-->(21,22), (36)-->(37), (45)-->(46), (49)-->(50), (6)==(18), (18)==(6), (22)==(36), (36)==(22), (3)==(45), (45)==(3), (4)==(49), (49)==(4)
 │    │    ├── inner-join (hash)
 │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null s_symb:18!null security.s_name:21!null s_ex_id:22!null ex_id:36!null exchange.ex_name:37!null st_id:45!null status_type.st_name:46!null tt_id:49!null trade_type.tt_name:50!null
 │    │    │    ├── flags: force hash join (store right side)
 │    │    │    ├── cardinality: [0 - 50]
 │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(9), (1)-->(2-4,6,7,10,12), (18)-->(21,22), (36)-->(37), (45)-->(46), (49)-->(50), (6)==(18), (18)==(6), (22)==(36), (36)==(22), (3)==(45), (45)==(3), (4)==(49), (49)==(4)
 │    │    │    ├── inner-join (hash)
 │    │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null s_symb:18!null security.s_name:21!null s_ex_id:22!null ex_id:36!null exchange.ex_name:37!null st_id:45!null status_type.st_name:46!null
 │    │    │    │    ├── flags: force hash join (store right side)
 │    │    │    │    ├── cardinality: [0 - 50]
 │    │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    │    ├── key: (1)
 │    │    │    │    ├── fd: ()-->(9), (1)-->(2-4,6,7,10,12), (18)-->(21,22), (36)-->(37), (45)-->(46), (6)==(18), (18)==(6), (22)==(36), (36)==(22), (3)==(45), (45)==(3)
 │    │    │    │    ├── inner-join (hash)
 │    │    │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null s_symb:18!null security.s_name:21!null s_ex_id:22!null ex_id:36!null exchange.ex_name:37!null
 │    │    │    │    │    ├── flags: force hash join (store right side)
 │    │    │    │    │    ├── cardinality: [0 - 50]
 │    │    │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    ├── fd: ()-->(9), (1)-->(2-4,6,7,10,12), (18)-->(21,22), (36)-->(37), (6)==(18), (18)==(6), (22)==(36), (36)==(22)
 │    │    │    │    │    ├── inner-join (lookup security)
 │    │    │    │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null s_symb:18!null security.s_name:21!null s_ex_id:22!null
 │    │    │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    │    │    ├── key columns: [6] = [18]
 │    │    │    │    │    │    ├── lookup columns are key
 │    │    │    │    │    │    ├── cardinality: [0 - 50]
 │    │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    │    ├── fd: ()-->(9), (1)-->(2-4,6,7,10,12), (18)-->(21,22), (6)==(18), (18)==(6)
 │    │    │    │    │    │    ├── scan trade@trade_t_ca_id_t_dts_idx
 │    │    │    │    │    │    │    ├── columns: trade.t_id:1!null trade.t_dts:2!null t_st_id:3!null t_tt_id:4!null trade.t_s_symb:6!null trade.t_qty:7!null t_ca_id:9!null trade.t_exec_name:10!null trade.t_chrg:12!null
 │    │    │    │    │    │    │    ├── constraint: /9/-2/1: [/0 - /0]
 │    │    │    │    │    │    │    ├── limit: 50
 │    │    │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    │    │    └── fd: ()-->(9), (1)-->(2-4,6,7,10,12)
 │    │    │    │    │    │    └── filters (true)
 │    │    │    │    │    ├── scan exchange
 │    │    │    │    │    │    ├── columns: ex_id:36!null exchange.ex_name:37!null
 │    │    │    │    │    │    ├── key: (36)
 │    │    │    │    │    │    └── fd: (36)-->(37)
 │    │    │    │    │    └── filters
 │    │    │    │    │         └── ex_id:36 = s_ex_id:22 [outer=(22,36), constraints=(/22: (/NULL - ]; /36: (/NULL - ]), fd=(22)==(36), (36)==(22)]
 │    │    │    │    ├── scan status_type
 │    │    │    │    │    ├── columns: st_id:45!null status_type.st_name:46!null
 │    │    │    │    │    ├── key: (45)
 │    │    │    │    │    └── fd: (45)-->(46)
 │    │    │    │    └── filters
 │    │    │    │         └── st_id:45 = t_st_id:3 [outer=(3,45), constraints=(/3: (/NULL - ]; /45: (/NULL - ]), fd=(3)==(45), (45)==(3)]
 │    │    │    ├── scan trade_type
 │    │    │    │    ├── columns: tt_id:49!null trade_type.tt_name:50!null
 │    │    │    │    ├── key: (49)
 │    │    │    │    └── fd: (49)-->(50)
 │    │    │    └── filters
 │    │    │         └── tt_id:49 = t_tt_id:4 [outer=(4,49), constraints=(/4: (/NULL - ]; /49: (/NULL - ]), fd=(4)==(49), (49)==(4)]
 │    │    └── windows
 │    │         └── row-number [as=row_number:55]
 │    └── projections
 │         ├── row_number:55 [as=row_number:98, outer=(55)]
 │         ├── trade.t_id:1 [as=t_id:99, outer=(1)]
 │         ├── trade.t_dts:2 [as=t_dts:100, outer=(2)]
 │         ├── status_type.st_name:46 [as=st_name:101, outer=(46)]
 │         ├── trade_type.tt_name:50 [as=tt_name:102, outer=(50)]
 │         ├── trade.t_s_symb:6 [as=t_s_symb:103, outer=(6)]
 │         ├── trade.t_qty:7 [as=t_qty:104, outer=(7)]
 │         ├── trade.t_exec_name:10 [as=t_exec_name:105, outer=(10)]
 │         ├── trade.t_chrg:12::FLOAT8 [as=t_chrg:106, outer=(12), immutable]
 │         ├── security.s_name:21 [as=s_name:107, outer=(21)]
 │         └── exchange.ex_name:37 [as=ex_name:108, outer=(37)]
 ├── project
 │    ├── columns: c_l_name:109!null c_f_name:110!null b_name:111!null
 │    ├── cardinality: [0 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(109-111)
 │    ├── inner-join (lookup broker)
 │    │    ├── columns: ca_id:57!null ca_b_id:58!null ca_c_id:59!null c_id:65!null customer.c_l_name:68!null customer.c_f_name:69!null b_id:91!null broker.b_name:93!null
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [58] = [91]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(57-59,65,68,69,91,93), (59)==(65), (65)==(59), (58)==(91), (91)==(58)
 │    │    ├── inner-join (lookup customer)
 │    │    │    ├── columns: ca_id:57!null ca_b_id:58!null ca_c_id:59!null c_id:65!null customer.c_l_name:68!null customer.c_f_name:69!null
 │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    ├── key columns: [59] = [65]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(57-59,65,68,69), (59)==(65), (65)==(59)
 │    │    │    ├── scan customer_account
 │    │    │    │    ├── columns: ca_id:57!null ca_b_id:58!null ca_c_id:59!null
 │    │    │    │    ├── constraint: /57: [/0 - /0]
 │    │    │    │    ├── cardinality: [0 - 1]
 │    │    │    │    ├── key: ()
 │    │    │    │    └── fd: ()-->(57-59)
 │    │    │    └── filters (true)
 │    │    └── filters (true)
 │    └── projections
 │         ├── customer.c_l_name:68 [as=c_l_name:109, outer=(68)]
 │         ├── customer.c_f_name:69 [as=c_f_name:110, outer=(69)]
 │         └── broker.b_name:93 [as=b_name:111, outer=(93)]
 └── filters
      └── row_number:98 = 1 [outer=(98), constraints=(/98: [/1 - /1]; tight), fd=()-->(98)]

# --------------------------------------------------
# T10
# Trade-Update
# Emulates the process of making minor corrections or updates to a set of
# trades. This is analogous to a customer or broker reviewing a set of trades,
# and discovering that some minor editorial corrections are required. The
# various sets of trades are chosen such that the work is representative of:
#   * reviewing general market trends
#   * reviewing trades for a period of time prior to the most recent account
#     statement
#   * reviewing past performance of a particular security
# --------------------------------------------------

# Q1
opt
   SELECT t_id,
          t_bid_price::FLOAT8,
          t_exec_name,
          t_is_cash,
          t_trade_price::FLOAT8,
          tt_is_mrkt,
          se_amt::FLOAT8,
          se_cash_due_date,
          se_cash_type,
          ct_amt::FLOAT8,
          ct_dts,
          ct_name
     FROM trade
LEFT JOIN trade_type       ON tt_id   = t_tt_id
LEFT JOIN settlement       ON se_t_id = t_id
LEFT JOIN cash_transaction ON ct_t_id = t_id
    WHERE t_id = ANY ARRAY [0, 1, 2, 3];
----
project
 ├── columns: t_id:1!null t_bid_price:36!null t_exec_name:10!null t_is_cash:5!null t_trade_price:37 tt_is_mrkt:21!null se_amt:38 se_cash_due_date:26 se_cash_type:25 ct_amt:39 ct_dts:31 ct_name:33
 ├── cardinality: [0 - 4]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(5,10,21,25,26,31,33,36-39)
 ├── left-join (merge)
 │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null se_t_id:24 se_cash_type:25 se_cash_due_date:26 settlement.se_amt:27 ct_t_id:30 ct_dts:31 cash_transaction.ct_amt:32 ct_name:33
 │    ├── left ordering: +1
 │    ├── right ordering: +30
 │    ├── cardinality: [0 - 4]
 │    ├── key: (1)
 │    ├── fd: (1)-->(4,5,8,10,11,24-27,30-33), (18)-->(21), (24)-->(25-27), (30)-->(31-33), (4)==(18), (18)==(4)
 │    ├── left-join (merge)
 │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null se_t_id:24 se_cash_type:25 se_cash_due_date:26 settlement.se_amt:27
 │    │    ├── left ordering: +1
 │    │    ├── right ordering: +24
 │    │    ├── cardinality: [0 - 4]
 │    │    ├── key: (1)
 │    │    ├── fd: (1)-->(4,5,8,10,11,24-27), (18)-->(21), (24)-->(25-27), (4)==(18), (18)==(4)
 │    │    ├── ordering: +1
 │    │    ├── inner-join (lookup trade_type)
 │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null
 │    │    │    ├── key columns: [4] = [18]
 │    │    │    ├── lookup columns are key
 │    │    │    ├── cardinality: [0 - 4]
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: (1)-->(4,5,8,10,11), (18)-->(21), (4)==(18), (18)==(4)
 │    │    │    ├── ordering: +1
 │    │    │    ├── scan trade
 │    │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    │    ├── constraint: /1: [/0 - /3]
 │    │    │    │    ├── cardinality: [0 - 4]
 │    │    │    │    ├── key: (1)
 │    │    │    │    ├── fd: (1)-->(4,5,8,10,11)
 │    │    │    │    └── ordering: +1
 │    │    │    └── filters (true)
 │    │    ├── scan settlement
 │    │    │    ├── columns: se_t_id:24!null se_cash_type:25!null se_cash_due_date:26!null settlement.se_amt:27!null
 │    │    │    ├── constraint: /24: [/0 - /3]
 │    │    │    ├── cardinality: [0 - 4]
 │    │    │    ├── key: (24)
 │    │    │    ├── fd: (24)-->(25-27)
 │    │    │    └── ordering: +24
 │    │    └── filters (true)
 │    ├── scan cash_transaction
 │    │    ├── columns: ct_t_id:30!null ct_dts:31!null cash_transaction.ct_amt:32!null ct_name:33
 │    │    ├── constraint: /30: [/0 - /3]
 │    │    ├── cardinality: [0 - 4]
 │    │    ├── key: (30)
 │    │    ├── fd: (30)-->(31-33)
 │    │    └── ordering: +30
 │    └── filters (true)
 └── projections
      ├── trade.t_bid_price:8::FLOAT8 [as=t_bid_price:36, outer=(8), immutable]
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:37, outer=(11), immutable]
      ├── settlement.se_amt:27::FLOAT8 [as=se_amt:38, outer=(27), immutable]
      └── cash_transaction.ct_amt:32::FLOAT8 [as=ct_amt:39, outer=(32), immutable]

# Q1 (With Hints)
opt
          SELECT t_id,
                 t_bid_price::FLOAT8,
                 t_exec_name,
                 t_is_cash,
                 t_trade_price::FLOAT8,
                 tt_is_mrkt,
                 se_amt::FLOAT8,
                 se_cash_due_date,
                 se_cash_type,
                 ct_amt::FLOAT8,
                 ct_dts,
                 ct_name
            FROM trade
  LEFT HASH JOIN trade_type       ON tt_id   = t_tt_id
LEFT LOOKUP JOIN settlement       ON se_t_id = t_id
LEFT LOOKUP JOIN cash_transaction ON ct_t_id = t_id
           WHERE t_id = ANY ARRAY [0, 1, 2, 3];
----
project
 ├── columns: t_id:1!null t_bid_price:36!null t_exec_name:10!null t_is_cash:5!null t_trade_price:37 tt_is_mrkt:21!null se_amt:38 se_cash_due_date:26 se_cash_type:25 ct_amt:39 ct_dts:31 ct_name:33
 ├── cardinality: [0 - 4]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(5,10,21,25,26,31,33,36-39)
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null se_t_id:24 se_cash_type:25 se_cash_due_date:26 settlement.se_amt:27 ct_t_id:30 ct_dts:31 cash_transaction.ct_amt:32 ct_name:33
 │    ├── flags: force lookup join (into right side)
 │    ├── key columns: [1] = [30]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 4]
 │    ├── key: (1)
 │    ├── fd: (1)-->(4,5,8,10,11,24-27,30-33), (18)-->(21), (24)-->(25-27), (30)-->(31-33), (4)==(18), (18)==(4)
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null se_t_id:24 se_cash_type:25 se_cash_due_date:26 settlement.se_amt:27
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [1] = [24]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 4]
 │    │    ├── key: (1)
 │    │    ├── fd: (1)-->(4,5,8,10,11,24-27), (18)-->(21), (24)-->(25-27), (4)==(18), (18)==(4)
 │    │    ├── inner-join (hash)
 │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_is_mrkt:21!null
 │    │    │    ├── flags: force hash join (store right side)
 │    │    │    ├── cardinality: [0 - 4]
 │    │    │    ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: (1)-->(4,5,8,10,11), (18)-->(21), (4)==(18), (18)==(4)
 │    │    │    ├── scan trade
 │    │    │    │    ├── columns: t_id:1!null t_tt_id:4!null t_is_cash:5!null trade.t_bid_price:8!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    │    ├── constraint: /1: [/0 - /3]
 │    │    │    │    ├── cardinality: [0 - 4]
 │    │    │    │    ├── key: (1)
 │    │    │    │    └── fd: (1)-->(4,5,8,10,11)
 │    │    │    ├── scan trade_type
 │    │    │    │    ├── columns: tt_id:18!null tt_is_mrkt:21!null
 │    │    │    │    ├── key: (18)
 │    │    │    │    └── fd: (18)-->(21)
 │    │    │    └── filters
 │    │    │         └── tt_id:18 = t_tt_id:4 [outer=(4,18), constraints=(/4: (/NULL - ]; /18: (/NULL - ]), fd=(4)==(18), (18)==(4)]
 │    │    └── filters
 │    │         └── se_t_id:24 IN (0, 1, 2, 3) [outer=(24), constraints=(/24: [/0 - /0] [/1 - /1] [/2 - /2] [/3 - /3]; tight)]
 │    └── filters
 │         └── ct_t_id:30 IN (0, 1, 2, 3) [outer=(30), constraints=(/30: [/0 - /0] [/1 - /1] [/2 - /2] [/3 - /3]; tight)]
 └── projections
      ├── trade.t_bid_price:8::FLOAT8 [as=t_bid_price:36, outer=(8), immutable]
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:37, outer=(11), immutable]
      ├── settlement.se_amt:27::FLOAT8 [as=se_amt:38, outer=(27), immutable]
      └── cash_transaction.ct_amt:32::FLOAT8 [as=ct_amt:39, outer=(32), immutable]

# Q2
opt
UPDATE trade
   SET t_exec_name = IF(
                    t_exec_name LIKE '% X %',
                    replace(t_exec_name, ' X ', ' '),
                    replace(t_exec_name, ' ', ' X ')
                   )
 WHERE t_id = ANY ARRAY [0, 1, 2, 3];
----
update trade
 ├── columns: <none>
 ├── fetch columns: t_id:18 t_dts:19 t_st_id:20 t_tt_id:21 t_is_cash:22 t_s_symb:23 t_qty:24 t_bid_price:25 t_ca_id:26 t_exec_name:27 t_trade_price:28 t_chrg:29 t_comm:30 t_lifo:32
 ├── update-mapping:
 │    └── t_exec_name_cast:36 => t_exec_name:10
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: t_exec_name_cast:36 t_id:18!null t_dts:19!null t_st_id:20!null t_tt_id:21!null t_is_cash:22!null t_s_symb:23!null t_qty:24!null t_bid_price:25!null t_ca_id:26!null t_exec_name:27!null t_trade_price:28 t_chrg:29!null t_comm:30!null t_lifo:32!null
      ├── cardinality: [0 - 4]
      ├── immutable
      ├── key: (18)
      ├── fd: (18)-->(19-30,32), (27)-->(36)
      ├── scan trade
      │    ├── columns: t_id:18!null t_dts:19!null t_st_id:20!null t_tt_id:21!null t_is_cash:22!null t_s_symb:23!null t_qty:24!null t_bid_price:25!null t_ca_id:26!null t_exec_name:27!null t_trade_price:28 t_chrg:29!null t_comm:30!null t_lifo:32!null
      │    ├── constraint: /18: [/0 - /3]
      │    ├── flags: avoid-full-scan
      │    ├── cardinality: [0 - 4]
      │    ├── key: (18)
      │    └── fd: (18)-->(19-30,32)
      └── projections
           └── assignment-cast: VARCHAR(49) [as=t_exec_name_cast:36, outer=(27), immutable]
                └── CASE t_exec_name:27 LIKE '% X %' WHEN true THEN replace(t_exec_name:27, ' X ', ' ') ELSE replace(t_exec_name:27, ' ', ' X ') END

# Q3
opt
  SELECT th_t_id, th_dts, th_st_id
    FROM trade_history
   WHERE th_t_id = ANY ARRAY [0, 1, 2, 3]
ORDER BY th_dts;
----
sort
 ├── columns: th_t_id:1!null th_dts:2!null th_st_id:3!null
 ├── key: (1,3)
 ├── fd: (1,3)-->(2)
 ├── ordering: +2
 └── scan trade_history
      ├── columns: th_t_id:1!null th_dts:2!null th_st_id:3!null
      ├── constraint: /1/3: [/0 - /3]
      ├── key: (1,3)
      └── fd: (1,3)-->(2)

# Q4
opt
   SELECT t_id,
          t_bid_price::FLOAT8,
          t_exec_name,
          t_is_cash,
          t_trade_price::FLOAT8,
          se_amt::FLOAT8,
          se_cash_due_date,
          se_cash_type,
          ct_amt::FLOAT8,
          ct_dts,
          ct_name
     FROM trade
LEFT JOIN settlement       ON se_t_id = t_id
LEFT JOIN cash_transaction ON ct_t_id = t_id
    WHERE t_ca_id = 0 AND
          t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP AND
          t_dts <= '2020-06-20 22:27:42.148484+00:00'::TIMESTAMP
 ORDER BY t_dts ASC
    LIMIT 20;
----
project
 ├── columns: t_id:1!null t_bid_price:30!null t_exec_name:10!null t_is_cash:5!null t_trade_price:31 se_amt:32 se_cash_due_date:20 se_cash_type:19 ct_amt:33 ct_dts:25 ct_name:27  [hidden: t_dts:2!null]
 ├── cardinality: [0 - 20]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(2,5,10,19,20,25,27,30-33)
 ├── ordering: +2
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21 ct_t_id:24 ct_dts:25 cash_transaction.ct_amt:26 ct_name:27
 │    ├── key columns: [1] = [24]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 20]
 │    ├── key: (1)
 │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11,18-21,24-27), (18)-->(19-21), (24)-->(25-27)
 │    ├── ordering: +2 opt(9) [actual: +2]
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21
 │    │    ├── key columns: [1] = [18]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 20]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11,18-21), (18)-->(19-21)
 │    │    ├── ordering: +2 opt(9) [actual: +2]
 │    │    ├── scan trade@trade_t_ca_id_t_dts_idx,rev
 │    │    │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    ├── constraint: /9/-2/1: [/0/'2020-06-20 22:27:42.148484' - /0/'2020-06-15 22:27:42.148484']
 │    │    │    ├── limit: 20(rev)
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11)
 │    │    │    └── ordering: +2 opt(9) [actual: +2]
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_bid_price:8::FLOAT8 [as=t_bid_price:30, outer=(8), immutable]
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:31, outer=(11), immutable]
      ├── settlement.se_amt:21::FLOAT8 [as=se_amt:32, outer=(21), immutable]
      └── cash_transaction.ct_amt:26::FLOAT8 [as=ct_amt:33, outer=(26), immutable]

# Q4 (With Hints)
opt
          SELECT t_id,
                 t_bid_price::FLOAT8,
                 t_exec_name,
                 t_is_cash,
                 t_trade_price::FLOAT8,
                 se_amt::FLOAT8,
                 se_cash_due_date,
                 se_cash_type,
                 ct_amt::FLOAT8,
                 ct_dts,
                 ct_name
            FROM trade
LEFT LOOKUP JOIN settlement       ON se_t_id = t_id
LEFT LOOKUP JOIN cash_transaction ON ct_t_id = t_id
           WHERE t_ca_id = 0 AND
                 t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP AND
                 t_dts <= '2020-06-20 22:27:42.148484+00:00'::TIMESTAMP
        ORDER BY t_dts ASC
           LIMIT 20;
----
project
 ├── columns: t_id:1!null t_bid_price:30!null t_exec_name:10!null t_is_cash:5!null t_trade_price:31 se_amt:32 se_cash_due_date:20 se_cash_type:19 ct_amt:33 ct_dts:25 ct_name:27  [hidden: t_dts:2!null]
 ├── cardinality: [0 - 20]
 ├── immutable
 ├── key: (1)
 ├── fd: (1)-->(2,5,10,19,20,25,27,30-33)
 ├── ordering: +2
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21 ct_t_id:24 ct_dts:25 cash_transaction.ct_amt:26 ct_name:27
 │    ├── flags: force lookup join (into right side)
 │    ├── key columns: [1] = [24]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 20]
 │    ├── key: (1)
 │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11,18-21,24-27), (18)-->(19-21), (24)-->(25-27)
 │    ├── ordering: +2 opt(9) [actual: +2]
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 se_t_id:18 se_cash_type:19 se_cash_due_date:20 settlement.se_amt:21
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [1] = [18]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 20]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11,18-21), (18)-->(19-21)
 │    │    ├── ordering: +2 opt(9) [actual: +2]
 │    │    ├── scan trade@trade_t_ca_id_t_dts_idx,rev
 │    │    │    ├── columns: t_id:1!null t_dts:2!null t_is_cash:5!null trade.t_bid_price:8!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    ├── constraint: /9/-2/1: [/0/'2020-06-20 22:27:42.148484' - /0/'2020-06-15 22:27:42.148484']
 │    │    │    ├── limit: 20(rev)
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(9), (1)-->(2,5,8,10,11)
 │    │    │    └── ordering: +2 opt(9) [actual: +2]
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_bid_price:8::FLOAT8 [as=t_bid_price:30, outer=(8), immutable]
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:31, outer=(11), immutable]
      ├── settlement.se_amt:21::FLOAT8 [as=se_amt:32, outer=(21), immutable]
      └── cash_transaction.ct_amt:26::FLOAT8 [as=ct_amt:33, outer=(26), immutable]

# Q5
opt
UPDATE settlement
   SET se_cash_type = IF(
                        t_is_cash,
                        IF(
                            se_cash_type = 'Cash Account',
                            'Cash',
                            'Cash Account'
                        ),
                        IF(
                            se_cash_type = 'Margin Account',
                            'Margin',
                            'Margin Account'
                        )
                    )
  FROM
  (
    SELECT unnest(ARRAY[0, 1, 2, 3]:::INT8[]) AS t_id,
           unnest(ARRAY[True, False, True, True]:::BOOL[]) AS t_is_cash
  )
 WHERE se_t_id = t_id;
----
update settlement
 ├── columns: <none>
 ├── fetch columns: se_t_id:7 se_cash_type:8 se_cash_due_date:9 se_amt:10
 ├── passthrough columns: unnest:13 unnest:14
 ├── update-mapping:
 │    └── se_cash_type_cast:16 => se_cash_type:2
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: se_cash_type_cast:16!null se_t_id:7!null se_cash_type:8!null se_cash_due_date:9!null se_amt:10!null unnest:13!null unnest:14!null
      ├── cardinality: [0 - 4]
      ├── immutable
      ├── key: (7)
      ├── fd: (7)-->(8-10,13,14), (8,14)-->(16), (7)==(13), (13)==(7)
      ├── distinct-on
      │    ├── columns: se_t_id:7!null se_cash_type:8!null se_cash_due_date:9!null se_amt:10!null unnest:13!null unnest:14!null
      │    ├── grouping columns: se_t_id:7!null
      │    ├── cardinality: [0 - 4]
      │    ├── key: (7)
      │    ├── fd: (7)-->(8-10,13,14), (7)==(13), (13)==(7)
      │    ├── inner-join (lookup settlement)
      │    │    ├── columns: se_t_id:7!null se_cash_type:8!null se_cash_due_date:9!null se_amt:10!null unnest:13!null unnest:14!null
      │    │    ├── key columns: [13] = [7]
      │    │    ├── lookup columns are key
      │    │    ├── cardinality: [0 - 4]
      │    │    ├── fd: (7)-->(8-10), (7)==(13), (13)==(7)
      │    │    ├── values
      │    │    │    ├── columns: unnest:13!null unnest:14!null
      │    │    │    ├── cardinality: [4 - 4]
      │    │    │    ├── (0, true)
      │    │    │    ├── (1, false)
      │    │    │    ├── (2, true)
      │    │    │    └── (3, true)
      │    │    └── filters (true)
      │    └── aggregations
      │         ├── first-agg [as=se_cash_type:8, outer=(8)]
      │         │    └── se_cash_type:8
      │         ├── first-agg [as=se_cash_due_date:9, outer=(9)]
      │         │    └── se_cash_due_date:9
      │         ├── first-agg [as=se_amt:10, outer=(10)]
      │         │    └── se_amt:10
      │         ├── first-agg [as=unnest:13, outer=(13)]
      │         │    └── unnest:13
      │         └── first-agg [as=unnest:14, outer=(14)]
      │              └── unnest:14
      └── projections
           └── assignment-cast: VARCHAR(40) [as=se_cash_type_cast:16, outer=(8,14), immutable]
                └── CASE unnest:14 WHEN true THEN CASE se_cash_type:8 = 'Cash Account' WHEN true THEN 'Cash' ELSE 'Cash Account' END ELSE CASE se_cash_type:8 = 'Margin Account' WHEN true THEN 'Margin' ELSE 'Margin Account' END END

# Q6
opt
   SELECT t_id,
          t_ca_id,
          t_exec_name,
          t_is_cash,
          t_trade_price::FLOAT8,
          t_qty,
          t_dts,
          t_tt_id,
          tt_name,
          s_name,
          se_amt::FLOAT8,
          se_cash_due_date,
          se_cash_type,
          ct_amt::FLOAT8,
          ct_dts,
          ct_name
     FROM trade
LEFT JOIN trade_type       ON tt_id   = t_tt_id
LEFT JOIN security         ON s_symb = t_s_symb
LEFT JOIN settlement       ON se_t_id = t_id
LEFT JOIN cash_transaction ON ct_t_id = t_id
    WHERE t_s_symb = 'ROACH' AND
          t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP AND
          t_dts <= '2020-06-20 22:27:42.148484+00:00'::TIMESTAMP
 ORDER BY t_dts ASC
    LIMIT 50;
----
project
 ├── columns: t_id:1!null t_ca_id:9!null t_exec_name:10!null t_is_cash:5!null t_trade_price:54 t_qty:7!null t_dts:2!null t_tt_id:4!null tt_name:19!null s_name:27!null se_amt:55 se_cash_due_date:44 se_cash_type:43 ct_amt:56 ct_dts:49 ct_name:51
 ├── cardinality: [0 - 50]
 ├── immutable
 ├── key: (1)
 ├── fd: ()-->(27), (1)-->(2,4,5,7,9,10,19,43,44,49,51,54-56), (4)-->(19)
 ├── ordering: +2 opt(27) [actual: +2]
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null s_symb:24!null s_name:27!null se_t_id:42 se_cash_type:43 se_cash_due_date:44 settlement.se_amt:45 ct_t_id:48 ct_dts:49 cash_transaction.ct_amt:50 ct_name:51
 │    ├── key columns: [1] = [48]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 50]
 │    ├── key: (1)
 │    ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11,42-45,48-51), (18)-->(19), (42)-->(43-45), (48)-->(49-51), (4)==(18), (18)==(4), (6)==(24), (24)==(6)
 │    ├── ordering: +2 opt(6,24,27) [actual: +2]
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null s_symb:24!null s_name:27!null se_t_id:42 se_cash_type:43 se_cash_due_date:44 settlement.se_amt:45
 │    │    ├── key columns: [1] = [42]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 50]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11,42-45), (18)-->(19), (42)-->(43-45), (4)==(18), (18)==(4), (6)==(24), (24)==(6)
 │    │    ├── ordering: +2 opt(6,24,27) [actual: +2]
 │    │    ├── top-k
 │    │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null s_symb:24!null s_name:27!null
 │    │    │    ├── internal-ordering: +2 opt(6,24,27)
 │    │    │    ├── k: 50
 │    │    │    ├── cardinality: [0 - 50]
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11), (18)-->(19), (4)==(18), (18)==(4), (6)==(24), (24)==(6)
 │    │    │    ├── ordering: +2 opt(6,24,27) [actual: +2]
 │    │    │    └── inner-join (hash)
 │    │    │         ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null s_symb:24!null s_name:27!null
 │    │    │         ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │         ├── key: (1)
 │    │    │         ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11), (18)-->(19), (4)==(18), (18)==(4), (6)==(24), (24)==(6)
 │    │    │         ├── inner-join (lookup trade@trade_t_s_symb_t_dts_idx)
 │    │    │         │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 s_symb:24!null s_name:27!null
 │    │    │         │    ├── lookup expression
 │    │    │         │    │    └── filters
 │    │    │         │    │         ├── (t_dts:2 >= '2020-06-15 22:27:42.148484') AND (t_dts:2 <= '2020-06-20 22:27:42.148484') [outer=(2), constraints=(/2: [/'2020-06-15 22:27:42.148484' - /'2020-06-20 22:27:42.148484']; tight)]
 │    │    │         │    │         └── s_symb:24 = t_s_symb:6 [outer=(6,24), constraints=(/6: (/NULL - ]; /24: (/NULL - ]), fd=(6)==(24), (24)==(6)]
 │    │    │         │    ├── key: (1)
 │    │    │         │    ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11), (6)==(24), (24)==(6)
 │    │    │         │    ├── scan security
 │    │    │         │    │    ├── columns: s_symb:24!null s_name:27!null
 │    │    │         │    │    ├── constraint: /24: [/'ROACH' - /'ROACH']
 │    │    │         │    │    ├── cardinality: [0 - 1]
 │    │    │         │    │    ├── key: ()
 │    │    │         │    │    └── fd: ()-->(24,27)
 │    │    │         │    └── filters
 │    │    │         │         └── t_s_symb:6 = 'ROACH' [outer=(6), constraints=(/6: [/'ROACH' - /'ROACH']; tight), fd=()-->(6)]
 │    │    │         ├── scan trade_type
 │    │    │         │    ├── columns: tt_id:18!null tt_name:19!null
 │    │    │         │    ├── key: (18)
 │    │    │         │    └── fd: (18)-->(19)
 │    │    │         └── filters
 │    │    │              └── tt_id:18 = t_tt_id:4 [outer=(4,18), constraints=(/4: (/NULL - ]; /18: (/NULL - ]), fd=(4)==(18), (18)==(4)]
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:54, outer=(11), immutable]
      ├── settlement.se_amt:45::FLOAT8 [as=se_amt:55, outer=(45), immutable]
      └── cash_transaction.ct_amt:50::FLOAT8 [as=ct_amt:56, outer=(50), immutable]

# Q6 (With Hints)
opt
          SELECT t_id,
                 t_ca_id,
                 t_exec_name,
                 t_is_cash,
                 t_trade_price::FLOAT8,
                 t_qty,
                 t_dts,
                 t_tt_id,
                 tt_name,
                 s_name,
                 se_amt::FLOAT8,
                 se_cash_due_date,
                 se_cash_type,
                 ct_amt::FLOAT8,
                 ct_dts,
                 ct_name
            FROM trade
  LEFT HASH JOIN trade_type       ON tt_id   = t_tt_id
LEFT LOOKUP JOIN security         ON s_symb = t_s_symb
LEFT LOOKUP JOIN settlement       ON se_t_id = t_id
LEFT LOOKUP JOIN cash_transaction ON ct_t_id = t_id
           WHERE t_s_symb = 'ROACH' AND
                 t_dts >= '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP AND
                 t_dts <= '2020-06-20 22:27:42.148484+00:00'::TIMESTAMP
        ORDER BY t_dts ASC
           LIMIT 50;
----
project
 ├── columns: t_id:1!null t_ca_id:9!null t_exec_name:10!null t_is_cash:5!null t_trade_price:54 t_qty:7!null t_dts:2!null t_tt_id:4!null tt_name:19!null s_name:27!null se_amt:55 se_cash_due_date:44 se_cash_type:43 ct_amt:56 ct_dts:49 ct_name:51
 ├── cardinality: [0 - 50]
 ├── immutable
 ├── key: (1)
 ├── fd: ()-->(27), (1)-->(2,4,5,7,9,10,19,43,44,49,51,54-56), (4)-->(19)
 ├── ordering: +2 opt(27) [actual: +2]
 ├── left-join (lookup cash_transaction)
 │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null s_symb:24!null s_name:27!null se_t_id:42 se_cash_type:43 se_cash_due_date:44 settlement.se_amt:45 ct_t_id:48 ct_dts:49 cash_transaction.ct_amt:50 ct_name:51
 │    ├── flags: force lookup join (into right side)
 │    ├── key columns: [1] = [48]
 │    ├── lookup columns are key
 │    ├── cardinality: [0 - 50]
 │    ├── key: (1)
 │    ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11,42-45,48-51), (18)-->(19), (42)-->(43-45), (48)-->(49-51), (4)==(18), (18)==(4), (6)==(24), (24)==(6)
 │    ├── ordering: +2 opt(6,24,27) [actual: +2]
 │    ├── left-join (lookup settlement)
 │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null s_symb:24!null s_name:27!null se_t_id:42 se_cash_type:43 se_cash_due_date:44 settlement.se_amt:45
 │    │    ├── flags: force lookup join (into right side)
 │    │    ├── key columns: [1] = [42]
 │    │    ├── lookup columns are key
 │    │    ├── cardinality: [0 - 50]
 │    │    ├── key: (1)
 │    │    ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11,42-45), (18)-->(19), (42)-->(43-45), (4)==(18), (18)==(4), (6)==(24), (24)==(6)
 │    │    ├── ordering: +2 opt(6,24,27) [actual: +2]
 │    │    ├── limit
 │    │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null s_symb:24!null s_name:27!null
 │    │    │    ├── internal-ordering: +2 opt(6,24,27)
 │    │    │    ├── cardinality: [0 - 50]
 │    │    │    ├── key: (1)
 │    │    │    ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11), (18)-->(19), (4)==(18), (18)==(4), (6)==(24), (24)==(6)
 │    │    │    ├── ordering: +2 opt(6,24,27) [actual: +2]
 │    │    │    ├── inner-join (lookup security)
 │    │    │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null s_symb:24!null s_name:27!null
 │    │    │    │    ├── flags: force lookup join (into right side)
 │    │    │    │    ├── key columns: [6] = [24]
 │    │    │    │    ├── lookup columns are key
 │    │    │    │    ├── key: (1)
 │    │    │    │    ├── fd: ()-->(6,24,27), (1)-->(2,4,5,7,9-11), (18)-->(19), (4)==(18), (18)==(4), (6)==(24), (24)==(6)
 │    │    │    │    ├── ordering: +2 opt(6,24,27) [actual: +2]
 │    │    │    │    ├── limit hint: 50.00
 │    │    │    │    ├── sort
 │    │    │    │    │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null
 │    │    │    │    │    ├── key: (1)
 │    │    │    │    │    ├── fd: ()-->(6), (1)-->(2,4,5,7,9-11), (18)-->(19), (4)==(18), (18)==(4)
 │    │    │    │    │    ├── ordering: +2 opt(6) [actual: +2]
 │    │    │    │    │    └── inner-join (hash)
 │    │    │    │    │         ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11 tt_id:18!null tt_name:19!null
 │    │    │    │    │         ├── flags: force hash join (store right side)
 │    │    │    │    │         ├── multiplicity: left-rows(exactly-one), right-rows(zero-or-more)
 │    │    │    │    │         ├── key: (1)
 │    │    │    │    │         ├── fd: ()-->(6), (1)-->(2,4,5,7,9-11), (18)-->(19), (4)==(18), (18)==(4)
 │    │    │    │    │         ├── scan trade@trade_t_s_symb_t_dts_idx
 │    │    │    │    │         │    ├── columns: t_id:1!null t_dts:2!null t_tt_id:4!null t_is_cash:5!null t_s_symb:6!null t_qty:7!null t_ca_id:9!null t_exec_name:10!null trade.t_trade_price:11
 │    │    │    │    │         │    ├── constraint: /6/2/1: [/'ROACH'/'2020-06-15 22:27:42.148484' - /'ROACH'/'2020-06-20 22:27:42.148484']
 │    │    │    │    │         │    ├── key: (1)
 │    │    │    │    │         │    └── fd: ()-->(6), (1)-->(2,4,5,7,9-11)
 │    │    │    │    │         ├── scan trade_type
 │    │    │    │    │         │    ├── columns: tt_id:18!null tt_name:19!null
 │    │    │    │    │         │    ├── key: (18)
 │    │    │    │    │         │    └── fd: (18)-->(19)
 │    │    │    │    │         └── filters
 │    │    │    │    │              └── tt_id:18 = t_tt_id:4 [outer=(4,18), constraints=(/4: (/NULL - ]; /18: (/NULL - ]), fd=(4)==(18), (18)==(4)]
 │    │    │    │    └── filters
 │    │    │    │         └── s_symb:24 = 'ROACH' [outer=(24), constraints=(/24: [/'ROACH' - /'ROACH']; tight), fd=()-->(24)]
 │    │    │    └── 50
 │    │    └── filters (true)
 │    └── filters (true)
 └── projections
      ├── trade.t_trade_price:11::FLOAT8 [as=t_trade_price:54, outer=(11), immutable]
      ├── settlement.se_amt:45::FLOAT8 [as=se_amt:55, outer=(45), immutable]
      └── cash_transaction.ct_amt:50::FLOAT8 [as=ct_amt:56, outer=(50), immutable]

# Q7
opt
UPDATE cash_transaction
   SET ct_name = concat_ws(
                ' ',
                tt_name,
                t_qty::STRING,
                IF(ct_name LIKE '% shares of %', 'Shares of', 'shares of'),
                s_name
               )
  FROM (
        SELECT unnest(ARRAY[0, 1, 2, 3]:::INT8[])   AS t_id,
               unnest(ARRAY['TLS', 'TLB', 'TLS', 'TLS']:::STRING[]) AS tt_name,
               unnest(ARRAY[10, 2, 100, 2]:::INT4[])   AS t_qty,
               unnest(ARRAY['ROACH', 'ROACH', 'GOOG', 'ROACH']:::STRING[]) AS s_name
       )
 WHERE ct_t_id = t_id;
----
update cash_transaction
 ├── columns: <none>
 ├── fetch columns: ct_t_id:7 ct_dts:8 ct_amt:9 ct_name:10
 ├── passthrough columns: unnest:13 unnest:14 unnest:15 unnest:16
 ├── update-mapping:
 │    └── ct_name_cast:18 => ct_name:4
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: ct_name_cast:18 ct_t_id:7!null ct_dts:8!null ct_amt:9!null ct_name:10 unnest:13!null unnest:14!null unnest:15!null unnest:16!null
      ├── cardinality: [0 - 4]
      ├── immutable
      ├── key: (7)
      ├── fd: (7)-->(8-10,13-16), (10,14-16)-->(18), (7)==(13), (13)==(7)
      ├── distinct-on
      │    ├── columns: ct_t_id:7!null ct_dts:8!null ct_amt:9!null ct_name:10 unnest:13!null unnest:14!null unnest:15!null unnest:16!null
      │    ├── grouping columns: ct_t_id:7!null
      │    ├── cardinality: [0 - 4]
      │    ├── key: (7)
      │    ├── fd: (7)-->(8-10,13-16), (7)==(13), (13)==(7)
      │    ├── inner-join (lookup cash_transaction)
      │    │    ├── columns: ct_t_id:7!null ct_dts:8!null ct_amt:9!null ct_name:10 unnest:13!null unnest:14!null unnest:15!null unnest:16!null
      │    │    ├── key columns: [13] = [7]
      │    │    ├── lookup columns are key
      │    │    ├── cardinality: [0 - 4]
      │    │    ├── fd: (7)-->(8-10), (7)==(13), (13)==(7)
      │    │    ├── values
      │    │    │    ├── columns: unnest:13!null unnest:14!null unnest:15!null unnest:16!null
      │    │    │    ├── cardinality: [4 - 4]
      │    │    │    ├── (0, 'TLS', 10, 'ROACH')
      │    │    │    ├── (1, 'TLB', 2, 'ROACH')
      │    │    │    ├── (2, 'TLS', 100, 'GOOG')
      │    │    │    └── (3, 'TLS', 2, 'ROACH')
      │    │    └── filters (true)
      │    └── aggregations
      │         ├── first-agg [as=ct_dts:8, outer=(8)]
      │         │    └── ct_dts:8
      │         ├── first-agg [as=ct_amt:9, outer=(9)]
      │         │    └── ct_amt:9
      │         ├── first-agg [as=ct_name:10, outer=(10)]
      │         │    └── ct_name:10
      │         ├── first-agg [as=unnest:13, outer=(13)]
      │         │    └── unnest:13
      │         ├── first-agg [as=unnest:14, outer=(14)]
      │         │    └── unnest:14
      │         ├── first-agg [as=unnest:15, outer=(15)]
      │         │    └── unnest:15
      │         └── first-agg [as=unnest:16, outer=(16)]
      │              └── unnest:16
      └── projections
           └── assignment-cast: VARCHAR(100) [as=ct_name_cast:18, outer=(10,14-16), immutable]
                └── concat_ws(' ', unnest:14, unnest:15::STRING, CASE ct_name:10 LIKE '% shares of %' WHEN true THEN 'Shares of' ELSE 'shares of' END, unnest:16)

# --------------------------------------------------
# T11
# Data-Maintenance
# Emulates the periodic modifications to data that is mainly static and used for
# reference. This is analogous to updating data that seldom changes.
# --------------------------------------------------

# Q1
opt
SELECT ap_acl
  FROM account_permission
 WHERE ap_ca_id = 0
 ORDER BY ap_acl DESC
 LIMIT 1
----
project
 ├── columns: ap_acl:2!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(2)
 └── top-k
      ├── columns: ap_ca_id:1!null ap_acl:2!null
      ├── internal-ordering: -2 opt(1)
      ├── k: 1
      ├── cardinality: [0 - 1]
      ├── key: ()
      ├── fd: ()-->(1,2)
      └── scan account_permission
           ├── columns: ap_ca_id:1!null ap_acl:2!null
           ├── constraint: /1/3: [/0 - /0]
           └── fd: ()-->(1)

# Q2
opt
UPDATE account_permission SET ap_acl = '0011' WHERE ap_ca_id = 0 AND ap_acl = NULL
----
update account_permission
 ├── columns: <none>
 ├── fetch columns: ap_ca_id:8 ap_acl:9 ap_tax_id:10 ap_l_name:11 ap_f_name:12
 ├── update-mapping:
 │    └── ap_acl_cast:16 => ap_acl:2
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── values
      ├── columns: ap_ca_id:8!null ap_acl:9!null ap_tax_id:10!null ap_l_name:11!null ap_f_name:12!null ap_acl_cast:16!null
      ├── cardinality: [0 - 0]
      ├── key: ()
      └── fd: ()-->(8-12,16)

# Q3
opt
SELECT ad_line2, ad_id
  FROM address, customer
 WHERE ad_id = c_ad_id AND c_id = 0
----
project
 ├── columns: ad_line2:3 ad_id:1!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(1,3)
 └── inner-join (lookup address)
      ├── columns: ad_id:1!null ad_line2:3 c_id:8!null c_ad_id:17!null
      ├── key columns: [17] = [1]
      ├── lookup columns are key
      ├── cardinality: [0 - 1]
      ├── key: ()
      ├── fd: ()-->(1,3,8,17), (1)==(17), (17)==(1)
      ├── scan customer
      │    ├── columns: c_id:8!null c_ad_id:17!null
      │    ├── constraint: /8: [/0 - /0]
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(8,17)
      └── filters (true)

# Q4
opt
SELECT ad_line2, ad_id
  FROM address, company
 WHERE ad_id = co_ad_id AND co_id = 0
----
project
 ├── columns: ad_line2:3 ad_id:1!null
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(1,3)
 └── inner-join (lookup address)
      ├── columns: ad_id:1!null ad_line2:3 co_id:8!null co_ad_id:14!null
      ├── key columns: [14] = [1]
      ├── lookup columns are key
      ├── cardinality: [0 - 1]
      ├── key: ()
      ├── fd: ()-->(1,3,8,14), (1)==(14), (14)==(1)
      ├── scan company
      │    ├── columns: co_id:8!null co_ad_id:14!null
      │    ├── constraint: /8: [/0 - /0]
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(8,14)
      └── filters (true)

# Q5
opt
UPDATE address SET ad_line2 = '' WHERE ad_id = 0
----
update address
 ├── columns: <none>
 ├── fetch columns: ad_id:8 ad_line1:9 ad_line2:10 ad_zc_code:11 ad_ctry:12
 ├── update-mapping:
 │    └── ad_line2_cast:16 => ad_line2:3
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: ad_line2_cast:16!null ad_id:8!null ad_line1:9 ad_line2:10 ad_zc_code:11!null ad_ctry:12
      ├── cardinality: [0 - 1]
      ├── key: ()
      ├── fd: ()-->(8-12,16)
      ├── scan address
      │    ├── columns: ad_id:8!null ad_line1:9 ad_line2:10 ad_zc_code:11!null ad_ctry:12
      │    ├── constraint: /8: [/0 - /0]
      │    ├── flags: avoid-full-scan
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(8-12)
      └── projections
           └── '' [as=ad_line2_cast:16]

# Q6
opt
UPDATE company SET co_sp_rate = IF(co_sp_rate != 'ABA', 'ABA', 'AAA') WHERE co_id = 0;
----
update company
 ├── columns: <none>
 ├── fetch columns: co_id:12 co_st_id:13 co_name:14 co_in_id:15 co_sp_rate:16 co_ceo:17 co_ad_id:18 co_desc:19 co_open_date:20
 ├── update-mapping:
 │    └── co_sp_rate_cast:24 => co_sp_rate:5
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: co_sp_rate_cast:24!null co_id:12!null co_st_id:13!null co_name:14!null co_in_id:15!null co_sp_rate:16!null co_ceo:17!null co_ad_id:18!null co_desc:19!null co_open_date:20!null
      ├── cardinality: [0 - 1]
      ├── immutable
      ├── key: ()
      ├── fd: ()-->(12-20,24)
      ├── scan company
      │    ├── columns: co_id:12!null co_st_id:13!null co_name:14!null co_in_id:15!null co_sp_rate:16!null co_ceo:17!null co_ad_id:18!null co_desc:19!null co_open_date:20!null
      │    ├── constraint: /12: [/0 - /0]
      │    ├── flags: avoid-full-scan
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(12-20)
      └── projections
           └── assignment-cast: VARCHAR(4) [as=co_sp_rate_cast:24, outer=(16), immutable]
                └── CASE co_sp_rate:16 != 'ABA' WHEN true THEN 'ABA' ELSE 'AAA' END

# Q7
opt
UPDATE customer
   SET c_email_2 = left(c_email_2, strpos(c_email_2, '@'))
                 || IF(c_email_2 LIKE '%@mindspring.com', 'earthlink.com', 'mindspring.com')
 WHERE c_id = 0;
----
update customer
 ├── columns: <none>
 ├── fetch columns: c_id:27 c_tax_id:28 c_st_id:29 c_l_name:30 c_f_name:31 c_m_name:32 c_gndr:33 c_tier:34 c_dob:35 c_ad_id:36 c_ctry_1:37 c_area_1:38 c_local_1:39 c_ext_1:40 c_ctry_2:41 c_area_2:42 c_local_2:43 c_ext_2:44 c_ctry_3:45 c_area_3:46 c_local_3:47 c_ext_3:48 c_email_1:49 c_email_2:50
 ├── update-mapping:
 │    └── c_email_2_cast:54 => c_email_2:24
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: c_email_2_cast:54 c_id:27!null c_tax_id:28!null c_st_id:29!null c_l_name:30!null c_f_name:31!null c_m_name:32 c_gndr:33 c_tier:34!null c_dob:35!null c_ad_id:36!null c_ctry_1:37 c_area_1:38 c_local_1:39 c_ext_1:40 c_ctry_2:41 c_area_2:42 c_local_2:43 c_ext_2:44 c_ctry_3:45 c_area_3:46 c_local_3:47 c_ext_3:48 c_email_1:49 c_email_2:50
      ├── cardinality: [0 - 1]
      ├── immutable
      ├── key: ()
      ├── fd: ()-->(27-50,54)
      ├── scan customer
      │    ├── columns: c_id:27!null c_tax_id:28!null c_st_id:29!null c_l_name:30!null c_f_name:31!null c_m_name:32 c_gndr:33 c_tier:34!null c_dob:35!null c_ad_id:36!null c_ctry_1:37 c_area_1:38 c_local_1:39 c_ext_1:40 c_ctry_2:41 c_area_2:42 c_local_2:43 c_ext_2:44 c_ctry_3:45 c_area_3:46 c_local_3:47 c_ext_3:48 c_email_1:49 c_email_2:50
      │    ├── constraint: /27: [/0 - /0]
      │    ├── flags: avoid-full-scan
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(27-50)
      └── projections
           └── assignment-cast: VARCHAR(50) [as=c_email_2_cast:54, outer=(50), immutable]
                └── left(c_email_2:50, strpos(c_email_2:50, '@')) || CASE c_email_2:50 LIKE '%@mindspring.com' WHEN true THEN 'earthlink.com' ELSE 'mindspring.com' END

# Q8
opt
UPDATE customer_taxrate
   SET cx_tx_id = IF(
                    left(cx_tx_id, 2) = 'US',
                    IF(
                        cx_tx_id = 'US5',
                        'US1',
                        'US' || (right(cx_tx_id, 1)::INT8 + 1)::STRING
                    ),
                    IF(
                        cx_tx_id = 'CN4',
                        'CN1',
                        'CN' || (right(cx_tx_id, 1)::INT8 + 1)::STRING
                    )
                )
 WHERE cx_c_id = 0 AND (cx_tx_id LIKE 'US%' OR cx_tx_id LIKE 'CN%');
----
update customer_taxrate
 ├── columns: <none>
 ├── fetch columns: customer_taxrate.cx_tx_id:5 cx_c_id:6
 ├── update-mapping:
 │    └── cx_tx_id_cast:10 => customer_taxrate.cx_tx_id:1
 ├── input binding: &1
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── project
 │    ├── columns: cx_tx_id_cast:10 customer_taxrate.cx_tx_id:5!null cx_c_id:6!null
 │    ├── immutable
 │    ├── key: (5)
 │    ├── fd: ()-->(6), (5)-->(10)
 │    ├── scan customer_taxrate@customer_taxrate_cx_c_id_idx
 │    │    ├── columns: customer_taxrate.cx_tx_id:5!null cx_c_id:6!null
 │    │    ├── constraint: /6/5
 │    │    │    ├── [/0/'CN' - /0/'CO')
 │    │    │    └── [/0/'US' - /0/'UT')
 │    │    ├── flags: avoid-full-scan
 │    │    ├── key: (5)
 │    │    └── fd: ()-->(6)
 │    └── projections
 │         └── assignment-cast: VARCHAR(4) [as=cx_tx_id_cast:10, outer=(5), immutable]
 │              └── CASE left(customer_taxrate.cx_tx_id:5, 2) = 'US' WHEN true THEN CASE customer_taxrate.cx_tx_id:5 = 'US5' WHEN true THEN 'US1' ELSE 'US' || (right(customer_taxrate.cx_tx_id:5, 1)::INT8 + 1)::STRING END ELSE CASE customer_taxrate.cx_tx_id:5 = 'CN4' WHEN true THEN 'CN1' ELSE 'CN' || (right(customer_taxrate.cx_tx_id:5, 1)::INT8 + 1)::STRING END END
 └── f-k-checks
      └── f-k-checks-item: customer_taxrate(cx_tx_id) -> taxrate(tx_id)
           └── anti-join (lookup taxrate)
                ├── columns: cx_tx_id:11
                ├── key columns: [11] = [12]
                ├── lookup columns are key
                ├── with-scan &1
                │    ├── columns: cx_tx_id:11
                │    └── mapping:
                │         └──  cx_tx_id_cast:10 => cx_tx_id:11
                └── filters (true)

# Q9
opt
UPDATE daily_market
   SET dm_vol = dm_vol + 10
 WHERE dm_s_symb = 'ROACH' AND extract('month', dm_date)::INT8 = 5;
----
update daily_market
 ├── columns: <none>
 ├── fetch columns: dm_date:9 dm_s_symb:10 dm_close:11 dm_high:12 dm_low:13 dm_vol:14
 ├── update-mapping:
 │    └── dm_vol_new:17 => dm_vol:6
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: dm_vol_new:17!null dm_date:9!null dm_s_symb:10!null dm_close:11!null dm_high:12!null dm_low:13!null dm_vol:14!null
      ├── immutable
      ├── key: (9)
      ├── fd: ()-->(10), (9)-->(11-14), (14)-->(17)
      ├── select
      │    ├── columns: dm_date:9!null dm_s_symb:10!null dm_close:11!null dm_high:12!null dm_low:13!null dm_vol:14!null
      │    ├── immutable
      │    ├── key: (9)
      │    ├── fd: ()-->(10), (9)-->(11-14)
      │    ├── scan daily_market@daily_market_dm_s_symb_dm_date_key
      │    │    ├── columns: dm_date:9!null dm_s_symb:10!null dm_close:11!null dm_high:12!null dm_low:13!null dm_vol:14!null
      │    │    ├── constraint: /10/9: [/'ROACH' - /'ROACH']
      │    │    ├── flags: avoid-full-scan
      │    │    ├── key: (9)
      │    │    └── fd: ()-->(10), (9)-->(11-14)
      │    └── filters
      │         └── extract('month', dm_date:9)::INT8 = 5 [outer=(9), immutable]
      └── projections
           └── dm_vol:14 + 10 [as=dm_vol_new:17, outer=(14), immutable]

# Q10
opt
UPDATE exchange
   SET ex_desc = IF(
                ex_desc NOT LIKE '%LAST UPDATED%',
                ex_desc || ' LAST UPDATED ' || now()::TIMESTAMP(0)::STRING,
                substring(ex_desc, 1, length(ex_desc) - length(now()::TIMESTAMP(0)::STRING))
                || now()::TIMESTAMP(0)::STRING
               );
----
update exchange
 ├── columns: <none>
 ├── fetch columns: ex_id:10 ex_name:11 ex_num_symb:12 ex_open:13 ex_close:14 ex_desc:15 ex_ad_id:16
 ├── update-mapping:
 │    └── ex_desc_cast:20 => ex_desc:6
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: ex_desc_cast:20 ex_id:10!null ex_name:11!null ex_num_symb:12!null ex_open:13!null ex_close:14!null ex_desc:15 ex_ad_id:16!null
      ├── immutable
      ├── key: (10)
      ├── fd: (10)-->(11-16), (15)-->(20)
      ├── scan exchange
      │    ├── columns: ex_id:10!null ex_name:11!null ex_num_symb:12!null ex_open:13!null ex_close:14!null ex_desc:15 ex_ad_id:16!null
      │    ├── flags: avoid-full-scan
      │    ├── key: (10)
      │    └── fd: (10)-->(11-16)
      └── projections
           └── assignment-cast: VARCHAR(150) [as=ex_desc_cast:20, outer=(15), immutable]
                └── CASE ex_desc:15 NOT LIKE '%LAST UPDATED%' WHEN true THEN (ex_desc:15 || ' LAST UPDATED ') || '2017-05-10 13:00:00' ELSE substring(ex_desc:15, 1, length(ex_desc:15) - 19) || '2017-05-10 13:00:00' END

# Q11
opt
UPDATE financial
   SET fi_qtr_start_date = IF(
                            extract('day', fi_qtr_start_date) = 1,
                            fi_qtr_start_date + '1 day',
                            fi_qtr_start_date - '1 day'
                         )::DATE
 WHERE fi_co_id = 0;
----
update financial
 ├── columns: <none>
 ├── fetch columns: fi_co_id:17 fi_year:18 fi_qtr:19 fi_qtr_start_date:20 fi_revenue:21 fi_net_earn:22 fi_basic_eps:23 fi_dilut_eps:24 fi_margin:25 fi_inventory:26 fi_assets:27 fi_liability:28 fi_out_basic:29 fi_out_dilut:30
 ├── update-mapping:
 │    └── fi_qtr_start_date_new:33 => fi_qtr_start_date:4
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: fi_qtr_start_date_new:33 fi_co_id:17!null fi_year:18!null fi_qtr:19!null fi_qtr_start_date:20!null fi_revenue:21!null fi_net_earn:22!null fi_basic_eps:23!null fi_dilut_eps:24!null fi_margin:25!null fi_inventory:26!null fi_assets:27!null fi_liability:28!null fi_out_basic:29!null fi_out_dilut:30!null
      ├── immutable
      ├── key: (18,19)
      ├── fd: ()-->(17), (18,19)-->(20-30), (20)-->(33)
      ├── scan financial
      │    ├── columns: fi_co_id:17!null fi_year:18!null fi_qtr:19!null fi_qtr_start_date:20!null fi_revenue:21!null fi_net_earn:22!null fi_basic_eps:23!null fi_dilut_eps:24!null fi_margin:25!null fi_inventory:26!null fi_assets:27!null fi_liability:28!null fi_out_basic:29!null fi_out_dilut:30!null
      │    ├── constraint: /17/18/19: [/0 - /0]
      │    ├── flags: avoid-full-scan
      │    ├── key: (18,19)
      │    └── fd: ()-->(17), (18,19)-->(20-30)
      └── projections
           └── CASE extract('day', fi_qtr_start_date:20) = 1.0 WHEN true THEN fi_qtr_start_date:20 + '1 day' ELSE fi_qtr_start_date:20 - '1 day' END::DATE [as=fi_qtr_start_date_new:33, outer=(20), immutable]

# Q12
opt
UPDATE news_item
   SET ni_dts = ni_dts + '1 day'
 WHERE ni_id IN (SELECT nx_ni_id FROM news_xref WHERE nx_co_id = 0);
----
update news_item
 ├── columns: <none>
 ├── fetch columns: ni_id:10 ni_headline:11 ni_summary:12 ni_dts:14 ni_source:15 ni_author:16
 ├── update-mapping:
 │    └── ni_dts_new:24 => ni_dts:5
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: ni_dts_new:24!null ni_id:10!null ni_headline:11!null ni_summary:12!null ni_dts:14!null ni_source:15!null ni_author:16
      ├── immutable
      ├── key: (10)
      ├── fd: (10)-->(11,12,14-16), (14)-->(24)
      ├── project
      │    ├── columns: ni_id:10!null ni_headline:11!null ni_summary:12!null ni_dts:14!null ni_source:15!null ni_author:16
      │    ├── key: (10)
      │    ├── fd: (10)-->(11,12,14-16)
      │    └── project
      │         ├── columns: ni_id:10!null ni_headline:11!null ni_summary:12!null ni_dts:14!null ni_source:15!null ni_author:16 nx_ni_id:19!null
      │         ├── key: (19)
      │         ├── fd: (10)-->(11,12,14-16), (10)==(19), (19)==(10)
      │         └── inner-join (lookup news_item)
      │              ├── columns: ni_id:10!null ni_headline:11!null ni_summary:12!null ni_dts:14!null ni_source:15!null ni_author:16 nx_ni_id:19!null nx_co_id:20!null
      │              ├── key columns: [19] = [10]
      │              ├── lookup columns are key
      │              ├── key: (19)
      │              ├── fd: ()-->(20), (10)-->(11,12,14-16), (10)==(19), (19)==(10)
      │              ├── scan news_xref@news_xref_nx_co_id_idx
      │              │    ├── columns: nx_ni_id:19!null nx_co_id:20!null
      │              │    ├── constraint: /20/19: [/0 - /0]
      │              │    ├── key: (19)
      │              │    └── fd: ()-->(20)
      │              └── filters (true)
      └── projections
           └── ni_dts:14 + '1 day' [as=ni_dts_new:24, outer=(14), immutable]

# Q13
opt
UPDATE security SET s_exch_date = (s_exch_date + '1 day')::DATE WHERE s_symb = 'ROACH';
----
update security
 ├── columns: <none>
 ├── fetch columns: s_symb:19 s_issue:20 s_st_id:21 s_name:22 s_ex_id:23 s_co_id:24 s_num_out:25 s_start_date:26 s_exch_date:27 s_pe:28 s_52wk_high:29 s_52wk_high_date:30 s_52wk_low:31 s_52wk_low_date:32 s_dividend:33 s_yield:34
 ├── update-mapping:
 │    └── s_exch_date_new:37 => s_exch_date:9
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: s_exch_date_new:37!null s_symb:19!null s_issue:20!null s_st_id:21!null s_name:22!null s_ex_id:23!null s_co_id:24!null s_num_out:25!null s_start_date:26!null s_exch_date:27!null s_pe:28!null s_52wk_high:29!null s_52wk_high_date:30!null s_52wk_low:31!null s_52wk_low_date:32!null s_dividend:33!null s_yield:34!null
      ├── cardinality: [0 - 1]
      ├── immutable
      ├── key: ()
      ├── fd: ()-->(19-34,37)
      ├── scan security
      │    ├── columns: s_symb:19!null s_issue:20!null s_st_id:21!null s_name:22!null s_ex_id:23!null s_co_id:24!null s_num_out:25!null s_start_date:26!null s_exch_date:27!null s_pe:28!null s_52wk_high:29!null s_52wk_high_date:30!null s_52wk_low:31!null s_52wk_low_date:32!null s_dividend:33!null s_yield:34!null
      │    ├── constraint: /19: [/'ROACH' - /'ROACH']
      │    ├── flags: avoid-full-scan
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(19-34)
      └── projections
           └── (s_exch_date:27 + '1 day')::DATE [as=s_exch_date_new:37, outer=(27), immutable]

# Q14
opt
UPDATE taxrate
   SET tx_name = IF(
                tx_name LIKE '% Tax %',
                replace(tx_name, ' Tax ', ' tax '),
                replace(tx_name, ' tax ', ' Tax ')
               )
 WHERE tx_id = 'US1';
----
update taxrate
 ├── columns: <none>
 ├── fetch columns: tx_id:6 tx_name:7 tx_rate:8
 ├── update-mapping:
 │    └── tx_name_cast:12 => tx_name:2
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 └── project
      ├── columns: tx_name_cast:12 tx_id:6!null tx_name:7!null tx_rate:8!null
      ├── cardinality: [0 - 1]
      ├── immutable
      ├── key: ()
      ├── fd: ()-->(6-8,12)
      ├── scan taxrate
      │    ├── columns: tx_id:6!null tx_name:7!null tx_rate:8!null
      │    ├── constraint: /6: [/'US1' - /'US1']
      │    ├── flags: avoid-full-scan
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(6-8)
      └── projections
           └── assignment-cast: VARCHAR(50) [as=tx_name_cast:12, outer=(7), immutable]
                └── CASE tx_name:7 LIKE '% Tax %' WHEN true THEN replace(tx_name:7, ' Tax ', ' tax ') ELSE replace(tx_name:7, ' tax ', ' Tax ') END

# Q15
opt
SELECT count(*) FROM watch_item, watch_list WHERE wl_c_id = 0 AND wi_wl_id = wl_id;
----
scalar-group-by
 ├── columns: count:9!null
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(9)
 ├── inner-join (lookup watch_item)
 │    ├── columns: wi_wl_id:1!null wl_id:5!null wl_c_id:6!null
 │    ├── key columns: [5] = [1]
 │    ├── fd: ()-->(6), (1)==(5), (5)==(1)
 │    ├── scan watch_list@watch_list_wl_c_id_idx
 │    │    ├── columns: wl_id:5!null wl_c_id:6!null
 │    │    ├── constraint: /6/5: [/0 - /0]
 │    │    ├── key: (5)
 │    │    └── fd: ()-->(6)
 │    └── filters (true)
 └── aggregations
      └── count-rows [as=count_rows:9]

# Q16
opt
SELECT wi_s_symb
  FROM (
          SELECT row_number() OVER (), wi_s_symb
            FROM watch_item, watch_list
           WHERE wl_c_id = 0 AND wi_wl_id = wl_id
        ORDER BY wi_s_symb ASC
       )
 WHERE row_number = 1;
----
project
 ├── columns: wi_s_symb:2!null
 └── select
      ├── columns: wi_wl_id:1!null wi_s_symb:2!null wl_id:5!null wl_c_id:6!null row_number:9!null
      ├── key: (2,5)
      ├── fd: ()-->(6,9), (1)==(5), (5)==(1)
      ├── window partition=()
      │    ├── columns: wi_wl_id:1!null wi_s_symb:2!null wl_id:5!null wl_c_id:6!null row_number:9
      │    ├── key: (2,5)
      │    ├── fd: ()-->(6), (2,5)-->(9), (1)==(5), (5)==(1)
      │    ├── inner-join (lookup watch_item)
      │    │    ├── columns: wi_wl_id:1!null wi_s_symb:2!null wl_id:5!null wl_c_id:6!null
      │    │    ├── key columns: [5] = [1]
      │    │    ├── key: (2,5)
      │    │    ├── fd: ()-->(6), (1)==(5), (5)==(1)
      │    │    ├── scan watch_list@watch_list_wl_c_id_idx
      │    │    │    ├── columns: wl_id:5!null wl_c_id:6!null
      │    │    │    ├── constraint: /6/5: [/0 - /0]
      │    │    │    ├── key: (5)
      │    │    │    └── fd: ()-->(6)
      │    │    └── filters (true)
      │    └── windows
      │         └── row-number [as=row_number:9]
      └── filters
           └── row_number:9 = 1 [outer=(9), constraints=(/9: [/1 - /1]; tight), fd=()-->(9)]

# Q17
opt
  SELECT s_symb
    FROM security
   WHERE s_symb > 'GOOG'
     AND s_symb
         NOT IN (
                SELECT wi_s_symb
                  FROM watch_item, watch_list
                 WHERE wl_c_id = 0 AND wi_wl_id = wl_id
            )
ORDER BY s_symb ASC
   LIMIT 1;
----
limit
 ├── columns: s_symb:1!null
 ├── internal-ordering: +1
 ├── cardinality: [0 - 1]
 ├── key: ()
 ├── fd: ()-->(1)
 ├── anti-join (merge)
 │    ├── columns: s_symb:1!null
 │    ├── left ordering: +1
 │    ├── right ordering: +20
 │    ├── key: (1)
 │    ├── ordering: +1
 │    ├── limit hint: 1.00
 │    ├── scan security
 │    │    ├── columns: s_symb:1!null
 │    │    ├── constraint: /1: [/e'GOOG\x00' - ]
 │    │    ├── key: (1)
 │    │    └── ordering: +1
 │    ├── sort
 │    │    ├── columns: wi_wl_id:19!null wi_s_symb:20!null wl_id:23!null wl_c_id:24!null
 │    │    ├── key: (20,23)
 │    │    ├── fd: ()-->(24), (19)==(23), (23)==(19)
 │    │    ├── ordering: +20 opt(24) [actual: +20]
 │    │    └── inner-join (lookup watch_item)
 │    │         ├── columns: wi_wl_id:19!null wi_s_symb:20!null wl_id:23!null wl_c_id:24!null
 │    │         ├── lookup expression
 │    │         │    └── filters
 │    │         │         ├── wi_s_symb:20 > 'GOOG' [outer=(20), constraints=(/20: [/e'GOOG\x00' - ]; tight)]
 │    │         │         └── wl_id:23 = wi_wl_id:19 [outer=(19,23), constraints=(/19: (/NULL - ]; /23: (/NULL - ]), fd=(19)==(23), (23)==(19)]
 │    │         ├── key: (20,23)
 │    │         ├── fd: ()-->(24), (19)==(23), (23)==(19)
 │    │         ├── scan watch_list@watch_list_wl_c_id_idx
 │    │         │    ├── columns: wl_id:23!null wl_c_id:24!null
 │    │         │    ├── constraint: /24/23: [/0 - /0]
 │    │         │    ├── key: (23)
 │    │         │    └── fd: ()-->(24)
 │    │         └── filters (true)
 │    └── filters (true)
 └── 1

# Q18
opt
UPDATE watch_item
   SET wi_s_symb = 'ROACH'
  FROM watch_list
 WHERE wl_c_id = 0 AND wi_wl_id = wl_id AND wi_s_symb = 'ROACH';
----
update watch_item
 ├── columns: <none>
 ├── fetch columns: wi_wl_id:5 watch_item.wi_s_symb:6
 ├── passthrough columns: wl_id:9 wl_c_id:10 watch_list.crdb_internal_mvcc_timestamp:11 watch_list.tableoid:12
 ├── update-mapping:
 │    └── wi_s_symb_cast:14 => watch_item.wi_s_symb:2
 ├── input binding: &1
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── project
 │    ├── columns: wi_s_symb_cast:14!null wi_wl_id:5!null watch_item.wi_s_symb:6!null wl_id:9!null wl_c_id:10!null watch_list.crdb_internal_mvcc_timestamp:11 watch_list.tableoid:12
 │    ├── key: (9)
 │    ├── fd: ()-->(6,10,14), (9)-->(11,12), (5)==(9), (9)==(5)
 │    ├── inner-join (lookup watch_item)
 │    │    ├── columns: wi_wl_id:5!null watch_item.wi_s_symb:6!null wl_id:9!null wl_c_id:10!null watch_list.crdb_internal_mvcc_timestamp:11 watch_list.tableoid:12
 │    │    ├── key columns: [9 35] = [5 6]
 │    │    ├── lookup columns are key
 │    │    ├── key: (9)
 │    │    ├── fd: ()-->(6,10), (9)-->(11,12), (5)==(9), (9)==(5)
 │    │    ├── project
 │    │    │    ├── columns: "lookup_join_const_col_@6":35!null wl_id:9!null wl_c_id:10!null watch_list.crdb_internal_mvcc_timestamp:11 watch_list.tableoid:12
 │    │    │    ├── key: (9)
 │    │    │    ├── fd: ()-->(10,35), (9)-->(11,12)
 │    │    │    ├── index-join watch_list
 │    │    │    │    ├── columns: wl_id:9!null wl_c_id:10!null watch_list.crdb_internal_mvcc_timestamp:11 watch_list.tableoid:12
 │    │    │    │    ├── key: (9)
 │    │    │    │    ├── fd: ()-->(10), (9)-->(11,12)
 │    │    │    │    └── scan watch_list@watch_list_wl_c_id_idx
 │    │    │    │         ├── columns: wl_id:9!null wl_c_id:10!null
 │    │    │    │         ├── constraint: /10/9: [/0 - /0]
 │    │    │    │         ├── key: (9)
 │    │    │    │         └── fd: ()-->(10)
 │    │    │    └── projections
 │    │    │         └── 'ROACH' [as="lookup_join_const_col_@6":35]
 │    │    └── filters (true)
 │    └── projections
 │         └── 'ROACH' [as=wi_s_symb_cast:14]
 └── f-k-checks
      └── f-k-checks-item: watch_item(wi_s_symb) -> security(s_symb)
           └── anti-join (lookup security)
                ├── columns: wi_s_symb:15!null
                ├── key columns: [15] = [16]
                ├── lookup columns are key
                ├── fd: ()-->(15)
                ├── with-scan &1
                │    ├── columns: wi_s_symb:15!null
                │    ├── mapping:
                │    │    └──  wi_s_symb_cast:14 => wi_s_symb:15
                │    └── fd: ()-->(15)
                └── filters (true)

# --------------------------------------------------
# T12
# Trade-Cleanup
# Cancel any pending or submitted trades from the database.
# --------------------------------------------------

# Q1
opt
SELECT tr_t_id FROM trade_request ORDER BY tr_t_id;
----
sort
 ├── columns: tr_t_id:1!null
 ├── key: (1)
 ├── ordering: +1
 └── scan trade_request@trade_request_tr_b_id_tr_s_symb_idx
      ├── columns: tr_t_id:1!null
      └── key: (1)

# Q2
opt
UPSERT INTO trade_history (th_t_id, th_dts, th_st_id)
(VALUES
  (0, '2020-06-17 22:27:42.148484+00:00'::TIMESTAMP, 'SBMT'),
  (0, '2020-06-20 22:27:42.148484+00:00'::TIMESTAMP, 'PNDG'),
  (0, '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP, 'ACTV')
)
----
upsert trade_history
 ├── columns: <none>
 ├── upsert-mapping:
 │    ├── column1:6 => trade_history.th_t_id:1
 │    ├── column2:7 => th_dts:2
 │    └── th_st_id_cast:9 => trade_history.th_st_id:3
 ├── input binding: &1
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── values
 │    ├── columns: column1:6!null column2:7!null th_st_id_cast:9!null
 │    ├── cardinality: [3 - 3]
 │    ├── (0, '2020-06-17 22:27:42.148484', 'SBMT')
 │    ├── (0, '2020-06-20 22:27:42.148484', 'PNDG')
 │    └── (0, '2020-06-15 22:27:42.148484', 'ACTV')
 └── f-k-checks
      ├── f-k-checks-item: trade_history(th_t_id) -> trade(t_id)
      │    └── anti-join (lookup trade)
      │         ├── columns: th_t_id:10!null
      │         ├── key columns: [10] = [11]
      │         ├── lookup columns are key
      │         ├── cardinality: [0 - 3]
      │         ├── with-scan &1
      │         │    ├── columns: th_t_id:10!null
      │         │    ├── mapping:
      │         │    │    └──  column1:6 => th_t_id:10
      │         │    └── cardinality: [3 - 3]
      │         └── filters (true)
      └── f-k-checks-item: trade_history(th_st_id) -> status_type(st_id)
           └── anti-join (lookup status_type)
                ├── columns: th_st_id:28!null
                ├── key columns: [28] = [29]
                ├── lookup columns are key
                ├── cardinality: [0 - 3]
                ├── with-scan &1
                │    ├── columns: th_st_id:28!null
                │    ├── mapping:
                │    │    └──  th_st_id_cast:9 => th_st_id:28
                │    └── cardinality: [3 - 3]
                └── filters (true)

# Q3
opt
UPDATE trade
SET t_st_id = 'PNDG', t_dts = '2020-06-15 22:27:42.148484+00:00'::TIMESTAMP
WHERE t_id IN (1, 2, 3, 4, 5);
----
update trade
 ├── columns: <none>
 ├── fetch columns: t_id:18 t_dts:19 trade.t_st_id:20 t_tt_id:21 t_is_cash:22 t_s_symb:23 t_qty:24 t_bid_price:25 t_ca_id:26 t_exec_name:27 t_trade_price:28 t_chrg:29 t_comm:30 t_lifo:32
 ├── update-mapping:
 │    ├── t_dts_new:36 => t_dts:2
 │    └── t_st_id_cast:37 => trade.t_st_id:3
 ├── input binding: &1
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── project
 │    ├── columns: t_st_id_cast:37!null t_dts_new:36!null t_id:18!null t_dts:19!null trade.t_st_id:20!null t_tt_id:21!null t_is_cash:22!null t_s_symb:23!null t_qty:24!null t_bid_price:25!null t_ca_id:26!null t_exec_name:27!null t_trade_price:28 t_chrg:29!null t_comm:30!null t_lifo:32!null
 │    ├── cardinality: [0 - 5]
 │    ├── key: (18)
 │    ├── fd: ()-->(36,37), (18)-->(19-30,32)
 │    ├── scan trade
 │    │    ├── columns: t_id:18!null t_dts:19!null trade.t_st_id:20!null t_tt_id:21!null t_is_cash:22!null t_s_symb:23!null t_qty:24!null t_bid_price:25!null t_ca_id:26!null t_exec_name:27!null t_trade_price:28 t_chrg:29!null t_comm:30!null t_lifo:32!null
 │    │    ├── constraint: /18: [/1 - /5]
 │    │    ├── flags: avoid-full-scan
 │    │    ├── cardinality: [0 - 5]
 │    │    ├── key: (18)
 │    │    └── fd: (18)-->(19-30,32)
 │    └── projections
 │         ├── 'PNDG' [as=t_st_id_cast:37]
 │         └── '2020-06-15 22:27:42.148484' [as=t_dts_new:36]
 └── f-k-checks
      └── f-k-checks-item: trade(t_st_id) -> status_type(st_id)
           └── anti-join (lookup status_type)
                ├── columns: t_st_id:43!null
                ├── key columns: [43] = [44]
                ├── lookup columns are key
                ├── cardinality: [0 - 5]
                ├── fd: ()-->(43)
                ├── with-scan &1
                │    ├── columns: t_st_id:43!null
                │    ├── mapping:
                │    │    └──  t_st_id_cast:37 => t_st_id:43
                │    ├── cardinality: [0 - 5]
                │    └── fd: ()-->(43)
                └── filters (true)

# Q4
opt
WITH empty_trade_requests AS (
    DELETE FROM trade_request WHERE tr_t_id > 0 ORDER BY tr_t_id LIMIT 20 RETURNING tr_t_id
)
SELECT max(tr_t_id) FROM empty_trade_requests;
----
with &1 (empty_trade_requests)
 ├── columns: max:19
 ├── cardinality: [1 - 1]
 ├── volatile, mutations
 ├── key: ()
 ├── fd: ()-->(19)
 ├── delete trade_request
 │    ├── columns: trade_request.tr_t_id:1!null
 │    ├── fetch columns: trade_request.tr_t_id:9 tr_s_symb:11 tr_b_id:14
 │    ├── return-mapping:
 │    │    └── trade_request.tr_t_id:9 => trade_request.tr_t_id:1
 │    ├── partial index del columns: partial_index_del1:17
 │    ├── cardinality: [0 - 20]
 │    ├── volatile, mutations
 │    ├── key: (1)
 │    └── project
 │         ├── columns: partial_index_del1:17!null trade_request.tr_t_id:9!null tr_s_symb:11!null tr_b_id:14!null
 │         ├── cardinality: [0 - 20]
 │         ├── key: (9)
 │         ├── fd: (9)-->(11,14,17)
 │         ├── scan trade_request
 │         │    ├── columns: trade_request.tr_t_id:9!null tr_tt_id:10!null tr_s_symb:11!null tr_b_id:14!null
 │         │    ├── constraint: /9: [/1 - ]
 │         │    ├── limit: 20
 │         │    ├── flags: avoid-full-scan
 │         │    ├── key: (9)
 │         │    └── fd: (9)-->(10,11,14)
 │         └── projections
 │              └── tr_tt_id:10 IN ('TLB', 'TLS', 'TSL') [as=partial_index_del1:17, outer=(10)]
 └── scalar-group-by
      ├── columns: max:19
      ├── cardinality: [1 - 1]
      ├── key: ()
      ├── fd: ()-->(19)
      ├── with-scan &1 (empty_trade_requests)
      │    ├── columns: tr_t_id:18!null
      │    ├── mapping:
      │    │    └──  trade_request.tr_t_id:1 => tr_t_id:18
      │    ├── cardinality: [0 - 20]
      │    └── key: (18)
      └── aggregations
           └── max [as=max:19, outer=(18)]
                └── tr_t_id:18

# Q5
opt
SELECT t_id FROM trade@trade_pkey WHERE t_id > 0 AND t_st_id = 'SBMT';
----
project
 ├── columns: t_id:1!null
 ├── key: (1)
 └── select
      ├── columns: t_id:1!null t_st_id:3!null
      ├── key: (1)
      ├── fd: ()-->(3)
      ├── scan trade
      │    ├── columns: t_id:1!null t_st_id:3!null
      │    ├── constraint: /1: [/1 - ]
      │    ├── flags: force-index=trade_pkey
      │    ├── key: (1)
      │    └── fd: (1)-->(3)
      └── filters
           └── t_st_id:3 = 'SBMT' [outer=(3), constraints=(/3: [/'SBMT' - /'SBMT']; tight), fd=()-->(3)]
